common.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. $rules = preg_split('/\r\n|\r|\n/', base64_decode(sys_config('param_filter_data')));
  591. if (filter_var($str, FILTER_VALIDATE_URL)) {
  592. $url = parse_url($str);
  593. if (!isset($url['scheme'])) return $str;
  594. $host = $url['scheme'] . '://' . $url['host'];
  595. $str = $host . preg_replace($rules, '', str_replace($host, '', $str));
  596. } else {
  597. $str = preg_replace($rules, '', $str);
  598. }
  599. return $str;
  600. }
  601. }
  602. if (!function_exists('is_brokerage_statu')) {
  603. /**
  604. * 是否能成为推广人
  605. * @param float $price
  606. * @return bool
  607. */
  608. function is_brokerage_statu(float $price)
  609. {
  610. if (!sys_config('brokerage_func_status')) {
  611. return false;
  612. }
  613. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  614. if ($storeBrokerageStatus == 1) {
  615. return false;
  616. } else if ($storeBrokerageStatus == 2) {
  617. return false;
  618. } else {
  619. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  620. return $price >= $storeBrokeragePrice;
  621. }
  622. }
  623. }
  624. if (!function_exists('array_unique_fb')) {
  625. /**
  626. * 二维数组去掉重复值
  627. * @param $array
  628. * @return array
  629. */
  630. function array_unique_fb($array)
  631. {
  632. $out = array();
  633. foreach ($array as $key => $value) {
  634. if (!in_array($value, $out)) {
  635. $out[$key] = $value;
  636. }
  637. }
  638. $out = array_values($out);
  639. return $out;
  640. }
  641. }
  642. if (!function_exists('get_crmeb_version')) {
  643. /**
  644. * 获取CRMEB系统版本号
  645. * @param string $default
  646. * @return string
  647. */
  648. function get_crmeb_version($default = 'v1.0.0')
  649. {
  650. try {
  651. $version = parse_ini_file(app()->getRootPath() . '.version');
  652. return $version['version'] ?? $default;
  653. } catch (\Throwable $e) {
  654. return $default;
  655. }
  656. }
  657. }
  658. if (!function_exists('get_file_link')) {
  659. /**
  660. * 获取文件带域名的完整路径
  661. * @param string $link
  662. * @return string
  663. */
  664. function get_file_link(string $link)
  665. {
  666. if (!$link) {
  667. return '';
  668. }
  669. if (substr($link, 0, 4) === "http" || substr($link, 0, 2) === "//") {
  670. return $link;
  671. } else {
  672. return app()->request->domain() . $link;
  673. }
  674. }
  675. }
  676. if (!function_exists('tidy_tree')) {
  677. /**
  678. * 格式化分类
  679. * @param $menusList
  680. * @param int $pid
  681. * @param array $navList
  682. * @return array
  683. */
  684. function tidy_tree($menusList, $pid = 0, $navList = [])
  685. {
  686. foreach ($menusList as $k => $menu) {
  687. if ($menu['parent_id'] == $pid) {
  688. unset($menusList[$k]);
  689. $menu['children'] = tidy_tree($menusList, $menu['id']);
  690. if ($menu['children']) $menu['expand'] = true;
  691. $navList[] = $menu;
  692. }
  693. }
  694. return $navList;
  695. }
  696. }
  697. if (!function_exists('create_form')) {
  698. /**
  699. * 表单生成方法
  700. * @param string $title
  701. * @param array $field
  702. * @param $url
  703. * @param string $method
  704. * @return array
  705. * @throws \FormBuilder\Exception\FormBuilderException
  706. */
  707. function create_form(string $title, array $field, $url, string $method = 'POST')
  708. {
  709. $form = Form::createForm((string)$url);//提交地址
  710. $form->setMethod($method);//提交方式
  711. $form->setRule($field);//表单字段
  712. $form->setTitle($title);//表单标题
  713. $rules = $form->formRule();
  714. $title = $form->getTitle();
  715. $action = $form->getAction();
  716. $method = $form->getMethod();
  717. $info = '';
  718. $status = true;
  719. $methodData = ['POST', 'PUT', 'GET', 'DELETE'];
  720. if (!in_array(strtoupper($method), $methodData)) {
  721. throw new ValidateException('请求方式有误');
  722. }
  723. return compact('rules', 'title', 'action', 'method', 'info', 'status');
  724. }
  725. }
  726. if (!function_exists('msectime')) {
  727. /**
  728. * 获取毫秒数
  729. * @return float
  730. */
  731. function msectime()
  732. {
  733. list($msec, $sec) = explode(' ', microtime());
  734. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  735. }
  736. }
  737. if (!function_exists('array_bc_sum')) {
  738. /**
  739. * 获取一维数组的总合高精度
  740. * @param array $data
  741. * @return string
  742. */
  743. function array_bc_sum(array $data)
  744. {
  745. $sum = '0';
  746. foreach ($data as $item) {
  747. $sum = bcadd($sum, (string)$item, 2);
  748. }
  749. return $sum;
  750. }
  751. }
  752. if (!function_exists('get_tree_children')) {
  753. /**
  754. * tree 子菜单
  755. * @param array $data 数据
  756. * @param string $childrenname 子数据名
  757. * @param string $keyName 数据key名
  758. * @param string $pidName 数据上级key名
  759. * @return array
  760. */
  761. function get_tree_children(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid')
  762. {
  763. $list = array();
  764. foreach ($data as $value) {
  765. $list[$value[$keyName]] = $value;
  766. }
  767. $tree = array(); //格式化好的树
  768. foreach ($list as $item) {
  769. if (isset($list[$item[$pidName]])) {
  770. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  771. } else {
  772. $tree[] = &$list[$item[$keyName]];
  773. }
  774. }
  775. return $tree;
  776. }
  777. }
  778. if (!function_exists('get_tree_children_value')) {
  779. function get_tree_children_value(array $data, $value, string $childrenname = 'children', string $keyName = 'id')
  780. {
  781. static $childrenValue = [];
  782. foreach ($data as $item) {
  783. $childrenData = $item[$childrenname] ?? [];
  784. if (count($childrenData)) {
  785. return get_tree_children_value($childrenData, $childrenname, $keyName);
  786. } else {
  787. if ($item[$keyName] == $value) {
  788. $childrenValue[] = $item['value'];
  789. }
  790. }
  791. }
  792. return $childrenValue;
  793. }
  794. }
  795. if (!function_exists('get_tree_value')) {
  796. /**
  797. * 获取
  798. * @param array $data
  799. * @param int|string $value
  800. * @return array
  801. */
  802. function get_tree_value(array $data, $value)
  803. {
  804. // static $childrenValue = [];
  805. // foreach ($data as &$item) {
  806. // if ($item['value'] == $value) {
  807. // $childrenValue[] = $item['value'];
  808. // if ($item['pid']) {
  809. // $value = $item['pid'];
  810. // unset($item);
  811. // return get_tree_value($data, $value);
  812. // }
  813. // }
  814. // }
  815. // return $childrenValue;
  816. $childrenValue = []; // 用于存储找到的子值的数组
  817. foreach ($data as $item) {
  818. if ($item['value'] == $value) { // 如果当前项的'value'键与给定值匹配
  819. $childrenValue[] = $item['value']; // 将当前值添加到子值数组中
  820. if ($item['pid']) { // 如果当前项有'pid'值,表示有父项
  821. // 递归调用get_tree_value函数,并将父项的'pid'值作为新的$value参数
  822. $childrenValue = array_merge($childrenValue, get_tree_value($data, $item['pid']));
  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. }
  848. $data = parse_url($image);
  849. if (!isset($data['host']) && (substr($image, 0, 2) == './' || substr($image, 0, 1) == '/')) {//不是完整地址
  850. $image = sys_config('site_url') . $image;
  851. }
  852. //请求是https 图片是http 需要改变图片地址
  853. if (strpos(request()->domain(), 'https:') !== false && strpos($image, 'https:') === false) {
  854. $image = str_replace('http:', 'https:', $image);
  855. }
  856. return $image;
  857. }
  858. }
  859. if (!function_exists('get_thumb_water')) {
  860. /**
  861. * 处理数组获取缩略图、水印
  862. * @param $list
  863. * @param string $type
  864. * @param array|string[] $field 1、['image','images'] type 取值参数:type 2、['small'=>'image','mid'=>'images'] type 取field数组的key
  865. * @param bool $is_remote_down
  866. * @return array|mixed|string|string[]
  867. */
  868. function get_thumb_water($list, string $type = 'small', array $field = ['image'], bool $is_remote_down = false)
  869. {
  870. if (!$list || !$field) return $list;
  871. $baseType = $type;
  872. $data = $list;
  873. if (is_string($list)) {
  874. $field = [$type => 'image'];
  875. $data = ['image' => $list];
  876. }
  877. if (is_array($data)) {
  878. foreach ($field as $type => $key) {
  879. if (is_integer($type)) {//索引数组,默认type
  880. $type = $baseType;
  881. }
  882. //一维数组
  883. if (isset($data[$key])) {
  884. if (is_array($data[$key])) {
  885. $path_data = [];
  886. foreach ($data[$key] as $k => $path) {
  887. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  888. }
  889. $data[$key] = $path_data;
  890. } else {
  891. $data[$key] = get_image_thumb($data[$key], $type, $is_remote_down);
  892. }
  893. } else {
  894. foreach ($data as &$item) {
  895. if (!isset($item[$key]))
  896. continue;
  897. if (is_array($item[$key])) {
  898. $path_data = [];
  899. foreach ($item[$key] as $k => $path) {
  900. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  901. }
  902. $item[$key] = $path_data;
  903. } else {
  904. $item[$key] = get_image_thumb($item[$key], $type, $is_remote_down);
  905. }
  906. }
  907. }
  908. }
  909. }
  910. return is_string($list) ? ($data['image'] ?? '') : $data;
  911. }
  912. }
  913. if (!function_exists('getLang')) {
  914. /**
  915. * 多语言
  916. * @param $code
  917. * @param array $replace
  918. * @return array|string|string[]
  919. */
  920. function getLang($code, array $replace = [])
  921. {
  922. //确保获取语言的时候不会报错
  923. try {
  924. /** @var LangCountryServices $langCountryServices */
  925. $langCountryServices = app()->make(LangCountryServices::class);
  926. /** @var LangTypeServices $langTypeServices */
  927. $langTypeServices = app()->make(LangTypeServices::class);
  928. /** @var LangCodeServices $langCodeServices */
  929. $langCodeServices = app()->make(LangCodeServices::class);
  930. $request = app()->request;
  931. //获取接口传入的语言类型
  932. if (!$range = $request->header('cb-lang')) {
  933. //没有传入则使用系统默认语言显示
  934. $range = CacheService::remember('range_name', function () use ($langTypeServices) {
  935. return $langTypeServices->value(['is_default' => 1], 'file_name');
  936. });
  937. if (!$range) {
  938. //系统没有设置默认语言的话,根据浏览器语言显示,如果浏览器语言在库中找不到,则使用简体中文
  939. if ($request->header('accept-language') !== null) {
  940. $range = explode(',', $request->header('accept-language'))[0];
  941. } else {
  942. $range = 'zh-CN';
  943. }
  944. }
  945. }
  946. // 获取type_id
  947. $typeId = CacheService::remember('type_id_' . $range, function () use ($langCountryServices, $range) {
  948. return $langCountryServices->value(['code' => $range], 'type_id') ?: 1;
  949. }, 3600);
  950. // 获取类型
  951. $langData = CacheService::remember('lang_type_data', function () use ($langTypeServices) {
  952. return $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name', 'id');
  953. }, 3600);
  954. // 获取缓存key
  955. $langStr = 'lang_' . str_replace('-', '_', $langData[$typeId]);
  956. //读取当前语言的语言包
  957. $lang = CacheService::remember($langStr, function () use ($typeId, $range, $langCodeServices) {
  958. return $langCodeServices->getColumn(['type_id' => $typeId, 'is_admin' => 1], 'lang_explain', 'code');
  959. }, 3600);
  960. //获取返回文字
  961. $message = (string)($lang[$code] ?? 'Code Error');
  962. //替换变量
  963. if (!empty($replace) && is_array($replace)) {
  964. // 关联索引解析
  965. $key = array_keys($replace);
  966. foreach ($key as &$v) {
  967. $v = "{:{$v}}";
  968. }
  969. $message = str_replace($key, $replace, $message);
  970. }
  971. return $message;
  972. } catch (\Throwable $e) {
  973. Log::error('获取语言code:' . $code . '发成错误,错误原因是:' . json_encode([
  974. 'file' => $e->getFile(),
  975. 'message' => $e->getMessage(),
  976. 'line' => $e->getLine()
  977. ]));
  978. return $code;
  979. }
  980. }
  981. }
  982. if (!function_exists('aj_captcha_check_one')) {
  983. /**
  984. * 验证滑块1次验证
  985. * @param string $token
  986. * @param string $pointJson
  987. * @return bool
  988. */
  989. function aj_captcha_check_one(string $captchaType, string $token, string $pointJson)
  990. {
  991. aj_get_serevice($captchaType)->check($token, $pointJson);
  992. return true;
  993. }
  994. }
  995. if (!function_exists('aj_captcha_check_two')) {
  996. /**
  997. * 验证滑块2次验证
  998. * @param string $token
  999. * @param string $pointJson
  1000. * @return bool
  1001. */
  1002. function aj_captcha_check_two(string $captchaType, string $captchaVerification)
  1003. {
  1004. aj_get_serevice($captchaType)->verificationByEncryptCode($captchaVerification);
  1005. return true;
  1006. }
  1007. }
  1008. if (!function_exists('aj_captcha_create')) {
  1009. /**
  1010. * 创建验证码
  1011. * @return array
  1012. */
  1013. function aj_captcha_create(string $captchaType)
  1014. {
  1015. return aj_get_serevice($captchaType)->get();
  1016. }
  1017. }
  1018. if (!function_exists('aj_get_serevice')) {
  1019. /**
  1020. * @param string $captchaType
  1021. * @return ClickWordCaptchaService|BlockPuzzleCaptchaService
  1022. */
  1023. function aj_get_serevice(string $captchaType)
  1024. {
  1025. $config = Config::get('ajcaptcha');
  1026. switch ($captchaType) {
  1027. case "clickWord":
  1028. $service = new ClickWordCaptchaService($config);
  1029. break;
  1030. case "blockPuzzle":
  1031. $service = new BlockPuzzleCaptchaService($config);
  1032. break;
  1033. default:
  1034. throw new ValidateException('captchaType参数不正确!');
  1035. }
  1036. return $service;
  1037. }
  1038. }
  1039. if (!function_exists('out_push')) {
  1040. /**
  1041. * 默认数据推送
  1042. * @param string $pushUrl
  1043. * @param array $data
  1044. * @param string $tip
  1045. * @return bool
  1046. */
  1047. function out_push(string $pushUrl, array $data, string $tip = ''): bool
  1048. {
  1049. $param = json_encode($data, JSON_UNESCAPED_UNICODE);
  1050. $res = HttpService::postRequest($pushUrl, $param, ['Content-Type:application/json', 'Content-Length:' . strlen($param)]);
  1051. $res = $res ? json_decode($res, true) : [];
  1052. if (!$res || !isset($res['code']) || $res['code'] != 0) {
  1053. \think\facade\Log::error(['msg' => $tip . '推送失败', 'data' => $res]);
  1054. return false;
  1055. }
  1056. return true;
  1057. }
  1058. }
  1059. if (!function_exists('dump_sql')) {
  1060. /**
  1061. * 打印sql
  1062. * @param string $pushUrl
  1063. * @param array $data
  1064. * @param string $tip
  1065. * @return bool
  1066. */
  1067. function dump_sql()
  1068. {
  1069. Db::listen(function ($sql) {
  1070. var_dump($sql);
  1071. });
  1072. }
  1073. }
  1074. if (!function_exists('stringToIntArray')) {
  1075. /**
  1076. * 处理ids等并过滤参数
  1077. * @param string $string
  1078. * @param string $separator
  1079. * @return array
  1080. */
  1081. function stringToIntArray(string $string, string $separator = ',')
  1082. {
  1083. return !empty($string) ? array_unique(array_diff(array_map('intval', explode($separator, $string)), [0])) : [];
  1084. }
  1085. }