common.php 34 KB

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