UtilService.php 15 KB

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