common.php 35 KB

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