diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index 1fabbf35..15695999 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -15,6 +15,22 @@ use Request; */ class FileController extends AbstractController { + /** + * 获取文件列表 + * + * @apiParam {Number} [pid] 父级ID + */ + public function lists() + { + $user = User::auth(); + // + $pid = intval(Request::input('pid')); + // + $list = File::whereUserid($user->userid)->wherePid($pid)->orderBy('name')->take(500)->get(); + // + return Base::retSuccess('success', $list); + } + /** * 添加项目 * @@ -46,10 +62,13 @@ class FileController extends AbstractController } // if ($pid > 0) { - if (!File::whereUserid($user->id)->whereId($pid)->exists()) { + if (!File::whereUserid($user->userid)->whereId($pid)->exists()) { return Base::retError('参数错误'); } } + if (File::whereUserid($user->userid)->wherePid($pid)->count() >= 300) { + return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); + } // 开始创建 $file = File::createInstance([ 'pid' => $pid, diff --git a/resources/assets/js/pages/manage/file.vue b/resources/assets/js/pages/manage/file.vue index c45060db..51a39f21 100644 --- a/resources/assets/js/pages/manage/file.vue +++ b/resources/assets/js/pages/manage/file.vue @@ -3,7 +3,7 @@
-

{{$L('文件')}}

+

{{$L('文件')}}

@@ -21,13 +21,13 @@
-
+

{{$L('没有任何文件')}}

    -
  • +
  • {{item.name}}
  • -
  • +
  • import {mapState} from "vuex"; +import {sortBy} from "lodash"; export default { data() { return { + loadIng: 0, + pid: 0, + types: [ {value: 'folder', name: "目录"}, {value: 'document', name: "文本"}, @@ -74,7 +78,6 @@ export default { {value: 'flow', name: "流程图"}, ], files: [], - temps: [], } }, @@ -90,27 +93,63 @@ export default { ...mapState(['userInfo']), folderList() { - const data = this.files.filter(({type}) => type == 'folder') - return Object.assign(data, this.temps.filter(({type}) => type == 'folder')) + return sortBy(this.files.filter(({type, pid}) => pid == this.pid && type == 'folder'), 'name') }, fileList() { - const data = this.files.filter(({type}) => type != 'folder') - return Object.assign(data, this.temps.filter(({type}) => type != 'folder')) + return sortBy(this.files.filter(({type, pid}) => pid == this.pid && type != 'folder'), 'name') } }, watch: { - + pid: { + handler() { + this.getLists(); + }, + immediate: true + } }, methods: { + getLists() { + this.loadIng++; + this.$store.dispatch("call", { + url: 'file/lists', + data: { + pid: this.pid, + }, + }).then(({data}) => { + this.loadIng--; + this.saveFile(data); + }).catch(({msg}) => { + $A.modalError(msg); + this.loadIng--; + }) + }, + + saveFile(file) { + if ($A.isArray(file)) { + file.forEach((item) => { + this.saveFile(item); + }); + return; + } + let index = this.files.findIndex(({id}) => id == file.id); + if (index > -1) { + this.files.splice(index, 1, file); + } else { + this.files.push(file) + } + }, + addFile(command) { let id = $A.randomString(8); - this.temps.push({ + this.files.push({ _edit: true, + pid: this.pid, id: id, type: command, + name: '', newname: this.$L('未命名') }); this.$nextTick(() => { @@ -120,21 +159,30 @@ export default { }) }, + openFile(item) { + if (item.type == 'folder') { + this.pid = item.id; + } + }, + onBlur(item) { - this.onEnter(item); + if (!item.newname) { + this.files = this.files.filter(({id}) => id != item.id); + } }, onEnter(item) { - if (item._load) { - return; - } if (!item.newname) { - this.temps = this.temps.filter(({id}) => id != item.id); + this.files = this.files.filter(({id}) => id != item.id); } else { + if (item._load) { + return; + } this.$set(item, '_load', true); this.$store.dispatch("call", { url: 'file/add', data: { + pid: item.pid, name: item.newname, type: item.type, }, @@ -142,18 +190,12 @@ export default { $A.messageSuccess(msg) this.$set(item, '_load', false); this.$set(item, '_edit', false); - if (this.temps.find(({id}) => id == item.id)) { - this.temps = this.temps.filter(({id}) => id != item.id); - this.files.push(data); - } else { - Object.keys(data).forEach((key) => { - this.$set(item, key, data[key]); - }) - } + this.files = this.files.filter(({id}) => id != item.id); + this.saveFile(data); }).catch(({msg}) => { $A.modalError(msg) this.$set(item, '_edit', false); - this.temps = this.temps.filter(({id}) => id != item.id); + this.files = this.files.filter(({id}) => id != item.id); }) } }