common.php 27 KB

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