common.php 33 KB

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