"", 'md5' => "", 'chunks' => "", 'chunk' => 0, 'is_test' => 1, 'file_name' => '', ]; $newData = $this->request->post($field); $file = $this->request->file('file'); if (empty($file)) { return $this->msg(504,'文件不能为空'); } if (!$newData['hash']) { return $this->msg(504,'hash值不能为空'); } //分片数据为第一片时 校验文件是否已经上传 如果上传直接返回地址 if ($newData['chunk'] == 0) { $file_data = Attachment::where('hash', $newData['hash']) ->find(); if (!empty($file_data)) { $file_data['id'] = (int)$file_data['id']; if ($file_data['id'] > 0 ) { return $this->msg(200, $file_data); } } } $uploader = new LocalUploader(); $result = $uploader->uploadFile($newData, $file); //异常 if ($result['code'] == -1) { return $this->msg(504,$uploader->getError()); } if ($result['code'] == 1) { // $data = [ // 'path' => $result['data']['save_path'], // 'url' => $result['data']['file_path'], // ]; //上传到七牛 $res = $this->qn->uploadLocalFile(public_path().ltrim($result['data']['save_path'],'/'),$result['data']['file_path']); $ret = [ 'code' => 1, 'data' => [ 'cdn' => Env::get('QINIU.cdn'), 'origin_name' => $file->getOriginalName(), 'filename' => explode('/',$res['filename'])[count(explode('/',$res['filename']))-1], 'md5' => $newData['hash'], 'url' => $res['remote_url'], 'relative_path' => $res['filename'] ] ]; //构建数据 $attach = [ 'platform_id' => $this->platformId, 'site_id' => '', 'name' => $file->getOriginalName(), 'type' => $file->getMime(), 'local' => $result['data']['save_path'], 'path' => $res['filename'], 'md5' => $newData['hash'], 'hash' => $newData['hash'] ]; Attachment::create($attach); return $this->msg(200, '上传成功', $ret); } else { $data = [ 'code' => 0, 'data' => $result['data'], ]; return $this->msg(200, '文件包不完整', $data); } } catch (\Exception $e) { return $this->msg(504,$e->getMessage()); } } /** * 分片上传文件检测 */ public function checkFile() { header("Access-Control-Allow-Origin:*"); header("Access-Control-Allow-Methods:GET, POST, PATCH, PUT, DELETE,OPTIONS"); header("Access-Control-Allow-Credentials:true"); header("Access-Control-Allow-Headers:Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With,x_requested_with,x-token"); $hash = $this->request->param('hash'); $chunks = $this->request->param('chunks'); if (!$hash) { return $this->msg(504,'hash值异常'); } if ($chunks <= 0) { return $this->msg(504,'分片总数错误'); } // $file_data = ImagesFileModel::where('file_hash', '=', $hash) // ->with('uploader') // ->find(); //查询该上传的文件是否存在 $file_data = Attachment::where('hash',$hash)->findOrEmpty(); if (!empty($file_data)) { $file_data['id'] = (int)$file_data['id']; if ($file_data['id'] > 0) { $arr = explode('/',$file_data['path']); $data = [ 'cdn' => Env::get('QINIU.cdn'), 'origin_name' => $file_data['name'], 'filename' => array_pop($arr), 'md5' => $file_data['md5'], 'url' => Env::get('QINIU.cdn').$file_data['path'], 'relative_path' => $file_data['path'] ]; return $this->msg(200,'成功', ['code' => 1, 'data' => $data]); } } //如果为需要上传的文件 则检测文件的分片情况 $uploader = new LocalUploader(); $result = $uploader->checkFile($hash, $chunks); return $this->msg(200,'成功', ['code' => 0, 'data' => $result]); } /** * 上传图片 */ public function uploadImg() { try { $uploader = new QiniuUploader(); $file = $this->request->file('file'); $result = $uploader->uploadFile('file'); $data = [ 'up_id' => isset($this->user['id']) ? $this->user['id'] : 0, 'path' => $result['filename'], 'url' => $result['remote_url'], 'file_hash' => $result['hash'], 'file_md5' => $file->md5(), 'created_at' => date("Y-m-d H:i:s"), 'file_name' => $file->getOriginalName(), 'file_type' => $file->getOriginalMime(), 'storage_type' => 'qiniu', ]; $file_id = ImagesFileModel::insert($data, true); $data['id'] = (int)$file_id; return $this->msg(200,'成功', $data); } catch (\Exception $e) { return $this->msg(504,$e->getMessage()); } } /** * 删除文件 * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function deleteFile() { $file_id = $this->request->get('file_id/d'); if ($file_id <= 0) { return $this->msg(504,'请求参数异常'); } $file_info = ImagesFileModel::where('id', '=', $file_id)->find(); switch ($file_info['storage_type']) { case 'qiniu': $uploader = new QiniuUploader(); $uploader->deleteFile($file_info['path']); break; case 'local': $uploader = new LocalUploader(); $uploader->deleteFile($file_info['path']); break; } ImagesFileModel::where('id', '=', $file_id)->delete(); return $this->msg(504,'操作成功'); } }