getRand(); $where = [ 'store_id'=>$storeId, 'extraction_code'=>$data, ]; $field = [ 'extraction_code' ]; $dataOne = $plugBlessingModel->getFind($where,$field); }while(count($dataOne) >= 1); return $data; } //随机提取码 public function getRand($length = 8){ // 密码字符集,可任意添加你需要的字符:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $chars = '0123456789'; $str = ""; for ( $i = 0; $i < $length; $i++ ) { $str .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } return $str ; } /** * 判断门店插件是否有权限 * @param $param * [ * store_id * identification * ] * @return mixed */ public function storeBlessingPlug($param){ $plugClass = new Plug(); $plug_info = $plugClass->plugStore([ 'store_id' => $param['store_id'], 'identification_arr' => [ $param['identification']//插件唯一标识符 ] ]); if ($plug_info === false) { return callBack(statusCode()['error'], "暂无插件权限"); }else{ return callBack(statusCode()['success'], "成功"); } } /** * 判断门店是否开启插件权限 * @param $param * [ * store_id * identification * ] * @return mixed */ public function storePlugStatus($param){ $plugClass = new Plug(); $plug_info = $plugClass->plugStoreStatus([ 'store_id' => $param['store_id'], 'identification_arr' => [ $param['identification']//插件唯一标识符 ] ]); if ($plug_info === false) { return callBack(statusCode()['error'], "暂无插件权限"); }else{ return callBack(statusCode()['success'], "成功"); } } /** * 判断商户插件是否有权限 * @param $param * [ * merchant_id * identification * ] * @return mixed */ public function merchantIdBlessingPlug($param){ $plugClass = new Plug(); $plug_info = $plugClass->PlugPermissions([ 'merchant_id' => $param['merchant_id'], 'identification_arr' => [ $param['identification']//插件唯一标识符 ] ]); if (empty($plug_info['data']['plug']['blessing'])) { return callBack(statusCode()['error'], "暂无插件权限"); }else{ return callBack(statusCode()['success'], "成功"); } } /** * 音视频上传:MP3、MP4 * @param $param * [ * storage_type 1本地,2七牛云,3腾讯云,4阿里云 * limit_size 大小:B * catalog 子目录地址 * ] * @return mixed */ public function videoUpload($param){ //判断存储模式:storage_type:1本地,2七牛云,3腾讯云,4阿里云 if (empty($param['storage_type'])) { $admin = M('retail_admin')->order('id asc')->find(); } else { $admin['storage_type'] = $param['storage_type']; } //文件子目录$param['catalog'] switch ($admin['storage_type']) { case 1: $param['storage_type'] = 1; if (isset($param['limit_size']) && $param['limit_size'] !== '' && $param['limit_size'] > 0) { $uploads = $this->localVideoUpload($param,$param['limit_size']); } else { $uploads = $this->localVideoUpload($param); } break; case 2: case 3: case 4: $uploads = $this->externalVideoUpload($param, $admin['storage_type']); break; default: $uploads = callBack(statusCode()['error'],'参数有误'); break; } return $uploads; } //本地上传(默认 10M) //视频:Mp4,mp4,avi,rm ,rmvb ,flv ,mpg ,mov,mkv .音频:mp3,MP3,m4a,wma, public function localVideoUpload($param,$limit_size = '10'){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = $limit_size*1024*1024 ;// 设置附件上传大小,单位:B $upload->exts = array('MP4','MP3','mp4','mp3','m4a','wma','avi','rm','rmvb','flv','mpg','mov','mkv');// 设置附件上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = $param['catalog'].'/'; // 设置附件上传(子)目录'blessing/' // 上传文件 $info = $upload->upload(); if(!$info) {// 上传错误提示错误信息 // $this->error($upload->getError()); $res = callBack(statusCode()['error'],$upload->getError()); return $res; }else{// 上传成功 // $this->success('上传成功!'); // $name = 'Uploads/'.$info['file']['savepath'].$info['file']['savename'];//文件路径 $info['file']['storage_type'] = '1'; $entrance_style = getServerUrl(); $info['file']['path_name'] = 'Uploads/'.$info['file']['savepath'].$info['file']['savename']; $info['file']['access_path'] = $entrance_style.'/Uploads/'.$info['file']['savepath'].$info['file']['savename']; return $info; } /*文件详情 "name": "mmexport1616228590123.mp4", "type": "video/mp4", "size": 702380, "key": "file", "ext": "mp4", "md5": "08c781edd4b471f1a5cf5c475a2e1187", "sha1": "28280d02d78c03f2b253478808084a89dba0c560", "savename": "605edeeb997075724.mp4", "savepath": "blessing/mp4/2021-03-27/" */ } //外部上传 public function externalVideoUpload($param, $storage_type){ $config = M('upload_config')->where(array('id' => 1))->find(); if (empty($config)) { $arr = callBack(statusCode()['error'],'未配置外部存储参数'); return $arr; } if (!empty($config)) { $arr = array(); foreach ($_FILES as $key => $value) { if ($value['error'] == 0) { if (isset($param['limit_size']) && $param['limit_size'] !== '' && $param['limit_size'] > 0) { // 大小需要限制 $param['limit_size'] = $param['limit_size']*1024*1024;//单位:B if (filesize($_FILES[$key]['tmp_name']) > $param['limit_size']) { $res = callBack(statusCode()['error'],'上传文件大小不符'); return $res; } }else{ // 大小需要限制(默认10M) $param['limit_size'] = 10*1024*1024;//单位:B if (filesize($_FILES[$key]['tmp_name']) > $param['limit_size']) { $res = callBack(statusCode()['error'],'上传文件大小不符'); return $res; } } $ext = pathinfo($_FILES[$key]['name']); //视频:Mp4,mp4,avi,rm ,rmvb ,flv ,mpg ,mov,mkv .音频:mp3,MP3,m4a,wma, if (in_array($ext['extension'], array('MP4','MP3','mp4','mp3','m4a','wma','avi','rm','rmvb','flv','mpg','mov','mkv'))) { switch ($storage_type) { case '2': //七牛云 $res = toUploadQiNiuYun($_FILES[$key]['tmp_name'], $ext['extension'], $config, $param['catalog']); break; case '3': //腾讯云 $res = uploadFilesToTencent($_FILES[$key]['tmp_name'], $ext['extension'], $config, $param['catalog']); $res['name'] = $param['catalog'].'/'.$res['name']; break; case '4': //阿里云 $res = uploadFilesToOSS($_FILES[$key]['tmp_name'], $ext['extension'], $config, $param['catalog']); $res['name'] = $param['catalog'].'/'.$res['name']; break; } if ($res) { $type = explode('/', $value['type']); $arr[$key]['savePath'] = $res['path']; $arr[$key]['path'] = $res['path']; $arr[$key]['size'] = $value['size']; $arr[$key]['ext'] = $ext['extension']; $arr[$key]['type'] = $value['type']; $arr[$key]['name'] = $value['name']; $arr[$key]['key'] = $type[0]; $arr[$key]['saveName'] = $res['name']; $arr[$key]['storage_type'] = $storage_type; $arr[$key]['original_name'] = $value['name']; $arr[$key]['access_path'] = $res['path']; //当前能访问的地址 } }else{ $res = callBack(statusCode()['error'],'上传文件格式错误'); return $res; } } } return $arr; } else { return false; } } /** * 音视频删除:MP3、MP4 * @param $storage_type 1本地,2七牛云,3腾讯云,4阿里云 * @param $url 目录地址 * @return mixed */ public function videoDelete($url,$storage_type){ switch ($storage_type) { case 1: $data = $this->localVideoDelete($url); break; case 2: case 3: case 4: $data = $this->externalVideoDelete($url,$storage_type); break; default: $data = callBack(statusCode()['error'],'参数有误'); break; } return $data; } //本地删除(返回true) public function localVideoDelete($url){ //本地删除 $voiceUrl = $_SERVER['DOCUMENT_ROOT'].'/'.$url;//路径 if(file_exists($voiceUrl)){ //文件存在,删除 unlink($voiceUrl);//删除文件,服务器删除 } $res = callBack(statusCode()['success'],'成功'); return $res; } //外部删除 public function externalVideoDelete($url,$storage_type){ $config = M('upload_config')->where(array('id' => 1))->find(); switch ($storage_type) { case '2': //七牛云 $del = $this->delQiNiuFile($url, $config); if (!$del) { $res['status'] = 0; $res['msg'] = '文件已被删除'; } else { $res['status'] = 1; $res['msg'] = '删除成功'; } break; case '3': //腾讯云 $del = $this->delTencentFile($url, $config); if ($del) { $res['status'] = 1; $res['msg'] = '删除成功'; } else { $res['status'] = 0; $res['msg'] = '删除出错'; } break; case '4': //阿里云 $del = $this->delOSSFile($url, $config); if ($del) { $res['status'] = 1; $res['msg'] = '删除成功'; } else { $res['status'] = 0; $res['msg'] = '文件已被删除或检查配置'; } break; } return $res; } /** * 删除七牛云图片 * @param $url * @param $config * @return bool */ public function delQiNiuFile($url, $config) { $auth = new Auth($config['qiniu_access_key'], $config['qiniu_secret_key']); $Qiniuconfig = new Config(); $bucketManager = new BucketManager($auth, $Qiniuconfig); //每次最多不能超过1000个 $keys[] = $url; $ops = $bucketManager->buildBatchDelete($config['qiniu_storage_space'], $keys); list($ret, $err) = $bucketManager->batch($ops); if ($err) { return false; } else { return true; } } /** * 删除腾讯云图片 * @param $url * @param $config * @return bool */ public function delTencentFile($url, $config) { $cosClient = new Client( array( 'region' => $config['tencent_region'], 'schema' => 'https', //协议头部,默认为http 'credentials' => array( 'secretId' => $config['tencent_secret_id'], 'secretKey' => $config['tencent_secret_key'] ) ) ); $delData[]['Key'] = $url; try { $result = $cosClient->deleteObjects(array( 'Bucket' => $config['tencent_storage_space'], 'Objects' => $delData )); return true; } catch (\Exception $e) { return false; } } /** * 删除阿里云OSS图片 * @param $url * @param $config * @return bool */ public function delOSSFile($url, $config) { $objects[] = $url; try { $ossClient = new OssClient($config['oss_access_key_id'], $config['oss_access_key_secret'], $config['oss_region']); $result = $ossClient->deleteObjects($config['oss_storage_space'], $objects); return true; } catch (OssException $e) { return false; } } /** * 根据订单id获取祝福码 * @param $orderId * @return bool */ public function obtainExtractionCode($orderId){ if($orderId == ''){ return null; } $blessingModel = new BlessingModel(); $where = [ 'order_id'=>$orderId, ]; $field = [ 'extraction_code', 'validity_time' ]; $data = $blessingModel->getFind($where,$field); if($data){ if(empty($data['validity_time']) || $data['validity_time'] == 1 || $data['validity_time'] > time()) return $data['extraction_code']; }else{ return null; } } /** * 获取插件设置 * @param $identification * @return bool */ public function plugSetInfo($identification){ $data['identification'] = $identification;//标识 $PlugSetClass = new PlugSet(); $result = $PlugSetClass->setInfo($data); if($result){ if($result['status'] == 1){ return $result['data']; }else{ return null; } } return null; } /** * 修改祝福语记录有效期 * @param $id * @param $type 1:首次扫码(id = 门店id),2:确认订单(id = 订单id) * @param $extractionCode = '' 祝福码为空,订单没有祝福语 * @return bool */ public function validityTimeSave($id,$type,$extractionCode = ''){ if($id == '' || $type == ''){ return false; } $plugBlessingModel = new BlessingModel(); if($type == 2 && $extractionCode == ''){ //确认订单 $where = [ 'order_id'=>$id,//订单id ]; $field = [ 'extraction_code',//提取码 ]; $getFind = $plugBlessingModel->getFind($where,$field); if($getFind){ //存在,返回祝福码 $extractionCode = $getFind['extraction_code']; }else{ //不存在,该订单不存在祝福语 return false; } } $identification = 'blessing';//祝福语标识 $set = $this->plugSetInfo($identification); if($set['duration_type'] == 1){ //永久有效 $save = [ 'validity_time'=>1,//1永久有效,有效期:时间戳 ]; if($type == 1){ //首次扫码 $where = [ 'store_id'=>$id,//门店id 'extraction_code'=>$extractionCode,//祝福码 ]; $plugBlessingModel->updateOrder($where,$save); }else if($type == 2){ //确认订单 $where = [ 'order_id'=>$id,//订单id 'extraction_code'=>$extractionCode,//祝福码 ]; $plugBlessingModel->updateOrder($where,$save); } return $save['validity_time']; }else if($set['duration_type'] == 2){ //固定时长 $time = time()+($set['duration_time'] * 86400);//固定时长的时间 $save = [ 'validity_time'=>$time,//1永久有效,有效期:时间戳 ]; if($set['duration_start_type'] == 2 && $type == 1){ //开始计时的时候 1确认收货后 2首次扫码后:首次扫码 $where = [ 'store_id'=>$id,//门店id 'extraction_code'=>$extractionCode,//祝福码 ]; $plugBlessingModel->updateOrder($where,$save); }else if($set['duration_start_type'] == 1 && $type == 2){ //开始计时的时候 1确认收货后 2首次扫码后:确认订单 $where = [ 'order_id'=>$id,//订单id 'extraction_code'=>$extractionCode,//祝福码 ]; $plugBlessingModel->updateOrder($where,$save); } return $save['validity_time']; }else{ return false; } } }