UtilService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\services;
  8. use crmeb\exceptions\UtilException;
  9. use think\facade\Config;
  10. use dh2y\qrcode\QRcode;
  11. use crmeb\services\upload\Upload;
  12. class UtilService
  13. {
  14. /**
  15. * 获取POST请求的数据
  16. * @param $params
  17. * @param null $request
  18. * @param bool $suffix
  19. * @return array
  20. */
  21. public static function postMore($params, $request = null, $suffix = false)
  22. {
  23. if ($request === null) $request = app('request');
  24. $p = [];
  25. $i = 0;
  26. foreach ($params as $param) {
  27. if (!is_array($param)) {
  28. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  29. } else {
  30. if (!isset($param[1])) $param[1] = null;
  31. if (!isset($param[2])) $param[2] = '';
  32. if (!isset($param[3])) $param[3] = '';
  33. if (!isset($param[4])) $param[4] = '';
  34. if (!isset($param[5])) $param[5] = '';
  35. if (is_array($param[0])) {
  36. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  37. $keyName = $param[0][0];
  38. } else {
  39. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  40. $keyName = $param[0];
  41. }
  42. $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  43. if (not_empty_check($param[4])) {
  44. if (!is_array($param[4])) {
  45. if (is_string($param[4]) && !function_exists($param[4])) {
  46. throw new UtilException('验证的方法' . $param[4] . '未定义');
  47. }
  48. if (!$param[4]($request->param($name, $param[1], $param[2]))) {
  49. throw new UtilException($param[5]);
  50. }
  51. } else {
  52. foreach ($param[4] as $k => $v) {
  53. if (is_string($v) && !function_exists($v)) {
  54. throw new UtilException('验证的方法' . $v . '未定义');
  55. }
  56. if (!$v($request->param($name, $param[1], $param[2]))) {
  57. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. return $p;
  65. }
  66. /**
  67. * 获取请求的数据
  68. * @param $params
  69. * @param null $request
  70. * @param bool $suffix
  71. * @return array
  72. */
  73. public static function getMore($params, $request = null, $suffix = false)
  74. {
  75. if ($request === null) $request = app('request');
  76. $p = [];
  77. $i = 0;
  78. foreach ($params as $param) {
  79. if (!is_array($param)) {
  80. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  81. } else {
  82. if (!isset($param[1])) $param[1] = null;
  83. if (!isset($param[2])) $param[2] = '';
  84. if (!isset($param[3])) $param[3] = '';
  85. if (!isset($param[4])) $param[4] = '';
  86. if (!isset($param[5])) $param[5] = '参数错误';
  87. if (is_array($param[0])) {
  88. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  89. $keyName = $param[0][0];
  90. } else {
  91. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  92. $keyName = $param[0];
  93. }
  94. $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  95. if (not_empty_check($param[4])) {
  96. if (!is_array($param[4])) {
  97. if (is_string($param[4]) && !function_exists($param[4])) {
  98. throw new UtilException('验证的方法' . $param[4] . '未定义');
  99. }
  100. if (!$param[4]($request->param($name, $param[1], $param[2]))) {
  101. throw new UtilException($param[5]);
  102. }
  103. } else {
  104. foreach ($param[4] as $k => $v) {
  105. if (is_string($v) && !function_exists($v)) {
  106. throw new UtilException('验证的方法' . $v . '未定义');
  107. }
  108. if (!$v($request->param($name, $param[1], $param[2]))) {
  109. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return $p;
  117. }
  118. /**
  119. * TODO 砍价 拼团 分享海报生成
  120. * @param array $data
  121. * @param $path
  122. * @return array|bool|string
  123. * @throws \Exception
  124. */
  125. public static function setShareMarketingPoster($data = array(), $path)
  126. {
  127. $config = array(
  128. 'text' => array(
  129. array(
  130. 'text' => $data['price'],//TODO 价格
  131. 'left' => 116,
  132. 'top' => 200,
  133. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  134. 'fontSize' => 50, //字号
  135. 'fontColor' => '255,0,0', //字体颜色
  136. 'angle' => 0,
  137. ),
  138. array(
  139. 'text' => $data['label'],//TODO 标签
  140. 'left' => 450,
  141. 'top' => 188,
  142. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  143. 'fontSize' => 24, //字号
  144. 'fontColor' => '255,255,255', //字体颜色
  145. 'angle' => 0,
  146. ),
  147. array(
  148. 'text' => $data['msg'],//TODO 简述
  149. 'left' => 80,
  150. 'top' => 270,
  151. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  152. 'fontSize' => 22, //字号
  153. 'fontColor' => '40,40,40', //字体颜色
  154. 'angle' => 0,
  155. )
  156. ),
  157. 'image' => array(
  158. array(
  159. 'url' => $data['image'], //图片
  160. 'stream' => 0,
  161. 'left' => 120,
  162. 'top' => 340,
  163. 'right' => 0,
  164. 'bottom' => 0,
  165. 'width' => 450,
  166. 'height' => 450,
  167. 'opacity' => 100
  168. ),
  169. array(
  170. 'url' => $data['url'], //二维码资源
  171. 'stream' => 0,
  172. 'left' => 260,
  173. 'top' => 890,
  174. 'right' => 0,
  175. 'bottom' => 0,
  176. 'width' => 160,
  177. 'height' => 160,
  178. 'opacity' => 100
  179. )
  180. ),
  181. 'background' => 'static/poster/poster.jpg'
  182. );
  183. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  184. if (strlen($data['title']) < 36) {
  185. $text = array(
  186. 'text' => $data['title'],//TODO 标题
  187. 'left' => 76,
  188. 'top' => 100,
  189. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  190. 'fontSize' => 32, //字号
  191. 'fontColor' => '0,0,0', //字体颜色
  192. 'angle' => 0,
  193. );
  194. array_push($config['text'], $text);
  195. } else {
  196. $titleOne = array(
  197. 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题
  198. 'left' => 76,
  199. 'top' => 70,
  200. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  201. 'fontSize' => 32, //字号
  202. 'fontColor' => '0,0,0', //字体颜色
  203. 'angle' => 0,
  204. );
  205. $titleTwo = array(
  206. 'text' => mb_strimwidth($data['title'], 24, 24),//TODO 标题
  207. 'left' => 76,
  208. 'top' => 120,
  209. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  210. 'fontSize' => 32, //字号
  211. 'fontColor' => '0,0,0', //字体颜色
  212. 'angle' => 0,
  213. );
  214. array_push($config['text'], $titleOne);
  215. array_push($config['text'], $titleTwo);
  216. }
  217. return self::setSharePoster($config, $path);
  218. }
  219. /**
  220. * TODO 生成分享二维码图片
  221. * @param array $config
  222. * @param $path
  223. * @return array|bool|string
  224. * @throws \Exception
  225. */
  226. public static function setSharePoster($config = array(), $path)
  227. {
  228. $imageDefault = array(
  229. 'left' => 0,
  230. 'top' => 0,
  231. 'right' => 0,
  232. 'bottom' => 0,
  233. 'width' => 100,
  234. 'height' => 100,
  235. 'opacity' => 100
  236. );
  237. $textDefault = array(
  238. 'text' => '',
  239. 'left' => 0,
  240. 'top' => 0,
  241. 'fontSize' => 32, //字号
  242. 'fontColor' => '255,255,255', //字体颜色
  243. 'angle' => 0,
  244. );
  245. $background = $config['background'];//海报最底层得背景
  246. if (substr($background, 0, 1) === '/') {
  247. $background = substr($background, 1);
  248. }
  249. $backgroundInfo = getimagesize($background);
  250. $background = imagecreatefromstring(file_get_contents($background));
  251. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  252. $backgroundHeight = $backgroundInfo[1]; //背景高度
  253. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  254. $color = imagecolorallocate($imageRes, 0, 0, 0);
  255. imagefill($imageRes, 0, 0, $color);
  256. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  257. if (!empty($config['image'])) {
  258. foreach ($config['image'] as $key => $val) {
  259. $val = array_merge($imageDefault, $val);
  260. $info = getimagesize($val['url']);
  261. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  262. if ($val['stream']) {
  263. $info = getimagesizefromstring($val['url']);
  264. $function = 'imagecreatefromstring';
  265. }
  266. $res = $function($val['url']);
  267. $resWidth = $info[0];
  268. $resHeight = $info[1];
  269. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  270. imagefill($canvas, 0, 0, $color);
  271. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  272. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  273. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  274. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  275. }
  276. }
  277. if (isset($config['text']) && !empty($config['text'])) {
  278. foreach ($config['text'] as $key => $val) {
  279. $val = array_merge($textDefault, $val);
  280. list($R, $G, $B) = explode(',', $val['fontColor']);
  281. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  282. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  283. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  284. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  285. }
  286. }
  287. ob_start();
  288. imagejpeg($imageRes);
  289. imagedestroy($imageRes);
  290. $res = ob_get_contents();
  291. ob_end_clean();
  292. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  293. $uploadType = (int)sys_config('upload_type', 1);
  294. $upload = new Upload($uploadType, [
  295. 'accessKey' => sys_config('accessKey'),
  296. 'secretKey' => sys_config('secretKey'),
  297. 'uploadUrl' => sys_config('uploadUrl'),
  298. 'storageName' => sys_config('storage_name'),
  299. 'storageRegion' => sys_config('storage_region'),
  300. ]);
  301. $res = $upload->to($path)->validate()->stream($res, $key);
  302. if ($res === false) {
  303. return $upload->getError();
  304. } else {
  305. $info = $upload->getUploadInfo();
  306. $info['image_type'] = $uploadType;
  307. return $info;
  308. }
  309. }
  310. /**
  311. * TODO 获取小程序二维码是否生成
  312. * @param $url
  313. * @return array
  314. */
  315. public static function remoteImage($url)
  316. {
  317. $curl = curl_init();
  318. curl_setopt($curl, CURLOPT_URL, $url);
  319. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  320. $result = curl_exec($curl);
  321. $result = json_decode($result, true);
  322. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  323. return ['status' => true];
  324. }
  325. /**
  326. * TODO 修改 https 和 http 移动到common
  327. * @param $url $url 域名
  328. * @param int $type 0 返回https 1 返回 http
  329. * @return string
  330. */
  331. public static function setHttpType($url, $type = 0)
  332. {
  333. $domainTop = substr($url, 0, 5);
  334. if ($type) {
  335. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  336. } else {
  337. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  338. }
  339. return $url;
  340. }
  341. /**
  342. * 获取二维码
  343. * @param $url
  344. * @param $name
  345. * @return array|bool|string
  346. */
  347. public static function getQRCodePath($url, $name)
  348. {
  349. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  350. try {
  351. $uploadType = sys_config('upload_type');
  352. //TODO 没有选择默认使用本地上传
  353. if (!$uploadType) $uploadType = 1;
  354. $uploadType = (int)$uploadType;
  355. $siteUrl = sys_config('site_url');
  356. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  357. $info = [];
  358. $outfile = Config::get('qrcode.cache_dir');
  359. $code = new QRcode();
  360. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  361. $content = file_get_contents('.' . $wapCodePath);
  362. if ($uploadType === 1) {
  363. $info["code"] = 200;
  364. $info["name"] = $name;
  365. $info["dir"] = $wapCodePath;
  366. $info["time"] = time();
  367. $info['size'] = 0;
  368. $info['type'] = 'image/png';
  369. $info["image_type"] = 1;
  370. $info['thumb_path'] = $wapCodePath;
  371. return $info;
  372. } else {
  373. $upload = new Upload($uploadType, [
  374. 'accessKey' => sys_config('accessKey'),
  375. 'secretKey' => sys_config('secretKey'),
  376. 'uploadUrl' => sys_config('uploadUrl'),
  377. 'storageName' => sys_config('storage_name'),
  378. 'storageRegion' => sys_config('storage_region'),
  379. ]);
  380. $res = $upload->to($outfile)->validate()->stream($content, $name);
  381. if ($res === false) {
  382. return $upload->getError();
  383. }
  384. $info = $upload->getUploadInfo();
  385. $info['image_type'] = $uploadType;
  386. return $info;
  387. }
  388. } catch (\Exception $e) {
  389. return $e->getMessage();
  390. }
  391. }
  392. /**
  393. * @param array $data
  394. * @param string $path
  395. * @return array|bool|string
  396. * @throws \Exception
  397. */
  398. public static function setShareProductPoster($data = array(), $path = '')
  399. {
  400. $config = array(
  401. 'text' => array(
  402. array(
  403. 'text' => $data['price'],//TODO 价格
  404. 'left' => 219,
  405. 'top' => 1055,
  406. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  407. 'fontSize' => 50, //字号
  408. 'fontColor' => '131,28,32', //字体颜色
  409. 'angle' => 0,
  410. ),
  411. array(
  412. 'text' => '原价:¥' . $data['ot_price'],//TODO 原价
  413. 'left' => 94,
  414. 'top' => 1105,
  415. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light-My.otf', //字体文件
  416. 'fontSize' => 18, //字号
  417. 'fontColor' => '0,0,0', //字体颜色
  418. 'angle' => 0,
  419. ),
  420. array(
  421. 'text' => $data['user_nickname'],//TODO 用户名
  422. 'left' => 220,
  423. 'top' => 240,
  424. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件
  425. 'fontSize' => 20, //字号
  426. 'fontColor' => '0,0,0', //字体颜色
  427. 'angle' => 0,
  428. ),
  429. array(
  430. 'text' => implode($data['user_nickname_pinyin'], ' '),//TODO 用户名拼音
  431. 'left' => 220,
  432. 'top' => 260,
  433. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件
  434. 'fontSize' => 10, //字号
  435. 'fontColor' => '0,0,0', //字体颜色
  436. 'angle' => 0,
  437. ),
  438. ),
  439. 'image' => array(
  440. array(
  441. 'url' => $data['image'], //图片
  442. 'stream' => 0,
  443. 'left' => 94,
  444. 'top' => 359,
  445. 'right' => 0,
  446. 'bottom' => 0,
  447. 'width' => 561,
  448. 'height' => 613,
  449. 'opacity' => 100
  450. ),
  451. array(
  452. 'url' => $data['url'], //二维码资源
  453. 'stream' => 0,
  454. 'left' => 557,
  455. 'top' => 1013,
  456. 'right' => 0,
  457. 'bottom' => 0,
  458. 'width' => 99,
  459. 'height' => 99,
  460. 'opacity' => 100
  461. ),
  462. array(
  463. 'url' => $data['user_avatar'], //头像资源
  464. 'stream' => 0,
  465. 'left' => 98,
  466. 'top' => 211,
  467. 'right' => 0,
  468. 'bottom' => 0,
  469. 'width' => 92,
  470. 'height' => 92,
  471. 'opacity' => 100
  472. )
  473. ),
  474. 'background' => 'static/poster/product.jpg'
  475. );
  476. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  477. if (strlen($data['title']) < 27) {
  478. $text = array(
  479. 'text' => $data['title'],//TODO 标题
  480. 'left' => 130,
  481. 'top' => 1172,
  482. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  483. 'fontSize' => 22, //字号
  484. 'fontColor' => '255,255,255', //字体颜色
  485. 'angle' => 0,
  486. );
  487. array_push($config['text'], $text);
  488. } else {
  489. $titleOne = array(
  490. 'text' => mb_substr($data['title'], 0, 8) . '…',//TODO 标题
  491. 'left' => 130,
  492. 'top' => 1172,
  493. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  494. 'fontSize' => 22, //字号
  495. 'fontColor' => '255,255,255', //字体颜色
  496. 'angle' => 0,
  497. );
  498. /*$titleTwo = array(
  499. 'text' => mb_substr($data['title'], 12, 12),//TODO 标题
  500. 'left' => 60,
  501. 'top' => 1310,
  502. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  503. 'fontSize' => 40, //字号
  504. 'fontColor' => '255,255,255', //字体颜色
  505. 'angle' => 0,
  506. );*/
  507. array_push($config['text'], $titleOne);
  508. // array_push($config['text'], $titleTwo);
  509. }
  510. return self::setSharePoster($config, $path);
  511. }
  512. }