common.php 32 KB

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