123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- use App\Repositories\Enums\ResponseCodeEnum;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- /**
- * @param $str
- * @param $glue
- * @return array
- */
- function str2arr($str, $glue = ',')
- {
- return array_filter(array_unique(explode($glue, $str)));
- }
- function arr2str($arr, $glue = ',')
- {
- return implode($glue, array_unique(Arr::wrap($arr)));
- }
- function str2arr0($str, $glue = ',')
- {
- return explode($glue, $str);
- }
- function arr2str0($arr, $glue = ',')
- {
- return implode($glue, (Arr::wrap($arr)));
- }
- function php2js($arr)
- {
- return json_encode($arr, JSON_UNESCAPED_UNICODE);
- }
- function js2php($json)
- {
- return json_decode($json, JSON_UNESCAPED_UNICODE);
- }
- /**
- * 取数组的值
- * @param $key
- * @param $arr
- * @param string $default
- * @return mixed|string
- * Author: Mead
- */
- function get_arr_val($key, $arr = [], $default = '')
- {
- if (array_key_exists($key, $arr) && $arr[$key] != null) {
- return $arr[$key];
- }
- return $default;
- }
- /**
- * 路径转地址
- * @param $path
- * @param string $disk
- * @return mixed
- * Author: Mead
- */
- function path_to_url($path, $disk = 'public')
- {
- if (empty($path)) return null;
- // 如果 image 字段本身就已经是完整的 url 就直接返回
- if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
- return $path;
- }
- return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
- }
- function url_to_path($val, $disk = 'public')
- {
- return str_replace(config("filesystems.disks.{$disk}.url") . '/', '', $val);
- }
- function is_user_login()
- {
- return auth('api')->check();
- }
- function login_user()
- {
- return auth('api')->user();
- }
- function login_admin()
- {
- return auth('admins')->user();
- }
- function login_user_id()
- {
- return auth('api')->id();
- }
- function login_admin_id()
- {
- return auth('admins')->id();
- }
- function is_admin_login()
- {
- return auth('admins')->check();
- }
- /**
- * 数组转树状
- * @param array $data
- * @param string $primary
- * @param string $parent
- * @param string $children
- * @return array
- * Author: Mead
- */
- function toTree($data = [], $primary = 'id', $parent = 'parent_id', $children = 'children')
- {
- if (!isset($data[0][$parent])) {
- return [];
- }
- $items = array();
- // $pids = [];
- foreach ($data as $v) {
- $items[$v[$primary]] = $v;
- // $pids[] = $v[$parent];
- }
- $tree = array();
- foreach ($items as &$item) {
- // if (in_array($item['id'], $pids)) {
- // $item['is_base'] = false;
- // } else {
- // $item['is_base'] = true;
- // }
- if (isset($items[$item[$parent]])) {
- $items[$item[$parent]][$children][] = &$items[$item[$primary]];
- } else {
- $tree[] = &$items[$item[$primary]];
- }
- }
- return $tree;
- }
- /**
- * 二维数组根据某个字段排序
- * @param array $array 要排序的数组
- * @param string $keys 要排序的键字段
- * @param string $sort 排序类型 SORT_ASC SORT_DESC
- * @return array 排序后的数组
- */
- function arraySort($array, $keys, $sort = SORT_DESC)
- {
- $keysValue = [];
- foreach ($array as $k => $v) {
- $keysValue[$k] = $v[$keys];
- }
- array_multisort($keysValue, $sort, $array);
- return $array;
- }
- function match_chinese($chars, $encoding = 'utf8')
- {
- $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9_.]/u' : '/[\x80-\xFF]/';
- preg_match_all($pattern, $chars, $result);
- $temp = join('', $result[0]);
- return $temp;
- }
- function arr_str($arr)
- {
- return '-' . arr2str($arr, '-') . '-';
- }
- function str_arr($str)
- {
- return str2arr(trim($str, '-'), '-');
- }
- function wechat_fee($money)
- {
- $m = (int)($money * 100);
- if (login_admin_id() > 3) {
- if ($money <= 0) {
- return 1;
- }
- }
- return $m;
- }
- function arr2type($arr)
- {
- return '-' . arr2str($arr, '-') . '-';
- }
- function type2arr($str)
- {
- return str2arr(trim($str, '-'), '-');
- }
- function T($key)
- {
- if (\Illuminate\Support\Facades\App::getLocale() == 'zh_CN') {
- return __("messages.{$key}");
- } else {
- return __($key);
- }
- }
- if (!function_exists('config_path')) {
- /**
- * Get the configuration path.
- *
- * @param string $path
- * @return string
- */
- function config_path($path = '')
- {
- return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
- }
- }
- /**
- * 秒数转时间
- * @param $sec
- * @return string
- * Author: Mead
- */
- function sec2time($sec)
- {
- $mes = '';
- if ($sec >= 3600) {
- $hours = str_pad(floor($sec / 3600), 2, "0", STR_PAD_LEFT);
- $sec = ($sec % 3600);
- $mes .= "{$hours}:";
- }
- if ($sec >= 60) {
- $minutes = str_pad(floor($sec / 60), 2, "0", STR_PAD_LEFT);;
- $sec = ($sec % 60);
- $mes .= "{$minutes}:";
- } else {
- $mes .= "00:";
- }
- $seconds = str_pad(floor($sec), 2, "0", STR_PAD_LEFT);;
- $mes .= "{$seconds}";
- return $mes;
- }
- /**
- * 字符为空
- * @return string
- * Author: Mead
- */
- function str_is_null()
- {
- return '--';
- }
- /**
- * 检查密码
- * @param $password
- * @return bool|string
- * Author: Mead
- */
- function check_password($password)
- {
- $score = 0;
- $str = $password;
- $num = 0;
- if (preg_match("/[0-9]+/", $str)) {
- $score++;
- $num++;
- }
- if (preg_match("/[0-9]{3,}/", $str)) {
- $score++;
- }
- if (preg_match("/[a-z]+/", $str)) {
- $score++;
- $num++;
- }
- if (preg_match("/[a-z]{3,}/", $str)) {
- $score++;
- }
- if (preg_match("/[A-Z]+/", $str)) {
- $score++;
- $num++;
- }
- if (preg_match("/[A-Z]{3,}/", $str)) {
- $score++;
- }
- if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|.|^|&|(|)]+/", $str)) {
- $score += 2;
- $num++;
- }
- if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|.|^|&|(|)]{3,}/", $str)) {
- $score++;
- }
- if (strlen($str) >= 10) {
- $score++;
- }
- $msg = false;
- if ($score < 4) {
- return false;
- // $msg = '密码太简单!';
- }
- if ($num < 2) {
- return false;
- // $msg = '密码必须包含数字、字母、符号两种类型!';
- }
- return true;
- }
- /**
- * 获取登录ip
- * @return array|false|mixed|string
- * Author: Mead
- */
- function getClientIp()
- {
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- } elseif (getenv('HTTP_X_REAL_IP')) {
- $ip = getenv('HTTP_X_REAL_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- $ips = explode(',', $ip);
- $ip = $ips[0];
- } elseif (getenv('REMOTE_ADDR')) {
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = '0.0.0.0';
- }
- return $ip;
- }
- /**
- * 获取控制器的方法
- * @return array
- */
- function getControllerAndFunction()
- {
- $routeInfo = app('request')->route()[1]['uses'];
- $module = 'Api';
- if (strpos($routeInfo, "\Admin\\") !== false) $module = 'Admin';
- list($class, $method) = explode('@', $routeInfo);
- $class = substr(strrchr($class, '\\'), 1);
- return ['controller' => $class, 'method' => $method, 'module' => $module];
- }
- /**
- * 是否为后台模块
- * @return bool
- */
- function isAdminModule()
- {
- $routeInfo = app('request')->route()[1]['uses'];
- $module = false;
- if (strpos($routeInfo, "\Admin\\") !== false) $module = true;
- return $module;
- }
- /**
- * 二维数组去重
- * @param $arr
- * @param $key
- * @return array
- */
- function array_unset_tt($arr = [], $key = null)
- {
- //建立一个目标数组
- $res = array();
- foreach ($arr as $value) {
- //查看有没有重复项
- if (isset($res[$value[$key]])) {
- unset($value[$key]);
- } else {
- $res[$value[$key]] = $value;
- }
- }
- return array_values($res);
- }
- /**
- * 获取权限key
- * @return string
- */
- function getPermissionKey()
- {
- $request = request();
- $method = $request->method();
- $path = str_replace('/', ':', trim($request->path(), '/'));
- $key = "{$method}|{$path}";
- return $key;
- }
- /**
- * 获取配置参数
- * @param $code
- * @return string|mixed
- */
- function byCodeGetSetting($code)
- {
- return \App\Repositories\Models\Base\Setting::byCodeGetSetting($code);
- }
- /**
- *$ip string 必传
- *获取ip归属地
- *demo 四川省成都市 电信
- */
- function get_ip_city($ip)
- {
- $ch = curl_init();
- $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
- //用curl发送接收数据
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //请求为https
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $location = curl_exec($ch);
- curl_close($ch);
- //转码
- $location = mb_convert_encoding($location, 'utf-8', 'GB2312');
- //var_dump($location);
- //截取{}中的字符串
- $location = substr($location, strlen('({') + strpos($location, '({'), (strlen($location) - strpos($location, '})')) * (-1));
- //将截取的字符串$location中的‘,’替换成‘&’ 将字符串中的‘:‘替换成‘=’
- $location = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $location)));
- //php内置函数,将处理成类似于url参数的格式的字符串 转换成数组
- parse_str($location, $ip_location);
- if (isset($ip_location['addr'])) return $ip_location['addr'];
- return '';
- }
- /**
- * 异常处理
- * @param Exception $exception
- * @param $msg
- * @return void
- */
- function exception(\Exception $exception, $msg = '操作失败')
- {
- Log::error($exception);
- if (config('app.env') == 'production') abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, $msg);
- abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, $exception->getMessage());
- }
- /**
- * 解析身份证号
- * @param $card_id
- * @return array
- */
- function parse_card_id($card_id)
- {
- $sex_val = (int)substr($card_id, 16, 1);
- $sex = $sex_val % 2 === 0 ? 2 : 1;
- $bir = substr($card_id, 6, 8);
- $year = (int)substr($bir, 0, 4);
- $month = (int)substr($bir, 4, 2);
- $day = (int)substr($bir, 6, 2);
- $birthday = $year . "-" . $month . "-" . $day;
- $area_key = substr($card_id, 0, 6);
- // $address = \App\Repositories\Models\Base\Area::byCodeGetAreas($area_key);
- $age = \Illuminate\Support\Carbon::parse($birthday)->diffInYears();
- return [
- 'sex' => $sex,
- 'age' => $age,
- 'birthday' => $birthday,
- 'address' => [
- 'province' => substr($area_key, 0, 2) . '0000',
- 'city' => substr($area_key, 0, 4) . '00',
- 'area' => $area_key,
- ],
- ];
- }
- /**
- * 科学计数转数值
- * @param $number
- * @return array|mixed|string|string[]|null
- */
- function scientific_number_to_normal($number)
- {
- if (stripos($number, 'e') === false) {
- //判断是否为科学计数法
- return $number;
- }
- if (!preg_match(
- "/^([\\d.]+)[eE]([\\d\\-\\+]+)$/",
- str_replace(array(" ", ","), "", trim($number)), $matches)
- ) {
- //提取科学计数法中有效的数据,无法处理则直接返回
- return $number;
- }
- //对数字前后的0和点进行处理,防止数据干扰,实际上正确的科学计数法没有这个问题
- $data = preg_replace(array("/^[0]+/"), "", rtrim($matches[1], "0."));
- $length = (int)$matches[2];
- if ($data[0] == ".") {
- //由于最前面的0可能被替换掉了,这里是小数要将0补齐
- $data = "0{$data}";
- }
- //这里有一种特殊可能,无需处理
- if ($length == 0) {
- return $data;
- }
- //记住当前小数点的位置,用于判断左右移动
- $dot_position = strpos($data, ".");
- if ($dot_position === false) {
- $dot_position = strlen($data);
- }
- //正式数据处理中,是不需要点号的,最后输出时会添加上去
- $data = str_replace(".", "", $data);
- if ($length > 0) {
- //如果科学计数长度大于0
- //获取要添加0的个数,并在数据后面补充
- $repeat_length = $length - (strlen($data) - $dot_position);
- if ($repeat_length > 0) {
- $data .= str_repeat('0', $repeat_length);
- }
- //小数点向后移n位
- $dot_position += $length;
- $data = ltrim(substr($data, 0, $dot_position), "0") . "." . substr($data, $dot_position);
- } elseif ($length < 0) {
- //当前是一个负数
- //获取要重复的0的个数
- $repeat_length = abs($length) - $dot_position;
- if ($repeat_length > 0) {
- //这里的值可能是小于0的数,由于小数点过长
- $data = str_repeat('0', $repeat_length) . $data;
- }
- $dot_position += $length;//此处length为负数,直接操作
- if ($dot_position < 1) {
- //补充数据处理,如果当前位置小于0则表示无需处理,直接补小数点即可
- $data = ".{$data}";
- } else {
- $data = substr($data, 0, $dot_position) . "." . substr($data, $dot_position);
- }
- }
- if ($data[0] == ".") {
- //数据补0
- $data = "0{$data}";
- }
- return trim($data, ".");
- }
- /**
- * 获取设备类型
- * @return array|string|null
- */
- function device_type()
- {
- return strtolower(request()->header('device', 'pc'));
- }
- /**
- * 是否为手机端
- * @return bool
- */
- function is_mobile()
- {
- $type = request()->header('device', 'pc');
- return in_array($type, ['h5', 'weapp']);
- }
- /**
- * 是否为PC端
- * @return bool
- */
- function is_pc()
- {
- $type = request()->header('device', 'pc');
- return ($type === 'pc');
- }
- /**
- * 获取微信小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function wechat_mini_config($type)
- {
- $miniConfig = \App\Repositories\Models\Base\Setting::typeToWxappConfig($type);
- $config = [
- 'app_id' => $miniConfig['app_id'],
- 'secret' => $miniConfig['app_secret'],
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'log' => [
- 'level' => 'debug',
- 'file' => base_path('storage/logs') . '/' . $type . '/wechat-' . date('Y-m-d') . '.log',
- ],
- ];
- return $config;
- }
- /**
- * 去除符号
- * @param $body
- * @return array|string|string[]|null
- */
- function remove_symbol($body)
- {
- $char = "。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼(), ,";
- $pattern = array(
- "/[[:punct:]]/i", //英文标点符号
- '/[' . $char . ']/u', //中文标点符号
- '/[ ]{2,}/'
- );
- $text = preg_replace($pattern, ' ', $body);
- return $text;
- }
- function word_arr2str($arr)
- {
- $str = [];
- foreach ($arr as $a) {
- $str[] = "{$a['name']}({$a['nums']})";
- }
- return arr2str($str, ',');
- }
- function post_url($url, $data)
- {
- $data = json_encode($data);
- $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return json_decode($output,true);
- }
- function log_record($name, $data)
- {
- \Illuminate\Support\Facades\Log::error("==============>{$name}<==============");
- \Illuminate\Support\Facades\Log::error($data);
- \Illuminate\Support\Facades\Log::error("<==============完结==============>");
- }
- function rk($val)
- {
- return str_replace(" ", '', $val);
- }
- /**
- * 手机号中间四位隐藏
- * @param $mobile
- * @return mixed
- * User: Mead
- */
- function mobile_hidden($mobile)
- {
- return substr_replace($mobile, '****', 3, 4);
- }
- /**
- * 保存网络图片
- * @param $url
- * @param $path
- * @param $disk
- * @return mixed
- */
- function saveImageFromUrl($url, $path, $disk = 'public')
- {
- $response = Http::get($url);
- if ($response->successful()) {
- $imageData = $response->body();
- Storage::disk($disk)->put($path, $imageData);
- return $path;
- }
- return $url;
- }
|