UtilService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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\Exception;
  10. use think\facade\Config;
  11. use dh2y\qrcode\QRcode;
  12. class UtilService
  13. {
  14. /**
  15. * 获取POST请求的数据
  16. * @param $params
  17. * @param null $request
  18. * @param bool $suffix
  19. * @return array
  20. * @throws \Exception
  21. */
  22. public static function postMore($params, $request = null, $suffix = false)
  23. {
  24. if ($request === null) $request = app('request');
  25. $p = [];
  26. $i = 0;
  27. foreach ($params as $param) {
  28. if (!is_array($param)) {
  29. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  30. } else {
  31. if (!isset($param[1])) $param[1] = null;
  32. if (!isset($param[2])) $param[2] = '';
  33. if (!isset($param[3])) $param[3] = '';
  34. if (!isset($param[4])) $param[4] = '';
  35. if (!isset($param[5])) $param[5] = '';
  36. if (is_array($param[0])) {
  37. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  38. $keyName = $param[0][0];
  39. } else {
  40. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  41. $keyName = $param[0];
  42. }
  43. $p[$suffix == true ? $i++ : (isset($param[3]) && $param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  44. if (!empty_check($param[4])) {
  45. if (!is_array($param[4])) {
  46. if (is_string($param[4]) && !function_exists($param[4])) {
  47. throw new UtilException('验证的方法' . $param[4] . '未定义');
  48. }
  49. if ($param[4]($request->param($name, $param[1], $param[2]))) {
  50. throw new UtilException($param[5]);
  51. }
  52. } else {
  53. foreach ($param[4] as $k => $v) {
  54. if (is_string($v) && !function_exists($v)) {
  55. throw new UtilException('验证的方法' . $v . '未定义');
  56. }
  57. if ($v($request->param($name, $param[1], $param[2]))) {
  58. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return $p;
  66. }
  67. /**
  68. * 获取请求的数据
  69. * @param $params
  70. * @param null $request
  71. * @param bool $suffix
  72. * @return array
  73. * @throws \Exception
  74. */
  75. public static function getMore($params, $request = null, $suffix = false)
  76. {
  77. if ($request === null) $request = app('request');
  78. $p = [];
  79. $i = 0;
  80. foreach ($params as $param) {
  81. if (!is_array($param)) {
  82. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  83. } else {
  84. if (!isset($param[1])) $param[1] = null;
  85. if (!isset($param[2])) $param[2] = '';
  86. if (!isset($param[3])) $param[3] = '';
  87. if (!isset($param[4])) $param[4] = '';
  88. if (!isset($param[5])) $param[5] = '';
  89. if (is_array($param[0])) {
  90. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  91. $keyName = $param[0][0];
  92. } else {
  93. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  94. $keyName = $param[0];
  95. }
  96. $p[$suffix == true ? $i++ : (isset($param[3]) && $param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  97. if (!empty_check($param[4])) {
  98. if (!is_array($param[4])) {
  99. if (is_string($param[4]) && !function_exists($param[4])) {
  100. throw new UtilException('验证的方法' . $param[4] . '未定义');
  101. }
  102. if ($param[4]($request->param($name, $param[1], $param[2]))) {
  103. throw new UtilException($param[5]);
  104. }
  105. } else {
  106. foreach ($param[4] as $k => $v) {
  107. if (is_string($v) && !function_exists($v)) {
  108. throw new UtilException('验证的方法' . $v . '未定义');
  109. }
  110. if ($v($request->param($name, $param[1], $param[2]))) {
  111. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return $p;
  119. }
  120. /**
  121. * TODO 砍价 拼团 分享海报生成
  122. * @param array $data
  123. * @param $path
  124. * @return array|bool|string
  125. * @throws \Exception
  126. */
  127. public
  128. static function setShareMarketingPoster($data = array(), $path)
  129. {
  130. $config = array(
  131. 'text' => array(
  132. array(
  133. 'text' => $data['price'],//TODO 价格
  134. 'left' => 116,
  135. 'top' => 200,
  136. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  137. 'fontSize' => 50, //字号
  138. 'fontColor' => '255,0,0', //字体颜色
  139. 'angle' => 0,
  140. ),
  141. array(
  142. 'text' => $data['label'],//TODO 标签
  143. 'left' => 450,
  144. 'top' => 188,
  145. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  146. 'fontSize' => 24, //字号
  147. 'fontColor' => '255,255,255', //字体颜色
  148. 'angle' => 0,
  149. ),
  150. array(
  151. 'text' => $data['msg'],//TODO 简述
  152. 'left' => 80,
  153. 'top' => 270,
  154. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  155. 'fontSize' => 22, //字号
  156. 'fontColor' => '40,40,40', //字体颜色
  157. 'angle' => 0,
  158. )
  159. ),
  160. 'image' => array(
  161. array(
  162. 'url' => $data['image'], //图片
  163. 'stream' => 0,
  164. 'left' => 120,
  165. 'top' => 340,
  166. 'right' => 0,
  167. 'bottom' => 0,
  168. 'width' => 450,
  169. 'height' => 450,
  170. 'opacity' => 100
  171. ),
  172. array(
  173. 'url' => $data['url'], //二维码资源
  174. 'stream' => 0,
  175. 'left' => 260,
  176. 'top' => 890,
  177. 'right' => 0,
  178. 'bottom' => 0,
  179. 'width' => 160,
  180. 'height' => 160,
  181. 'opacity' => 100
  182. )
  183. ),
  184. 'background' => 'statics/poster/poster.jpg'
  185. );
  186. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  187. if (strlen($data['title']) < 36) {
  188. $text = array(
  189. 'text' => $data['title'],//TODO 标题
  190. 'left' => 76,
  191. 'top' => 100,
  192. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  193. 'fontSize' => 32, //字号
  194. 'fontColor' => '0,0,0', //字体颜色
  195. 'angle' => 0,
  196. );
  197. array_push($config['text'], $text);
  198. } else {
  199. $titleOne = array(
  200. 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题
  201. 'left' => 76,
  202. 'top' => 70,
  203. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  204. 'fontSize' => 32, //字号
  205. 'fontColor' => '0,0,0', //字体颜色
  206. 'angle' => 0,
  207. );
  208. $titleTwo = array(
  209. 'text' => mb_strimwidth($data['title'], mb_strlen(mb_strimwidth($data['title'], 0, 24)), 24),//TODO 标题
  210. 'left' => 76,
  211. 'top' => 120,
  212. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  213. 'fontSize' => 32, //字号
  214. 'fontColor' => '0,0,0', //字体颜色
  215. 'angle' => 0,
  216. );
  217. array_push($config['text'], $titleOne);
  218. array_push($config['text'], $titleTwo);
  219. }
  220. return self::setSharePoster($config, $path);
  221. }
  222. /**
  223. * TODO 生成分享二维码图片
  224. * @param array $config
  225. * @param $path
  226. * @return array|bool|string
  227. * @throws \Exception
  228. */
  229. public
  230. static function setSharePoster($config = array(), $path)
  231. {
  232. $imageDefault = array(
  233. 'left' => 0,
  234. 'top' => 0,
  235. 'right' => 0,
  236. 'bottom' => 0,
  237. 'width' => 100,
  238. 'height' => 100,
  239. 'opacity' => 100
  240. );
  241. $textDefault = array(
  242. 'text' => '',
  243. 'left' => 0,
  244. 'top' => 0,
  245. 'fontSize' => 32, //字号
  246. 'fontColor' => '255,255,255', //字体颜色
  247. 'angle' => 0,
  248. );
  249. $background = $config['background'];//海报最底层得背景
  250. if (substr($background, 0, 1) === '/') {
  251. $background = substr($background, 1);
  252. }
  253. $backgroundInfo = getimagesize($background);
  254. $background = imagecreatefromstring(file_get_contents($background));
  255. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  256. $backgroundHeight = $backgroundInfo[1]; //背景高度
  257. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  258. $color = imagecolorallocate($imageRes, 0, 0, 0);
  259. imagefill($imageRes, 0, 0, $color);
  260. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  261. if (!empty($config['image'])) {
  262. foreach ($config['image'] as $key => $val) {
  263. $val = array_merge($imageDefault, $val);
  264. $info = getimagesize($val['url']);
  265. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  266. if ($val['stream']) {
  267. $info = getimagesizefromstring($val['url']);
  268. $function = 'imagecreatefromstring';
  269. }
  270. $res = $function($val['url']);
  271. $resWidth = $info[0];
  272. $resHeight = $info[1];
  273. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  274. imagefill($canvas, 0, 0, $color);
  275. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  276. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  277. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  278. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  279. }
  280. }
  281. if (isset($config['text']) && !empty($config['text'])) {
  282. foreach ($config['text'] as $key => $val) {
  283. $val = array_merge($textDefault, $val);
  284. list($R, $G, $B) = explode(',', $val['fontColor']);
  285. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  286. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  287. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  288. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  289. }
  290. }
  291. ob_start();
  292. imagejpeg($imageRes);
  293. imagedestroy($imageRes);
  294. $res = ob_get_contents();
  295. ob_end_clean();
  296. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  297. $uploadType = (int)sys_config('upload_type', 1);
  298. $upload = UploadService::init();
  299. $res = $upload->to($path)->validate()->stream($res, $key);
  300. if ($res === false) {
  301. return $upload->getError();
  302. } else {
  303. $info = $upload->getUploadInfo();
  304. $info['image_type'] = $uploadType;
  305. return $info;
  306. }
  307. }
  308. /**
  309. * TODO 获取小程序二维码是否生成
  310. * @param $url
  311. * @return array
  312. */
  313. public
  314. static function remoteImage($url)
  315. {
  316. $curl = curl_init();
  317. curl_setopt($curl, CURLOPT_URL, $url);
  318. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  319. $result = curl_exec($curl);
  320. $result = json_decode($result, true);
  321. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  322. return ['status' => true];
  323. }
  324. /**
  325. * TODO 修改 https 和 http 移动到common
  326. * @param $url $url 域名
  327. * @param int $type 0 返回https 1 返回 http
  328. * @return string
  329. */
  330. public
  331. 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
  348. static function getQRCodePath($url, $name)
  349. {
  350. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  351. try {
  352. $uploadType = sys_config('upload_type');
  353. //TODO 没有选择默认使用本地上传
  354. if (!$uploadType) $uploadType = 1;
  355. $uploadType = (int)$uploadType;
  356. $siteUrl = sys_config('site_url');
  357. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  358. $info = [];
  359. $outfile = Config::get('qrcode.cache_dir');
  360. $code = new QRcode();
  361. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  362. $content = file_get_contents('.' . $wapCodePath);
  363. if ($uploadType === 1) {
  364. $info["code"] = 200;
  365. $info["name"] = $name;
  366. $info["dir"] = $wapCodePath;
  367. $info["time"] = time();
  368. $info['size'] = 0;
  369. $info['type'] = 'image/png';
  370. $info["image_type"] = 1;
  371. $info['thumb_path'] = $wapCodePath;
  372. return $info;
  373. } else {
  374. $upload = UploadService::init($uploadType);
  375. $res = $upload->to($outfile)->validate()->stream($content, $name);
  376. if ($res === false) {
  377. return $upload->getError();
  378. }
  379. $info = $upload->getUploadInfo();
  380. $info['image_type'] = $uploadType;
  381. return $info;
  382. }
  383. } catch (\Exception $e) {
  384. return $e->getMessage();
  385. }
  386. }
  387. /**分级返回下级所有分类ID
  388. * @param $data
  389. * @param string $children
  390. * @param string $field
  391. * @param string $pk
  392. * @return string
  393. */
  394. public
  395. static function getChildrenPid($data, $pid, $field = 'pid', $pk = 'id')
  396. {
  397. static $pids = '';
  398. foreach ($data as $k => $res) {
  399. if ($res[$field] == $pid) {
  400. $pids .= ',' . $res[$pk];
  401. self::getChildrenPid($data, $res[$pk], $field, $pk);
  402. }
  403. }
  404. return $pids;
  405. }
  406. }