helpers.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. use App\Repositories\Enums\ResponseCodeEnum;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Facades\Log;
  13. /**
  14. * @param $str
  15. * @param $glue
  16. * @return array
  17. */
  18. function str2arr($str, $glue = ',')
  19. {
  20. return array_filter(array_unique(explode($glue, $str)));
  21. }
  22. function arr2str($arr, $glue = ',')
  23. {
  24. return implode($glue, array_unique(Arr::wrap($arr)));
  25. }
  26. function str2arr0($str, $glue = ',')
  27. {
  28. return explode($glue, $str);
  29. }
  30. function arr2str0($arr, $glue = ',')
  31. {
  32. return implode($glue, (Arr::wrap($arr)));
  33. }
  34. function php2js($arr)
  35. {
  36. return json_encode($arr, true);
  37. }
  38. function js2php($json)
  39. {
  40. return json_decode($json, true);
  41. }
  42. /**
  43. * 取数组的值
  44. * @param $key
  45. * @param $arr
  46. * @param string $default
  47. * @return mixed|string
  48. * Author: Mead
  49. */
  50. function get_arr_val($key, $arr = [], $default = '')
  51. {
  52. if (array_key_exists($key, $arr) && $arr[$key] != null) {
  53. return $arr[$key];
  54. }
  55. return $default;
  56. }
  57. /**
  58. * 路径转地址
  59. * @param $path
  60. * @param string $disk
  61. * @return mixed
  62. * Author: Mead
  63. */
  64. function path_to_url($path, $disk = 'public')
  65. {
  66. if (empty($path)) return null;
  67. // 如果 image 字段本身就已经是完整的 url 就直接返回
  68. if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
  69. return $path;
  70. }
  71. return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
  72. }
  73. function is_user_login()
  74. {
  75. return auth('api')->check();
  76. }
  77. function login_user()
  78. {
  79. return auth('api')->user();
  80. }
  81. function login_admin()
  82. {
  83. return auth('admins')->user();
  84. }
  85. function login_user_id()
  86. {
  87. return auth('api')->id();
  88. }
  89. function login_admin_id()
  90. {
  91. return auth('admins')->id();
  92. }
  93. function is_admin_login()
  94. {
  95. return auth('admins')->check();
  96. }
  97. /**
  98. * 数组转树状
  99. * @param array $data
  100. * @param string $primary
  101. * @param string $parent
  102. * @param string $children
  103. * @return array
  104. * Author: Mead
  105. */
  106. function toTree($data = [], $primary = 'id', $parent = 'parent_id', $children = 'children')
  107. {
  108. if (!isset($data[0][$parent])) {
  109. return [];
  110. }
  111. $items = array();
  112. // $pids = [];
  113. foreach ($data as $v) {
  114. $items[$v[$primary]] = $v;
  115. // $pids[] = $v[$parent];
  116. }
  117. $tree = array();
  118. foreach ($items as &$item) {
  119. // if (in_array($item['id'], $pids)) {
  120. // $item['is_base'] = false;
  121. // } else {
  122. // $item['is_base'] = true;
  123. // }
  124. if (isset($items[$item[$parent]])) {
  125. $items[$item[$parent]][$children][] = &$items[$item[$primary]];
  126. } else {
  127. $tree[] = &$items[$item[$primary]];
  128. }
  129. }
  130. return $tree;
  131. }
  132. /**
  133. * 二维数组根据某个字段排序
  134. * @param array $array 要排序的数组
  135. * @param string $keys 要排序的键字段
  136. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  137. * @return array 排序后的数组
  138. */
  139. function arraySort($array, $keys, $sort = SORT_DESC)
  140. {
  141. $keysValue = [];
  142. foreach ($array as $k => $v) {
  143. $keysValue[$k] = $v[$keys];
  144. }
  145. array_multisort($keysValue, $sort, $array);
  146. return $array;
  147. }
  148. function match_chinese($chars, $encoding = 'utf8')
  149. {
  150. $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9_.]/u' : '/[\x80-\xFF]/';
  151. preg_match_all($pattern, $chars, $result);
  152. $temp = join('', $result[0]);
  153. return $temp;
  154. }
  155. function arr_str($arr)
  156. {
  157. return '-' . arr2str($arr, '-') . '-';
  158. }
  159. function str_arr($str)
  160. {
  161. return str2arr(trim($str, '-'), '-');
  162. }
  163. function wechat_fee($money)
  164. {
  165. $m = (int)($money * 100);
  166. if (login_admin_id() > 3) {
  167. if ($money <= 0) {
  168. return 1;
  169. }
  170. }
  171. return $m;
  172. }
  173. function arr2type($arr)
  174. {
  175. return '-' . arr2str($arr, '-') . '-';
  176. }
  177. function type2arr($str)
  178. {
  179. return str2arr(trim($str, '-'), '-');
  180. }
  181. function T($key)
  182. {
  183. if (\Illuminate\Support\Facades\App::getLocale() == 'zh_CN') {
  184. return __("messages.{$key}");
  185. } else {
  186. return __($key);
  187. }
  188. }
  189. if (!function_exists('config_path')) {
  190. /**
  191. * Get the configuration path.
  192. *
  193. * @param string $path
  194. * @return string
  195. */
  196. function config_path($path = '')
  197. {
  198. return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
  199. }
  200. }
  201. /**
  202. * 秒数转时间
  203. * @param $sec
  204. * @return string
  205. * Author: Mead
  206. */
  207. function sec2time($sec)
  208. {
  209. $mes = '';
  210. if ($sec >= 3600) {
  211. $hours = str_pad(floor($sec / 3600), 2, "0", STR_PAD_LEFT);
  212. $sec = ($sec % 3600);
  213. $mes .= "{$hours}:";
  214. }
  215. if ($sec >= 60) {
  216. $minutes = str_pad(floor($sec / 60), 2, "0", STR_PAD_LEFT);;
  217. $sec = ($sec % 60);
  218. $mes .= "{$minutes}:";
  219. } else {
  220. $mes .= "00:";
  221. }
  222. $seconds = str_pad(floor($sec), 2, "0", STR_PAD_LEFT);;
  223. $mes .= "{$seconds}";
  224. return $mes;
  225. }
  226. /**
  227. * 字符为空
  228. * @return string
  229. * Author: Mead
  230. */
  231. function str_is_null()
  232. {
  233. return '--';
  234. }
  235. /**
  236. * 检查密码
  237. * @param $password
  238. * @return bool|string
  239. * Author: Mead
  240. */
  241. function check_password($password)
  242. {
  243. $score = 0;
  244. $str = $password;
  245. $num = 0;
  246. if (preg_match("/[0-9]+/", $str)) {
  247. $score++;
  248. $num++;
  249. }
  250. if (preg_match("/[0-9]{3,}/", $str)) {
  251. $score++;
  252. }
  253. if (preg_match("/[a-z]+/", $str)) {
  254. $score++;
  255. $num++;
  256. }
  257. if (preg_match("/[a-z]{3,}/", $str)) {
  258. $score++;
  259. }
  260. if (preg_match("/[A-Z]+/", $str)) {
  261. $score++;
  262. $num++;
  263. }
  264. if (preg_match("/[A-Z]{3,}/", $str)) {
  265. $score++;
  266. }
  267. if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|.|^|&|(|)]+/", $str)) {
  268. $score += 2;
  269. $num++;
  270. }
  271. if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|.|^|&|(|)]{3,}/", $str)) {
  272. $score++;
  273. }
  274. if (strlen($str) >= 10) {
  275. $score++;
  276. }
  277. $msg = false;
  278. if ($score < 4) {
  279. return false;
  280. // $msg = '密码太简单!';
  281. }
  282. if ($num < 2) {
  283. return false;
  284. // $msg = '密码必须包含数字、字母、符号两种类型!';
  285. }
  286. return true;
  287. }
  288. /**
  289. * 获取登录ip
  290. * @return array|false|mixed|string
  291. * Author: Mead
  292. */
  293. function getClientIp()
  294. {
  295. if (getenv('HTTP_CLIENT_IP')) {
  296. $ip = getenv('HTTP_CLIENT_IP');
  297. }
  298. if (getenv('HTTP_X_REAL_IP')) {
  299. $ip = getenv('HTTP_X_REAL_IP');
  300. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  301. $ip = getenv('HTTP_X_FORWARDED_FOR');
  302. $ips = explode(',', $ip);
  303. $ip = $ips[0];
  304. } elseif (getenv('REMOTE_ADDR')) {
  305. $ip = getenv('REMOTE_ADDR');
  306. } else {
  307. $ip = '0.0.0.0';
  308. }
  309. return $ip;
  310. }
  311. /**
  312. * 获取控制器的方法
  313. * @return array
  314. */
  315. function getControllerAndFunction()
  316. {
  317. $routeInfo = app('request')->route()[1]['uses'];
  318. list($class, $method) = explode('@', $routeInfo);
  319. $class = substr(strrchr($class, '\\'), 1);
  320. return ['controller' => $class, 'method' => $method];
  321. }
  322. /**
  323. * 二维数组去重
  324. * @param $arr
  325. * @param $key
  326. * @return array
  327. */
  328. function array_unset_tt($arr = [], $key = null)
  329. {
  330. //建立一个目标数组
  331. $res = array();
  332. foreach ($arr as $value) {
  333. //查看有没有重复项
  334. if (isset($res[$value[$key]])) {
  335. unset($value[$key]);
  336. } else {
  337. $res[$value[$key]] = $value;
  338. }
  339. }
  340. return array_values($res);
  341. }
  342. /**
  343. * 获取权限key
  344. * @return string
  345. */
  346. function getPermissionKey()
  347. {
  348. $request = request();
  349. $method = $request->method();
  350. $path = str_replace('/', ':', trim($request->path(), '/'));
  351. $key = "{$method}|{$path}";
  352. return $key;
  353. }
  354. /**
  355. * 获取配置参数
  356. * @param $code
  357. * @return string|mixed
  358. */
  359. function byCodeGetSetting($code)
  360. {
  361. return \App\Repositories\Models\Base\Setting::byCodeGetSetting($code);
  362. }
  363. /**
  364. *$ip string 必传
  365. *获取ip归属地
  366. *demo 四川省成都市 电信
  367. */
  368. function get_ip_city($ip)
  369. {
  370. $ch = curl_init();
  371. $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
  372. //用curl发送接收数据
  373. curl_setopt($ch, CURLOPT_URL, $url);
  374. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  375. //请求为https
  376. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  377. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  378. $location = curl_exec($ch);
  379. curl_close($ch);
  380. //转码
  381. $location = mb_convert_encoding($location, 'utf-8', 'GB2312');
  382. //var_dump($location);
  383. //截取{}中的字符串
  384. $location = substr($location, strlen('({') + strpos($location, '({'), (strlen($location) - strpos($location, '})')) * (-1));
  385. //将截取的字符串$location中的‘,’替换成‘&’ 将字符串中的‘:‘替换成‘=’
  386. $location = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $location)));
  387. //php内置函数,将处理成类似于url参数的格式的字符串 转换成数组
  388. parse_str($location, $ip_location);
  389. return $ip_location['addr'];
  390. }
  391. /**
  392. * 异常处理
  393. * @param Exception $exception
  394. * @param $msg
  395. * @return void
  396. */
  397. function exception(\Exception $exception, $msg = '操作失败')
  398. {
  399. Log::error($exception);
  400. if (config('app.env') == 'production') abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, $msg);
  401. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, $exception->getMessage());
  402. }
  403. /**
  404. * 解析身份证号
  405. * @param $card_id
  406. * @return array
  407. */
  408. function parse_card_id($card_id)
  409. {
  410. $sex_val = (int)substr($card_id, 16, 1);
  411. $sex = $sex_val % 2 === 0 ? 2 : 1;
  412. $bir = substr($card_id, 6, 8);
  413. $year = (int)substr($bir, 0, 4);
  414. $month = (int)substr($bir, 4, 2);
  415. $day = (int)substr($bir, 6, 2);
  416. $birthday = $year . "-" . $month . "-" . $day;
  417. $area_key = substr($card_id, 0, 6);
  418. // $address = \App\Repositories\Models\Base\Area::byCodeGetAreas($area_key);
  419. $age = \Illuminate\Support\Carbon::parse($birthday)->diffInYears();
  420. return [
  421. 'sex' => $sex,
  422. 'age' => $age,
  423. 'birthday' => $birthday,
  424. 'address' => [
  425. 'province' => substr($area_key, 0, 2) . '0000',
  426. 'city' => substr($area_key, 0, 4) . '00',
  427. 'area' => $area_key,
  428. ],
  429. ];
  430. }
  431. /**
  432. * 科学计数转数值
  433. * @param $number
  434. * @return array|mixed|string|string[]|null
  435. */
  436. function scientific_number_to_normal($number)
  437. {
  438. if (stripos($number, 'e') === false) {
  439. //判断是否为科学计数法
  440. return $number;
  441. }
  442. if (!preg_match(
  443. "/^([\\d.]+)[eE]([\\d\\-\\+]+)$/",
  444. str_replace(array(" ", ","), "", trim($number)), $matches)
  445. ) {
  446. //提取科学计数法中有效的数据,无法处理则直接返回
  447. return $number;
  448. }
  449. //对数字前后的0和点进行处理,防止数据干扰,实际上正确的科学计数法没有这个问题
  450. $data = preg_replace(array("/^[0]+/"), "", rtrim($matches[1], "0."));
  451. $length = (int)$matches[2];
  452. if ($data[0] == ".") {
  453. //由于最前面的0可能被替换掉了,这里是小数要将0补齐
  454. $data = "0{$data}";
  455. }
  456. //这里有一种特殊可能,无需处理
  457. if ($length == 0) {
  458. return $data;
  459. }
  460. //记住当前小数点的位置,用于判断左右移动
  461. $dot_position = strpos($data, ".");
  462. if ($dot_position === false) {
  463. $dot_position = strlen($data);
  464. }
  465. //正式数据处理中,是不需要点号的,最后输出时会添加上去
  466. $data = str_replace(".", "", $data);
  467. if ($length > 0) {
  468. //如果科学计数长度大于0
  469. //获取要添加0的个数,并在数据后面补充
  470. $repeat_length = $length - (strlen($data) - $dot_position);
  471. if ($repeat_length > 0) {
  472. $data .= str_repeat('0', $repeat_length);
  473. }
  474. //小数点向后移n位
  475. $dot_position += $length;
  476. $data = ltrim(substr($data, 0, $dot_position), "0") . "." . substr($data, $dot_position);
  477. } elseif ($length < 0) {
  478. //当前是一个负数
  479. //获取要重复的0的个数
  480. $repeat_length = abs($length) - $dot_position;
  481. if ($repeat_length > 0) {
  482. //这里的值可能是小于0的数,由于小数点过长
  483. $data = str_repeat('0', $repeat_length) . $data;
  484. }
  485. $dot_position += $length;//此处length为负数,直接操作
  486. if ($dot_position < 1) {
  487. //补充数据处理,如果当前位置小于0则表示无需处理,直接补小数点即可
  488. $data = ".{$data}";
  489. } else {
  490. $data = substr($data, 0, $dot_position) . "." . substr($data, $dot_position);
  491. }
  492. }
  493. if ($data[0] == ".") {
  494. //数据补0
  495. $data = "0{$data}";
  496. }
  497. return trim($data, ".");
  498. }
  499. /**
  500. * 获取设备类型
  501. * @return array|string|null
  502. */
  503. function device_type()
  504. {
  505. return strtolower(request()->header('device', 'pc'));
  506. }
  507. /**
  508. * 是否为手机端
  509. * @return bool
  510. */
  511. function is_mobile()
  512. {
  513. $type = request()->header('device', 'pc');
  514. return in_array($type, ['h5', 'weapp']);
  515. }
  516. /**
  517. * 是否为PC端
  518. * @return bool
  519. */
  520. function is_pc()
  521. {
  522. $type = request()->header('device', 'pc');
  523. return ($type === 'pc');
  524. }
  525. /**
  526. * 获取微信小程序配置
  527. *
  528. * @param array|string|null $key
  529. * @param mixed $default
  530. * @return \Illuminate\Http\Request|string|array
  531. */
  532. function wechat_mini_config($type)
  533. {
  534. $miniConfig = \App\Repositories\Models\Base\Setting::typeToWxappConfig($type);
  535. $config = [
  536. 'app_id' => $miniConfig['app_id'],
  537. 'secret' => $miniConfig['app_secret'],
  538. // 下面为可选项
  539. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  540. 'response_type' => 'array',
  541. 'log' => [
  542. 'level' => 'debug',
  543. 'file' => base_path('storage/logs') . '/' . $type . '/wechat-' . date('Y-m-d') . '.log',
  544. ],
  545. ];
  546. return $config;
  547. }
  548. /**
  549. * 去除符号
  550. * @param $body
  551. * @return array|string|string[]|null
  552. */
  553. function remove_symbol($body)
  554. {
  555. $char = "。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼(), ,";
  556. $pattern = array(
  557. "/[[:punct:]]/i", //英文标点符号
  558. '/[' . $char . ']/u', //中文标点符号
  559. '/[ ]{2,}/'
  560. );
  561. $text = preg_replace($pattern, ' ', $body);
  562. return $text;
  563. }
  564. function word_arr2str($arr)
  565. {
  566. $str = [];
  567. foreach ($arr as $a) {
  568. $str[] = "{$a['name']}({$a['nums']})";
  569. }
  570. return arr2str($str, ',');
  571. }
  572. function post_url($url, $data)
  573. {
  574. $data = json_encode($data);
  575. $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
  576. $curl = curl_init();
  577. curl_setopt($curl, CURLOPT_URL, $url);
  578. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  579. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  580. curl_setopt($curl, CURLOPT_POST, 1);
  581. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  582. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
  583. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  584. $output = curl_exec($curl);
  585. curl_close($curl);
  586. return json_decode($output,true);
  587. }