common.php 28 KB

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