common.php 37 KB

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