文件共享

This commit is contained in:
kuaifan 2021-07-07 11:18:07 +08:00
parent e5ccdee472
commit 6991e01201
4 changed files with 88 additions and 60 deletions

View File

@ -113,10 +113,8 @@ class FileController extends AbstractController
// //
if ($id > 0) { if ($id > 0) {
// 修改 // 修改
$file = File::whereUserid($user->userid)->whereId($id)->first(); $file = File::allowFind($id);
if (empty($file)) { //
return Base::retError('文件不存在或已被删除');
}
$file->name = $name; $file->name = $name;
$file->save(); $file->save();
return Base::retSuccess('修改成功', $file); return Base::retSuccess('修改成功', $file);
@ -132,20 +130,25 @@ class FileController extends AbstractController
return Base::retError('类型错误'); return Base::retError('类型错误');
} }
// //
$userid = $user->userid;
if ($pid > 0) { if ($pid > 0) {
if (!File::whereUserid($user->userid)->whereId($pid)->exists()) { if (File::wherePid($pid)->count() >= 300) {
return Base::retError('参数错误'); return Base::retError('每个文件夹里最多只能创建300个文件或文件夹');
}
$row = File::allowFind($pid, '主文件不存在');
$userid = $row->userid;
} else {
if (File::whereUserid($user->userid)->wherePid(0)->count() >= 300) {
return Base::retError('每个文件夹里最多只能创建300个文件或文件夹');
} }
}
if (File::whereUserid($user->userid)->wherePid($pid)->count() >= 300) {
return Base::retError('每个文件夹里最多只能创建300个文件或文件夹');
} }
// 开始创建 // 开始创建
$file = File::createInstance([ $file = File::createInstance([
'pid' => $pid, 'pid' => $pid,
'name' => $name, 'name' => $name,
'type' => $type, 'type' => $type,
'userid' => $user->userid, 'userid' => $userid,
'created_id' => $user->userid,
]); ]);
$file->save(); $file->save();
// //
@ -165,10 +168,13 @@ class FileController extends AbstractController
// //
$id = intval(Request::input('id')); $id = intval(Request::input('id'));
// //
$row = File::whereUserid($user->userid)->whereId($id)->first(); $row = File::allowFind($id);
if (empty($row)) { //
return Base::retError('文件不存在或已被删除'); $userid = $user->userid;
if ($row->pid > 0) {
$userid = intval(File::whereId($row->pid)->value('userid'));
} }
//
if ($row->type == 'folder') { if ($row->type == 'folder') {
return Base::retError('不支持复制文件夹'); return Base::retError('不支持复制文件夹');
} }
@ -180,7 +186,8 @@ class FileController extends AbstractController
'pid' => $row->pid, 'pid' => $row->pid,
'name' => $name, 'name' => $name,
'type' => $row->type, 'type' => $row->type,
'userid' => $user->userid, 'userid' => $userid,
'created_id' => $user->userid,
]); ]);
$file->save(); $file->save();
// //
@ -201,10 +208,13 @@ class FileController extends AbstractController
$id = intval(Request::input('id')); $id = intval(Request::input('id'));
$pid = intval(Request::input('pid')); $pid = intval(Request::input('pid'));
// //
$file = File::whereUserid($user->userid)->whereId($id)->first(); $file = File::whereId($id)->first();
if (empty($file)) { if (empty($file)) {
return Base::retError('文件不存在或已被删除'); return Base::retError('文件不存在或已被删除');
} }
if ($file->userid != $user->userid) {
return Base::retError('仅限所有者操作');
}
// //
if ($pid > 0) { if ($pid > 0) {
if (!File::whereUserid($user->userid)->whereId($pid)->exists()) { if (!File::whereUserid($user->userid)->whereId($pid)->exists()) {
@ -233,14 +243,9 @@ class FileController extends AbstractController
*/ */
public function remove() public function remove()
{ {
$user = User::auth();
//
$id = intval(Request::input('id')); $id = intval(Request::input('id'));
// //
$file = File::whereUserid($user->userid)->whereId($id)->first(); $file = File::allowFind($id);
if (empty($file)) {
return Base::retError('文件不存在或已被删除');
}
$file->deleteFile(); $file->deleteFile();
return Base::retSuccess('删除成功', $file); return Base::retSuccess('删除成功', $file);
} }
@ -256,10 +261,7 @@ class FileController extends AbstractController
// //
$id = intval(Request::input('id')); $id = intval(Request::input('id'));
// //
$file = File::whereUserid($user->userid)->whereId($id)->first(); $file = File::allowFind($id);
if (empty($file)) {
return Base::retError('文件不存在或已被删除');
}
// //
$content = FileContent::whereFid($file->id)->orderByDesc('id')->first(); $content = FileContent::whereFid($file->id)->orderByDesc('id')->first();
if (empty($content)) { if (empty($content)) {
@ -289,10 +291,7 @@ class FileController extends AbstractController
$id = Base::getPostInt('id'); $id = Base::getPostInt('id');
$content = Base::getPostValue('content'); $content = Base::getPostValue('content');
// //
$file = File::whereUserid($user->userid)->whereId($id)->first(); $file = File::allowFind($id);
if (empty($file)) {
return Base::retError('文件不存在或已被删除');
}
// //
$text = ''; $text = '';
if ($file->type == 'document') { if ($file->type == 'document') {
@ -346,7 +345,6 @@ class FileController extends AbstractController
if (empty($file)) { if (empty($file)) {
return Base::retError('文件不存在或已被删除'); return Base::retError('文件不存在或已被删除');
} }
//
if ($file->userid != $user->userid) { if ($file->userid != $user->userid) {
return Base::retError('仅限所有者操作'); return Base::retError('仅限所有者操作');
} }
@ -366,7 +364,7 @@ class FileController extends AbstractController
* @apiParam {String} action 动作 * @apiParam {String} action 动作
* - share: 设置共享 * - share: 设置共享
* - unshare: 取消共享 * - unshare: 取消共享
* @apiParam {Number} [share] 共享方式 * @apiParam {Number} [share] 共享对象
* - 1: 共享给所有人 * - 1: 共享给所有人
* - 2: 共享给指定成员 * - 2: 共享给指定成员
* @apiParam {Array} [userids] 共享成员,格式: [userid1, userid2, userid3] * @apiParam {Array} [userids] 共享成员,格式: [userid1, userid2, userid3]
@ -384,7 +382,6 @@ class FileController extends AbstractController
if (empty($file)) { if (empty($file)) {
return Base::retError('文件不存在或已被删除'); return Base::retError('文件不存在或已被删除');
} }
//
if ($file->userid != $user->userid) { if ($file->userid != $user->userid) {
return Base::retError('仅限所有者操作'); return Base::retError('仅限所有者操作');
} }
@ -400,7 +397,7 @@ class FileController extends AbstractController
} else { } else {
// 设置共享 // 设置共享
if (!in_array($share, [1, 2])) { if (!in_array($share, [1, 2])) {
return Base::retError('请选择共享类型'); return Base::retError('请选择共享对象');
} }
$file->setShare($share); $file->setShare($share);
if ($share == 2) { if ($share == 2) {

View File

@ -154,4 +154,20 @@ class File extends AbstractModel
}); });
return true; return true;
} }
/**
* 获取文件并检测权限
* @param $id
* @param null $noExistTis
* @return File
*/
public static function allowFind($id, $noExistTis = null)
{
$file = File::find($id);
if (empty($file)) {
throw new ApiException($noExistTis ?: '文件不存在或已被删除');
}
$file->chackAllow(User::userid());
return $file;
}
} }

View File

@ -5,6 +5,7 @@
<div class="file-head"> <div class="file-head">
<div class="file-nav"> <div class="file-nav">
<h1>{{$L('文件')}}</h1> <h1>{{$L('文件')}}</h1>
<div v-if="loadIng == 0" class="file-refresh" @click="getFileList"><i class="taskfont">&#xe6ae;</i></div>
</div> </div>
<div :class="['file-search', searchKey ? 'has-value' : '']"> <div :class="['file-search', searchKey ? 'has-value' : '']">
<Input v-model="searchKey" suffix="ios-search" @on-change="onSearchChange" :placeholder="$L('搜索名称')"/> <Input v-model="searchKey" suffix="ios-search" @on-change="onSearchChange" :placeholder="$L('搜索名称')"/>
@ -70,18 +71,12 @@
@command="dropFile(item, $event)"> @command="dropFile(item, $event)">
<Icon @click.stop="" type="ios-more" /> <Icon @click.stop="" type="ios-more" />
<EDropdownMenu slot="dropdown"> <EDropdownMenu slot="dropdown">
<template v-if="item.userid == userId"> <EDropdownItem command="open">{{$L('打开')}}</EDropdownItem>
<EDropdownItem command="open">{{$L('打开')}}</EDropdownItem> <EDropdownItem command="rename" divided>{{$L('重命名')}}</EDropdownItem>
<EDropdownItem command="rename" divided>{{$L('重命名')}}</EDropdownItem> <EDropdownItem command="copy" :disabled="item.type=='folder'">{{$L('复制')}}</EDropdownItem>
<EDropdownItem command="copy" :disabled="item.type=='folder'">{{$L('复制')}}</EDropdownItem> <EDropdownItem command="shear" :disabled="item.userid != userId">{{$L('剪切')}}</EDropdownItem>
<EDropdownItem command="shear">{{$L('剪切')}}</EDropdownItem> <EDropdownItem command="share" :disabled="item.userid != userId" divided>{{$L('共享')}}</EDropdownItem>
<EDropdownItem command="share" divided>{{$L('共享')}}</EDropdownItem> <EDropdownItem command="delete" divided style="color:red">{{$L('删除')}}</EDropdownItem>
<EDropdownItem command="delete" divided style="color:red">{{$L('删除')}}</EDropdownItem>
</template>
<template v-else>
<EDropdownItem command="open">{{$L('打开')}}</EDropdownItem>
<EDropdownItem command="copy" :disabled="item.type=='folder'">{{$L('复制')}}</EDropdownItem>
</template>
</EDropdownMenu> </EDropdownMenu>
</EDropdown> </EDropdown>
<div class="file-icon"> <div class="file-icon">
@ -110,7 +105,7 @@
:title="$L('共享设置')" :title="$L('共享设置')"
:mask-closable="false"> :mask-closable="false">
<Form ref="addProject" :model="shareInfo" label-width="auto" @submit.native.prevent> <Form ref="addProject" :model="shareInfo" label-width="auto" @submit.native.prevent>
<FormItem prop="type" :label="$L('共享类型')"> <FormItem prop="type" :label="$L('共享对象')">
<RadioGroup v-model="shareInfo.share"> <RadioGroup v-model="shareInfo.share">
<Radio :label="1">{{$L('所有人')}}</Radio> <Radio :label="1">{{$L('所有人')}}</Radio>
<Radio :label="2">{{$L('指定成员')}}</Radio> <Radio :label="2">{{$L('指定成员')}}</Radio>
@ -229,14 +224,7 @@ export default {
watch: { watch: {
pid: { pid: {
handler() { handler() {
this.loadIng++; this.getFileList();
this.$store.dispatch("getFiles", this.pid).then(() => {
this.loadIng--;
this.$store.state.method.setStorage("fileOpenPid", this.pid)
}).catch(({msg}) => {
$A.modalError(msg);
this.loadIng--;
});
}, },
immediate: true immediate: true
}, },
@ -373,6 +361,17 @@ export default {
] ]
}, },
getFileList() {
this.loadIng++;
this.$store.dispatch("getFiles", this.pid).then(() => {
this.loadIng--;
this.$store.state.method.setStorage("fileOpenPid", this.pid)
}).catch(({msg}) => {
$A.modalError(msg);
this.loadIng--;
});
},
addFile(command) { addFile(command) {
let id = $A.randomString(8); let id = $A.randomString(8);
this.files.push({ this.files.push({
@ -595,7 +594,7 @@ export default {
return; return;
} }
if (![1, 2].includes(this.shareInfo.share)) { if (![1, 2].includes(this.shareInfo.share)) {
$A.messageWarning("请选择共享类型") $A.messageWarning("请选择共享对象")
return; return;
} }
this.shareLoad++; this.shareLoad++;

View File

@ -23,6 +23,19 @@
font-size: 28px; font-size: 28px;
font-weight: 600; font-weight: 600;
} }
.file-refresh {
display: none;
cursor: pointer;
margin-left: 12px;
> i {
font-size: 18px;
}
}
&:hover {
.file-refresh {
display: block;
}
}
} }
.file-search { .file-search {
flex-shrink: 0; flex-shrink: 0;
@ -106,7 +119,7 @@
} }
} }
.nav-load { .nav-load {
margin-left: 4px; margin-left: 8px;
width: 24px; width: 24px;
height: 24px; height: 24px;
display: flex; display: flex;
@ -117,6 +130,9 @@
height: 12px; height: 12px;
} }
} }
.nav-refresh {
margin-left: 8px;
}
.ivu-btn { .ivu-btn {
font-size: 12px; font-size: 12px;
margin-left: 12px; margin-left: 12px;
@ -332,8 +348,8 @@
.taskfont { .taskfont {
position: absolute; position: absolute;
right: 10px; right: 10px;
bottom: -1px; bottom: -2px;
font-size: 16px; font-size: 18px;
color: #ffffff; color: #ffffff;
} }
} }
@ -343,8 +359,8 @@
&.folder .file-icon { &.folder .file-icon {
background-image: url("../images/file/folder.svg"); background-image: url("../images/file/folder.svg");
.taskfont { .taskfont {
right: 7px; right: 6px;
bottom: 3px; bottom: 2px;
} }
} }
&.document .file-icon { &.document .file-icon {