UtilService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ UtilService ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:30
  10. // +----------------------------------------------------------------------
  11. namespace library\services;
  12. use library\exceptions\UtilException;
  13. use library\services\upload\Upload as Upload;
  14. use think\facade\Config;
  15. use dh2y\qrcode\QRcode;
  16. class UtilService {
  17. /**
  18. * 参数判断模型
  19. * @param $params [
  20. * [ 'parm1' => 'post|get',
  21. * 'parm2' => '',//默认值
  22. * 'parm3' => 'empty',//函数
  23. * 'parm4' => '错误信息',
  24. * ]
  25. * ]
  26. * @param null $request
  27. * @param bool $suffix
  28. */
  29. public static function getMore($params, $request = null, $suffix = false)
  30. {
  31. $fiAr = ['empty','is_null','isset'];
  32. if ($request === null) {
  33. $request = request();
  34. }
  35. $i = 0;
  36. foreach ($params as $param) {
  37. if (!is_array($param)) {
  38. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  39. } else {
  40. if (!isset($param[1])) $param[1] = null;
  41. if (!isset($param[2])) $param[2] = '';
  42. if (!isset($param[3])) $param[3] = '';
  43. if (is_array($param[0])) {
  44. $name = $param[0][0];
  45. $keyName = $param[0][0];
  46. foreach ($param[0] as $k=>$v) {
  47. $name = $k;
  48. $keyName = $v;
  49. }
  50. } else {
  51. $name = $param[0];
  52. $keyName = $param[0];
  53. }
  54. $value = $request->param($name, $param[1]);
  55. $p[$suffix == true ? $i++ : $keyName ] = $value;
  56. if(in_array($param[2],$fiAr)) {
  57. if($param[2] == 'empty') {
  58. if(empty($value)) {
  59. throw new UtilException($param[3]);
  60. }
  61. }
  62. if($param[2] == 'is_null') {
  63. if(is_null($value)) {
  64. throw new UtilException($param[3]);
  65. }
  66. }
  67. if($param[2] == 'isset') {
  68. if(!isset($value)) {
  69. throw new UtilException($param[3]);
  70. }
  71. }
  72. } else {
  73. if (!empty($param[2]) && is_string($param[2]) && !function_exists($param[2])) {
  74. throw new UtilException('方法' . $param[2] . '不存在');
  75. }
  76. if (!empty($param[2]) && !$param[2]($request->param($name, $param[1]))) {
  77. throw new UtilException($param[3]);
  78. }
  79. }
  80. }
  81. }
  82. return $p;
  83. }
  84. /**
  85. * 参数解析 | 返回
  86. * @param $params
  87. * @param $data
  88. */
  89. public static function getParam($params,$data) {
  90. $result = [];
  91. foreach ($data as $k => $vv) {
  92. $d = [];
  93. foreach ($params as $v) {
  94. if (is_array($v)) {
  95. $alias = empty($v[0]) ? $v[1] : $v[0];
  96. $key = $v[1];
  97. $callStr = empty($v[2]) ? '' : $v[2];
  98. $val = $vv[$key];
  99. if($callStr instanceof \Closure) {
  100. $d[$alias] = $callStr($val);
  101. }
  102. else if(!empty($callStr)){
  103. $e = null;
  104. $callStr = str_replace('$1', '$val', $callStr);
  105. eval('$e =' . $callStr . ';');
  106. $d[$alias] = $e;
  107. }
  108. else {
  109. $d[$alias] = $val;
  110. }
  111. } else {
  112. $d[$v] = $vv[$v];
  113. }
  114. }
  115. $result[] = $d;
  116. }
  117. return $result;
  118. }
  119. /**
  120. * 获取POST请求的数据
  121. * @param $params
  122. * @param null $request
  123. * @param bool $suffix
  124. * @return array
  125. * @throws \Exception
  126. */
  127. public static function postMore($params, $request = null, $suffix = false)
  128. {
  129. if ($request === null) $request = app('request');
  130. $p = [];
  131. $i = 0;
  132. foreach ($params as $param) {
  133. if (!is_array($param)) {
  134. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  135. } else {
  136. if (!isset($param[1])) $param[1] = null;
  137. if (!isset($param[2])) $param[2] = '';
  138. if (!isset($param[3])) $param[3] = '';
  139. if (!isset($param[4])) $param[4] = '';
  140. if (!isset($param[5])) $param[5] = '';
  141. if (is_array($param[0])) {
  142. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  143. $keyName = $param[0][0];
  144. } else {
  145. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  146. $keyName = $param[0];
  147. }
  148. $p[$suffix == true ? $i++ : (isset($param[3]) && $param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  149. if (!empty_check($param[4])) {
  150. if (!is_array($param[4])) {
  151. if (is_string($param[4]) && !function_exists($param[4])) {
  152. throw new UtilException('验证的方法' . $param[4] . '未定义');
  153. }
  154. if ($param[4]($request->param($name, $param[1], $param[2]))) {
  155. throw new UtilException($param[5]);
  156. }
  157. } else {
  158. foreach ($param[4] as $k => $v) {
  159. if (is_string($v) && !function_exists($v)) {
  160. throw new UtilException('验证的方法' . $v . '未定义');
  161. }
  162. if ($v($request->param($name, $param[1], $param[2]))) {
  163. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return $p;
  171. }
  172. /**
  173. * TODO 生成分享二维码图片
  174. * @param array $config
  175. * @param $path
  176. * @return array|bool|string
  177. * @throws \Exception
  178. */
  179. public static function setSharePoster($config = array(), $path)
  180. {
  181. $imageDefault = array(
  182. 'left' => 0,
  183. 'top' => 0,
  184. 'right' => 0,
  185. 'bottom' => 0,
  186. 'width' => 100,
  187. 'height' => 100,
  188. 'opacity' => 100
  189. );
  190. $textDefault = array(
  191. 'text' => '',
  192. 'left' => 0,
  193. 'top' => 0,
  194. 'fontSize' => 32, //字号
  195. 'fontColor' => '255,255,255', //字体颜色
  196. 'angle' => 0,
  197. );
  198. $background = $config['background'];//海报最底层得背景
  199. if (substr($background, 0, 1) === '/') {
  200. $background = substr($background, 1);
  201. }
  202. $backgroundInfo = getimagesize($background);
  203. $background = imagecreatefromstring(file_get_contents($background));
  204. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  205. $backgroundHeight = $backgroundInfo[1]; //背景高度
  206. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  207. $color = imagecolorallocate($imageRes, 0, 0, 0);
  208. imagefill($imageRes, 0, 0, $color);
  209. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  210. if (!empty($config['image'])) {
  211. foreach ($config['image'] as $key => $val) {
  212. $val = array_merge($imageDefault, $val);
  213. $info = getimagesize($val['url']);
  214. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  215. if ($val['stream']) {
  216. $info = getimagesizefromstring($val['url']);
  217. $function = 'imagecreatefromstring';
  218. }
  219. $res = $function($val['url']);
  220. $resWidth = $info[0];
  221. $resHeight = $info[1];
  222. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  223. imagefill($canvas, 0, 0, $color);
  224. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  225. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  226. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  227. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  228. }
  229. }
  230. if (isset($config['text']) && !empty($config['text'])) {
  231. foreach ($config['text'] as $key => $val) {
  232. $val = array_merge($textDefault, $val);
  233. list($R, $G, $B) = explode(',', $val['fontColor']);
  234. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  235. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  236. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  237. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  238. }
  239. }
  240. ob_start();
  241. imagejpeg($imageRes);
  242. imagedestroy($imageRes);
  243. $res = ob_get_contents();
  244. ob_end_clean();
  245. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  246. $uploadType = (int)sys_config('upload_type', 1);
  247. $upload = UploadService::init();
  248. $res = $upload->to($path)->validate()->stream($res, $key);
  249. if ($res === false) {
  250. return $upload->getError();
  251. } else {
  252. $info = $upload->getUploadInfo();
  253. $info['image_type'] = $uploadType;
  254. return $info;
  255. }
  256. }
  257. /**
  258. * TODO 获取小程序二维码是否生成
  259. * @param $url
  260. * @return array
  261. */
  262. public static function remoteImage($url)
  263. {
  264. $curl = curl_init();
  265. curl_setopt($curl, CURLOPT_URL, $url);
  266. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  267. $result = curl_exec($curl);
  268. $result = json_decode($result, true);
  269. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  270. return ['status' => true];
  271. }
  272. /**
  273. * TODO 修改 https 和 http 移动到common
  274. * @param $url $url 域名
  275. * @param int $type 0 返回https 1 返回 http
  276. * @return string
  277. */
  278. public static function setHttpType($url, $type = 0)
  279. {
  280. $domainTop = substr($url, 0, 5);
  281. if ($type) {
  282. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  283. } else {
  284. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  285. }
  286. return $url;
  287. }
  288. /**
  289. * 获取二维码
  290. * @param $url
  291. * @param $name
  292. * @return array|bool|string
  293. */
  294. public static function getQRCodePath($url, $name)
  295. {
  296. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  297. try {
  298. $uploadType = 2;
  299. $info = [];
  300. $outfile = Config::get('qrcode.cache_dir');
  301. $code = new QRcode();
  302. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  303. $content = file_get_contents('.' . $wapCodePath);
  304. if ($uploadType === 1) {
  305. $info["code"] = 200;
  306. $info["name"] = $name;
  307. $info["dir"] = $wapCodePath;
  308. $info["time"] = time();
  309. $info['size'] = 0;
  310. $info['type'] = 'image/png';
  311. $info["image_type"] = 1;
  312. $info['thumb_path'] = $wapCodePath;
  313. return $info;
  314. } else {
  315. $upload = new Upload(2,config('qiniu')['upload']);
  316. $res = $upload->to($outfile)->validate()->stream($content, $name);
  317. if ($res === false) {
  318. return $upload->getError();
  319. }
  320. $info = $upload->getUploadInfo();
  321. $info['image_type'] = $uploadType;
  322. return $info;
  323. }
  324. } catch (\Exception $e) {
  325. return $e->getMessage();
  326. }
  327. }
  328. }