common.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. use crmeb\services\CacheService;
  13. use think\exception\ValidateException;
  14. use crmeb\services\FormBuilder as Form;
  15. use app\services\other\UploadService;
  16. use think\facade\Config;
  17. use think\facade\Lang;
  18. if (!function_exists('getWorkerManUrl')) {
  19. /**
  20. * 获取客服数据
  21. * @return mixed
  22. */
  23. function getWorkerManUrl()
  24. {
  25. $ws = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'wss://' : 'ws://';
  26. $host = $_SERVER['HTTP_HOST'];
  27. $data['admin'] = $ws . $host . '/notice';
  28. $data['chat'] = $ws . $host . '/msg';
  29. return $data;
  30. }
  31. }
  32. if (!function_exists('object2array')) {
  33. /**
  34. * 对象转数组
  35. * @param $object
  36. * @return array|mixed
  37. */
  38. function object2array($object)
  39. {
  40. $array = [];
  41. if (is_object($object)) {
  42. foreach ($object as $key => $value) {
  43. $array[$key] = $value;
  44. }
  45. } else {
  46. $array = $object;
  47. }
  48. return $array;
  49. }
  50. }
  51. if (!function_exists('exception')) {
  52. /**
  53. * 抛出异常处理
  54. * @param $msg
  55. * @param int $code
  56. * @param string $exception
  57. * @throws \think\Exception
  58. */
  59. function exception($msg, $code = 0, $exception = '')
  60. {
  61. $e = $exception ?: '\think\Exception';
  62. throw new $e($msg, $code);
  63. }
  64. }
  65. if (!function_exists('sys_config')) {
  66. /**
  67. * 获取系统单个配置
  68. * @param string $name
  69. * @param string $default
  70. * @return string
  71. */
  72. function sys_config(string $name, $default = '')
  73. {
  74. if (empty($name))
  75. return $default;
  76. $sysConfig = app('sysConfig')->get($name);
  77. if (is_array($sysConfig)) {
  78. foreach ($sysConfig as &$item) {
  79. if (strpos($item, '/uploads/system/') !== false || strpos($item, '/statics/system_images/') !== false) $item = set_file_url($item);
  80. }
  81. } else {
  82. if (strpos($sysConfig, '/uploads/system/') !== false || strpos($sysConfig, '/statics/system_images/') !== false) $sysConfig = set_file_url($sysConfig);
  83. }
  84. $config = is_array($sysConfig) ? $sysConfig : trim($sysConfig);
  85. if ($config === '' || $config === false) {
  86. return $default;
  87. } else {
  88. return $config;
  89. }
  90. }
  91. }
  92. if (!function_exists('sys_data')) {
  93. /**
  94. * 获取系统单个配置
  95. * @param string $name
  96. * @return string
  97. */
  98. function sys_data(string $name, int $limit = 0)
  99. {
  100. return app('sysGroupData')->getData($name, $limit);
  101. }
  102. }
  103. if (!function_exists('filter_emoji')) {
  104. // 过滤掉emoji表情
  105. function filter_emoji($str)
  106. {
  107. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  108. '/./u',
  109. function (array $match) {
  110. return strlen($match[0]) >= 4 ? '' : $match[0];
  111. },
  112. $str);
  113. return $str;
  114. }
  115. }
  116. if (!function_exists('str_middle_replace')) {
  117. /** TODO 系统未使用
  118. * @param string $string 需要替换的字符串
  119. * @param int $start 开始的保留几位
  120. * @param int $end 最后保留几位
  121. * @return string
  122. */
  123. function str_middle_replace($string, $start, $end)
  124. {
  125. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  126. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  127. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  128. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  129. }
  130. }
  131. if (!function_exists('sensitive_words_filter')) {
  132. /**
  133. * 敏感词过滤
  134. *
  135. * @param string
  136. * @return string
  137. */
  138. function sensitive_words_filter($str)
  139. {
  140. if (!$str) return '';
  141. $file = app()->getAppPath() . 'public/statics/plug/censorwords/CensorWords';
  142. $words = file($file);
  143. foreach ($words as $word) {
  144. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  145. if (!$word) continue;
  146. $ret = preg_match("/$word/", $str, $match);
  147. if ($ret) {
  148. return $match[0];
  149. }
  150. }
  151. return '';
  152. }
  153. }
  154. if (!function_exists('make_path')) {
  155. /**
  156. * 上传路径转化,默认路径
  157. * @param $path
  158. * @param int $type
  159. * @param bool $force
  160. * @return string
  161. */
  162. function make_path($path, int $type = 2, bool $force = false)
  163. {
  164. $path = DS . ltrim(rtrim($path));
  165. switch ($type) {
  166. case 1:
  167. $path .= DS . date('Y');
  168. break;
  169. case 2:
  170. $path .= DS . date('Y') . DS . date('m');
  171. break;
  172. case 3:
  173. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  174. break;
  175. }
  176. try {
  177. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  178. return trim(str_replace(DS, '/', $path), '.');
  179. } else return '';
  180. } catch (\Exception $e) {
  181. if ($force)
  182. throw new \Exception($e->getMessage());
  183. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  184. }
  185. }
  186. }
  187. if (!function_exists('curl_file_exist')) {
  188. /**
  189. * CURL 检测远程文件是否在
  190. * @param $url
  191. * @return bool
  192. */
  193. function curl_file_exist($url)
  194. {
  195. $ch = curl_init();
  196. try {
  197. curl_setopt($ch, CURLOPT_URL, $url);
  198. curl_setopt($ch, CURLOPT_HEADER, 1);
  199. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  200. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  201. $contents = curl_exec($ch);
  202. if (preg_match("/404/", $contents)) return false;
  203. if (preg_match("/403/", $contents)) return false;
  204. return true;
  205. } catch (\Exception $e) {
  206. return false;
  207. }
  208. }
  209. }
  210. if (!function_exists('set_file_url')) {
  211. /**
  212. * 设置附加路径
  213. * @param $url
  214. * @return bool
  215. */
  216. function set_file_url($image, $siteUrl = '')
  217. {
  218. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  219. if (!$image) return $image;
  220. if (is_array($image)) {
  221. foreach ($image as &$item) {
  222. $domainTop1 = substr($item, 0, 4);
  223. $domainTop2 = substr($item, 0, 2);
  224. if ($domainTop1 != 'http' && $domainTop2 != '//')
  225. $item = $siteUrl . str_replace('\\', '/', $item);
  226. }
  227. } else {
  228. $domainTop1 = substr($image, 0, 4);
  229. $domainTop2 = substr($image, 0, 2);
  230. if ($domainTop1 != 'http' && $domainTop2 != '//')
  231. $image = $siteUrl . str_replace('\\', '/', $image);
  232. }
  233. return $image;
  234. }
  235. }
  236. if (!function_exists('set_http_type')) {
  237. /**
  238. * 修改 https 和 http
  239. * @param $url $url 域名
  240. * @param int $type 0 返回https 1 返回 http
  241. * @return string
  242. */
  243. function set_http_type($url, $type = 0)
  244. {
  245. $domainTop = substr($url, 0, 5);
  246. if ($type) {
  247. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  248. } else {
  249. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  250. }
  251. return $url;
  252. }
  253. }
  254. if (!function_exists('check_card')) {
  255. /**
  256. * 身份证验证
  257. * @param $card
  258. * @return bool
  259. */
  260. function check_card($card)
  261. {
  262. $city = [11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁", 22 => "吉林", 23 => "黑龙江 ", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北 ", 43 => "湖南", 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州", 53 => "云南", 54 => "西藏 ", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏", 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外 "];
  263. $tip = "";
  264. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  265. $pass = true;
  266. if (!$card || !preg_match($match, $card)) {
  267. //身份证格式错误
  268. $pass = false;
  269. } else if (!$city[substr($card, 0, 2)]) {
  270. //地址错误
  271. $pass = false;
  272. } else {
  273. //18位身份证需要验证最后一位校验位
  274. if (strlen($card) == 18) {
  275. $card = str_split($card);
  276. //∑(ai×Wi)(mod 11)
  277. //加权因子
  278. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  279. //校验位
  280. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  281. $sum = 0;
  282. $ai = 0;
  283. $wi = 0;
  284. for ($i = 0; $i < 17; $i++) {
  285. $ai = $card[$i];
  286. $wi = $factor[$i];
  287. $sum += $ai * $wi;
  288. }
  289. $last = $parity[$sum % 11];
  290. if ($parity[$sum % 11] != $card[17]) {
  291. // $tip = "校验位错误";
  292. $pass = false;
  293. }
  294. } else {
  295. $pass = false;
  296. }
  297. }
  298. if (!$pass) return false;/* 身份证格式错误*/
  299. return true;/* 身份证格式正确*/
  300. }
  301. }
  302. if (!function_exists('check_link')) {
  303. /**
  304. * 地址验证
  305. * @param string $link
  306. * @return false|int
  307. */
  308. function check_link(string $link)
  309. {
  310. return preg_match("/^(http|https|ftp):\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”])*$/", $link);
  311. }
  312. }
  313. if (!function_exists('check_phone')) {
  314. /**
  315. * 手机号验证
  316. * @param $phone
  317. * @return false|int
  318. */
  319. function check_phone($phone)
  320. {
  321. return preg_match("/^1[3456789]\d{9}$/", $phone);
  322. }
  323. }
  324. if (!function_exists('anonymity')) {
  325. /**
  326. * 匿名处理处理用户昵称
  327. * @param $name
  328. * @return string
  329. */
  330. function anonymity($name, $type = 1)
  331. {
  332. if ($type == 1) {
  333. return mb_substr($name, 0, 1, 'UTF-8') . '**' . mb_substr($name, -1, 1, 'UTF-8');
  334. } else {
  335. $strLen = mb_strlen($name, 'UTF-8');
  336. $min = 3;
  337. if ($strLen <= 1)
  338. return '*';
  339. if ($strLen <= $min)
  340. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  341. else
  342. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  343. }
  344. }
  345. }
  346. if (!function_exists('sort_list_tier')) {
  347. /**
  348. * 分级排序
  349. * @param $data
  350. * @param int $pid
  351. * @param string $field
  352. * @param string $pk
  353. * @param string $html
  354. * @param int $level
  355. * @param bool $clear
  356. * @return array
  357. */
  358. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  359. {
  360. static $list = [];
  361. if ($clear) $list = [];
  362. foreach ($data as $k => $res) {
  363. if ($res[$field] == $pid) {
  364. $res['html'] = str_repeat($html, $level);
  365. $list[] = $res;
  366. unset($data[$k]);
  367. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  368. }
  369. }
  370. return $list;
  371. }
  372. }
  373. if (!function_exists('sort_city_tier')) {
  374. /**
  375. * 城市数据整理
  376. * @param $data
  377. * @param int $pid
  378. * @param string $field
  379. * @param string $pk
  380. * @param string $html
  381. * @param int $level
  382. * @param bool $clear
  383. * @return array
  384. */
  385. function sort_city_tier($data, $pid = 0, $navList = [])
  386. {
  387. foreach ($data as $k => $menu) {
  388. if ($menu['parent_id'] == $pid) {
  389. unset($menu['parent_id']);
  390. unset($data[$k]);
  391. $menu['c'] = sort_city_tier($data, $menu['v']);
  392. $navList[] = $menu;
  393. }
  394. }
  395. return $navList;
  396. }
  397. }
  398. if (!function_exists('time_tran')) {
  399. /**
  400. * 时间戳人性化转化
  401. * @param $time
  402. * @return string
  403. */
  404. function time_tran($time)
  405. {
  406. $t = time() - $time;
  407. $f = array(
  408. '31536000' => '年',
  409. '2592000' => '个月',
  410. '604800' => '星期',
  411. '86400' => '天',
  412. '3600' => '小时',
  413. '60' => '分钟',
  414. '1' => '秒'
  415. );
  416. foreach ($f as $k => $v) {
  417. if (0 != $c = floor($t / (int)$k)) {
  418. return $c . $v . '前';
  419. }
  420. }
  421. }
  422. }
  423. if (!function_exists('url_to_path')) {
  424. /**
  425. * url转换路径
  426. * @param $url
  427. * @return string
  428. */
  429. function url_to_path($url)
  430. {
  431. $path = trim(str_replace('/', DS, $url), DS);
  432. if (0 !== strripos($path, 'public'))
  433. $path = 'public' . DS . $path;
  434. return app()->getRootPath() . $path;
  435. }
  436. }
  437. if (!function_exists('path_to_url')) {
  438. /**
  439. * 路径转url路径
  440. * @param $path
  441. * @return string
  442. */
  443. function path_to_url($path)
  444. {
  445. return trim(str_replace(DS, '/', $path), '.');
  446. }
  447. }
  448. if (!function_exists('image_to_base64')) {
  449. /**
  450. * 获取图片转为base64
  451. * @param string $avatar
  452. * @return bool|string
  453. */
  454. function image_to_base64($avatar = '', $timeout = 9)
  455. {
  456. $avatar = str_replace('https', 'http', $avatar);
  457. try {
  458. $url = parse_url($avatar);
  459. $url = $url['host'];
  460. $header = [
  461. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  462. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  463. 'Accept-Encoding: gzip, deflate, br',
  464. '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',
  465. 'Host:' . $url
  466. ];
  467. $dir = pathinfo($url);
  468. $host = $dir['dirname'];
  469. $refer = $host . '/';
  470. $curl = curl_init();
  471. curl_setopt($curl, CURLOPT_REFERER, $refer);
  472. curl_setopt($curl, CURLOPT_URL, $avatar);
  473. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  474. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  475. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  476. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  477. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  478. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  479. $data = curl_exec($curl);
  480. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  481. curl_close($curl);
  482. if ($code == 200) {
  483. return "data:image/jpeg;base64," . base64_encode($data);
  484. } else {
  485. return false;
  486. }
  487. } catch (\Exception $e) {
  488. return false;
  489. }
  490. }
  491. }
  492. if (!function_exists('put_image')) {
  493. /**
  494. * 获取图片转为base64
  495. * @param string $avatar
  496. * @return bool|string
  497. */
  498. function put_image($url, $filename = '')
  499. {
  500. if ($url == '') {
  501. return false;
  502. }
  503. try {
  504. if ($filename == '') {
  505. $ext = pathinfo($url);
  506. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  507. return false;
  508. }
  509. $filename = time() . "." . $ext['extension'];
  510. }
  511. //文件保存路径
  512. ob_start();
  513. readfile($url);
  514. $img = ob_get_contents();
  515. ob_end_clean();
  516. $path = 'uploads/qrcode';
  517. $fp2 = fopen($path . '/' . $filename, 'a');
  518. fwrite($fp2, $img);
  519. fclose($fp2);
  520. return $path . '/' . $filename;
  521. } catch (\Exception $e) {
  522. return false;
  523. }
  524. }
  525. }
  526. if (!function_exists('debug_file')) {
  527. /**
  528. * 文件调试
  529. * @param $content
  530. */
  531. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  532. {
  533. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  534. $pach = app()->getRuntimePath();
  535. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  536. }
  537. }
  538. if (!function_exists('sql_filter')) {
  539. /**
  540. * sql 参数过滤
  541. * @param string $str
  542. * @return mixed
  543. */
  544. function sql_filter(string $str)
  545. {
  546. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  547. $toupper = array_map(function ($str) {
  548. return strtoupper($str);
  549. }, $filter);
  550. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  551. }
  552. }
  553. if (!function_exists('is_brokerage_statu')) {
  554. /**
  555. * 是否能成为推广人
  556. * @param float $price
  557. * @return bool
  558. */
  559. function is_brokerage_statu(float $price)
  560. {
  561. if (!sys_config('brokerage_func_status')) {
  562. return false;
  563. }
  564. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  565. if ($storeBrokerageStatus == 1) {
  566. return false;
  567. } else if ($storeBrokerageStatus == 2) {
  568. return false;
  569. } else {
  570. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  571. return $price >= $storeBrokeragePrice;
  572. }
  573. }
  574. }
  575. if (!function_exists('array_unique_fb')) {
  576. /**
  577. * 二维数组去掉重复值
  578. * @param $array
  579. * @return array
  580. */
  581. function array_unique_fb($array)
  582. {
  583. $out = array();
  584. foreach ($array as $key => $value) {
  585. if (!in_array($value, $out)) {
  586. $out[$key] = $value;
  587. }
  588. }
  589. $out = array_values($out);
  590. return $out;
  591. }
  592. }
  593. if (!function_exists('get_crmeb_version')) {
  594. /**
  595. * 获取CRMEB系统版本号
  596. * @param string $default
  597. * @return string
  598. */
  599. function get_crmeb_version($default = 'v1.0.0')
  600. {
  601. try {
  602. $version = parse_ini_file(app()->getRootPath() . '.version');
  603. return $version['version'] ?? $default;
  604. } catch (\Throwable $e) {
  605. return $default;
  606. }
  607. }
  608. }
  609. if (!function_exists('get_file_link')) {
  610. /**
  611. * 获取文件带域名的完整路径
  612. * @param string $link
  613. * @return string
  614. */
  615. function get_file_link(string $link)
  616. {
  617. if (!$link) {
  618. return '';
  619. }
  620. if (strstr('http', $link) === false) {
  621. return app()->request->domain() . $link;
  622. } else {
  623. return $link;
  624. }
  625. }
  626. }
  627. if (!function_exists('tidy_tree')) {
  628. /**
  629. * 格式化分类
  630. * @param $menusList
  631. * @param int $pid
  632. * @param array $navList
  633. * @return array
  634. */
  635. function tidy_tree($menusList, $pid = 0, $navList = [])
  636. {
  637. foreach ($menusList as $k => $menu) {
  638. if ($menu['parent_id'] == $pid) {
  639. unset($menusList[$k]);
  640. $menu['children'] = tidy_tree($menusList, $menu['id']);
  641. if ($menu['children']) $menu['expand'] = true;
  642. $navList[] = $menu;
  643. }
  644. }
  645. return $navList;
  646. }
  647. }
  648. if (!function_exists('create_form')) {
  649. /**
  650. * 表单生成方法
  651. * @param string $title
  652. * @param array $field
  653. * @param $url
  654. * @param string $method
  655. * @return array
  656. * @throws \FormBuilder\Exception\FormBuilderException
  657. */
  658. function create_form(string $title, array $field, $url, string $method = 'POST')
  659. {
  660. $form = Form::createForm((string)$url);//提交地址
  661. $form->setMethod($method);//提交方式
  662. $form->setRule($field);//表单字段
  663. $form->setTitle($title);//表单标题
  664. $rules = $form->formRule();
  665. $title = $form->getTitle();
  666. $action = $form->getAction();
  667. $method = $form->getMethod();
  668. $info = '';
  669. $status = true;
  670. $methodData = ['POST', 'PUT', 'GET', 'DELETE'];
  671. if (!in_array(strtoupper($method), $methodData)) {
  672. throw new ValidateException('请求方式有误');
  673. }
  674. return compact('rules', 'title', 'action', 'method', 'info', 'status');
  675. }
  676. }
  677. if (!function_exists('msectime')) {
  678. /**
  679. * 获取毫秒数
  680. * @return float
  681. */
  682. function msectime()
  683. {
  684. list($msec, $sec) = explode(' ', microtime());
  685. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  686. }
  687. }
  688. if (!function_exists('array_bc_sum')) {
  689. /**
  690. * 获取一维数组的总合高精度
  691. * @param array $data
  692. * @return string
  693. */
  694. function array_bc_sum(array $data)
  695. {
  696. $sum = '0';
  697. foreach ($data as $item) {
  698. $sum = bcadd($sum, (string)$item, 2);
  699. }
  700. return $sum;
  701. }
  702. }
  703. if (!function_exists('get_tree_children')) {
  704. /**
  705. * tree 子菜单
  706. * @param array $data 数据
  707. * @param string $childrenname 子数据名
  708. * @param string $keyName 数据key名
  709. * @param string $pidName 数据上级key名
  710. * @return array
  711. */
  712. function get_tree_children(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid')
  713. {
  714. $list = array();
  715. foreach ($data as $value) {
  716. $list[$value[$keyName]] = $value;
  717. }
  718. static $tree = array(); //格式化好的树
  719. foreach ($list as $item) {
  720. if (isset($list[$item[$pidName]])) {
  721. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  722. } else {
  723. $tree[] = &$list[$item[$keyName]];
  724. }
  725. }
  726. return $tree;
  727. }
  728. }
  729. if (!function_exists('get_tree_children_value')) {
  730. function get_tree_children_value(array $data, $value, string $childrenname = 'children', string $keyName = 'id')
  731. {
  732. static $childrenValue = [];
  733. foreach ($data as $item) {
  734. $childrenData = $item[$childrenname] ?? [];
  735. if (count($childrenData)) {
  736. return get_tree_children_value($childrenData, $childrenname, $keyName);
  737. } else {
  738. if ($item[$keyName] == $value) {
  739. $childrenValue[] = $item['value'];
  740. }
  741. }
  742. }
  743. return $childrenValue;
  744. }
  745. }
  746. if (!function_exists('get_tree_value')) {
  747. /**
  748. * 获取
  749. * @param array $data
  750. * @param int|string $value
  751. * @return array
  752. */
  753. function get_tree_value(array $data, $value)
  754. {
  755. static $childrenValue = [];
  756. foreach ($data as &$item) {
  757. if ($item['value'] == $value) {
  758. $childrenValue[] = $item['value'];
  759. if ($item['pid']) {
  760. $value = $item['pid'];
  761. unset($item);
  762. return get_tree_value($data, $value);
  763. }
  764. }
  765. }
  766. return $childrenValue;
  767. }
  768. }
  769. if (!function_exists('get_image_thumb')) {
  770. /**
  771. * 获取缩略图
  772. * @param $filePath
  773. * @param string $type all|big|mid|small
  774. * @param bool $is_remote_down
  775. * @return mixed|string|string[]
  776. */
  777. function get_image_thumb($filePath, string $type = 'all', bool $is_remote_down = false)
  778. {
  779. if (!$filePath || !is_string($filePath) || strpos($filePath, '?') !== false) return $filePath;
  780. try {
  781. $upload = UploadService::getOssInit($filePath, $is_remote_down);
  782. $fileArr = explode('/', $filePath);
  783. $data = $upload->thumb($filePath, end($fileArr), $type);
  784. $image = $type == 'all' ? $data : $data[$type] ?? $filePath;
  785. } catch (\Throwable $e) {
  786. $image = $filePath;
  787. \think\facade\Log::error('获取缩略图失败,原因:' . $e->getMessage() . '----' . $e->getFile() . '----' . $e->getLine() . '----' . $filePath);
  788. }
  789. $data = parse_url($image);
  790. if (!isset($data['host']) && (substr($image, 0, 2) == './' || substr($image, 0, 1) == '/')) {//不是完整地址
  791. $image = sys_config('site_url') . $image;
  792. }
  793. //请求是https 图片是http 需要改变图片地址
  794. if (strpos(request()->domain(), 'https:') !== false && strpos($image, 'https:') === false) {
  795. $image = str_replace('http:', 'https:', $image);
  796. }
  797. return $image;
  798. }
  799. }
  800. if (!function_exists('get_thumb_water')) {
  801. /**
  802. * 处理数组获取缩略图、水印
  803. * @param $list
  804. * @param string $type
  805. * @param array|string[] $field 1、['image','images'] type 取值参数:type 2、['small'=>'image','mid'=>'images'] type 取field数组的key
  806. * @param bool $is_remote_down
  807. * @return array|mixed|string|string[]
  808. */
  809. function get_thumb_water($list, string $type = 'small', array $field = ['image'], bool $is_remote_down = false)
  810. {
  811. if (!$list || !$field) return $list;
  812. $baseType = $type;
  813. $data = $list;
  814. if (is_string($list)) {
  815. $field = [$type => 'image'];
  816. $data = ['image' => $list];
  817. }
  818. if (is_array($data)) {
  819. foreach ($field as $type => $key) {
  820. if (is_integer($type)) {//索引数组,默认type
  821. $type = $baseType;
  822. }
  823. //一维数组
  824. if (isset($data[$key])) {
  825. if (is_array($data[$key])) {
  826. $path_data = [];
  827. foreach ($data[$key] as $k => $path) {
  828. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  829. }
  830. $data[$key] = $path_data;
  831. } else {
  832. $data[$key] = get_image_thumb($data[$key], $type, $is_remote_down);
  833. }
  834. } else {
  835. foreach ($data as &$item) {
  836. if (!isset($item[$key]))
  837. continue;
  838. if (is_array($item[$key])) {
  839. $path_data = [];
  840. foreach ($item[$key] as $k => $path) {
  841. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  842. }
  843. $item[$key] = $path_data;
  844. } else {
  845. $item[$key] = get_image_thumb($item[$key], $type, $is_remote_down);
  846. }
  847. }
  848. }
  849. }
  850. }
  851. return is_string($list) ? ($data['image'] ?? '') : $data;
  852. }
  853. }
  854. if (!function_exists('getLang')) {
  855. /**
  856. * 多语言
  857. * @param $code
  858. * @param array $replace
  859. * @return array|string|string[]
  860. */
  861. function getLang($code, array $replace = [])
  862. {
  863. $config = Config::get('lang');
  864. $request = app()->request;
  865. //获取接口传入的语言类型
  866. if (!$range = $request->header('cb-lang')) {
  867. $range = $request->cookie($config['cookie_var']);
  868. }
  869. //如果找到当前语言类型,默认中文
  870. $langData = array_values($config['accept_language']);
  871. if (!in_array($range, $langData)) {
  872. $range = 'zh_cn';
  873. }
  874. CacheService::redisHandler()->delete('lang_' . $range);
  875. //读取当前语言的语言包
  876. $lang = CacheService::redisHandler()->remember('lang_' . $range, function () use ($config, $range) {
  877. return include $config['extend_list'][$range];
  878. }, 3600);
  879. //获取返回文字
  880. $message = (string)($lang[$code] ?? 'Code Error');
  881. //替换变量
  882. if (!empty($replace) && is_array($replace)) {
  883. // 关联索引解析
  884. $key = array_keys($replace);
  885. foreach ($key as &$v) {
  886. $v = "{:{$v}}";
  887. }
  888. $message = str_replace($key, $replace, $message);
  889. }
  890. return $message;
  891. }
  892. }