From 0a99ecdd9b218508470af2a6f4752df22ce17fd8 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 15 Jul 2022 18:25:18 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/FileController.php | 44 ++++++++++++++---- app/Models/File.php | 27 ++++++++--- .../2022_03_30_070029_add_files_pids.php | 2 +- .../2022_07_15_165114_add_files_pshare.php | 46 +++++++++++++++++++ resources/assets/js/pages/manage/file.vue | 4 +- 5 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 database/migrations/2022_07_15_165114_add_files_pshare.php diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index df4462175..11a5506b1 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -93,8 +93,7 @@ class FileController extends AbstractController ->join('file_users', 'files.id', '=', 'file_users.file_id') ->where('files.userid', '!=', $user->userid) ->where(function ($query) use ($user) { - $query->where('file_users.userid', 0); - $query->orWhere('file_users.userid', $user->userid); + $query->whereIn('file_users.userid', [0, $user->userid]); }) ->groupBy('files.id') ->take(100) @@ -175,11 +174,36 @@ class FileController extends AbstractController if (empty($key)) { return Base::retError('请输入关键词'); } - // + // 搜索自己的 $builder = File::whereUserid($user->userid)->where("name", "like", "%{$key}%"); - $list = $builder->take(50)->get(); + $array = $builder->take(50)->get()->toArray(); + // 搜索共享的 + $take = 50 - count($array); + if ($take > 0) { + $list = File::where("name", "like", "%{$key}%") + ->whereIn('pshare', function ($queryA) use ($user) { + $queryA->select('files.id') + ->from('files') + ->join('file_users', 'files.id', '=', 'file_users.file_id') + ->where('files.userid', '!=', $user->userid) + ->where(function ($queryB) use ($user) { + $queryB->whereIn('file_users.userid', [0, $user->userid]); + }); + }) + ->take($take) + ->get(); + if ($list->isNotEmpty()) { + foreach ($list as $file) { + $temp = $file->toArray(); + if ($file->pshare === $file->id) { + $temp['pid'] = 0; + } + $array[] = $temp; + } + } + } // - return Base::retSuccess('success', $list); + return Base::retSuccess('success', $array); } /** @@ -274,7 +298,7 @@ class FileController extends AbstractController 'created_id' => $user->userid, ]); $file->handleDuplicateName(); - $file->saveBeforePids(); + $file->saveBeforePP(); // $data = File::find($file->id); $data->pushMsg('add', $data); @@ -328,7 +352,7 @@ class FileController extends AbstractController $data = AbstractModel::transaction(function() use ($file) { $content = FileContent::select(['content', 'text', 'size'])->whereFid($file->cid)->orderByDesc('id')->first(); $file->size = $content?->size ?: 0; - $file->saveBeforePids(); + $file->saveBeforePP(); if ($content) { $content = $content->toArray(); $content['fid'] = $file->id; @@ -407,7 +431,7 @@ class FileController extends AbstractController // $file->pid = $pid; $file->handleDuplicateName(); - $file->saveBeforePids(); + $file->saveBeforePP(); $files[] = $file; } }); @@ -716,7 +740,7 @@ class FileController extends AbstractController 'created_id' => $user->userid, ]); $dirRow->handleDuplicateName(); - if ($dirRow->saveBeforePids()) { + if ($dirRow->saveBeforePP()) { $addItem[] = File::find($dirRow->id); } } @@ -786,7 +810,7 @@ class FileController extends AbstractController // 开始创建 return AbstractModel::transaction(function () use ($addItem, $webkitRelativePath, $type, $user, $data, $file) { $file->size = $data['size'] * 1024; - $file->saveBeforePids(); + $file->saveBeforePP(); // $data = Base::uploadMove($data, "uploads/file/" . $file->type . "/" . date("Ym") . "/" . $file->id . "/"); $content = [ diff --git a/app/Models/File.php b/app/Models/File.php index 234f30b65..d9515d699 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -13,8 +13,8 @@ use Request; * App\Models\File * * @property int $id - * @property string|null $pids 上级ID递归 * @property int|null $pid 上级ID + * @property string|null $pids 上级ID递归 * @property int|null $cid 复制ID * @property string|null $name 名称 * @property string|null $type 类型 @@ -22,6 +22,7 @@ use Request; * @property int|null $size 大小(B) * @property int|null $userid 拥有者ID * @property int|null $share 是否共享 + * @property int|null $pshare 所属分享ID * @property int|null $created_id 创建者 * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at @@ -39,6 +40,7 @@ use Request; * @method static \Illuminate\Database\Eloquent\Builder|File whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|File wherePid($value) * @method static \Illuminate\Database\Eloquent\Builder|File wherePids($value) + * @method static \Illuminate\Database\Eloquent\Builder|File wherePshare($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereShare($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereSize($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereType($value) @@ -183,6 +185,7 @@ class File extends AbstractModel AbstractModel::transaction(function () use ($share) { $this->share = $share; $this->save(); + File::where("pids", "like", "%,{$this->id},%")->update(['pshare' => $share ? $this->id : 0]); if ($share === 0) { FileUser::deleteFileAll($this->id, $this->userid); } @@ -223,16 +226,25 @@ class File extends AbstractModel } /** - * 保存前更新pids + * 保存前更新pids/pshare * @return bool */ - public function saveBeforePids() + public function saveBeforePP() { $pid = $this->pid; + $pshare = $this->share ? $this->id : 0; $array = []; while ($pid > 0) { $array[] = $pid; - $pid = intval(self::whereId($pid)->value('pid')); + $file = self::select(['id', 'pid', 'share'])->find($pid); + if ($file) { + $pid = $file->pid; + if ($file->share) { + $pshare = $file->id; + } + } else { + $pid = 0; + } } $opids = $this->pids; if ($array) { @@ -241,6 +253,7 @@ class File extends AbstractModel } else { $this->pids = ''; } + $this->pshare = $pshare; if (!$this->save()) { return false; } @@ -249,7 +262,7 @@ class File extends AbstractModel self::wherePid($this->id)->chunkById(100, function ($lists) { /** @var self $item */ foreach ($lists as $item) { - $item->saveBeforePids(); + $item->saveBeforePP(); } }); } @@ -494,7 +507,7 @@ class File extends AbstractModel 'created_id' => 0, ]); $file->handleDuplicateName(); - $file->saveBeforePids(); + $file->saveBeforePP(); // 移交文件 self::whereUserid($originalUserid)->chunkById(100, function($list) use ($file, $newUserid) { @@ -504,7 +517,7 @@ class File extends AbstractModel $item->pid = $file->id; } $item->userid = $newUserid; - $item->saveBeforePids(); + $item->saveBeforePP(); } }); diff --git a/database/migrations/2022_03_30_070029_add_files_pids.php b/database/migrations/2022_03_30_070029_add_files_pids.php index 573f9a664..1006d4868 100644 --- a/database/migrations/2022_03_30_070029_add_files_pids.php +++ b/database/migrations/2022_03_30_070029_add_files_pids.php @@ -25,7 +25,7 @@ class AddFilesPids extends Migration \App\Models\File::where('pid', '>', 0)->chunkById(100, function ($lists) { /** @var \App\Models\File $item */ foreach ($lists as $item) { - $item->saveBeforePids(); + $item->saveBeforePP(); } }); \App\Models\File::whereShare(0)->chunkById(100, function ($lists) { diff --git a/database/migrations/2022_07_15_165114_add_files_pshare.php b/database/migrations/2022_07_15_165114_add_files_pshare.php new file mode 100644 index 000000000..1912f41dc --- /dev/null +++ b/database/migrations/2022_07_15_165114_add_files_pshare.php @@ -0,0 +1,46 @@ +bigInteger('pshare')->nullable()->default(0)->after('share')->comment('所属分享ID'); + } + }); + if ($isAdd) { + \App\Models\File::whereShare(1)->chunkById(100, function ($lists) { + /** @var \App\Models\File $item */ + foreach ($lists as $item) { + \App\Models\File::where("pids", "like", "%,{$item->id},%")->update(['pshare' => $item->id]); + $item->pshare = $item->id; + $item->save(); + } + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('files', function (Blueprint $table) { + $table->dropColumn("pshare"); + }); + } +} diff --git a/resources/assets/js/pages/manage/file.vue b/resources/assets/js/pages/manage/file.vue index 844e56903..38d7ff4fa 100644 --- a/resources/assets/js/pages/manage/file.vue +++ b/resources/assets/js/pages/manage/file.vue @@ -1410,11 +1410,11 @@ export default { }, onSearchChange() { - clearTimeout(this.searchTimeout); + this.searchTimeout && clearTimeout(this.searchTimeout); if (this.searchKey.trim() != '') { this.searchTimeout = setTimeout(() => { this.loadIng++; - this.$store.dispatch("searchFiles", this.searchKey).then(() => { + this.$store.dispatch("searchFiles", this.searchKey.trim()).then(() => { this.loadIng--; }).catch(() => { this.loadIng--;