_Account = $this->getKey($this->_apiCode); //验证是否具有访问这个接口的权限 if(!isset($this->_Account['UploadImage'])){ HelperService::returnJson(['code'=>400,'msg'=>'UploadImage interface unauthorized access','data'=>[]]); } $this->_addHeader(); } /** * 添加跨域安全域名 */ private function _addHeader(){ if(strpos($this->_Account['AllowOrigin'], 'https://') !== false){ header("Access-Control-Allow-Credentials: true"); } if(isset($this->_Account['AllowOrigin'])){ header('Access-Control-Allow-Origin:*'); header("Access-Control-Allow-Methods", "*"); header("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE"); } } /** * 上传 */ public function fileUpload(){ $fileStream = $this->request->file('file'); if($fileStream){ $info = $fileStream->move(ROOT_PATH . 'public' . DS . 'Oss'.DS.$this->_apiCode); if($info){ $url = HelperService::getHttpHeader().$_SERVER['SERVER_NAME'].DS . 'Oss'.DS.$this->_apiCode.DS.$info->getSaveName(); HelperService::returnJson(['code'=>200,'msg'=>"success",'data'=>['url'=>$url]]); }else{ // 上传失败获取错误信息 HelperService::returnJson(['code'=>400,'msg'=>$fileStream->getError(),'data'=>[]]); } } HelperService::returnJson(['code'=>400,'msg'=>'没有上传图片','data'=>[]]); } }