common.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. // 应用公共文件
  3. if (!function_exists('isDebug')) {
  4. function isDebug(): bool
  5. {
  6. return !!env('APP_DEBUG');
  7. }
  8. }
  9. if (!function_exists('formToData')) {
  10. function formToData($form): array
  11. {
  12. $rule = $form->formRule();
  13. $action = $form->getAction();
  14. $method = $form->getMethod();
  15. $title = $form->getTitle();
  16. $config = (object)$form->formConfig();
  17. return compact('rule', 'action', 'method', 'title', 'config');
  18. }
  19. }
  20. /**
  21. * 无线级分类处理
  22. *
  23. * @param array $data 数据源
  24. * @param string $idName 主键
  25. * @param string $fieldName 父级字段
  26. * @param string $childrenKey 子级字段名
  27. * @return array
  28. * @author 张先生
  29. * @date 2020-03-27
  30. */
  31. if (!function_exists('formatCategory')) {
  32. function formatCategory(array $data, string $idName = "id", string $fieldName = 'pid', $childrenKey = 'children')
  33. {
  34. $items = [];
  35. foreach ($data as $item) {
  36. $items[$item[$idName]] = $item;
  37. }
  38. $result = array();
  39. foreach ($items as $item) {
  40. if (isset($items[$item[$fieldName]])) {
  41. $items[$item[$fieldName]][$childrenKey][] = &$items[$item[$idName]];
  42. } else if ($item[$fieldName] == 0) {
  43. $result[] = &$items[$item[$idName]];
  44. }
  45. }
  46. return $result;
  47. }
  48. }
  49. if (!function_exists('formatTreeList')) {
  50. function formatTreeList(&$options, $name, $pidName = 'pid', $pid = 0, $level = 0, &$data = []): array
  51. {
  52. $_options = $options;
  53. foreach ($_options as $k => $option) {
  54. if ($option[$pidName] == $pid) {
  55. $data[] = ['value' => $k, 'label' => str_repeat('|---', $level + 1) . $option[$name]];
  56. unset($options[$k]);
  57. formatTreeList($options, $name, $pidName, $k, $level + 1, $data);
  58. }
  59. }
  60. return $data;
  61. }
  62. }
  63. if (!function_exists('formatTree')) {
  64. function formatTree(&$options, $name, $pidName = 'pid', $pid = 0, $level = 0, $data = []): array
  65. {
  66. $_options = $options;
  67. foreach ($_options as $k => $option) {
  68. if ($option[$pidName] == $pid) {
  69. $value = ['id' => $k, 'title' => $option[$name]];
  70. unset($options[$k]);
  71. $value['children'] = formatTree($options, $name, $pidName, $k, $level + 1);
  72. $data[] = $value;
  73. }
  74. }
  75. return $data;
  76. }
  77. }
  78. if (!function_exists('formatCascaderData')) {
  79. function formatCascaderData(&$options, $name, $baseLevel = 0, $pidName = 'pid', $pid = 0, $level = 0, $data = []): array
  80. {
  81. $_options = $options;
  82. foreach ($_options as $k => $option) {
  83. if ($option[$pidName] == $pid) {
  84. $value = ['value' => $k, 'label' => $option[$name]];
  85. unset($options[$k]);
  86. $value['children'] = formatCascaderData($options, $name, $baseLevel, $pidName, $k, $level + 1);
  87. if (!count($value['children'])) unset($value['children']);
  88. $data[] = $value;
  89. }
  90. }
  91. return $data;
  92. }
  93. }
  94. /**
  95. * @function toMap 数组重新组装
  96. * @param array $data 数据
  97. * @param string $field key
  98. * @param string $value value default null
  99. * @return array
  100. * @author 张先生
  101. * @date 2020-04-01
  102. */
  103. if (!function_exists('toMap')) {
  104. function toMap(array $data, $field = 'id', $value = '')
  105. {
  106. $result = array();
  107. if (empty($data)) {
  108. return $result;
  109. }
  110. //开始处理数据
  111. foreach ($data as $item) {
  112. $val = $item;
  113. if (!empty($value)) {
  114. $val = $item[$value];
  115. }
  116. $result[$item[$field]] = $val;
  117. }
  118. return $result;
  119. }
  120. }
  121. /**
  122. * @function getUniqueListByArray 从数组中获取某个字段的值,重新拼装成新的一维数组
  123. * @param array $data 数据
  124. * @param string $field key
  125. * @return array
  126. * @author 张先生
  127. * @date 2020-04-01
  128. */
  129. if (!function_exists('getUniqueListByArray')) {
  130. function getUniqueListByArray(array $data, $field = 'id')
  131. {
  132. return array_unique(array_values(array_column($data, $field)));
  133. }
  134. }
  135. if (!function_exists('isPhone')) {
  136. function isPhone($test)
  137. {
  138. return !preg_match("/^1[3456789]{1}\d{9}$/", $test);
  139. }
  140. }
  141. if (!function_exists('getMonth')) {
  142. /**
  143. * 获取本季度 time
  144. * @param int|string $time
  145. * @param $ceil
  146. * @return array
  147. */
  148. function getMonth($time = '', $ceil = 0)
  149. {
  150. if ($ceil != 0)
  151. $season = ceil(date('n') / 3) - $ceil;
  152. else
  153. $season = ceil(date('n') / 3);
  154. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  155. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  156. return array($firstday, $lastday);
  157. }
  158. }
  159. if (!function_exists('getMonthTime')) {
  160. /**
  161. * 获取本季度 time
  162. * @param int|string $time
  163. * @param $ceil
  164. * @return array
  165. */
  166. function getMonthTime($time = '', $ceil = 0)
  167. {
  168. if ($ceil != 0)
  169. $season = ceil(date('n') / 3) - $ceil;
  170. else
  171. $season = ceil(date('n') / 3);
  172. $firstday = mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y'));
  173. $lastday =mktime(0, 0, 0, $season * 3, 1, date('Y'));
  174. return array($firstday, $lastday);
  175. }
  176. }
  177. if (!function_exists('getModelTime')) {
  178. /**
  179. * @param \think\db\BaseQuery $model
  180. * @param string $section
  181. * @param string $prefix
  182. * @param string $field
  183. * @return mixed
  184. * @author xaboy
  185. * @day 2020-04-29
  186. */
  187. function getModelTime(\think\db\BaseQuery $model, string $section, $prefix = 'create_time', $field = '-')
  188. {
  189. if (!isset($section)) return $model;
  190. switch ($section) {
  191. case 'today':
  192. $model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('today')), date('Y-m-d H:i:s', strtotime('tomorrow -1second'))]);
  193. break;
  194. case 'week':
  195. $model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('this week 00:00:00')), date('Y-m-d H:i:s', strtotime('next week 00:00:00 -1second'))]);
  196. break;
  197. case 'month':
  198. $model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('first Day of this month 00:00:00')), date('Y-m-d H:i:s', strtotime('first Day of next month 00:00:00 -1second'))]);
  199. break;
  200. case 'year':
  201. $model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('this year 1/1')), date('Y-m-d H:i:s', strtotime('next year 1/1 -1second'))]);
  202. break;
  203. case 'yesterday':
  204. $model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('yesterday')), date('Y-m-d H:i:s', strtotime('today -1second'))]);
  205. break;
  206. case 'quarter':
  207. list($startTime, $endTime) = getMonth();
  208. $model = $model->where($prefix, '>', $startTime);
  209. $model = $model->where($prefix, '<', $endTime);
  210. break;
  211. case 'lately7':
  212. $model = $model->where($prefix, 'between', [date('Y-m-d', strtotime("-7 day")), date('Y-m-d H:i:s')]);
  213. break;
  214. case 'lately30':
  215. $model = $model->where($prefix, 'between', [date('Y-m-d', strtotime("-30 day")), date('Y-m-d H:i:s')]);
  216. break;
  217. default:
  218. if (strstr($section, $field) !== false) {
  219. list($startTime, $endTime) = explode($field, $section);
  220. if (strlen($startTime) == 4) {
  221. $model->whereBetweenTime($prefix, date('Y-m-d H:i:s', strtotime($startTime)), date('Y-m-d H:i:s', strtotime($startTime . ' +1day -1second')));
  222. } else {
  223. // if ($startTime == $endTime) {
  224. // $model = $model->whereDay($prefix, $startTime);
  225. // } else {
  226. $model = $model->where($prefix, '>', date('Y-m-d H:i:s', strtotime($startTime)));
  227. $model = $model->where($prefix, '<', date('Y-m-d H:i:s', strtotime($endTime)));
  228. // }
  229. }
  230. }
  231. break;
  232. }
  233. return $model;
  234. }
  235. }
  236. if (!function_exists('systemConfig')) {
  237. /**
  238. * 获取系统配置
  239. *
  240. * @param string|string[] $key
  241. * @return mixed
  242. * @author xaboy
  243. * @day 2020-05-08
  244. */
  245. function systemConfig($key)
  246. {
  247. $make = app()->make(\app\common\repositories\system\config\ConfigValueRepository::class);
  248. if (is_array($key)) {
  249. return $make->more($key, 0);
  250. } else {
  251. return $make->get($key, 0);
  252. }
  253. }
  254. }
  255. if (!function_exists('getDatesBetweenTwoDays')) {
  256. function getDatesBetweenTwoDays($startDate, $endDate)
  257. {
  258. $dates = [];
  259. if (strtotime($startDate) > strtotime($endDate)) {
  260. //如果开始日期大于结束日期,直接return 防止下面的循环出现死循环
  261. return $dates;
  262. } elseif ($startDate == $endDate) {
  263. //开始日期与结束日期是同一天时
  264. array_push($dates, date('m-d', strtotime($startDate)));
  265. return $dates;
  266. } else {
  267. array_push($dates, date('m-d', strtotime($startDate)));
  268. $currentDate = $startDate;
  269. do {
  270. $nextDate = date('Y-m-d', strtotime($currentDate . ' +1 days'));
  271. array_push($dates, date('m-d', strtotime($currentDate . ' +1 days')));
  272. $currentDate = $nextDate;
  273. } while ($endDate != $currentDate);
  274. return $dates;
  275. }
  276. }
  277. }
  278. if (!function_exists('getStartModelTime')) {
  279. function getStartModelTime(string $section)
  280. {
  281. switch ($section) {
  282. case 'today':
  283. case 'yesterday':
  284. return date('Y-m-d', strtotime($section));
  285. case 'week':
  286. return date('Y-m-d', strtotime('this week'));
  287. case 'month':
  288. return date('Y-m-d', strtotime('first Day of this month'));
  289. case 'year':
  290. return date('Y-m-d', strtotime('this year 1/1'));
  291. case 'quarter':
  292. list($startTime, $endTime) = getMonth();
  293. return $startTime;
  294. case 'lately7':
  295. return date('Y-m-d', strtotime("-7 day"));
  296. case 'lately30':
  297. return date('Y-m-d', strtotime("-30 day"));
  298. default:
  299. if (strstr($section, '-') !== false) {
  300. list($startTime, $endTime) = explode('-', $section);
  301. return date('Y-m-d H:i:s', strtotime($startTime));
  302. }
  303. return date('Y-m-d H:i:s');
  304. }
  305. }
  306. }
  307. if (!function_exists('merchantConfig')) {
  308. /**
  309. * 获取商户配置
  310. *
  311. * @param int $merId
  312. * @param string|string[] $key
  313. * @return mixed
  314. * @author xaboy
  315. * @day 2020-05-08
  316. */
  317. function merchantConfig(int $merId, $key)
  318. {
  319. $make = app()->make(\app\common\repositories\system\config\ConfigValueRepository::class);
  320. if (is_array($key)) {
  321. return $make->more($key, $merId);
  322. } else {
  323. return $make->get($key, $merId);
  324. }
  325. }
  326. }
  327. if (!function_exists('systemGroupData')) {
  328. /**
  329. * 获取总后台组合数据配置
  330. *
  331. * @param string $key
  332. * @param int|null $page
  333. * @param int|null $limit
  334. * @return array
  335. * @author xaboy
  336. * @day 2020/5/27
  337. */
  338. function systemGroupData(string $key, ?int $page = null, ?int $limit = 10)
  339. {
  340. $make = app()->make(\app\common\repositories\system\groupData\GroupDataRepository::class);
  341. return $make->groupData($key, 0, $page, $limit);
  342. }
  343. }
  344. if (!function_exists('merchantGroupData')) {
  345. /**
  346. * 获取商户后台组合数据配置
  347. *
  348. * @param int $merId
  349. * @param string $key
  350. * @param int|null $page
  351. * @param int|null $limit
  352. * @return array
  353. * @author xaboy
  354. * @day 2020/5/27
  355. */
  356. function merchantGroupData(int $merId, string $key, ?int $page = null, ?int $limit = 10)
  357. {
  358. $make = app()->make(\app\common\repositories\system\groupData\GroupDataRepository::class);
  359. return $make->groupData($key, $merId, $page, $limit);
  360. }
  361. }
  362. if (!function_exists('filter_emoji')) {
  363. // 过滤掉emoji表情
  364. function filter_emoji($str)
  365. {
  366. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  367. '/./u',
  368. function (array $match) {
  369. return strlen($match[0]) >= 4 ? '' : $match[0];
  370. },
  371. $str);
  372. return $str;
  373. }
  374. }
  375. if (!function_exists('setHttpType')) {
  376. /**
  377. * TODO 修改 https 和 http 移动到common
  378. * @param $url $url 域名
  379. * @param int $type 0 返回https 1 返回 http
  380. * @return string
  381. */
  382. function setHttpType($url, $type = 0)
  383. {
  384. $domainTop = substr($url, 0, 5);
  385. if ($type) {
  386. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  387. } else {
  388. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  389. }
  390. return $url;
  391. }
  392. }
  393. if (!function_exists('remoteImage')) {
  394. /**
  395. * TODO 获取小程序二维码是否生成
  396. * @param $url
  397. * @return array
  398. */
  399. function remoteImage($url)
  400. {
  401. $curl = curl_init();
  402. curl_setopt($curl, CURLOPT_URL, $url);
  403. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  404. $result = curl_exec($curl);
  405. $result = json_decode($result, true);
  406. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  407. return ['status' => true];
  408. }
  409. }
  410. if (!function_exists('image_to_base64')) {
  411. /**
  412. * 获取图片转为base64
  413. * @param string $avatar
  414. * @return bool|string
  415. */
  416. function image_to_base64($avatar = '', $timeout = 9)
  417. {
  418. try {
  419. $url = parse_url($avatar);
  420. $url = $url['host'];
  421. $header = [
  422. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  423. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  424. 'Accept-Encoding: gzip, deflate, br',
  425. 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  426. 'Host:' . $url
  427. ];
  428. $dir = pathinfo($url);
  429. $host = $dir['dirname'];
  430. $refer = $host . '/';
  431. $curl = curl_init();
  432. curl_setopt($curl, CURLOPT_REFERER, $refer);
  433. curl_setopt($curl, CURLOPT_URL, $avatar);
  434. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  435. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  436. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  437. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  438. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  439. $data = curl_exec($curl);
  440. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  441. curl_close($curl);
  442. if ($code == 200) {
  443. return "data:image/jpeg;base64," . base64_encode($data);
  444. } else {
  445. return false;
  446. }
  447. } catch (\Exception $e) {
  448. return false;
  449. }
  450. }
  451. }
  452. if (!function_exists('put_image')) {
  453. /**
  454. * 获取图片转为base64
  455. * @param string $avatar
  456. * @return bool|string
  457. */
  458. function put_image($url, $filename = '')
  459. {
  460. if ($url == '') {
  461. return false;
  462. }
  463. try {
  464. if ($filename == '') {
  465. $ext = pathinfo($url);
  466. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  467. return false;
  468. }
  469. $filename = time() . "." . $ext['extension'];
  470. }
  471. //文件保存路径
  472. ob_start();
  473. readfile($url);
  474. $img = ob_get_contents();
  475. ob_end_clean();
  476. $path = 'public/uploads/qrcode';
  477. $fp2 = fopen($path . '/' . $filename, 'a');
  478. fwrite($fp2, $img);
  479. fclose($fp2);
  480. return $path . '/' . $filename;
  481. } catch (\Exception $e) {
  482. return false;
  483. }
  484. }
  485. }
  486. if (!function_exists('path_to_url')) {
  487. /**
  488. * 路径转url路径
  489. * @param $path
  490. * @return string
  491. */
  492. function path_to_url($path)
  493. {
  494. return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
  495. }
  496. }
  497. if (!function_exists('curl_file_exist')) {
  498. /**
  499. * CURL 检测远程文件是否在
  500. * @param $url
  501. * @return bool
  502. */
  503. function curl_file_exist($url)
  504. {
  505. $ch = curl_init();
  506. try {
  507. curl_setopt($ch, CURLOPT_URL, $url);
  508. curl_setopt($ch, CURLOPT_HEADER, 1);
  509. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  510. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  511. $contents = curl_exec($ch);
  512. if (preg_match("/404/", $contents)) return false;
  513. if (preg_match("/403/", $contents)) return false;
  514. return true;
  515. } catch (\Exception $e) {
  516. return false;
  517. }
  518. }
  519. }
  520. if (!function_exists('set_http_type')) {
  521. /**
  522. * 修改 https 和 http
  523. * @param $url $url 域名
  524. * @param int $type 0 返回https 1 返回 http
  525. * @return string
  526. */
  527. function set_http_type($url, $type = 0)
  528. {
  529. $domainTop = substr($url, 0, 5);
  530. if ($type) {
  531. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  532. } else {
  533. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  534. }
  535. return $url;
  536. }
  537. }
  538. if (!function_exists('setSharePoster')) {
  539. /**
  540. * TODO 生成分享二维码图片
  541. * @param array $config
  542. * @param $path
  543. * @return array|bool|string
  544. * @throws \Exception
  545. */
  546. function setSharePoster($config, $path)
  547. {
  548. $imageDefault = array(
  549. 'left' => 0,
  550. 'top' => 0,
  551. 'right' => 0,
  552. 'bottom' => 0,
  553. 'width' => 100,
  554. 'height' => 100,
  555. 'opacity' => 100
  556. );
  557. $textDefault = array(
  558. 'text' => '',
  559. 'left' => 0,
  560. 'top' => 0,
  561. 'fontSize' => 32, //字号
  562. 'fontColor' => '255,255,255', //字体颜色
  563. 'angle' => 0,
  564. );
  565. $background = $config['background'];//海报最底层得背景
  566. if (substr($background, 0, 1) === '/') {
  567. $background = substr($background, 1);
  568. }
  569. $backgroundInfo = getimagesize($background);
  570. $background = imagecreatefromstring(file_get_contents($background));
  571. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  572. $backgroundHeight = $backgroundInfo[1]; //背景高度
  573. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  574. // $color = imagecolorallocate($imageRes, 0, 0, 0);
  575. $color = imagecolorallocate($imageRes, 255, 255, 255);
  576. imagefill($imageRes, 0, 0, $color);
  577. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  578. if (!empty($config['image'])) {
  579. foreach ($config['image'] as $key => $val) {
  580. $val = array_merge($imageDefault, $val);
  581. $info = getimagesize($val['url']);
  582. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  583. if ($val['stream']) {
  584. $info = getimagesizefromstring($val['url']);
  585. $function = 'imagecreatefromstring';
  586. }
  587. $res = $function($val['url']);
  588. $resWidth = $info[0];
  589. $resHeight = $info[1];
  590. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  591. imagefill($canvas, 0, 0, $color);
  592. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  593. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  594. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  595. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  596. }
  597. }
  598. if (isset($config['text']) && !empty($config['text'])) {
  599. foreach ($config['text'] as $key => $val) {
  600. $val = array_merge($textDefault, $val);
  601. list($R, $G, $B) = explode(',', $val['fontColor']);
  602. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  603. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  604. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  605. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  606. }
  607. }
  608. ob_start();
  609. imagejpeg($imageRes);
  610. imagedestroy($imageRes);
  611. $res = ob_get_contents();
  612. ob_end_clean();
  613. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  614. $uploadType = (int)systemConfig('upload_type') ?: 1;
  615. $upload = \crmeb\services\UploadService::create($uploadType);
  616. if($uploadType != 1){
  617. $time = date('Y-m-d');
  618. $paths = "chuxubao/qrCode/" . $time . '/';
  619. $key = $paths.$key;
  620. }
  621. $res = $upload->to($path)->validate()->stream($res, $key);
  622. if ($res === false) {
  623. return $upload->getError();
  624. } else {
  625. $info = $upload->getUploadInfo();
  626. $info['image_type'] = $uploadType;
  627. return $info;
  628. }
  629. }
  630. }
  631. if (!function_exists('getTimes')) {
  632. function getTimes()
  633. {
  634. $dates = [];
  635. for ($i = 0; $i <= 24; $i++) {
  636. for ($j = 0; $j < 60; $j++) {
  637. $dates[] = sprintf('%02.d', $i) . ':' . sprintf('%02.d', $j);
  638. }
  639. }
  640. return $dates;
  641. }
  642. }
  643. if (!function_exists('monday')) {
  644. /**
  645. * 获取周一
  646. *
  647. * @param null $time
  648. * @return false|string
  649. * @author xaboy
  650. * @day 2020/6/22
  651. */
  652. function monday($time = null)
  653. {
  654. return date('Y-m-d', strtotime('Sunday -6 day', $time ?: time()));
  655. }
  656. }
  657. if (!function_exists('orderLock')) {
  658. /**
  659. * @param string $name
  660. * @return \Swoole\Lock
  661. * @author xaboy
  662. * @day 2020/8/25
  663. */
  664. function makeLock($name = 'default'): \Swoole\Lock
  665. {
  666. return $GLOBALS['_swoole_order_lock'][$name];
  667. }
  668. }
  669. if (!function_exists('get_crmeb_version')) {
  670. /**
  671. * 获取CRMEB系统版本号
  672. * @param string $default
  673. * @return string
  674. */
  675. function get_crmeb_version($default = 'v1.0.0')
  676. {
  677. try {
  678. $version = parse_ini_file(app()->getRootPath() . '.version');
  679. return $version['version'] ?? $default;
  680. } catch (\Throwable $e) {
  681. return $default;
  682. }
  683. }
  684. }
  685. if (!function_exists('attr_format')) {
  686. /**
  687. * 格式化属性
  688. * @param $arr
  689. * @return array
  690. */
  691. function attr_format($arr)
  692. {
  693. $data = [];
  694. $res = [];
  695. $count = count($arr);
  696. if ($count > 1) {
  697. for ($i = 0; $i < $count - 1; $i++) {
  698. if ($i == 0) $data = $arr[$i]['detail'];
  699. //替代变量1
  700. $rep1 = [];
  701. foreach ($data as $v) {
  702. foreach ($arr[$i + 1]['detail'] as $g) {
  703. //替代变量2
  704. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  705. $tmp[] = $rep2;
  706. if ($i == $count - 2) {
  707. foreach (explode('-$-', $rep2) as $k => $h) {
  708. //替代变量3
  709. $rep3 = explode('_$_', $h);
  710. //替代变量4
  711. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  712. }
  713. if ($count == count($rep4['detail']))
  714. $res[] = $rep4;
  715. }
  716. }
  717. }
  718. $data = isset($tmp) ? $tmp : [];
  719. }
  720. } else {
  721. $dataArr = [];
  722. foreach ($arr as $k => $v) {
  723. foreach ($v['detail'] as $kk => $vv) {
  724. $dataArr[$kk] = $v['value'] . '_' . $vv;
  725. $res[$kk]['detail'][$v['value']] = $vv;
  726. }
  727. }
  728. $data[] = implode('-', $dataArr);
  729. }
  730. return [$data, $res];
  731. }
  732. }
  733. //通过curl发送get请求
  734. function curlGet($url)
  735. {
  736. $header = array(
  737. 'Accept: application/json',
  738. );
  739. $curl = curl_init();
  740. //设置抓取的url
  741. curl_setopt($curl, CURLOPT_URL, $url);
  742. //设置头文件的信息作为数据流输出
  743. curl_setopt($curl, CURLOPT_HEADER, 0);
  744. // 超时设置,以秒为单位
  745. curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  746. // 超时设置,以毫秒为单位
  747. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  748. // 设置请求头
  749. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  750. //设置获取的信息以文件流的形式返回,而不是直接输出。
  751. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  752. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  753. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  754. //执行命令
  755. $data = curl_exec($curl);
  756. // 显示错误信息
  757. if (curl_error($curl)) {
  758. //exception(curl_error($curl));
  759. } else {
  760. curl_close($curl);
  761. return json_decode($data, true);
  762. }
  763. }
  764. function createChat($params)
  765. {
  766. $url = "http://123.59.232.189:3333/api/Shop/addshop/?" . http_build_query($params);//数据
  767. $file_content = file_get_contents($url);//返回标准的json
  768. $arr = (array)json_decode($file_content);//对json格式的字符串进行编码,同时进行数组化
  769. return $arr;
  770. }
  771. //通过curl发送post请求带body传参
  772. function curlPost($url, $body)
  773. {
  774. $header = array(
  775. 'Content-Type: application/json; charset=utf-8',
  776. 'Accept: application/json',
  777. );
  778. $curl = curl_init();
  779. //设置抓取的url
  780. curl_setopt($curl, CURLOPT_URL, $url);
  781. //设置头文件的信息作为数据流输出
  782. curl_setopt($curl, CURLOPT_HEADER, 0);
  783. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  784. // 超时设置,以秒为单位
  785. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  786. //body传参
  787. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($body));
  788. // 超时设置,以毫秒为单位
  789. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  790. // 设置请求头
  791. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  792. //设置获取的信息以文件流的形式返回,而不是直接输出。
  793. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  794. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  795. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  796. //执行命令
  797. $data = curl_exec($curl);
  798. // 显示错误信息
  799. if (curl_error($curl)) {
  800. return curl_error($curl);
  801. } else {
  802. curl_close($curl);
  803. return json_decode($data, true);
  804. }
  805. }
  806. if (!function_exists("decimal2")){
  807. function decimal2($num)
  808. {
  809. if ($num < 0.01) {
  810. return sprintf("%01.2f", $num);
  811. }
  812. $check = strpos($num,".");
  813. if (strpos($num,".")){
  814. $num = substr($num,0,$check+3);
  815. }
  816. return sprintf("%01.2f", $num);
  817. }
  818. }
  819. if (!function_exists("set_price_rtrim")){
  820. function set_price_rtrim($value):string
  821. {
  822. return (string)(float)$value;
  823. // return rtrim(rtrim($value, '0'), '.');
  824. }
  825. }
  826. if (!function_exists("is_json")){
  827. /**
  828. * 判断是否符合json格式
  829. *
  830. * @param $json
  831. *
  832. * @return bool
  833. */
  834. function is_json($json): bool
  835. {
  836. if (is_bool($json) || empty($json) || is_array($json) || is_object($json) || is_int($json) || is_float($json)) {
  837. return false;
  838. }
  839. return true;
  840. }
  841. }