common.php 30 KB

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