Browse Source

文件夹 复制

master
unknown 1 year ago
parent
commit
83dc733a1f
  1. 23
      app/controller/ResourceDirController.php
  2. 83
      app/service/ResourceDirService.php
  3. 18
      app/service/ResourceService.php
  4. 4
      route/app.php

23
app/controller/ResourceDirController.php

@ -28,7 +28,7 @@ class ResourceDirController
//选中的文件夹基础信息
$resource_dir_info = $resource_dir::find($select_dir_id);
//选中文件夹下所有内容
$resource_list = $resource_dir->getResourceDirAndResource($select_dir_id);
$resource_list = (new ResourceDirService())->getResourceDirAndResource($select_dir_id);
$ret = [
'top_resource_dir' => $top_resource_dir,
@ -94,4 +94,25 @@ class ResourceDirController
return jsonReturn(-5, $e->getMessage());
}
}
/**
* 资源 移动到
*/
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);
}
}

83
app/service/ResourceDirService.php

@ -43,26 +43,30 @@ class ResourceDirService extends Service
/**
* @param $resource 资源对象
* @param $resource_dir_id 移入/移出的文件夹id
* @param $resource_dir_id 移入/移出的资源文件夹id
* @param $resource_dir_id method + 移入 - 移除
* @return array
* 将一普通资源移入或者移出文件夹 文件夹修改资源数量 资源大小
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 资源大小
*/
public function changeDirSizeAndCount($resource, $resource_dir_id, $method = '+')
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+')
{
//资源还不属于文件夹
if ($resource_dir_id == 0) {
if ($target_resource_dir_id == 0) {
return true;
}
$resource_dir = ResourceDir::find($resource_dir_id);
//获取资源的大小
$resource_info = (new ResourceService())->getResourceSizeAndCount($resource, true);
//目标文件夹
$target_resource_dir = ResourceDir::find($target_resource_dir_id);
if ($method === '+') {
ResourceDir::whereIn('id', $resource_dir['id_path'])->inc('resource_size', $resource->file_size)
->inc('resource_count', 1)
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', $resource_dir['id_path'])->dec('resource_size', $resource->file_size)
->dec('resource_count', 1)
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);
@ -80,4 +84,63 @@ class ResourceDirService extends Service
// 反转路径数组,使顶级文件夹在前
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);
}
/**
* 递归更新文件夹的 id_path
*
* @param int $folderId 要更新的文件夹 ID
* @param int $newParentId 新的父文件夹 ID
* @return void
*/
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']);
}
}
}
}

18
app/service/ResourceService.php

@ -70,12 +70,12 @@ class ResourceService extends Service
//1 更新原文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-');
//2 更新parent_id
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+');
//3 更新parent_id
$resource->parent_id = $to_id;
$resource->save();
//3 更新 新的文件夹 以及所有父文件夹 大小和数量
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+');
}
return dataReturn($this->sucCode, $this->moveMsg);
}
@ -158,18 +158,20 @@ class ResourceService extends Service
}
/**
* @param $resource 资源对象
* @param $need_resource true 表示普通资源直接返回size是1 计算是资源数量
*通过字段 获得一个资源大小文件数量
*/
public function getResourceSizeAndCount($resource)
public function getResourceSizeAndCount($resource, $need_resource = false)
{
switch ($resource->type) {
case 1:
//普通资源
$count = $resource->file_count;
//普通资源 (素材的大小和数量)
$count = $need_resource ? 1 : $resource->file_count;
$size = $resource->file_size;
break;
case 2;
//文件夹资源
//文件夹资源 (资源的大小和数量)
$count = $resource->resource_count;
$size = $resource->resource_size;
break;

4
route/app.php

@ -33,12 +33,14 @@ Route::group('share', function () {
})->prefix('share/');
//文件夹
Route::group('resourceDir', function () {
Route::group('resource_dir', function () {
Route::get('/index/[:id]', 'index');
// Route::get('/detail/:id', 'detail');
Route::post('/save', 'save'); //新建文件夹 子文件夹
Route::post('/rename', 'rename');
Route::post('/move_to', 'moveTo'); //移动文件夹
Route::post('/copy_to', 'copyTo'); //复制文件夹
})->prefix('resourceDir/');
Route::group('oss', function () {

Loading…
Cancel
Save