common.php 19 KB

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