common.php 34 KB

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