|
|
@@ -0,0 +1,402 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+use App\Facades\Servers\Logs\Log;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Intervention\Image\Facades\Image;
|
|
|
+/**
|
|
|
+ * 响应请求
|
|
|
+ * @param mixed $data 数据
|
|
|
+ * @param string $code 状态码
|
|
|
+ * @param mixed $log 是否记录日志
|
|
|
+ *
|
|
|
+ * */
|
|
|
+function json_send($data,$code=200){
|
|
|
+ // 日志名称
|
|
|
+ $name = empty($data['code']) ? 'unknown' : $data['code'];
|
|
|
+ // 获取提示消息
|
|
|
+ $msg = empty($data['msg']) ? '' : $data['msg'];
|
|
|
+ // 如果提示数据不是成功或者是local
|
|
|
+ if( ($name != 'success' && $name != 'retry' ) || config('app.env') == 'local' ) {
|
|
|
+ // 消息key
|
|
|
+ $msgKey = 'message.'.$msg;
|
|
|
+ // 提示信息本地化
|
|
|
+ $trans = trans($msgKey);
|
|
|
+ // 如果没有找到对应的提示,使用原提示信息
|
|
|
+ $data['msg'] = ( $trans == $msgKey ) ? $msg : $trans;
|
|
|
+ // 消息
|
|
|
+ $msg = request()->ip().' ' .request()->method().' '. request()->getPathInfo().' '.$msg;
|
|
|
+ // 记录内容
|
|
|
+ $content = ['param'=>request()->all(),'return'=>$data,'user_agent'=>request()->header('user-agent')];
|
|
|
+ // 记录日志
|
|
|
+ Log::info($name,$msg,$content,['filename'=>str_ireplace('/','_',trim(request()->getPathInfo(),'/'))]);
|
|
|
+ }
|
|
|
+ // 默认返回数据
|
|
|
+ $data['data'] = isset($data['data']) ? $data['data'] : '';
|
|
|
+ // 返回数据
|
|
|
+ return response()->json($data,$code,[],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 雪花算法生成分布式ID
|
|
|
+ *
|
|
|
+ * @param int $dataCenterId 数据中心ID 0-31
|
|
|
+ * @param int $machineId 任务进程ID 0-31
|
|
|
+ *
|
|
|
+ * @return string 分布式ID
|
|
|
+ * */
|
|
|
+function make_snow_flake($dataCenterID=0,$machineId=0){
|
|
|
+ // 获取redis配置
|
|
|
+ $redis = config('database.redis.default');
|
|
|
+ // 主机地址
|
|
|
+ $config['host'] = empty($redis['host']) ? '127.0.0.1' : $redis['host'];
|
|
|
+ // 端口
|
|
|
+ $config['port'] = empty($redis['port']) ? 6379 : $redis['port'];
|
|
|
+ // 密码
|
|
|
+ if( !empty($redis['password']) ) $config['auth'] = $redis['password'];
|
|
|
+ // 数据库
|
|
|
+ if( !empty($redis['database']) ) $config['dbIndex'] = $redis['database'];
|
|
|
+ // 实例化
|
|
|
+ $IdWorker = \wantp\Snowflake\IdWorker::getIns($dataCenterID,$machineId);
|
|
|
+ // 生成
|
|
|
+ return $IdWorker->setRedisCountServer($config)->id();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 终端设备系统
|
|
|
+ *
|
|
|
+ * @param string $agent HTTP_USER_AGENT数据
|
|
|
+ *
|
|
|
+ * */
|
|
|
+function get_agent_system($agent=''){
|
|
|
+ //HTTP_USER_AGENT数据
|
|
|
+ $agent = $agent ? $agent : request()->header('user-agent');
|
|
|
+ //iOS
|
|
|
+ if( stripos($agent, 'iphone') || stripos($agent, 'ipad') ) return 'iOS';
|
|
|
+ //iOS
|
|
|
+ if( stripos($agent, 'macintosh') ) return 'MacOS';
|
|
|
+ //安卓
|
|
|
+ if( stripos($agent, 'android') ) return 'Android';
|
|
|
+ //安卓
|
|
|
+ if( stripos($agent, 'okhttp') ) return 'Android';
|
|
|
+ //Windows
|
|
|
+ if( stripos($agent, 'win') ) return 'Windows';
|
|
|
+ //Linux
|
|
|
+ if( stripos($agent, 'linux') ) return 'Linux';
|
|
|
+ //Unix
|
|
|
+ if( stripos($agent, 'unix') ) return 'Unix';
|
|
|
+ //Other
|
|
|
+ return 'Other';
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 路径兼容输出
|
|
|
+ *
|
|
|
+ * @param string $url 文件路径
|
|
|
+ * @param int|null $width 新图片宽度
|
|
|
+ * @param int|null $height 新图片高度(如果没有填写高度,把高度等比例缩小)
|
|
|
+ * @param string $type 文件类型
|
|
|
+ *
|
|
|
+ * @author 刘相欣
|
|
|
+ * */
|
|
|
+function path_compat( $url,$witdh=null,$higth=null,$type='') {
|
|
|
+ // 没有路径的统一返回原数据
|
|
|
+ if( !$url ) return $type ? $url : request()->root().'/'.ltrim(resize($url,$witdh,$higth),'/');
|
|
|
+ // 如果是带有http的路径,直接返回
|
|
|
+ if( stripos('$'.$url, 'http') ) {
|
|
|
+ // 腾讯云链
|
|
|
+ if( stripos( $url, 'myqcloud.com') ){
|
|
|
+ // 开启全球域名全球加速时, 替换COS全球加速域名
|
|
|
+ if( config('open_accelerate','') ) $url = str_ireplace('.cos.'.config('tencent.cos.region','').'.myqcloud.com','.cos.accelerate.myqcloud.com',$url);
|
|
|
+ // 如果需要开启CDN,局部开启,全局开启
|
|
|
+ if( request()->is('api/*') && (request('open_cdn',0) || config('open_cdn',0) ) && config('open_cdn_domain','') ){
|
|
|
+ // 先把https 换成 http
|
|
|
+ $url = str_ireplace('https','http',$url);
|
|
|
+ // 替换域名
|
|
|
+ $url = str_ireplace(config('tencent.cos.bucket','').'-'.config('tencent.cos.app_id','').'.cos.'.config('tencent.cos.region','').'.myqcloud.com',config('open_cdn_domain',''),$url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 获取扩展信息
|
|
|
+ $type = strtolower(pathinfo($url, PATHINFO_EXTENSION));
|
|
|
+ // 如果信息不存在
|
|
|
+ if( !$type ) return $url;
|
|
|
+ // 不是图片直接返回路径
|
|
|
+ if( !in_array($type,['jpg','png','jpeg','gif','bmp']) ) return $url;
|
|
|
+ // 需要后缀的OSS参数
|
|
|
+ $param = '';
|
|
|
+ // 阿里云的OSS
|
|
|
+ if( stripos( $url, 'aliyuncs.com') ){
|
|
|
+ // 判断图片的宽高进行缩放处理
|
|
|
+ if( $witdh && $higth ){
|
|
|
+ // 宽高都存在时,固定宽高,将延伸出指定w与h的矩形框外的最小图片进行居中裁剪
|
|
|
+ $param = '?x-oss-process=image/resize,m_fill,limit_0,w_'.$witdh.',h_'.$higth;
|
|
|
+ }else if($witdh){
|
|
|
+ $param = '?x-oss-process=image/resize,w_'.$witdh;
|
|
|
+ }else if($higth){
|
|
|
+ $param = '?x-oss-process=image/resize,h_'.$higth;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 腾讯云的COS
|
|
|
+ if( stripos( $url, 'myqcloud.com') ){
|
|
|
+ if( $witdh && $higth ){
|
|
|
+ // 宽高都存在时,固定宽高,将延伸出指定w与h的矩形框外的最小图片进行居中裁剪
|
|
|
+ $param = '?imageView2/2/w/'.$witdh.'/h/'.$higth;
|
|
|
+ }else if($witdh){
|
|
|
+ $param = '?imageView2/2/w/'.$witdh;
|
|
|
+ }else if($higth){
|
|
|
+ $param = '?imageView2/2/h/'.$higth;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 路径
|
|
|
+ return $param ? $url.$param : $url;
|
|
|
+ }
|
|
|
+ // 类型
|
|
|
+ $type = strtolower(pathinfo($url, PATHINFO_EXTENSION));
|
|
|
+ // 是图片则返回缩略图
|
|
|
+ if( in_array($type,['jpg','png','jpeg','gif','bmp']) ) return rtrim((config('img_cdn_domain')?config('img_cdn_domain'):request()->root()),'/').'/'.ltrim(resize($url,$witdh,$higth),'/');
|
|
|
+ // 判断是否存在文件
|
|
|
+ if( file_exists('uploads/'.$url) ) return request()->root().'/uploads/'.ltrim($url,'/');
|
|
|
+ // 其他类型文件,统一返回
|
|
|
+ return request()->root().'/'.ltrim($url,'/');
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 自动生成新尺寸 的图片
|
|
|
+ * @param string $filename 文件名
|
|
|
+ * @param int $width 新图片宽度
|
|
|
+ * @param int $height 新图片高度(如果没有填写高度,把高度等比例缩小)
|
|
|
+ */
|
|
|
+function resize( $filename, $width=null, $height=null) {
|
|
|
+ // 如果不存在图片路径
|
|
|
+ if( !is_file(DIR_IMAGE.$filename)||empty($filename) ) $filename = config('app.no_pic');
|
|
|
+ // 如果没有宽高,给原图
|
|
|
+ if( !$width && !$height ) return 'uploads/'.$filename;
|
|
|
+ // 调整当前图像的边界到给定的宽和高
|
|
|
+ $img = Image::make(DIR_IMAGE.$filename)->resizeCanvas($width, $height);
|
|
|
+ // 获取文件名称
|
|
|
+ $name = pathinfo($filename,PATHINFO_FILENAME);
|
|
|
+ // 缓存名
|
|
|
+ $cacheName = $width ? $name.'_w' .$width : $name;
|
|
|
+ $cacheName = $height ? $cacheName.'_h' .$height : $cacheName;
|
|
|
+ // 名称拼接宽高
|
|
|
+ $filename = str_replace($name,$cacheName,$filename);
|
|
|
+ // 缓存文件路径
|
|
|
+ $newFile = 'cache/'.$filename;
|
|
|
+ // 如果目录不存在,创建目录
|
|
|
+ if( !is_dir(dirname(DIR_IMAGE.$newFile)) ) mkdir(dirname(DIR_IMAGE.$newFile),0755,true);
|
|
|
+ // 保存新图
|
|
|
+ $img->save(DIR_IMAGE.$newFile);
|
|
|
+ // 返回结果
|
|
|
+ return 'uploads/'.$newFile;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 新检查用户操作权限
|
|
|
+ *
|
|
|
+ * @param int $uid 账号ID
|
|
|
+ * @param string $module 模块
|
|
|
+ *
|
|
|
+ */
|
|
|
+function is_super($uid,$module='manager'){
|
|
|
+ switch ($module){
|
|
|
+ case 'manager':
|
|
|
+ $AdminUserModel = new App\Models\Manager\AdminUser();
|
|
|
+ break;
|
|
|
+ case 'company':
|
|
|
+ $AdminUserModel = new App\Models\Manager\AdminUserCompany();
|
|
|
+ break;
|
|
|
+ case 'shop':
|
|
|
+ $AdminUserModel = new App\Models\Manager\AdminUserShop();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 返回结果
|
|
|
+ $isSuper = $AdminUserModel->query()->where([['uid','=',$uid]])->value('is_super');
|
|
|
+ // 默认无权限
|
|
|
+ return $isSuper ? true : false;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 新检查用户操作权限
|
|
|
+ *
|
|
|
+ * @param int $uid 账号ID
|
|
|
+ * @param string $uid 模块
|
|
|
+ */
|
|
|
+function allow_show_phone($uid,$module='manager'){
|
|
|
+ switch ($module){
|
|
|
+ case 'manager':
|
|
|
+ $AuthGroupAccess = new App\Models\Manager\AuthGroupAccess();
|
|
|
+ break;
|
|
|
+ case 'company':
|
|
|
+ $AuthGroupAccess = new App\Models\Manager\AuthGroupAccessCompany();
|
|
|
+ break;
|
|
|
+ case 'shop':
|
|
|
+ $AuthGroupAccess = new App\Models\Manager\AuthGroupAccessShop();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 返回结果
|
|
|
+ return $AuthGroupAccess->showPhoneByUid($uid);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 把返回的数据集转换成Tree
|
|
|
+ * @param array $list 要转换的数据集
|
|
|
+ * @param string $pid parent标记字段
|
|
|
+ * @param string $level level标记字段
|
|
|
+ * @return array
|
|
|
+ *
|
|
|
+ * @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
|
|
+ *
|
|
|
+ */
|
|
|
+function list_to_tree($list, $pk='id', $pid = 'pid', $child = 'children', $root = 0) {
|
|
|
+ // 创建Tree
|
|
|
+ $tree = array();
|
|
|
+ if(is_array($list)) {
|
|
|
+ // 创建基于主键的数组引用
|
|
|
+ $refer = array();
|
|
|
+ foreach ($list as $key => $data) {
|
|
|
+ $refer[$data[$pk]] =& $list[$key];
|
|
|
+ }
|
|
|
+ foreach ($list as $key => $data) {
|
|
|
+ // 判断是否存在parent
|
|
|
+ $parentId = $data[$pid];
|
|
|
+ if ($root == $parentId) {
|
|
|
+ $tree[] =& $list[$key];
|
|
|
+ }else{
|
|
|
+ if (isset($refer[$parentId])) {
|
|
|
+ $parent =& $refer[$parentId];
|
|
|
+ $parent[$child][] =& $list[$key];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $tree;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 格式化字节
|
|
|
+ * @param int $byte 字节数值
|
|
|
+ *
|
|
|
+ * */
|
|
|
+function byte_format($byte){
|
|
|
+ // 如果是GB
|
|
|
+ if( $byte >= (1024 * 1024 * 1024) ) return round( $byte / (1024 * 1024 * 1024),2 ).' GB';
|
|
|
+ // 如果是MB
|
|
|
+ if( $byte >= (1024 * 1024) ) return round( $byte / (1024 * 1024),2 ).' MB';
|
|
|
+ // 如果是KB
|
|
|
+ if( $byte >= 1024 ) return round( $byte / (1024),2 ).' KB';
|
|
|
+ // 如果是字节
|
|
|
+ return $byte.' B';
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 二维数组排序,根据某个字段
|
|
|
+ * @param array $array 要排序的数组
|
|
|
+ * @param string $key 要排序的键字段
|
|
|
+ * @param string $sort 排序类型 SORT_ASC SORT_DESC
|
|
|
+ * @return array 排序后的数组
|
|
|
+ */
|
|
|
+function array_sort($array, $key, $sort = SORT_DESC) {
|
|
|
+ // 获取字段数据
|
|
|
+ $keys = array_column($array,$key);
|
|
|
+ // 进行排序
|
|
|
+ array_multisort($keys, $sort, $array);
|
|
|
+ // 返回结果
|
|
|
+ return $array;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 二维数组排序,根据某个字段
|
|
|
+ * @param string $phoneNumber 手机号
|
|
|
+ */
|
|
|
+function hide_phone($phoneNumber) {
|
|
|
+ return substr_replace($phoneNumber, '****', 3, 4);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 省份去除自治区等后缀
|
|
|
+ *
|
|
|
+ * @param string $province 省份
|
|
|
+ *
|
|
|
+ */
|
|
|
+function province_rtrim($province) {
|
|
|
+ // 长度2个字的不处理
|
|
|
+ if( mb_strlen($province) <= 2 ) return $province;
|
|
|
+ // 省份信息
|
|
|
+ $province = str_ireplace(['省','回族','维吾尔','壮族','自治区','特别行政区'],'',$province);
|
|
|
+ // 返回结果
|
|
|
+ return $province;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 城市去除自治州等后缀
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param string $city 地级市单位
|
|
|
+ *
|
|
|
+ */
|
|
|
+function city_rtrim($city) {
|
|
|
+ // 长度2个字的不处理
|
|
|
+ if( mb_strlen($city) <= 2 ) return $city;
|
|
|
+ // 名称
|
|
|
+ $result = mb_strrchr($city,'市',true);
|
|
|
+ // 返回结果
|
|
|
+ if( $result ) return $result;
|
|
|
+ // 名称
|
|
|
+ $result = mb_strrchr($city,'自治州',true);
|
|
|
+ // 返回结果
|
|
|
+ if( $result ) return $result;
|
|
|
+ // 名称
|
|
|
+ $result = mb_strrchr($city,'盟',true);
|
|
|
+ // 返回结果
|
|
|
+ if( $result ) return $result;
|
|
|
+ // 名称
|
|
|
+ $result = mb_strrchr($city,'地区',true);
|
|
|
+ // 返回结果
|
|
|
+ if( $result ) return $result;
|
|
|
+ // 返回结果
|
|
|
+ return $city;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 将字符串追加到某个需要切割成数组的字符串数组
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param string $dest 目标字符串
|
|
|
+ * @param string $source 需要追加的字符串
|
|
|
+ * @param string $sep 目标字符串切割方式
|
|
|
+ *
|
|
|
+ */
|
|
|
+function push_str_arr($dest,$source,$sep=',') {
|
|
|
+ // 转数组
|
|
|
+ $result = $dest ? explode($sep,$dest) : [];
|
|
|
+ $result[] = $source;
|
|
|
+ $result = array_values(array_unique($result));
|
|
|
+ // 返回结果
|
|
|
+ return $result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 多行多条数据范围转数组
|
|
|
+ *
|
|
|
+ * @param string $scope 范围字符串
|
|
|
+ *
|
|
|
+ * @return array 范围数组
|
|
|
+ */
|
|
|
+function scope_to_array($scope){
|
|
|
+ // 兼容逗号问题
|
|
|
+ $scope = str_ireplace(',',',',$scope);
|
|
|
+ $scope = str_ireplace("\r\n",',',$scope);
|
|
|
+ $scope = str_ireplace("\n",',',$scope);
|
|
|
+ // 转数组处理
|
|
|
+ $scope = explode(',',$scope);
|
|
|
+ // 去重
|
|
|
+ $scope = array_unique($scope);
|
|
|
+ // 返回结果
|
|
|
+ return array_values($scope);
|
|
|
+}
|
|
|
+
|
|
|
+?>
|