UtilService.php 16 KB

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