common.php 31 KB

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