* @date 2020/3/19 21:09 * * @return \think\response\Json * @throws FailedException */ public function index() { // 当前路径 $path = input('dir', '/'); $page = input('page', 1); $limit = input('limit', 10); // 实列 $filesystem = File::init('uploads' . $path); // 数据 $annex = []; // 取出文件夹 $dirs = []; foreach ($filesystem->getDirs() as $dir) { $dirs[] = [ 'name' => $dir, 'type' => 'dir', 'isDir' => true, 'create_at' => time() + 99 // 'create_at' => filectime(app()->getRootPath() . '/public/uploads/' . rawurlencode($dir)) ]; } // halt($dirs); // 取出本地存储文件 foreach ($filesystem->getFiles() as $file) { $type = get_file_ext($file); $info = [ 'name' => $file, 'type' => $type, 'isDir' => false, 'url' => get_annex_url($path . '/' . $file), 'create_at' => filectime(app()->getRootPath() . "/public/uploads/" . $path . '/' . $file) ]; if (in_array($type, ['png', 'jpg', 'jpeg', 'gif'])) { $info['thumbs'] = get_annex_url($path . '/' . $file); } $annex[] = $info; } $total = count($annex); $array = []; // 分页处理 for ($j = $limit * ($page -1); $j < ($limit * $page ) && $j < $total;++$j){//循环条件控制显示图片张数 $array[] = $annex[$j]; } // 获取数据库存储 $model = model('common/SystemAnnex')->where(['path' => input('dir', '')]) ->limit((($page - 1) * $limit) . "," . $limit); // 取出文件 foreach ($model->order(['created_at' => 'desc'])->select() as $file) { $type = get_file_ext($file['url']); $info = [ 'name' => $file['url'], 'type' => $type, 'isDir' => false, 'url' => get_annex_url($file['path'] . '/' . $file['url']), 'create_at' => $file['created_at'] ]; if (in_array($type, ['png', 'jpg', 'jpeg', 'gif'])) { $info['thumbs'] = get_annex_url($file['path'] . '/' . $file['url'],$file['driver']); } $array[] = $info; } $array = array_values(array_unique($array, SORT_REGULAR )); array_multisort(array_column($array,'create_at'),SORT_DESC,$array); $array = array_merge($dirs, $array); return IResponse::success([ 'count' => $total, 'list' => $array ]); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/19 21:09 * * @return \think\response\Json * @throws FailedException */ public function operation() { $params = $this->request->param(); // 是否文件夹操作 $folder = (isset($params['folder']) && $params['folder'] == 'true'); // 内置验证 $valid = $this->validate($params,[ //'name|名称' => 'require|regex:([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{1,14}', 'dir|文件夹' => 'require' ],[ 'name.regex' => '名称需英文名称以及英文后缀!' ]); // 验证失报错 (true !== $valid) && IResponse::failure($valid); // 文件操作实例 $filesystem = File::init('uploads' . input('dir', '/')); switch ($this->request->method()) { case "GET": $contents = $filesystem->getFile('/' . $params['name']); return IResponse::success(['name' => $params['name'],'content' => $contents]); break; case "POST": $filesystem->{$folder ? 'mkdir' : 'touch'}('/' . $params['name']); break; case "PUT": // 更新文件内容或者重命名文件夹 if (isset($params['content'])){ // 更新文件内容 $filesystem->changeFile('/' . $params['name'], $params['content']); }else{ // 重命名 $filesystem->renameFile('/' . $params['name'],'/' .$params['change']); } return IResponse::success(); break; case "DELETE": //todo 删除文件夹 删除操作应该是转移目录 if ($folder) { // 初始化到删除目录 File::init('uploads' . input('dir', '') . '/' . $params['name'] )->moveAll(); } else { // 删除数据库内容 $file = model('common/SystemAnnex')->getBy(['path' => input('dir', '')]); if ($file && $file->driver == 'qiniu') { // 七牛云的删除 } else { // 本地删除 $file && $file->delete(); $filesystem->removeFile('/' . $params['name']); } } break; default: return IResponse::failure('错误的操作!'); } return IResponse::success([], '操作成功!'); } /** * 普通附件上传 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/19 21:09 * * @param $name * * @return \think\response\Json */ public function upload() { $upload = new Upload(config('upload.')); $upload->setPath(input('dir','/'))->upload($this->request->file('file')); return IResponse::success([],'上传成功'); } /** * ck编辑器附件上传 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/8 21:05 * * @return \think\response\Json */ public function ckeditor() { $file = $this->request->file('upload'); $info = $file->move( 'uploads/ckeditor' ,''); return json([ 'fileName' => $info->getSaveName(), 'uploaded' => 1, 'url' => 'http://'.$_SERVER['SERVER_NAME'] . '/uploads/ckeditor/'. $info->getSaveName() ]); // return IResponse::success([ // 'name' => $info->getSaveName() // ]); } }