common.php 23 KB

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