1, 'audio' => 2, 'video' => 3, 'text' => 4, 'file' => 5, 'pdf' => 6 ); public function _initialize() { parent::_initialize(); } private function setConfig($bucket){ switch($bucket){ case 'viva-private': Config::set('OSS_ACCESS_KEY_ID','LTAI4G9Zz1yWLASYkBmXRFF6'); Config::set('ACCESS_KEY_SECRET','pnMsp9wlTEo4lP0JQgVukMGURlcC7y'); Config::set('OSS_ENDPOINT','oss-cn-hangzhou.aliyuncs.com'); break; default: throw new \RuntimeException('无此bucket'); } } public function newDiffParam($rule, $params) { $validate = new Validate($rule); if (!$validate->check($params)) { HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]); } } /** * 图片上传 */ public function upload() { header('Access-Control-Allow-Origin:*'); header("Access-Control-Allow-Methods", "*"); header("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE"); $params = $this->request->param(); $rule = [ 'fileName' => 'require', 'upType' => 'require', 'isRoot' => 'require', 'fileType' => 'require|number', 'bucket' => 'require', 'isPrivate' => 'boolean' ]; if (isset($params['upType']) && $params['upType'] === 'base64') { $rule['content'] = 'require'; } $this->newDiffParam($rule, $params); $fileName = (string)$params['fileName']; $upType = (string)$params['upType']; $isRoot = (string)$params['isRoot']; $fileType = (string)$params['fileType']; $isPrivate = isset($params['isPrivate']) ?$params['isPrivate']: true; $bucket = $params['bucket']; $this->setConfig($bucket); Config::set('ALIYUN_OSS_BUCKET', $bucket); $ossService = new OssServiceV2($bucket); $ext = !empty($fileName) ? (string)pathinfo($fileName, PATHINFO_EXTENSION) : ''; list($fileName, $realContent) = $ossService->getFileNameAndRealContent($upType, $params, $fileName, $ext); //向阿里推送图片 $type = (array)array_flip(self::$fileType); if(empty($type[$fileType])){ HelperService::returnJson(['code'=>400,'msg'=>'文件类型不正确']); } $fileSavePath = $isRoot ? ltrim($fileName, '/') : $type[$fileType] . '/' . date('Ym') . '/' . $fileName; //重试3次 $times = 3; $message = ''; while ($times--) { try { if (true === $ossService->fileExist($fileSavePath)) { $path = (string)pathinfo($fileSavePath, PATHINFO_DIRNAME); $path = ($path === '.' || $path === '..') ? '' : $path; $fileName = pathinfo($fileSavePath, PATHINFO_BASENAME); $ext = pathinfo($fileSavePath, PATHINFO_EXTENSION); $fileName = substr($fileName, 0, strpos($fileName, '.' . $ext)); $newFileName = $fileName . '_' . date('YmdHis') . mt_rand(10000, 99999) . '.' . $ext; $fileSavePath = ltrim($path . '/' . $newFileName, '/'); } $result = $ossService->fileUpload($fileSavePath, $realContent); if ($isPrivate === true) { $ossService->putObjectAcl($result, 'private'); } if ($result) { $aLiYunUrl = 'http://' . $bucket . '.' . Config::get('OSS_ENDPOINT') . '/' . trim($result, '/'); HelperService::returnJson(['data'=>[ 'url' => $result, 'bucket' => $bucket, 'aliyunUrl' => $aLiYunUrl, 'signUrl' => $ossService->getSignUrl([ 'object' => $result, 'bucket' => $bucket ]) ],'msg'=>'success','code'=>200]); } else { HelperService::returnJson(['data'=>['times' => $times, 'bucket' => $bucket], 'msg'=>'fail','code'=>400]); } } catch (Exception $ex) { $message = $ex->getMessage(); continue; } } HelperService::returnJson(['data'=>['times' => $times, 'bucket' => $bucket], 'msg'=>'上传失败: ' . $message,'code'=>400]); } protected function input() { $contents = file_get_contents('php://input'); $param = json_decode($contents, true); if (empty($param)) return $this->output([], 'json error', '0004'); return $param; } public function output($data=[],$msg='success',$code=200){ HelperService::returnJson([ 'data'=>$data, 'msg'=>$msg, 'code'=>$code ]); } /** * 下载文件 */ public function downFile() { $params = $this->input(); $this->newDiffParam([ 'filePath' => 'require', 'bucket' => 'require' ], $params); $this->setConfig($params['bucket']); Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket'])); $res = (new OssServiceV2((string)($params['bucket'])))->downFile((string)($params['filePath'])); die($res); } /** * 文件删除 */ public function fileDelete() { $params = $this->input(); $this->newDiffParam([ 'filePath' => 'require', 'bucket' => 'require' ], $params); $filePath = (string)($params['filePath']); $this->setConfig($params['bucket']); Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket'])); $ossService = new OssServiceV2((string)($params['bucket'])); //重试3次 $times = 3; $message = ''; while ($times--) { try { $exist = $ossService->fileExist($filePath); if (true !== $exist) { $this->output([], 'filePath is not exist'); } $result = $ossService->fileDelete($filePath); if ($result) { $this->output([], 'del success'); } else { $this->output(['times' => $times], 'del fail'); } } catch (Exception $ex) { $message = $ex->getMessage(); continue; } } $this->output(['times' => $times], 'del fail: ' . $message); } /** * 图片信息 */ public function imageInfo() { $params = $this->input(); $this->newDiffParam([ 'imagePath' => 'require', 'ossDomain' => 'require', 'bucket' => 'require', ], $params); $imagePath = (string)($params['imagePath']); Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket'])); $this->setConfig($params['bucket']); $ossService = new OssServiceV2((string)($params['bucket'])); //重试3次 $times = 3; $message = ''; while ($times--) { try { $exist = (bool) $ossService->fileExist($imagePath); if (true !== $exist) { $this->output([], 'image is not exist'); } $result = $ossService->imageInfo($imagePath, (string)($params['ossDomain'])); if ($result) { $this->output($result, 'success'); } else { $this->output(['times' => $times], 'fail'); } } catch (Exception $ex) { $message = $ex->getMessage(); continue; } } $this->output(['times' => $times], 'fail: ' . $message); } /** * 文件是否存在 */ public function fileExist() { $params = $this->input(); $this->newDiffParam([ 'filePath' => 'require', 'bucket' => 'require' ], $params); $filePath = (string)($params['filePath']); Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket'])); $this->setConfig($params['bucket']); $ossService = new OssServiceV2((string)($params['bucket'])); //重试3次 $times = 3; $message = ''; while ($times--) { try { $result = $ossService->fileExist($filePath); if ($result) { $this->output([], 'success'); } else { $this->output(['times' => $times], 'fail'); } } catch (Exception $ex) { $message = $ex->getMessage(); continue; } } $this->output(['times' => $times], 'fail: ' . $message); } /** * bucket列表 */ public function bucketList() { $ossService = new OssServiceV2(); //重试3次 $times = 3; $message = ''; while ($times--) { try { $bucketList = $ossService->bucketList(); $this->output($bucketList, 'success'); } catch (Exception $ex) { $message = $ex->getMessage(); continue; } } $this->output(['times' => $times], 'fail: ' . $message); } /** * 获取带有签名的路径 */ public function getSignUrl() { $params = $this->input(); $this->newDiffParam([ 'object' => 'require', 'bucket' => 'require', 'isIntranet' => 'boolean' //是否是内网 ], $params); $this->setConfig($params['bucket']); $url = (new OssServiceV2($params['bucket']))->getSignUrl($params); $this->output(['url' => $url]); } /** * 批量获取带有签名的路径 */ public function getMultiSignUrl() { $params = $this->input(); $this->newDiffParam(['data' => 'require|array'], $params); $data = (new OssServiceV2())->getMultiSignUrl($params['data']); $this->output(['urls' => $data]); } /** * 获取阿里云处理后的图片 */ public function getProcessImageUrl() { $params = $this->input(); $this->newDiffParam([ 'process' => 'require', 'object' => 'require', 'bucket' => 'require', 'endpoint' => 'require', ], $params); $this->setConfig($params['bucket']); $objectInfo = parse_url($params['object']); if (empty($objectInfo['path'])) { $this->output([], '传入的地址信息有误'); } $params['path'] = $objectInfo['path']; $data = (new OssServiceV2($params['bucket']))->getProcessImageUrl($params); $this->output(['url' => $data]); } }