Skip to content

Commit

Permalink
perf: 文件共享成员支持分享链接
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Mar 31, 2022
1 parent f403014 commit 775fdec
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
24 changes: 8 additions & 16 deletions app/Http/Controllers/Api/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,18 +853,15 @@ public function share__update()
// 取消共享
$action = "delete";
foreach ($userids as $userid) {
if (FileUser::where([
'file_id' => $file->id,
'userid' => $userid,
])->delete()) {
if (FileUser::deleteFileUser($file->id, $userid)) {
$array[] = $userid;
}
}
} else {
// 设置共享
$action = "update";
if ($force === 0) {
if (File::where('pids', 'like', ",{$file->id},")->whereShare(1)->exists()) {
if (File::where("pids", "like", "%,{$file->id},%")->whereShare(1)->exists()) {
return Base::retError('此文件夹内已有共享文件夹', [], -3001);
}
}
Expand Down Expand Up @@ -913,16 +910,10 @@ public function share__out()
if ($file->userid == $user->userid) {
return Base::retError('不能退出自己共享的文件');
}
if (FileUser::where([
'file_id' => $file->id,
'userid' => 0,
])->exists()) {
if (FileUser::whereFileId($file->id)->whereUserid(0)->exists()) {
return Base::retError('无法退出共享所有人的文件或文件夹');
}
FileUser::where([
'file_id' => $file->id,
'userid' => $user->userid,
])->delete();
FileUser::deleteFileUser($file->id, $user->userid);
//
$file->updataShare();
return Base::retSuccess("退出成功");
Expand All @@ -947,20 +938,21 @@ public function share__out()
*/
public function link()
{
User::auth();
$user = User::auth();
//
$id = intval(Request::input('id'));
$refresh = Request::input('refresh', 'no');
//
$file = File::permissionFind($id, 1000);
$file = File::permissionFind($id);
if ($file->type == 'folder') {
return Base::retError('文件夹暂不支持此功能');
}
//
$fileLink = FileLink::whereFileId($file->id)->first();
$fileLink = FileLink::whereFileId($file->id)->whereUserid($user->userid)->first();
if (empty($fileLink)) {
$fileLink = FileLink::createInstance([
'file_id' => $file->id,
'userid' => $user->userid,
'code' => Base::generatePassword(64),
]);
$fileLink->save();
Expand Down
5 changes: 2 additions & 3 deletions app/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function updataShare($share = null)
$this->share = $share;
$this->save();
if ($share === 0) {
FileUser::whereFileId($this->id)->delete();
FileUser::deleteFileAll($this->id, $this->userid);
}
$list = self::wherePid($this->id)->get();
if ($list->isNotEmpty()) {
Expand Down Expand Up @@ -224,8 +224,7 @@ public function deleteFile()
AbstractModel::transaction(function () {
$this->delete();
$this->pushMsg('delete');
FileLink::whereFileId($this->id)->delete();
FileUser::whereFileId($this->id)->delete();
FileUser::deleteFileAll($this->id);
FileContent::whereFid($this->id)->delete();
$list = self::wherePid($this->id)->get();
if ($list->isNotEmpty()) {
Expand Down
30 changes: 30 additions & 0 deletions app/Models/FileUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,34 @@
*/
class FileUser extends AbstractModel
{
/**
* 删除所有共享成员(同时删除成员分享的链接)
* @param $file_id
* @param int $retain_link_userid 保留指定会员的链接
* @return mixed
*/
public static function deleteFileAll($file_id, $retain_link_userid = 0)
{
return AbstractModel::transaction(function() use ($retain_link_userid, $file_id) {
if ($retain_link_userid > 0) {
FileLink::whereFileId($file_id)->where('userid', '!=', $retain_link_userid)->delete();
} else {
FileLink::whereFileId($file_id)->delete();
}
FileUser::whereFileId($file_id)->delete();
});
}
/**
* 删除指定共享成员(同时删除成员分享的链接)
* @param $file_id
* @param $userid
* @return mixed
*/
public static function deleteFileUser($file_id, $userid)
{
return AbstractModel::transaction(function() use ($userid, $file_id) {
FileLink::whereFileId($file_id)->whereUserid($userid)->delete();
return self::whereFileId($file_id)->whereUserid($userid)->delete();
});
}
}
34 changes: 34 additions & 0 deletions database/migrations/2022_03_31_093754_add_file_links_userid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddFileLinksUserid extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('file_links', function (Blueprint $table) {
if (!Schema::hasColumn('file_links', 'userid')) {
$table->integer('userid')->nullable()->default(0)->after('code')->comment('会员ID');
}
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('file_links', function (Blueprint $table) {
$table->dropColumn("userid");
});
}
}
29 changes: 14 additions & 15 deletions resources/assets/js/pages/manage/file.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<template v-if="contextMenuItem.id">
<DropdownItem @click.native="handleContextClick('open')">{{$L('打开')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('select')">{{$L(selectIds.includes(contextMenuItem.id) ? '取消选择' : '选择')}}</DropdownItem>

<Dropdown placement="right-start" transfer>
<DropdownItem divided>
<div class="arrow-forward-item">{{$L('新建')}}<Icon type="ios-arrow-forward"></Icon></div>
Expand All @@ -161,17 +162,16 @@
</DropdownItem>
</DropdownMenu>
</Dropdown>

<DropdownItem @click.native="handleContextClick('rename')" divided>{{$L('重命名')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('copy')" :disabled="contextMenuItem.type == 'folder'">{{$L('复制')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('shear')" :disabled="contextMenuItem.userid != userId">{{$L('剪切')}}</DropdownItem>
<template v-if="contextMenuItem.userid == userId">
<DropdownItem @click.native="handleContextClick('share')" divided>{{$L('共享')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('link')" :disabled="contextMenuItem.type == 'folder'">{{$L('链接')}}</DropdownItem>
</template>
<template v-else-if="contextMenuItem.share">
<DropdownItem @click.native="handleContextClick('outshare')" divided>{{$L('退出共享')}}</DropdownItem>
</template>

<DropdownItem v-if="contextMenuItem.userid == userId" @click.native="handleContextClick('share')" divided>{{$L('共享')}}</DropdownItem>
<DropdownItem v-else-if="contextMenuItem.share" @click.native="handleContextClick('outshare')" divided>{{$L('退出共享')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('link')" :divided="contextMenuItem.userid != userId && !contextMenuItem.share" :disabled="contextMenuItem.type == 'folder'">{{$L('链接')}}</DropdownItem>
<DropdownItem @click.native="handleContextClick('download')" :disabled="contextMenuItem.ext == ''">{{$L('下载')}}</DropdownItem>

<DropdownItem @click.native="handleContextClick('delete')" divided style="color:red">{{$L('删除')}}</DropdownItem>
</template>
<template v-else>
Expand Down Expand Up @@ -722,11 +722,6 @@ export default {
size: 20
},
}))
if (row.permission == 0) {
iconArray.push(h('span', {
class: 'permission',
}, this.$L('只读')))
}
} else {
iconArray.push(h('i', {
class: 'taskfont',
Expand Down Expand Up @@ -1304,7 +1299,7 @@ export default {
})
},
upShare(item) {
upShare(item, force = false) {
if (item.loading === true) {
return;
}
Expand All @@ -1316,6 +1311,7 @@ export default {
id: this.shareInfo.id,
userids: [item.userid],
permission: item.permission,
force: force === true ? 1 : 0
},
}).then(({data, msg}) => {
item.loading = false;
Expand All @@ -1330,15 +1326,18 @@ export default {
}
}).catch(({ret, msg}) => {
item.loading = false;
item.permission = item._permission;
if (ret === -3001) {
$A.modalConfirm({
content: '此文件夹内已有共享文件夹,子文件的共享状态将被取消,是否继续?',
onOk: () => {
this.onShare(true)
this.upShare(item, true)
},
onCancel: () => {
item.permission = item._permission;
}
})
} else {
item.permission = item._permission;
$A.modalError(msg, force === true ? 301 : 0)
}
})
Expand Down

0 comments on commit 775fdec

Please sign in to comment.