123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- <?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\ModelStatusEnum;
- use Carbon\Carbon;
- use Illuminate\Support\Arr;
- 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, true);
- }
- function js2php($json)
- {
- return json_decode($json, true);
- }
- /**
- * 取数组的值
- * @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 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, $reBool = false)
- {
- $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++;
- }
- if ($reBool) {
- if ($score < 4) return false;
- if ($num < 2) return false;
- return true;
- }
- $msg = false;
- if ($score < 4) {
- $msg = '密码太简单!';
- }
- if ($num < 2) {
- $msg = '密码必须包含数字、字母、符号两种类型!';
- }
- return $msg;
- }
- /**
- * 获取登录ip
- * @return array|false|mixed|string
- * Author: Mead
- */
- function getClientIp()
- {
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- }
- if (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;
- }
- /**
- * 二维数组去重
- * @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);
- }
- /**
- * 获取控制器的方法
- * @return array
- */
- function getControllerAndFunction()
- {
- $routeInfo = app('request')->route()[1]['uses'];
- list($class, $method) = explode('@', $routeInfo);
- $module = 'admin';
- if (strpos($class, "\\Api\\") !== false) {
- $module = 'api';
- }
- $class = substr(strrchr($class, '\\'), 1);
- return ['controller' => $class, 'method' => $method, 'module' => $module];
- }
- /**
- * 删除空格「mead」
- * @param $str
- * @return array|string|string[]
- */
- function rk($str)
- {
- return str_replace(" ", '', $str);
- }
- /**
- * 获取值
- * @param $val
- * @param $d
- * @return mixed|string
- */
- function getVal($val, $d = '')
- {
- return isset($val) ? $val : $d;
- }
- /**
- * 异常处理
- * @param Exception $exception
- * @param $msg
- * @return void
- */
- function exception(\Exception $exception, $msg = '操作失败')
- {
- \Illuminate\Support\Facades\Log::error($exception);
- if (config('app.env') == 'production') abort(\App\Repositories\Enums\ResponseCodeEnum::SERVICE_OPERATION_ERROR, $msg);
- abort(\App\Repositories\Enums\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');
- }
- /**
- * 是否为 Api 路由
- * @return bool
- */
- function isApi()
- {
- // $con = getControllerAndFunction();
- // if ($con['module'] == 'api') {
- // return true;
- // }
- // return false;
- return isApiModule();
- }
- function isAdmin()
- {
- // $con = getControllerAndFunction();
- // if ($con['module'] == 'admin') {
- // return true;
- // }
- // return false;
- return isAdminModule();
- }
- /**
- * 是否为后台模块
- * @return bool
- */
- function isAdminModule()
- {
- $routeInfo = app('request')->route()[1]['uses'];
- $module = false;
- if (strpos($routeInfo, "\Admin\\") !== false) $module = true;
- return $module;
- }
- function isApiModule()
- {
- $routeInfo = app('request')->route()[1]['uses'];
- $module = false;
- if (strpos($routeInfo, "\Api\\") !== false) $module = true;
- return $module;
- }
- /**
- * 获取配置参数
- * @param $code
- * @return string|mixed
- */
- function byCodeGetSetting($code)
- {
- return \App\Repositories\Models\Base\Setting::byCodeGetSetting($code);
- }
|