Browse Source

资源调整 审核文件夹

master
unknown 1 year ago
parent
commit
ae977eef93
  1. 8
      app/controller/substance/ResourceController.php
  2. 68
      app/controller/substance/ResourceDirController.php
  3. 3
      app/controller/substance/ShareController.php
  4. 9
      app/model/Resource.php
  5. 20
      app/model/ResourceDir.php
  6. 35
      app/service/AuditResourceService.php
  7. 4
      app/service/AuditService.php
  8. 162
      app/service/ResourceDirService.php
  9. 142
      app/service/ResourceService.php
  10. 8
      route/substance.php

8
app/controller/substance/ResourceController.php

@ -103,14 +103,6 @@ class ResourceController
return jsonReturn(0, Lang::get('成功'), $resource_detail);
}
/**
* 资源 下载素材文件
* @return void
*/
public function downloadFile()
{
}
/**
* 资源 分享

68
app/controller/substance/ResourceDirController.php

@ -111,38 +111,38 @@ class ResourceDirController
/**
* 资源 移动到
*/
public function moveTo(Request $request)
{
//保存到数据库
Db::startTrans();
try {
// 移动资源
$res = (new ResourceDirService())->moveTo($request->param('ids'), $request->param('to_id'));
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return jsonReturn(-5, $e->getMessage());
}
return json($res);
}
/**
* 资源 移动到
*/
public function copyTo(Request $request)
{
Db::startTrans();
try {
// 复制资源
$res = (new ResourceDirService())->copyTo($request->param('ids'), $request->param('to_id'));
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return jsonReturn(-5, $e->getMessage());
}
return json($res);
}
// public function moveTo(Request $request)
// {
// //保存到数据库
// Db::startTrans();
// try {
// // 移动资源
// $res = (new ResourceDirService())->moveTo($request->param('ids'), $request->param('to_id'));
// Db::commit();
// } catch (\Exception $e) {
// Db::rollback();
// return jsonReturn(-5, $e->getMessage());
// }
//
// return json($res);
// }
//
//
// /**
// * 资源 移动到
// */
// public function copyTo(Request $request)
// {
// Db::startTrans();
// try {
// // 复制资源
// $res = (new ResourceDirService())->copyTo($request->param('ids'), $request->param('to_id'));
// Db::commit();
// } catch (\Exception $e) {
// Db::rollback();
// return jsonReturn(-5, $e->getMessage());
// }
//
// return json($res);
// }
}

3
app/controller/substance/ShareController.php

@ -16,6 +16,9 @@ class ShareController
$share = Share::with(['share_item', 'share_item.target'])
->where('code', $code)->find();
if (!$share) {
return jsonReturn(-1, Lang::get('错误的code'));
}
$service = ShareItemService::createShareItemTargetService($share->type);
foreach ($share->share_item as $item) {
$item->share_read_url = $service->getShareReadUrl($item);

9
app/model/Resource.php

@ -49,8 +49,13 @@ class Resource extends Model
* @return \think\model\relation\BelongsTo
* 所属目录 关联关系
*/
public function resourceDir()
public function fatherResources()
{
return $this->belongsTo(ResourceDir::class, 'parent_id');
return $this->belongsTo(Resource::class, 'parent_id');
}
public function childrenResources()
{
return $this->hasMany(Resource::class, 'parent_id', 'id');
}
}

20
app/model/ResourceDir.php

@ -1,20 +0,0 @@
<?php
namespace app\model;
use app\exception\ModelException;
use think\facade\Db;
class ResourceDir extends Model
{
protected $table = 'zl_resource';
/**
* @return \think\model\relation\HasMany
* 子资源 关联关系
*/
public function childrenResources()
{
return $this->hasMany(Resource::class, 'parent_id', 'id');
}
}

35
app/service/AuditResourceService.php

@ -83,7 +83,13 @@ class AuditResourceService extends AuditService
switch ($type) {
case 1:
//上传审核通过
$this->changeAuditAndTargetStatus($param, 2, 'audit_status');
foreach ($param['ids'] as $id) {
$audit = Audit::with('target')->find($id);
$audit->status = 2;
$audit->save();
$this->approveUploadResourceRecursively($audit->target);
}
break;
case 2:
//删除审核通过
@ -105,6 +111,31 @@ class AuditResourceService extends AuditService
/**
* 递归通过所有的资源
* @param $resource
* @return void
*/
private function approveUploadResourceRecursively($resource)
{
$resource->audit_status = 2;
$resource->save();
$children = $this->model::where('parent_id', $resource->id)->select();
if ($children) {
foreach ($children as $child) {
if ($child['type'] == '2') {
// 递归通过子文件夹
$this->approveUploadResourceRecursively($child);
} else {
// 复制文件
$child->audit_status = 2;
$child->save();
}
}
}
}
/**
* 资源上传/删除 审核驳回
* @param $param
* @param $type 1 上传 2 删除
@ -117,7 +148,7 @@ class AuditResourceService extends AuditService
}
switch ($type) {
case 1:
//上传审核驳回
//上传审核驳回(仅需要修改状态)
$this->changeAuditAndTargetStatus($param, 3, 'audit_status');
break;
case 2:

4
app/service/AuditService.php

@ -25,8 +25,10 @@ class AuditService extends Service
$target_type_ary = [];
$type_ary = [];
$audits = Audit::whereIn('id', $ids)->field('id,target_type,type')->select();
if ($audits->isEmpty()) {
throw new \Exception('审核记录不存在');
}
foreach ($audits as $audit) {
if (!in_array($audit->target_type, $target_type_ary)) {
$target_type_ary[] = $audit->target_type;
}

162
app/service/ResourceDirService.php

@ -8,23 +8,7 @@ use app\model\ResourceDir;
class ResourceDirService extends Service
{
/**
* @param $param 前端提交的参数
* @return array
* 添加文件夹
*/
public function addResourceDir($param): array
{
try {
$resource_dir = ResourceDir::create($param);
//更新id_path
$resource_dir->id_path = $this->getResourceDirIdPath($resource_dir->id);
$resource_dir->save();
} catch (\Exception $e) {
throw new ModelException($e->getMessage());
}
return dataReturn($this->sucCode, $this->addMsg, $resource_dir->id);
}
/**
* @param $resource_dir_id 文件夹id
@ -40,84 +24,6 @@ class ResourceDirService extends Service
return $resource_list;
}
/**
* @param $resource 资源对象
* @param $resource_dir_id 移入/移出的资源文件夹id
* @param $resource_dir_id method + 移入 - 移除
* @return array
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 资源大小
*/
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+')
{
//资源还不属于文件夹
if ($target_resource_dir_id == 0) {
return true;
}
//获取资源的大小
$resource_info = (new ResourceService())->getResourceSizeAndCount($resource, true);
//目标文件夹
$target_resource_dir = ResourceDir::find($target_resource_dir_id);
if ($method === '+') {
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size'])
->inc('resource_count', $resource_info['count'])
->update();
} elseif ($method === '-') {
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->dec('resource_size', $resource_info['size'])
->dec('resource_count', $resource_info['count'])
->update();
}
return dataReturn($this->sucCode, $this->addMsg);
}
/**
* @param $resource_dir_id 文件夹id
* 获取文件夹 id_path(从顶级文件夹到自己的路径)
*/
public function getResourceDirIdPath($resource_dir_id)
{
$path = [];
// 调用递归函数
findParent(new ResourceDir(), $resource_dir_id, $path);
// 反转路径数组,使顶级文件夹在前
return implode(',', array_reverse($path));
}
/**
* @param $ids 需要移动的文件夹资源ids
* @param $to_id 文件夹资源id
* @return array
* 移动操作
*/
public function moveTo($ids, $to_id)
{
foreach ($ids as $resource_id) {
$resource = Resource::find($resource_id);
if ($resource->parent_id == $to_id) {
//移动到同文件夹 无需操作
continue;
}
//1 更新原文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-');
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+');
//3 更新当前文件夹 parent_id
$resource->parent_id = $to_id;
$resource->save();
//4 递归修改当前文件夹 子文件夹id_path
$this->updateChildParentIds($resource_id);
}
return dataReturn($this->sucCode, $this->moveMsg);
}
/**
* @param $ids 需要复制的文件夹资源ids
@ -146,73 +52,7 @@ class ResourceDirService extends Service
* @return array
* 递归复制资源文件夹及其内容
*/
public function copyFolderRecursively($ids, $to_id)
{
foreach ($ids as $resource_id) {
//获取源文件 和子文件
$resource = Resource::find($resource_id);
$children = Resource::with(['files', 'accredit_files'])->where('parent_id', $resource->id)->select();
// 复制源文件夹
$new_resource_dir = $this->copyResourceDir($resource->toArray(), $to_id);
$new_resource_dir_id = $new_resource_dir['data'];
// 复制子文件和子文件夹
if ($children) {
foreach ($children as $child) {
if ($child['type'] == '2') {
// 递归复制子文件夹
$this->copyFolderRecursively([$child['id']], $new_resource_dir_id);
} else {
// 复制文件
(new ResourceService())->copyResource($child->toArray(), $new_resource_dir_id);
}
}
}
}
}
/**
* @param $resource 资源数组(因为前台创建是数组 保持一致)
* @param $to_id 复制到的文件夹id
* @return array
* 复制一个文件夹资源数据
*/
public function copyResourceDir($resource, $to_id)
{
unset($resource['id']);
$resource['create_time'] = date('Y-m-d H:i:s');
$resource['update_time'] = date('Y-m-d H:i:s');
$resource['parent_id'] = $to_id;
//重新创建资源
return $this->addResourceDir($resource);
}
/**
* @param int $folderId 要更新的文件夹 ID
* @param int $newParentId 新的父文件夹 ID
* @return void
* 递归更新文件夹的 id_path
*/
protected function updateChildParentIds($resource_id)
{
// 更新当前文件夹的 parent_id id_path
$id_path = $this->getResourceDirIdPath($resource_id);
ResourceDir::where('id', $resource_id)->update(['id_path' => $id_path]);
// 获取所有子文件和子文件夹
$children = ResourceDir::where('parent_id', $resource_id)->select();
foreach ($children as $child) {
// 递归更新子文件夹的 parent_id
if ($child['type'] == '2') {
$this->updateChildParentIds($child['id']);
}
}
}
/**
* @param $resource

142
app/service/ResourceService.php

@ -23,11 +23,14 @@ class ResourceService extends Service
$resource = Resource::create($param);
//存在授权文件 插入素材表
if ($param['copyright_type'] == 2 && isset($param['accredit_files'])) {
if (isset($param['copyright_type']) && $param['copyright_type'] == 2 && isset($param['accredit_files'])) {
(new FileService())->addAccreditFile($resource, $param['accredit_files']);
}
//插入素材表
if (isset($param['files']) && isset($param['file_input_mode'])) {
(new FileService())->addFile($resource, $param['files'], $param['file_input_mode']);
}
//添加资源的后置操作
$this->afterAddResource($resource);
} catch (\Exception $e) {
@ -49,14 +52,16 @@ class ResourceService extends Service
$resource->file_size = $file_size_and_count['file_size'];
$resource->file_count = $file_size_and_count['file_count'];
$resource->audit_status = (new ProcessService())->getAuditStatusByProcess('resource_upload');
$resource->save();
//如果资源状态是待审核 添加审核记录
if ($resource->audit_status == 1) {
(new AuditResourceService())->addOneAudit($resource, 1);
}
if ($resource->type == 2) {
$resource->id_path = $this->getResourceDirIdPath($resource->id);
}
$resource->save();
}
/**
@ -75,14 +80,17 @@ class ResourceService extends Service
}
//1 更新原文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-');
$this->changeDirSizeAndCount($resource, $resource->parent_id, '-');
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+');
$this->changeDirSizeAndCount($resource, $to_id, '+');
//3 更新parent_id
$resource->parent_id = $to_id;
$resource->save();
//4 更新id path
$this->updateChildParentIds($resource_id);
}
return dataReturn($this->sucCode, $this->moveMsg);
}
@ -96,17 +104,96 @@ class ResourceService extends Service
*/
public function copyTo($ids, $to_id)
{
// 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
foreach ($ids as $resource_id) {
//复制需要 需要复制里面所有的素材
$resource = Resource::with(['files', 'accredit_files'])->find($resource_id);
$resource = Resource::find($resource_id);
$this->changeDirSizeAndCount($resource, $to_id, '+');
}
//1.创建一个新普通资源
//递归复制
$this->copyResrouceRecursively($ids, $to_id);
return dataReturn($this->sucCode, $this->copyMsg);
}
public function copyResrouceRecursively($ids, $to_id)
{
foreach ($ids as $resource_id) {
//获取源文件 和子文件
$resource = Resource::with(['files', 'accredit_files'])->find($resource_id);
if ($resource->type == 1) {
//说明要复制的是个文件资源 直接复制
$this->copyResource($resource->toArray(), $to_id);
} elseif ($resource->type == 2) {
$children = Resource::with(['files', 'accredit_files'])->where('parent_id', $resource->id)->select();
$new_resource_dir = $this->copyResourceDir($resource->toArray(), $to_id);
$new_resource_dir_id = $new_resource_dir['data'];
// 复制子文件和子文件夹
if ($children) {
foreach ($children as $child) {
if ($child['type'] == '2') {
// 递归复制子文件夹
$this->copyResrouceRecursively([$child['id']], $new_resource_dir_id);
} else {
// 复制文件
$this->copyResource($child->toArray(), $new_resource_dir_id);
}
}
}
}
}
}
//2 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+');
/**
* @param $resource 资源数组(因为前台创建是数组 保持一致)
* @param $to_id 复制到的文件夹id
* @return array
* 复制一个文件夹资源数据
*/
public function copyResourceDir($resource, $to_id)
{
unset($resource['id']);
$resource['create_time'] = date('Y-m-d H:i:s');
$resource['update_time'] = date('Y-m-d H:i:s');
$resource['parent_id'] = $to_id;
//重新创建资源
return $this->addResourceDir($resource);
}
/**
* @param int $folderId 要更新的文件夹 ID
* @param int $newParentId 新的父文件夹 ID
* @return void
* 递归更新文件夹的 id_path
*/
public function updateChildParentIds($resource_id)
{
// 更新当前文件夹的 parent_id id_path
$id_path = $this->getResourceDirIdPath($resource_id);
Resource::where('id', $resource_id)->update(['id_path' => $id_path]);
// 获取所有子文件和子文件夹
$children = Resource::where('parent_id', $resource_id)->select();
foreach ($children as $child) {
// 递归更新子文件夹的 parent_id
if ($child['type'] == '2') {
$this->updateChildParentIds($child['id']);
}
}
return dataReturn($this->sucCode, $this->copyMsg);
}
/**
* @param $resource_dir_id 文件夹id
* 获取文件夹 id_path(从顶级文件夹到自己的路径)
*/
public function getResourceDirIdPath($resource_dir_id)
{
$path = [];
// 调用递归函数
findParent(new Resource(), $resource_dir_id, $path);
// 反转路径数组,使顶级文件夹在前
return implode(',', array_reverse($path));
}
/**
@ -250,4 +337,35 @@ class ResourceService extends Service
}
return $url;
}
/**
* @param $resource 资源对象
* @param $resource_dir_id 移入/移出的资源文件夹id
* @param $resource_dir_id method + 移入 - 移除
* @return array
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 资源大小
*/
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+')
{
//资源还不属于文件夹
if ($target_resource_dir_id == 0) {
return true;
}
//获取资源的大小
$resource_info = $this->getResourceSizeAndCount($resource, true);
//目标文件夹
$target_resource_dir = Resource::find($target_resource_dir_id);
if ($method === '+') {
Resource::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size'])
->inc('resource_count', $resource_info['count'])
->update();
} elseif ($method === '-') {
Resource::whereIn('id', $target_resource_dir['id_path'])->dec('resource_size', $resource_info['size'])
->dec('resource_count', $resource_info['count'])
->update();
}
return dataReturn($this->sucCode, $this->addMsg);
}
}

8
route/substance.php

@ -37,10 +37,10 @@ Route::group('resource_dir', function () {
Route::get('/index/[:id]', 'index');
Route::get('/read', 'read');
Route::post('/save', 'save'); //新建文件夹 子文件夹
Route::post('/rename', 'rename');
Route::post('/move_to', 'moveTo'); //移动文件夹
Route::post('/copy_to', 'copyTo'); //复制文件夹
// Route::post('/save', 'save'); //新建文件夹 子文件夹
// Route::post('/rename', 'rename');
// Route::post('/move_to', 'moveTo'); //移动文件夹
// Route::post('/copy_to', 'copyTo'); //复制文件夹
})->prefix('substance.resourceDir/');
//文件夹

Loading…
Cancel
Save