common.php 35 KB

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