common.php 17 KB

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