common.php 17 KB

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