From 37719f96c0cc7a1a1895bd6eacb9821852f8c128 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 24 Jul 2024 17:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20=E5=9B=9E=E6=94=B6?= =?UTF-8?q?=E7=AB=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 59 +++++++++++++++---------- app/controller/Audit/AuditController.php | 19 ++++++-- app/controller/substance/RecycleController.php | 21 +++++++++ app/controller/substance/ResourceController.php | 13 +++++- app/model/Recycle.php | 9 ++++ app/model/Resource.php | 8 ++-- app/model/ResourceDir.php | 11 ++++- app/service/AuditService.php | 57 ++++++++++++++++++++++++ app/service/RecycleService.php | 39 ++++++++++++++++ app/service/ResourceDirService.php | 20 ++++++++- app/service/ResourceService.php | 37 ++++++++++++++-- route/substance.php | 8 ++++ 12 files changed, 262 insertions(+), 39 deletions(-) create mode 100644 app/controller/substance/RecycleController.php create mode 100644 app/model/Recycle.php create mode 100644 app/service/RecycleService.php diff --git a/app/common.php b/app/common.php index f3ee1c8..22fa5e9 100644 --- a/app/common.php +++ b/app/common.php @@ -13,7 +13,7 @@ use think\facade\Request; function jsonReturn($code, string $msg = 'success', $data = []): \think\response\Json { - return json(['code' => $code, 'data' => $data, 'msg' => $msg]); + return json(['code' => $code, 'data' => $data, 'msg' => $msg]); } /** @@ -26,30 +26,30 @@ function jsonReturn($code, string $msg = 'success', $data = []): \think\response function dataReturn($code, $msg = 'success', $data = []) { - return ['code' => $code, 'data' => $data, 'msg' => $msg]; + return ['code' => $code, 'data' => $data, 'msg' => $msg]; } /** * 递归查找父级路径 * @param $obj 递归的对象 - * @param string $id + * @param string $id * @param array $path 路径数组 * @return array */ function findParent($obj, $id, &$path) { - // 查找当前文件夹的信息 - $resource_dir = $obj::where('id', $id)->where('type', 2)->find(); + // 查找当前文件夹的信息 + $resource_dir = $obj::where('id', $id)->where('type', 2)->find(); - if ($resource_dir) { - // 将当前文件夹ID添加到路径 - $path[] = $resource_dir['id']; + if ($resource_dir) { + // 将当前文件夹ID添加到路径 + $path[] = $resource_dir['id']; - // 如果有父级ID,继续查找父级 - if (!is_null($resource_dir['parent_id'])) { - findParent($obj, $resource_dir['parent_id'], $path); + // 如果有父级ID,继续查找父级 + if (!is_null($resource_dir['parent_id'])) { + findParent($obj, $resource_dir['parent_id'], $path); + } } - } } @@ -59,14 +59,14 @@ function findParent($obj, $id, &$path) */ function getResourceShareInfo() { - $code = uniqid(); - $share_link = url('share/index', ['code' => $code])->domain(true); - $password = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT); - return [ - 'code' => $code, - 'share_link' => (string)$share_link, - 'password' => $password, - ]; + $code = uniqid(); + $share_link = url('share/index', ['code' => $code])->domain(true); + $password = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT); + return [ + 'code' => $code, + 'share_link' => (string)$share_link, + 'password' => $password, + ]; } /** @@ -77,8 +77,8 @@ function getResourceShareInfo() */ function daysToMilliseconds($days) { - // 1天 = 24小时,1小时 = 60分钟,1分钟 = 60秒,1秒 = 1000毫秒 - return $days * 24 * 60 * 60 * 1000; + // 1天 = 24小时,1小时 = 60分钟,1分钟 = 60秒,1秒 = 1000毫秒 + return $days * 24 * 60 * 60 * 1000; } /** @@ -89,6 +89,17 @@ function daysToMilliseconds($days) */ function daysToSeconds($days) { - // 1天 = 24小时,1小时 = 60分钟,1分钟 = 60秒,1秒 = 1000毫秒 - return $days * 24 * 60 * 60; + // 1天 = 24小时,1小时 = 60分钟,1分钟 = 60秒,1秒 = 1000毫秒 + return $days * 24 * 60 * 60; +} + +/** + * 获取指定天数后的日期 + * @param $days + * @return string + */ +function getdaysLater($days) +{ + $ninetyDaysLater = strtotime("+$days days", time()); + return date("Y-m-d H:i:s", $ninetyDaysLater); } diff --git a/app/controller/Audit/AuditController.php b/app/controller/Audit/AuditController.php index e1e8fd0..2806749 100644 --- a/app/controller/Audit/AuditController.php +++ b/app/controller/Audit/AuditController.php @@ -24,7 +24,7 @@ class AuditController ->when(isset($param['status']), function ($query) use ($request) { $query->whereIn('status', $request->param('status')); }) - ->with('target') + ->with(['target', 'launch_user']) ->select(); return jsonReturn(0, Lang::get('成功'), $list); } @@ -57,9 +57,20 @@ class AuditController //保存到数据库 Db::startTrans(); try { - dd($request->param()); - // 1. 保存站点到数据库 - $res = (new AuditService())->approve($request->param()); + $audit_service = new AuditService(); + //批量审核操作 只支持同文档类型 同操作类型 + $target_type_and_type_info = $audit_service->checkIdsTargetTypeAndType($request->param('ids')); + if (!$target_type_and_type_info) { + throw new \Exception('批量操作只支持同类型操作'); + } + + //区分 资源还是内容 + if ($target_type_and_type_info['target_type'] == 1) { + //资源上传 批量审核通过 + $res = (new AuditService())->resourceApprove($request->param(), $target_type_and_type_info['type']); + } + + Db::commit(); return json($res); } catch (\Exception $e) { diff --git a/app/controller/substance/RecycleController.php b/app/controller/substance/RecycleController.php new file mode 100644 index 0000000..00e2441 --- /dev/null +++ b/app/controller/substance/RecycleController.php @@ -0,0 +1,21 @@ +delete($request->param('ids')); + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + return jsonReturn(-5, $e->getMessage()); + } + + return json($res); } } diff --git a/app/model/Recycle.php b/app/model/Recycle.php new file mode 100644 index 0000000..4868b49 --- /dev/null +++ b/app/model/Recycle.php @@ -0,0 +1,9 @@ +hasMany(File::class, 'resource_id', 'id'); } + /** - * 子资源 + * @return \think\model\relation\BelongsTo + * 所属目录 关联关系 */ - public function childrenResources() + public function resourceDir() { - return $this->hasMany(Resource::class, 'parent_id', 'id'); + return $this->belongsTo(ResourceDir::class, 'parent_id'); } } diff --git a/app/model/ResourceDir.php b/app/model/ResourceDir.php index e5f9d42..cf81680 100644 --- a/app/model/ResourceDir.php +++ b/app/model/ResourceDir.php @@ -7,5 +7,14 @@ use think\facade\Db; class ResourceDir extends Model { - protected $table = 'zl_resource'; + protected $table = 'zl_resource'; + + /** + * @return \think\model\relation\HasMany + * 子资源 关联关系 + */ + public function childrenResources() + { + return $this->hasMany(Resource::class, 'parent_id', 'id'); + } } diff --git a/app/service/AuditService.php b/app/service/AuditService.php index 96ceb26..d12b7c1 100644 --- a/app/service/AuditService.php +++ b/app/service/AuditService.php @@ -3,6 +3,7 @@ namespace app\service; use app\model\Audit; +use app\model\Resource; class AuditService extends Service { @@ -32,8 +33,64 @@ class AuditService extends Service } + /** + * @param $ids + * @return array|false 是同一种 返回文档类型和操作 不是返回false + * 检测批量操作的时候是同一种文档(素材/内容) 是否是同一种操作(上传/删除) + */ + public function checkIdsTargetTypeAndType($ids) + { + $target_type_ary = []; + $type_ary = []; + $audits = Audit::whereIn('id', $ids)->field('id,target_type,type')->select(); + foreach ($audits as $audit) { + + if (!in_array($audit->target_type, $target_type_ary)) { + $target_type_ary[] = $audit->target_type; + } + + if (!in_array($audit->type, $type_ary)) { + $type_ary[] = $audit->type; + } + } + if (count($target_type_ary) > 1 || count($type_ary) > 1) { + return false; + } + return [ + 'target_type' => $target_type_ary[0], + 'type' => $type_ary[0], + ]; + } + + public function resourceApprove($param, $type) + { + switch ($type) { + case 1: + //上传审核通过 + + //审核记录改为通过 + Audit::whereIn('id', $param['ids'])->update(['status' => 2]); + //上传审核的资源ids + $upload_target_ids_ary = Audit::whereIn('id', $param['ids'])->where('type', 1)->column('target_id'); + Resource::whereIn('id', $upload_target_ids_ary)->update(['audit_status' => 2]); + break; + case 2: + //删除审核通过 + break; + } + + + return dataReturn($this->sucCode, '审核通过成功'); + } + + /** + * @param $param + * @return array + * 批量审核通过 + */ public function approve($param) { + dd($param); //审核记录改为通过 Audit::whereIn('id', $param['ids'])->update(['status' => 2]); diff --git a/app/service/RecycleService.php b/app/service/RecycleService.php new file mode 100644 index 0000000..7c643cc --- /dev/null +++ b/app/service/RecycleService.php @@ -0,0 +1,39 @@ +seller_id = 1; + $recycle->target_id = $resource->id; + $recycle->table_name = 'resource'; + $recycle->user_id = 1; + $recycle->dir_name = $resource_dir_service->getResourceDirNamePath($resource); + $recycle->expire_time = getdaysLater(90); + $recycle->save(); + + //修改资源的删除状态 + $resource->is_del = 1; + $resource->save(); + + return dataReturn(0, '加入回收站成功'); + } +} diff --git a/app/service/ResourceDirService.php b/app/service/ResourceDirService.php index 202c431..bece01b 100644 --- a/app/service/ResourceDirService.php +++ b/app/service/ResourceDirService.php @@ -192,11 +192,10 @@ class ResourceDirService extends Service } /** - * 递归更新文件夹的 id_path - * * @param int $folderId 要更新的文件夹 ID * @param int $newParentId 新的父文件夹 ID * @return void + * 递归更新文件夹的 id_path */ protected function updateChildParentIds($resource_id) { @@ -214,4 +213,21 @@ class ResourceDirService extends Service } } } + + /** + * @param $resource + * @return string + * 获取一个资源的 所属文件夹路径名称 + */ + public function getResourceDirNamePath($resource) + { + //没有所属目录 + if (!$resource->resource_dir) { + return ''; + } + //根据所属目录的idpath找到所有目录路径 + $all_resource_dir_name = ResourceDir::whereIn('id', $resource->resource_dir->id_path)->column('name'); + + return implode('-', $all_resource_dir_name); + } } diff --git a/app/service/ResourceService.php b/app/service/ResourceService.php index bfcc3f1..fa95685 100644 --- a/app/service/ResourceService.php +++ b/app/service/ResourceService.php @@ -11,9 +11,9 @@ class ResourceService extends Service { /** + * 添加普通资源 * @param $param 前端提交的参数 * @return array - * 添加普通资源 */ public function addResource($param): array { @@ -38,9 +38,9 @@ class ResourceService extends Service } /** + * 添加普通资源后的的操作 * @param $resource 资源对象 * @return array - * 添加普通资源后的的操作 */ public function afterAddResource($resource) { @@ -56,10 +56,10 @@ class ResourceService extends Service } /** + * 移动普通资源 * @param $ids 需要移动的资源ids * @param $to_id 文件夹id * @return array - * 移动操作 */ public function moveTo($ids, $to_id) { @@ -85,10 +85,10 @@ class ResourceService extends Service /** + * 复制普通资源 * @param $ids 需要复制的普通资源ids * @param $to_id 文件夹资源id * @return array - * 复制操作 */ public function copyTo($ids, $to_id) { @@ -107,6 +107,35 @@ class ResourceService extends Service /** + * 删除普通资源数据 + * @param $ids 需要删除的普通资源ids + * @return array + */ + public function delete($ids) + { + $recycle_service = new RecycleService(); + $resource_status_map = Resource::STATUS_MAP; + + foreach ($ids as $resource_id) { + //已经是删除状态的资源 无需处理 + $resource = Resource::where('is_del', 0)->find($resource_id); + if (!$resource) { + continue; + } + + if ($resource->audit_status == $resource_status_map['success']) { + //todo 审核通过的资源 删除走审核流程 + + } else { + //其他状态的资源 删除直接放入回收站 + $recycle_service->putResourceInRecycle($resource); + } + + } + return dataReturn($this->sucCode, $this->delMsg); + } + + /** * @param $resource 资源数组(因为前台创建是数组 保持一致) * @param $to_id 复制到的文件夹id * @return array diff --git a/route/substance.php b/route/substance.php index 7c5db33..9451cb8 100644 --- a/route/substance.php +++ b/route/substance.php @@ -19,6 +19,8 @@ Route::group('resource', function () { Route::post('/rename', 'rename'); Route::post('/move_to', 'moveTo'); Route::post('/copy_to', 'copyTo'); + Route::post('/delete', 'delete'); + })->prefix('substance.resource/')->middleware('login'); //分享 @@ -37,6 +39,12 @@ Route::group('resource_dir', function () { Route::post('/copy_to', 'copyTo'); //复制文件夹 })->prefix('substance.resourceDir/'); +//文件夹 +Route::group('recycle', function () { + Route::get('/index', 'index'); + +})->prefix('substance.recycle/'); +