common.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. use \think\facade\Config as ThinkConf;
  12. // 应用公共文件
  13. // 订单常量
  14. // 订单状态(-1 : 申请退款 -2 : 退货成功 0:待发货;1:待收货;2:已收货;3:待评价;-1:已退款)
  15. define('ORDER_REFUND_REQUSTED', -1);
  16. define('ORDER_REFUNDED', -2);
  17. define('ORDER_WAITING_SHIP', 0);
  18. define('ORDER_WAITING_RECEIPT', 1);
  19. define('ORDER_RECEIPTED', 2);
  20. define('ORDER_WAITING_COMMENT', 3);
  21. // 退款状态
  22. // 0 未退款 1 申请中 2 已退款
  23. define('REFUND_NO', 0);
  24. define('REFUNDING', 1);
  25. define('REFUNDED', 2);
  26. // 配送方式
  27. // 配送方式 1=快递 ,2=门店自提
  28. define('SHIPPING_EXPRESS', 1);
  29. define('SHIPPING_SELF_COLLECT', 2);
  30. // 分銷方式
  31. define('DISTRIBUTE_SPECIFIED', 1); // 指定分銷
  32. define('DISTRIBUTE_EVERYONE', 2); // 人人分銷
  33. //
  34. define('SECONDS_OF_ONEDAY', 86400);
  35. /**
  36. * 获取所有 幸运2021 活动及其子活动 ID 列表
  37. */
  38. function get_luckies(): Array
  39. {
  40. return [
  41. ThinkConf::get('activity.lucky_cate_id'),
  42. ThinkConf::get('activity.lucky_a_cate_id'),
  43. ThinkConf::get('activity.lucky_b_cate_id'),
  44. ];
  45. }
  46. /**
  47. * 获取 提现类型, 帐号,用于日志或者消息
  48. */
  49. function get_extract_name($extract): Array
  50. {
  51. if (!$extract || !isset($extract['extract_type'])) {
  52. return ['未知', ''];
  53. }
  54. switch($extract['extract_type']) {
  55. case 'weixin':
  56. return ['微信支付', $extract['wechat'] ?? ''];
  57. case 'alipay':
  58. return ['支付宝', $extract['alipay_code'] ?? ''];
  59. case 'bank':
  60. return [$extract['bank_address'] ?? '未知银行', $extract['bank_code'] ?? ''];
  61. }
  62. return ['未知', ''];
  63. }
  64. if (!function_exists('exception')) {
  65. /**
  66. * 抛出异常处理
  67. *
  68. * @param string $msg 异常消息
  69. * @param integer $code 异常代码 默认为0
  70. * @param string $exception 异常类
  71. *
  72. * @throws Exception
  73. */
  74. function exception($msg, $code = 0, $exception = '')
  75. {
  76. $e = $exception ?: '\think\Exception';
  77. throw new $e($msg, $code);
  78. }
  79. }
  80. if (!function_exists('sys_config')) {
  81. /**
  82. * 获取系统单个配置
  83. * @param string $name
  84. * @param string $default
  85. * @return string
  86. */
  87. function sys_config(string $name, $default = '')
  88. {
  89. if (empty($name))
  90. return $default;
  91. $config = trim(app('sysConfig')->get($name));
  92. if ($config === '' || $config === false) {
  93. return $default;
  94. } else {
  95. return $config;
  96. }
  97. }
  98. }
  99. if (!function_exists('sys_data')) {
  100. /**
  101. * 获取系统单个配置
  102. * @param string $name
  103. * @return string|array
  104. */
  105. function sys_data(string $name, int $limit = 0)
  106. {
  107. return app('sysGroupData')->getData($name, $limit);
  108. }
  109. }
  110. if (!function_exists('filter_emoji')) {
  111. // 过滤掉emoji表情
  112. function filter_emoji($str)
  113. {
  114. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  115. '/./u',
  116. function (array $match) {
  117. return strlen($match[0]) >= 4 ? '' : $match[0];
  118. },
  119. $str);
  120. return $str;
  121. }
  122. }
  123. if (!function_exists('str_middle_replace')) {
  124. /** TODO 系统未使用
  125. * @param string $string 需要替换的字符串
  126. * @param int $start 开始的保留几位
  127. * @param int $end 最后保留几位
  128. * @return string
  129. */
  130. function str_middle_replace($string, $start, $end)
  131. {
  132. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  133. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  134. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  135. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  136. }
  137. }
  138. if (!function_exists('sensitive_words_filter')) {
  139. /**
  140. * 敏感词过滤
  141. *
  142. * @param string
  143. * @return string
  144. */
  145. function sensitive_words_filter($str)
  146. {
  147. if (!$str) return '';
  148. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  149. $words = file($file);
  150. foreach ($words as $word) {
  151. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  152. if (!$word) continue;
  153. $ret = preg_match("/$word/", $str, $match);
  154. if ($ret) {
  155. return $match[0];
  156. }
  157. }
  158. return '';
  159. }
  160. }
  161. if (!function_exists('make_path')) {
  162. /**
  163. * 上传路径转化,默认路径
  164. * @param $path
  165. * @param int $type
  166. * @param bool $force
  167. * @return string
  168. */
  169. function make_path($path, int $type = 2, bool $force = false)
  170. {
  171. $path = DS . ltrim(rtrim($path));
  172. switch ($type) {
  173. case 1:
  174. $path .= DS . date('Y');
  175. break;
  176. case 2:
  177. $path .= DS . date('Y') . DS . date('m');
  178. break;
  179. case 3:
  180. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  181. break;
  182. }
  183. try {
  184. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  185. return trim(str_replace(DS, '/', $path), '.');
  186. } else return '';
  187. } catch (\Exception $e) {
  188. if ($force)
  189. throw new \Exception($e->getMessage());
  190. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  191. }
  192. }
  193. }
  194. if (!function_exists('curl_file_exist')) {
  195. /**
  196. * CURL 检测远程文件是否在
  197. * @param $url
  198. * @return bool
  199. */
  200. function curl_file_exist($url)
  201. {
  202. $ch = curl_init();
  203. try {
  204. curl_setopt($ch, CURLOPT_URL, $url);
  205. curl_setopt($ch, CURLOPT_HEADER, 1);
  206. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  207. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  208. $contents = curl_exec($ch);
  209. if (preg_match("/404/", $contents)) return false;
  210. if (preg_match("/403/", $contents)) return false;
  211. return true;
  212. } catch (\Exception $e) {
  213. return false;
  214. }
  215. }
  216. }
  217. if (!function_exists('set_file_url')) {
  218. /**
  219. * 设置附加路径
  220. * @param $url
  221. * @return bool
  222. */
  223. function set_file_url($image, $siteUrl = '')
  224. {
  225. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  226. $domainTop = substr($image, 0, 4);
  227. if ($domainTop == 'http') return $image;
  228. $image = str_replace('\\', '/', $image);
  229. return $siteUrl . $image;
  230. }
  231. }
  232. if (!function_exists('set_http_type')) {
  233. /**
  234. * 修改 https 和 http
  235. * @param string $url 域名
  236. * @param int $type 0 返回https 1 返回 http
  237. * @return string
  238. */
  239. function set_http_type($url, $type = 0)
  240. {
  241. $domainTop = substr($url, 0, 5);
  242. if ($type) {
  243. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  244. } else {
  245. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  246. }
  247. return $url;
  248. }
  249. }
  250. if (!function_exists('check_card')) {
  251. /**
  252. * 身份证验证
  253. * @param $card
  254. * @return bool
  255. */
  256. function check_card($card)
  257. {
  258. $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 => "国外 "];
  259. $tip = "";
  260. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  261. $pass = true;
  262. if (!$card || !preg_match($match, $card)) {
  263. //身份证格式错误
  264. $pass = false;
  265. } else if (!$city[substr($card, 0, 2)]) {
  266. //地址错误
  267. $pass = false;
  268. } else {
  269. //18位身份证需要验证最后一位校验位
  270. if (strlen($card) == 18) {
  271. $card = str_split($card);
  272. //∑(ai×Wi)(mod 11)
  273. //加权因子
  274. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  275. //校验位
  276. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  277. $sum = 0;
  278. $ai = 0;
  279. $wi = 0;
  280. for ($i = 0; $i < 17; $i++) {
  281. $ai = $card[$i];
  282. $wi = $factor[$i];
  283. $sum += $ai * $wi;
  284. }
  285. $last = $parity[$sum % 11];
  286. if ($parity[$sum % 11] != $card[17]) {
  287. // $tip = "校验位错误";
  288. $pass = false;
  289. }
  290. } else {
  291. $pass = false;
  292. }
  293. }
  294. if (!$pass) return false;/* 身份证格式错误*/
  295. return true;/* 身份证格式正确*/
  296. }
  297. }
  298. if (!function_exists('check_phone')) {
  299. /**
  300. * 手机号验证
  301. * @param $phone
  302. * @return false|int
  303. */
  304. function check_phone($phone)
  305. {
  306. return preg_match( "/^1[3456789]\d{9}$/", $phone);
  307. }
  308. }
  309. if (!function_exists('anonymity')) {
  310. /**
  311. * 匿名处理处理用户昵称
  312. * @param $name
  313. * @return string
  314. */
  315. function anonymity($name)
  316. {
  317. $strLen = mb_strlen($name, 'UTF-8');
  318. $min = 3;
  319. if ($strLen <= 1)
  320. return '*';
  321. if ($strLen <= $min)
  322. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  323. else
  324. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  325. }
  326. }
  327. if (!function_exists('sort_list_tier')) {
  328. /**
  329. * 分级排序
  330. * @param $data
  331. * @param int $pid
  332. * @param string $field
  333. * @param string $pk
  334. * @param string $html
  335. * @param int $level
  336. * @param bool $clear
  337. * @return array
  338. */
  339. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  340. {
  341. static $list = [];
  342. if ($clear) $list = [];
  343. foreach ($data as $k => $res) {
  344. if ($res[$field] == $pid) {
  345. $res['html'] = str_repeat($html, $level);
  346. $list[] = $res;
  347. unset($data[$k]);
  348. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  349. }
  350. }
  351. return $list;
  352. }
  353. }
  354. if (!function_exists('time_tran')) {
  355. /**
  356. * 时间戳人性化转化
  357. * @param $time
  358. * @return string
  359. */
  360. function time_tran($time)
  361. {
  362. $t = time() - $time;
  363. $f = array(
  364. '31536000' => '年',
  365. '2592000' => '个月',
  366. '604800' => '星期',
  367. '86400' => '天',
  368. '3600' => '小时',
  369. '60' => '分钟',
  370. '1' => '秒'
  371. );
  372. foreach ($f as $k => $v) {
  373. if (0 != $c = floor($t / (int)$k)) {
  374. return $c . $v . '前';
  375. }
  376. }
  377. }
  378. }
  379. if (!function_exists('url_to_path')) {
  380. /**
  381. * url转换路径
  382. * @param $url
  383. * @return string
  384. */
  385. function url_to_path($url)
  386. {
  387. $path = trim(str_replace('/', DS, $url), DS);
  388. if (0 !== strripos($path, 'public'))
  389. $path = 'public' . DS . $path;
  390. return app()->getRootPath() . $path;
  391. }
  392. }
  393. if (!function_exists('path_to_url')) {
  394. /**
  395. * 路径转url路径
  396. * @param $path
  397. * @return string
  398. */
  399. function path_to_url($path)
  400. {
  401. return trim(str_replace(DS, '/', $path), '.');
  402. }
  403. }
  404. if (!function_exists('image_to_base64')) {
  405. /**
  406. * 获取图片转为base64
  407. * @param string $avatar
  408. * @return bool|string
  409. */
  410. function image_to_base64($avatar = '', $timeout = 9)
  411. {
  412. try {
  413. $url = parse_url($avatar);
  414. $url = $url['host'];
  415. $header = [
  416. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  417. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  418. 'Accept-Encoding: gzip, deflate, br',
  419. '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',
  420. 'Host:' . $url
  421. ];
  422. $dir = pathinfo($url);
  423. $host = $dir['dirname'];
  424. $refer = $host . '/';
  425. $curl = curl_init();
  426. curl_setopt($curl, CURLOPT_REFERER, $refer);
  427. curl_setopt($curl, CURLOPT_URL, $avatar);
  428. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  429. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  430. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  431. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  432. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  433. $data = curl_exec($curl);
  434. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  435. curl_close($curl);
  436. if ($code == 200) {
  437. return "data:image/jpeg;base64," . base64_encode($data);
  438. } else {
  439. return false;
  440. }
  441. } catch (\Exception $e) {
  442. return false;
  443. }
  444. }
  445. }
  446. if (!function_exists('put_image')) {
  447. /**
  448. * 获取图片转为base64
  449. * @param string $avatar
  450. * @return bool|string
  451. */
  452. function put_image($url, $filename = '')
  453. {
  454. if ($url == '') {
  455. return false;
  456. }
  457. try {
  458. if ($filename == '') {
  459. $ext = pathinfo($url);
  460. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  461. return false;
  462. }
  463. $filename = time() . "." . $ext['extension'];
  464. }
  465. //文件保存路径
  466. ob_start();
  467. readfile($url);
  468. $img = ob_get_contents();
  469. ob_end_clean();
  470. $path = 'uploads/qrcode';
  471. $fp2 = fopen($path . '/' . $filename, 'a');
  472. fwrite($fp2, $img);
  473. fclose($fp2);
  474. return $path . '/' . $filename;
  475. } catch (\Exception $e) {
  476. return false;
  477. }
  478. }
  479. }
  480. if (!function_exists('debug_file')) {
  481. /**
  482. * 文件调试
  483. * @param $content
  484. */
  485. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  486. {
  487. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  488. $pach = app()->getRuntimePath();
  489. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  490. }
  491. }
  492. if (!function_exists('sql_filter')) {
  493. /**
  494. * sql 参数过滤
  495. * @param string $str
  496. * @return mixed
  497. */
  498. function sql_filter(string $str)
  499. {
  500. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  501. $toupper = array_map(function ($str) {
  502. return strtoupper($str);
  503. }, $filter);
  504. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  505. }
  506. }
  507. if (!function_exists('is_brokerage_statu')) {
  508. /**
  509. * 是否能成为推广人
  510. * @param float $price
  511. * @return bool
  512. */
  513. function is_brokerage_statu(float $price)
  514. {
  515. $storeBrokerageStatus = sys_config('store_brokerage_statu', DISTRIBUTE_SPECIFIED);
  516. if ($storeBrokerageStatus == DISTRIBUTE_SPECIFIED) {
  517. return false;
  518. } else {
  519. $storeBrokeragePrice = sys_config('store_brokerage_price', 0); // 滿足金額
  520. return $price >= $storeBrokeragePrice;
  521. }
  522. }
  523. }
  524. if (!function_exists('array_unique_fb')) {
  525. /**
  526. * 二维数组去掉重复值
  527. * @param $array
  528. * @return array
  529. */
  530. function array_unique_fb($array)
  531. {
  532. $out = array();
  533. foreach ($array as $key => $value) {
  534. if (!in_array($value, $out)) {
  535. $out[$key] = $value;
  536. }
  537. }
  538. $out = array_values($out);
  539. return $out;
  540. }
  541. }
  542. if (!function_exists('ts_of_day')) {
  543. function ts_of_day($timestamp=null) {
  544. if ($timestamp == null) {
  545. $timestamp = time();
  546. }
  547. $tm = localtime($timestamp, true);
  548. return $timestamp - $tm['tm_hour'] * 60 * 60 - $tm['tm_min'] * 60 - $tm['tm_sec'];
  549. }
  550. }
  551. if (!function_exists('mapped_implode')) {
  552. function mapped_implode($glue, $array, $symbol='=') {
  553. return implode($glue, array_map(
  554. function($k, $v) use($symbol) {
  555. return $k . $symbol . $v;
  556. },
  557. array_keys($array),
  558. array_values($array)
  559. )
  560. );
  561. }
  562. }