common.php 18 KB

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