common.php 34 KB

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