UtilService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace service;
  8. use think\Request;
  9. use service\JsonService;
  10. class UtilService
  11. {
  12. public static function postMore($params,Request $request = null,$suffix = false)
  13. {
  14. if($request === null) $request = Request::instance();
  15. $p = [];
  16. $i = 0;
  17. foreach ($params as $param){
  18. if(!is_array($param)) {
  19. $p[$suffix == true ? $i++ : $param] = $request->post($param);
  20. }else{
  21. if(!isset($param[1])) $param[1] = null;
  22. if(!isset($param[2])) $param[2] = '';
  23. $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
  24. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->post($name,$param[1],$param[2]);
  25. }
  26. }
  27. return $p;
  28. }
  29. public static function getMore($params,Request $request=null,$suffix = false)
  30. {
  31. if($request === null) $request = Request::instance();
  32. $p = [];
  33. $i = 0;
  34. foreach ($params as $param){
  35. if(!is_array($param)) {
  36. $p[$suffix == true ? $i++ : $param] = $request->get($param);
  37. }else{
  38. if(!isset($param[1])) $param[1] = null;
  39. if(!isset($param[2])) $param[2] = '';
  40. $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
  41. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->get($name,$param[1],$param[2]);
  42. }
  43. }
  44. return $p;
  45. }
  46. public static function encrypt($string, $operation = false, $key = '', $expiry = 0) {
  47. // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
  48. $ckey_length = 6;
  49. // 密匙
  50. $key = md5($key);
  51. // 密匙a会参与加解密
  52. $keya = md5(substr($key, 0, 16));
  53. // 密匙b会用来做数据完整性验证
  54. $keyb = md5(substr($key, 16, 16));
  55. // 密匙c用于变化生成的密文
  56. $keyc = $ckey_length ? ($operation == false ? substr($string, 0, $ckey_length):
  57. substr(md5(microtime()), -$ckey_length)) : '';
  58. // 参与运算的密匙
  59. $cryptkey = $keya.md5($keya.$keyc);
  60. $key_length = strlen($cryptkey);
  61. // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),
  62. //解密时会通过这个密匙验证数据完整性
  63. // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
  64. $string = $operation == false ? base64_decode(substr($string, $ckey_length)) :
  65. sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  66. $string_length = strlen($string);
  67. $result = '';
  68. $box = range(0, 255);
  69. $rndkey = array();
  70. // 产生密匙簿
  71. for($i = 0; $i <= 255; $i++) {
  72. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  73. }
  74. // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
  75. for($j = $i = 0; $i < 256; $i++) {
  76. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  77. $tmp = $box[$i];
  78. $box[$i] = $box[$j];
  79. $box[$j] = $tmp;
  80. }
  81. // 核心加解密部分
  82. for($a = $j = $i = 0; $i < $string_length; $i++) {
  83. $a = ($a + 1) % 256;
  84. $j = ($j + $box[$a]) % 256;
  85. $tmp = $box[$a];
  86. $box[$a] = $box[$j];
  87. $box[$j] = $tmp;
  88. // 从密匙簿得出密匙进行异或,再转成字符
  89. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  90. }
  91. if($operation == false) {
  92. // 验证数据有效性,请看未加密明文的格式
  93. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
  94. substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  95. return substr($result, 26);
  96. } else {
  97. return '';
  98. }
  99. } else {
  100. // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
  101. // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
  102. return $keyc.str_replace('=', '', base64_encode($result));
  103. }
  104. }
  105. /**
  106. * 路径转url路径
  107. * @param $path
  108. * @return string
  109. */
  110. public static function pathToUrl($path)
  111. {
  112. return trim(str_replace(DS, '/', $path),'.');
  113. }
  114. /**
  115. * url转换路径
  116. * @param $url
  117. * @return string
  118. */
  119. public static function urlToPath($url)
  120. {
  121. return ROOT_PATH.trim(str_replace('/',DS,$url),DS);
  122. }
  123. public static function timeTran($time)
  124. {
  125. $t = time() - $time;
  126. $f = array(
  127. '31536000' => '年',
  128. '2592000' => '个月',
  129. '604800' => '星期',
  130. '86400' => '天',
  131. '3600' => '小时',
  132. '60' => '分钟',
  133. '1' => '秒'
  134. );
  135. foreach ($f as $k => $v) {
  136. if (0 != $c = floor($t / (int)$k)) {
  137. return $c . $v . '前';
  138. }
  139. }
  140. }
  141. /**
  142. * 分级排序
  143. * @param $data
  144. * @param int $pid
  145. * @param string $field
  146. * @param string $pk
  147. * @param string $html
  148. * @param int $level
  149. * @param bool $clear
  150. * @return array
  151. */
  152. public static function sortListTier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  153. {
  154. static $list = [];
  155. if ($clear) $list = [];
  156. foreach ($data as $k => $res) {
  157. if ($res[$field] == $pid) {
  158. $res['html'] = str_repeat($html, $level);
  159. $list[] = $res;
  160. unset($data[$k]);
  161. self::sortListTier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  162. }
  163. }
  164. return $list;
  165. }
  166. /**
  167. * 分级返回多维数组
  168. * @param $data
  169. * @param int $pid
  170. * @param string $field
  171. * @param string $pk
  172. * @param int $level
  173. * @return array
  174. */
  175. public static function getChindNode($data, $pid = 0, $field = 'pid', $pk = 'id', $level = 1)
  176. {
  177. static $list = [];
  178. foreach ($data as $k => $res) {
  179. if ($res['pid'] == $pid) {
  180. $list[] = $res;
  181. unset($data[$k]);
  182. self::getChindNode($data, $res['id'], $field, $pk, $level + 1);
  183. }
  184. }
  185. return $list;
  186. }
  187. /**分级返回下级所有分类ID
  188. * @param $data
  189. * @param string $children
  190. * @param string $field
  191. * @param string $pk
  192. * @return string
  193. */
  194. public static function getChildrenPid($data,$pid, $field = 'pid', $pk = 'id')
  195. {
  196. static $pids = '';
  197. foreach ($data as $k => $res) {
  198. if ($res[$field] == $pid) {
  199. $pids .= ','.$res[$pk];
  200. self::getChildrenPid($data, $res[$pk], $field, $pk);
  201. }
  202. }
  203. return $pids;
  204. }
  205. /**
  206. * 删除公告资源
  207. * @param $url
  208. * @return \StdClass
  209. */
  210. public static function rmPublicResource($url,$isPath = false)
  211. {
  212. $path = $isPath ? $url : realpath(self::urlToPath($url));
  213. if(!$path) return JsonService::fail('删除文件不存在!');
  214. if(!file_exists($path)) return JsonService::fail('删除路径不合法!');
  215. // if(0 !== strpos($path,ROOT_PATH.'public/uploads/')) return JsonService::fail('删除路径不合法!');
  216. if(!unlink($path)) return JsonService::fail('删除文件失败!');
  217. return JsonService::successful();
  218. }
  219. /**
  220. * 是否为微信内部浏览器
  221. * @return bool
  222. */
  223. public static function isWechatBrowser()
  224. {
  225. return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
  226. }
  227. /**
  228. * 匿名处理
  229. * @param $name
  230. * @return string
  231. */
  232. public static function anonymity($name)
  233. {
  234. $strLen = mb_strlen($name,'UTF-8');
  235. $min = 3;
  236. if($strLen <= 1)
  237. return '*';
  238. if($strLen<= $min)
  239. return mb_substr($name,0,1,'UTF-8').str_repeat('*',$min-1);
  240. else
  241. return mb_substr($name,0,1,'UTF-8').str_repeat('*',$strLen-1).mb_substr($name,-1,1,'UTF-8');
  242. }
  243. /**
  244. * 身份证验证
  245. * @param $card
  246. * @return bool
  247. */
  248. public static function setCard($card){
  249. $city = [11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江 ",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北 ",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏 ",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外 "];
  250. $tip = "";
  251. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  252. $pass= true;
  253. if(!$card || !preg_match($match,$card)){
  254. //身份证格式错误
  255. $pass = false;
  256. }else if(!$city[substr($card,0,2)]){
  257. //地址错误
  258. $pass = false;
  259. }else{
  260. //18位身份证需要验证最后一位校验位
  261. if(strlen($card) == 18){
  262. $card = str_split($card);
  263. //∑(ai×Wi)(mod 11)
  264. //加权因子
  265. $factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  266. //校验位
  267. $parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  268. $sum = 0;
  269. $ai = 0;
  270. $wi = 0;
  271. for ($i = 0; $i < 17; $i++)
  272. {
  273. $ai = $card[$i];
  274. $wi = $factor[$i];
  275. $sum += $ai * $wi;
  276. }
  277. $last = $parity[$sum % 11];
  278. if($parity[$sum % 11] != $card[17]){
  279. // $tip = "校验位错误";
  280. $pass =false;
  281. }
  282. }else{
  283. $pass =false;
  284. }
  285. }
  286. if(!$pass) return false;/* 身份证格式错误*/
  287. return true;/* 身份证格式正确*/
  288. }
  289. /**
  290. * TODO 砍价 拼团 分享海报生成
  291. * @param array $data
  292. * @param $path
  293. * @return array|bool|string
  294. * @throws \Exception
  295. */
  296. public static function setShareMarketingPoster($data = array(), $path){
  297. $config = array(
  298. 'text'=>array(
  299. array(
  300. 'text'=>$data['price'],//TODO 价格
  301. 'left'=>116,
  302. 'top'=>200,
  303. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  304. 'fontSize'=>50, //字号
  305. 'fontColor'=>'255,0,0', //字体颜色
  306. 'angle'=>0,
  307. ),
  308. array(
  309. 'text'=>$data['label'],//TODO 标签
  310. 'left'=>394,
  311. 'top'=>190,
  312. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  313. 'fontSize'=>24, //字号
  314. 'fontColor'=>'255,255,255', //字体颜色
  315. 'angle'=>0,
  316. ),
  317. array(
  318. 'text'=>$data['msg'],//TODO 简述
  319. 'left'=>80,
  320. 'top'=>270,
  321. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  322. 'fontSize'=>22, //字号
  323. 'fontColor'=>'40,40,40', //字体颜色
  324. 'angle'=>0,
  325. )
  326. ),
  327. 'image'=>array(
  328. array(
  329. 'url'=>$data['image'], //图片
  330. 'stream'=>0,
  331. 'left'=>120,
  332. 'top'=>340,
  333. 'right'=>0,
  334. 'bottom'=>0,
  335. 'width'=>450,
  336. 'height'=>450,
  337. 'opacity'=>100
  338. ),
  339. array(
  340. 'url'=>$data['url'], //二维码资源
  341. 'stream'=>0,
  342. 'left'=>260,
  343. 'top'=>890,
  344. 'right'=>0,
  345. 'bottom'=>0,
  346. 'width'=>160,
  347. 'height'=>160,
  348. 'opacity'=>100
  349. )
  350. ),
  351. 'background'=>ROOT_PATH.'/public/static/poster/poster.jpg'
  352. );
  353. if(!file_exists($config['background'])) exception('缺少系统预设背景图片');
  354. if(strlen($data['title']) < 36){
  355. $text = array(
  356. 'text'=>$data['title'],//TODO 标题
  357. 'left'=>76,
  358. 'top'=>100,
  359. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  360. 'fontSize'=>32, //字号
  361. 'fontColor'=>'0,0,0', //字体颜色
  362. 'angle'=>0,
  363. );
  364. array_push($config['text'],$text);
  365. }else{
  366. $titleOne = array(
  367. 'text'=>mb_substr($data['title'], 0, 12),//TODO 标题
  368. 'left'=>76,
  369. 'top'=>70,
  370. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  371. 'fontSize'=>32, //字号
  372. 'fontColor'=>'0,0,0', //字体颜色
  373. 'angle'=>0,
  374. );
  375. $titleTwo = array(
  376. 'text'=> mb_substr($data['title'], 12, 12),//TODO 标题
  377. 'left'=>76,
  378. 'top'=>120,
  379. 'fontPath'=>ROOT_PATH.'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  380. 'fontSize'=>32, //字号
  381. 'fontColor'=>'0,0,0', //字体颜色
  382. 'angle'=>0,
  383. );
  384. array_push($config['text'],$titleOne);
  385. array_push($config['text'],$titleTwo);
  386. }
  387. return self::setSharePoster($config, $path);
  388. }
  389. /**
  390. * TODO 生成分享二维码图片
  391. * @param array $config
  392. * @param $path
  393. * @return array|bool|string
  394. * @throws \Exception
  395. */
  396. public static function setSharePoster($config = array(), $path){
  397. $imageDefault = array(
  398. 'left'=>0,
  399. 'top'=>0,
  400. 'right'=>0,
  401. 'bottom'=>0,
  402. 'width'=>100,
  403. 'height'=>100,
  404. 'opacity'=>100
  405. );
  406. $textDefault = array(
  407. 'text'=>'',
  408. 'left'=>0,
  409. 'top'=>0,
  410. 'fontSize'=>32, //字号
  411. 'fontColor'=>'255,255,255', //字体颜色
  412. 'angle'=>0,
  413. );
  414. $background = $config['background'];//海报最底层得背景
  415. $backgroundInfo = getimagesize($background);
  416. $background = imagecreatefromstring(file_get_contents($background));
  417. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  418. $backgroundHeight = $backgroundInfo[1]; //背景高度
  419. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  420. $color = imagecolorallocate($imageRes, 0, 0, 0);
  421. imagefill($imageRes, 0, 0, $color);
  422. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  423. if(!empty($config['image'])){
  424. foreach ($config['image'] as $key => $val) {
  425. $val = array_merge($imageDefault,$val);
  426. $info = getimagesize($val['url']);
  427. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  428. if($val['stream']){
  429. $info = getimagesizefromstring($val['url']);
  430. $function = 'imagecreatefromstring';
  431. }
  432. $res = $function($val['url']);
  433. $resWidth = $info[0];
  434. $resHeight = $info[1];
  435. $canvas=imagecreatetruecolor($val['width'], $val['height']);
  436. imagefill($canvas, 0, 0, $color);
  437. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  438. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  439. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  440. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  441. }
  442. }
  443. if(isset($config['text']) && !empty($config['text'])){
  444. foreach ($config['text'] as $key => $val) {
  445. $val = array_merge($textDefault,$val);
  446. list($R,$G,$B) = explode(',', $val['fontColor']);
  447. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  448. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  449. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  450. try{
  451. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  452. }catch (\Exception $e){
  453. return JsonService::fail('error',$e->getMessage());
  454. }
  455. }
  456. }
  457. ob_start();
  458. imagejpeg ($imageRes);
  459. imagedestroy($imageRes);
  460. $res = ob_get_contents();
  461. ob_end_clean();
  462. $key = substr(md5(rand(0, 9999)) , 0, 5). date('YmdHis') . rand(0, 999999) . '.jpg';
  463. return UploadService::imageStream($key,$res,$path);
  464. }
  465. /*
  466. * 获取当前控制器模型方法组合成的字符串
  467. * @paran object $request Request 实例化后的对象
  468. * @retun string
  469. * */
  470. public static function getCurrentController(Request $request)
  471. {
  472. return strtolower($request->module().'/'.$request->controller().'/'.$request->action());
  473. }
  474. /**
  475. * TODO 获取小程序二维码是否生成
  476. * @param $url
  477. * @return array
  478. */
  479. public static function remoteImage($url)
  480. {
  481. $curl = curl_init();
  482. curl_setopt($curl, CURLOPT_URL, $url);
  483. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  484. $result = curl_exec($curl);
  485. $result = json_decode($result,true);
  486. if(is_array($result)) return ['status'=>false,'msg'=>$result['errcode'].'---'.$result['errmsg']];
  487. return ['status'=>true];
  488. }
  489. /**
  490. * TODO 修改 https 和 http
  491. * @param $url $url 域名
  492. * @param int $type 0 返回https 1 返回 http
  493. * @return string
  494. */
  495. public static function setHttpType($url, $type = 0)
  496. {
  497. $domainTop = substr($url,0,5);
  498. if($type){ if($domainTop == 'https') $url = 'http'.substr($url,5,strlen($url)); }
  499. else{ if($domainTop != 'https') $url = 'https:'.substr($url,5,strlen($url)); }
  500. return $url;
  501. }
  502. /*
  503. * CURL 检测远程文件是否在
  504. *
  505. * */
  506. public static function CurlFileExist($url)
  507. {
  508. $ch = curl_init();
  509. try{
  510. curl_setopt ($ch, CURLOPT_URL, $url);
  511. curl_setopt($ch, CURLOPT_HEADER, 1);
  512. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  513. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
  514. $contents = curl_exec($ch);
  515. if (preg_match("/404/", $contents)) return false;
  516. if (preg_match("/403/", $contents)) return false;
  517. return true;
  518. }catch (\Exception $e){
  519. return false;
  520. }
  521. }
  522. }