common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. // | 数组函数
  14. // +---------------------------------------------------------------------+
  15. if (!function_exists('sys_config')) {
  16. /**
  17. * 数组转换为对象
  18. *
  19. * @author 许祖兴 < zuxing.xu@lettered.cn>
  20. * @date 2020/3/16 10:17
  21. *
  22. * @param string $name 要查找的配置名称
  23. * @param string $group 要查找的配置组名称
  24. * @return mixed
  25. */
  26. function sys_config($name = "", $group= "app")
  27. {
  28. // 直接配置 OR 读取数库
  29. return config($group . '.' . $name);
  30. // return cache('system_config')[$group][$name];
  31. }
  32. }
  33. if (!function_exists('get_annex_url')) {
  34. /**
  35. *
  36. * @author 许祖兴 < zuxing.xu@lettered.cn>
  37. * @date 2020/3/20 0:09
  38. *
  39. * @param string $url
  40. * @param string $driver
  41. * @return mixed|string
  42. */
  43. function get_annex_url($url, $driver = 'local')
  44. {
  45. // return ($driver == 'qiniu' ? 'http://q6tf36mtr.bkt.clouddn.com/'
  46. // : 'http://img.gxrrj.cn') . $url;
  47. return site_url().'/uploads'.$url;
  48. }
  49. }
  50. if (!function_exists('filter_annex_url')) {
  51. /**
  52. *
  53. * @author 许祖兴 < zuxing.xu@lettered.cn>
  54. * @date 2020/7/17 0:09
  55. *
  56. * @param string $url
  57. * @param string $replace
  58. * @param string $driver
  59. * @return mixed|string
  60. */
  61. function filter_annex_url($url,$replace = "", $driver = 'local')
  62. {
  63. // return str_replace(($driver == 'qiniu' ? 'http://q6tf36mtr.bkt.clouddn.com/'
  64. // : 'http://img.gxrrj.cn'), $replace , '/uploads' . $url);
  65. return site_url().$url;
  66. }
  67. }
  68. // +---------------------------------------------------------------------+
  69. // | 数组函数
  70. // +---------------------------------------------------------------------+
  71. if (!function_exists('arr2tree')) {
  72. /**
  73. * 把返回的数据集转换成Tree
  74. *
  75. * @author 许祖兴 < zuxing.xu@lettered.cn>
  76. * @date 2020/3/16 10:07
  77. *
  78. * @param array $arr 传入数组
  79. * @param string $pk 主键
  80. * @param string $pid 父键
  81. * @param string $child 子集
  82. * @param int $root 根
  83. * @return array|bool
  84. */
  85. function arr2tree($arr = [], $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
  86. {
  87. $tree = [];
  88. if (!is_array($arr)) return false;
  89. // 创建基于主键的数组引用
  90. $refer = [];
  91. foreach ($arr as $key => $data) {
  92. $refer[$data[$pk]] =& $arr[$key];
  93. }
  94. foreach ($arr as $key => $data) {
  95. // 判断是否存在parent
  96. $parentId = $data[$pid];
  97. if ($root == $parentId) {
  98. $tree[] =& $arr[$key];
  99. } else if (isset($refer[$parentId])) {
  100. is_object($refer[$parentId]) && $refer[$parentId] = $refer[$parentId]->toArray();
  101. $parent =& $refer[$parentId];
  102. $parent[$child][] =& $arr[$key];
  103. }
  104. }
  105. return $tree;
  106. }
  107. }
  108. if (!function_exists('parse_attr')) {
  109. /**
  110. * 分析数组及枚举类型配置值 格式 a:名称1,b:名称2
  111. *
  112. * @author 许祖兴 < zuxing.xu@lettered.cn>
  113. * @date 2020/3/16 10:17
  114. *
  115. * @param string $string 字符串 key:value
  116. * @return array|array[]|false|string[]
  117. */
  118. function parse_attr($string)
  119. {
  120. $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
  121. $value = [];
  122. if (strpos($string, ':')) {
  123. foreach ($array as $val) {
  124. list($k, $v) = explode(':', $val);
  125. $value[$k] = $v;
  126. }
  127. } else $value = $array;
  128. return $value;
  129. }
  130. }
  131. if (!function_exists('arr2obj')) {
  132. /**
  133. * 数组转换为对象
  134. *
  135. * @author 许祖兴 < zuxing.xu@lettered.cn>
  136. * @date 2020/3/16 10:17
  137. *
  138. * @param array $arr 要转换的数组
  139. * @return object|null
  140. */
  141. function arr2obj($arr)
  142. {
  143. if (gettype($arr) != 'array') {
  144. return null;
  145. }
  146. foreach ($arr as $k => $v) {
  147. if (gettype($v) == 'array' || getType($v) == 'object') {
  148. $arr[$k] = (object)arr2obj($v);
  149. }
  150. }
  151. return (object)$arr;
  152. }
  153. }
  154. if (!function_exists('arr2str')) {
  155. /**
  156. * 数组转换为字符串,主要用于把分隔符调整到第二个参数
  157. *
  158. * @author 许祖兴 < zuxing.xu@lettered.cn>
  159. * @date 2020/3/16 10:20
  160. *
  161. * @param array $arr 要连接的数组
  162. * @param string $glue 分割符
  163. * @return string 返回字符串
  164. */
  165. function arr2str($arr, $glue = ',')
  166. {
  167. return implode($glue, $arr);
  168. }
  169. }
  170. // +---------------------------------------------------------------------+
  171. // | 字符串函数
  172. // +---------------------------------------------------------------------+
  173. if (!function_exists('get_rand_char')) {
  174. /**
  175. * 获取随机字符
  176. *
  177. * @param string $length 长度
  178. * @param bool $strtoup 是否大写
  179. * @return null|string
  180. */
  181. function get_rand_char($length = '32', $strtoup = false)
  182. {
  183. $str = null;
  184. $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789abcdefghijklmnopqrstuvwxyz';
  185. for ($i = 0; $i < $length; $i++) {
  186. $str .= $strPol[rand(0, strlen($strPol) - 1)];
  187. }
  188. return $strtoup ? strtoupper($str) : $str;
  189. }
  190. }
  191. if (!function_exists('get_micro_time')) {
  192. /**
  193. * 获取毫秒级13位时间戳
  194. *
  195. * @return string
  196. */
  197. function get_micro_time()
  198. {
  199. list($t1, $t2) = explode(' ', microtime());
  200. return $t2 . ceil($t1 * 1000);
  201. }
  202. }
  203. if (!function_exists('str2arr')) {
  204. /**
  205. * 字符串转换为数组,主要用于把分隔符调整到第二个参数
  206. *
  207. * @param string $str 要分割的字符串
  208. * @param string $glue 分割符
  209. *
  210. * @return array
  211. */
  212. function str2arr($str, $glue = ',')
  213. {
  214. return explode($glue, preg_replace('/[ ]/', '', $str));
  215. }
  216. }
  217. if (!function_exists('obj2arr')) {
  218. /**
  219. * 对象转数组
  220. *
  221. * @param object $obj 对象
  222. * @return array
  223. */
  224. function obj2arr($obj)
  225. {
  226. $obj = (array)$obj;
  227. foreach ($obj as $k => $v) {
  228. if (gettype($v) == 'resource') {
  229. return null;
  230. }
  231. if (gettype($v) == 'object' || gettype($v) == 'array') {
  232. $obj[$k] = (array)obj2arr($v);
  233. }
  234. }
  235. return $obj;
  236. }
  237. }
  238. if (!function_exists('str_unique')) {
  239. /**
  240. * 唯一字符串
  241. *
  242. * @param string $str 要唯一的字符串
  243. * @return string
  244. */
  245. function str_unique($str)
  246. {
  247. // 先转数组排序 -- 去重
  248. $arr = array_unique(str2arr($str));
  249. asort($arr);
  250. // 转字符串去空格
  251. return trim(arr2str($arr),',');
  252. }
  253. }
  254. // +---------------------------------------------------------------------+
  255. // | 其他函数
  256. // +---------------------------------------------------------------------+
  257. if (!function_exists('enjson')) {
  258. /**
  259. * JSON ENCODE
  260. *
  261. * @param array $arr 数据组
  262. * @return string
  263. */
  264. function enjson($arr)
  265. {
  266. return json_encode($arr, JSON_UNESCAPED_UNICODE);
  267. }
  268. }
  269. if (!function_exists('dejson')) {
  270. /**
  271. * JSON DECODE
  272. *
  273. * @param string $str 字符串
  274. * @return array
  275. */
  276. function dejson($str)
  277. {
  278. return json_decode($str, true);
  279. }
  280. }
  281. if (!function_exists('get_user_agent')){
  282. /**
  283. *
  284. * @author 许祖兴 < zuxing.xu@lettered.cn>
  285. * @date 2020/3/16 13:07
  286. *
  287. * @param string $match
  288. * @return string
  289. */
  290. function get_user_agent($match = 'os')
  291. {
  292. // 请求agent
  293. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  294. // 返回值
  295. $out = "Unknown";
  296. if (!empty($user_agent)) {
  297. if ($match == 'os'){
  298. if (preg_match('/win/i', $user_agent)) {
  299. $out = 'Windows';
  300. } else if (preg_match('/mac/i', $user_agent)) {
  301. $out = 'MAC';
  302. } else if (preg_match('/linux/i', $user_agent)) {
  303. $out = 'Linux';
  304. } else if (preg_match('/unix/i', $user_agent)) {
  305. $out = 'Unix';
  306. } else if (preg_match('/bsd/i', $user_agent)) {
  307. $out = 'BSD';
  308. } else {
  309. $out = 'Other';
  310. }
  311. }else{
  312. if (preg_match('/MSIE/i', $user_agent)) {
  313. $out = 'MSIE';
  314. } else if (preg_match('/Firefox/i', $user_agent)) {
  315. $out = 'Firefox';
  316. } else if (preg_match('/Chrome/i', $user_agent)) {
  317. $out = 'Chrome';
  318. } else if (preg_match('/Safari/i', $user_agent)) {
  319. $out = 'Safari';
  320. } else if (preg_match('/Opera/i', $user_agent)) {
  321. $out = 'Opera';
  322. } else {
  323. $out = 'Other';
  324. }
  325. }
  326. }
  327. return $out;
  328. }
  329. }
  330. if (!function_exists('get_file_ext')) {
  331. /**
  332. * 取得文件后缀
  333. *
  334. * @author 许祖兴 < zuxing.xu@lettered.cn>
  335. * @date 2020/3/19 23:31
  336. *
  337. * @param string $path
  338. * @return mixed|string
  339. */
  340. function get_file_ext($path = '')
  341. {
  342. if(!empty(pathinfo($path))){
  343. return isset(pathinfo($path)['extension']) ? pathinfo($path)['extension'] : 'none';
  344. }
  345. return 'none';
  346. }
  347. }
  348. /********2020-06-11 新增**********/
  349. if (!function_exists('make_mcard_id')) {
  350. /**
  351. * 获取唯一ID
  352. *
  353. * @author 许祖兴 < zuxing.xu@lettered.cn>
  354. * @date 2020/6/11 14:33
  355. *
  356. * @param string $url
  357. * @return mixed|string
  358. */
  359. function make_mcard_id()
  360. {
  361. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  362. $rand = $code[rand(0, 25)]
  363. . strtoupper(dechex(date('m')))
  364. . date('d') . substr(time(), -2)
  365. . substr(microtime(), 2, 8)
  366. . sprintf('%02d', rand(0, 99));
  367. for (
  368. $a = md5($rand, true),
  369. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
  370. $d = '',
  371. $f = 0;
  372. $f < 10;
  373. $g = ord($a[$f]),
  374. $d .= $s[($g ^ ord($a[$f + 5])) - $g & 0x1F],
  375. $f++
  376. ) ;
  377. return $d;
  378. }
  379. }
  380. if (!function_exists('get_order_no')) {
  381. /**
  382. * 获取订单号
  383. *
  384. * @author 许祖兴 < zuxing.xu@lettered.cn>
  385. * @date 2020/7/1 14:20
  386. *
  387. * @return string
  388. */
  389. function get_order_no()
  390. {
  391. return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  392. }
  393. }
  394. if (!function_exists('curl_post')) {
  395. /**
  396. * CURL POST
  397. *
  398. * @param string $url post请求地址
  399. * @param array $params
  400. *
  401. * @return mixed
  402. */
  403. function curl_post($url, array $params = array())
  404. {
  405. $data_string = json_encode($params);
  406. $ch = curl_init();
  407. curl_setopt($ch, CURLOPT_URL, $url);
  408. curl_setopt($ch, CURLOPT_HEADER, 0);
  409. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  410. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  411. curl_setopt($ch, CURLOPT_POST, 1);
  412. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  413. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  414. curl_setopt(
  415. $ch, CURLOPT_HTTPHEADER,
  416. array(
  417. 'Content-Type: application/json'
  418. )
  419. );
  420. $data = curl_exec($ch);
  421. curl_close($ch);
  422. return ($data);
  423. }
  424. }
  425. if (!function_exists('curl_post_raw')) {
  426. /**
  427. * CURL POST
  428. *
  429. * @param string $url post请求地址
  430. * @param array $rawData
  431. *
  432. * @return mixed
  433. */
  434. function curl_post_raw($url, $rawData)
  435. {
  436. $ch = curl_init();
  437. curl_setopt($ch, CURLOPT_URL, $url);
  438. curl_setopt($ch, CURLOPT_HEADER, 0);
  439. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  440. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  441. curl_setopt($ch, CURLOPT_POST, 1);
  442. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  443. curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
  444. curl_setopt(
  445. $ch, CURLOPT_HTTPHEADER,
  446. array(
  447. 'Content-Type: text'
  448. )
  449. );
  450. $data = curl_exec($ch);
  451. curl_close($ch);
  452. return ($data);
  453. }
  454. }
  455. if (!function_exists('curl_get')) {
  456. /**
  457. * CURL GET
  458. *
  459. * @param string $url get请求地址
  460. * @param int $httpCode 返回状态码
  461. *
  462. * @return mixed
  463. */
  464. function curl_get($url, &$httpCode = 0)
  465. {
  466. $ch = curl_init();
  467. curl_setopt($ch, CURLOPT_URL, $url);
  468. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  469. //不做证书校验,部署在linux环境下请改为true
  470. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  471. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  472. $file_contents = curl_exec($ch);
  473. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  474. curl_close($ch);
  475. return $file_contents;
  476. }
  477. }
  478. if(!function_exists('push_socket_data')){
  479. /**
  480. *
  481. * @author 许祖兴 < zuxing.xu@lettered.cn>
  482. * @date 2020/8/27 16:46
  483. *
  484. * @param string $type
  485. * @param $data
  486. */
  487. function push_socket_data($type = 'goods', $data = ''){
  488. curl_post('http://127.0.0.1:2121?type=' . $type . '&content=' . enjson($data));
  489. }
  490. }
  491. if(!function_exists('p')){
  492. /**
  493. *
  494. * @param string $data
  495. * @param bool $die
  496. * @author 许祖兴 < zuxing.xu@lettered.cn>
  497. * @date 2020/8/27 16:46
  498. *
  499. */
  500. function p($data = '', $die = false){
  501. echo "<pre>";
  502. print_r($data);
  503. if ($die) {
  504. die();
  505. }
  506. }
  507. }
  508. if (!function_exists('site_url')) {
  509. /**
  510. * @desc 返回URL协议和域名
  511. * @link https://www.sunzhongwei.com/php-request-for-domain-name-and-agreement?from=sidebar_new
  512. * @author 大象笔记<zhongwei.sun2008在gmail.com>
  513. * @return string
  514. * @date 2020/04/14
  515. */
  516. function site_url() {
  517. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  518. $domainName = $_SERVER['HTTP_HOST'];
  519. return $protocol . $domainName;
  520. }
  521. }