common.php 35 KB

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