common.php 27 KB

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