22 changed files with 501 additions and 380 deletions
-
28app/controller/ResourceController.php
-
5app/controller/ResourceDirController.php
-
5app/event.php
-
20app/event/ResourceEvent.php
-
19app/listener/ResourceEvent.php
-
30app/middleware/LoginMiddleware.php
-
18app/model/AccreditFile.php
-
44app/model/File.php
-
150app/model/Resource.php
-
73app/model/ResourceDir.php
-
28app/model/ResourceFile.php
-
23app/model/ResourceOperationLog.php
-
9app/model/Share.php
-
50app/service/FileService.php
-
31app/service/FileSystemService.php
-
83app/service/ResourceDirService.php
-
177app/service/ResourceService.php
-
55app/service/Service.php
-
22app/service/UserService.php
-
3app/validate/ResourceValidate.php
-
4config/middleware.php
-
4route/app.php
@ -0,0 +1,20 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace app\event; |
||||
|
|
||||
|
use app\model\Resource; |
||||
|
|
||||
|
//resource 事件类
|
||||
|
class ResourceEvent |
||||
|
{ |
||||
|
public $resource; |
||||
|
public $method; |
||||
|
|
||||
|
public function __construct(Resource $resource, $method) |
||||
|
{ |
||||
|
$this->resource = $resource; |
||||
|
$this->method = $method; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace app\listener; |
||||
|
|
||||
|
class ResourceEvent |
||||
|
{ |
||||
|
/** |
||||
|
* 事件监听处理 |
||||
|
* |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function handle($resource) |
||||
|
{ |
||||
|
//
|
||||
|
dd($resource->resource); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace app\middleware; |
||||
|
|
||||
|
use app\exception\TokenException; |
||||
|
use think\facade\Cache; |
||||
|
use xiaodi\JWTAuth\Facade\Jwt; |
||||
|
|
||||
|
|
||||
|
class LoginMiddleware |
||||
|
{ |
||||
|
/** |
||||
|
* 处理请求 |
||||
|
* |
||||
|
* @param \think\Request $request |
||||
|
* @param \Closure $next |
||||
|
* @return Response |
||||
|
* @throws TokenException |
||||
|
*/ |
||||
|
public function handle($request, \Closure $next) |
||||
|
{ |
||||
|
$userId = 2; |
||||
|
$request->user_id = $userId; |
||||
|
|
||||
|
// 继续处理请求
|
||||
|
return $next($request); |
||||
|
} |
||||
|
} |
||||
@ -1,18 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace app\model; |
|
||||
|
|
||||
use app\exception\ModelException; |
|
||||
|
|
||||
class AccreditFile extends Model |
|
||||
{ |
|
||||
/** |
|
||||
* @param $resource 资源对象 |
|
||||
* @param $accredit_files 授权文件数组 |
|
||||
* @param $file_input_mode 素材录入方式 |
|
||||
* 添加资源 素材关联表 |
|
||||
*/ |
|
||||
public function addAccreditFile($resource, $accredit_files) |
|
||||
{ |
|
||||
} |
|
||||
} |
|
||||
@ -1,28 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace app\model; |
|
||||
|
|
||||
use app\exception\ModelException; |
|
||||
use think\model\Pivot; |
|
||||
|
|
||||
class ResourceFile extends Pivot |
|
||||
{ |
|
||||
protected $autoWriteTimestamp = true; |
|
||||
/** |
|
||||
* @param int $resource_id 资源id |
|
||||
* @param array $file_ids 素材ids |
|
||||
* @return array |
|
||||
* 手动添加 添加资源素材关联表 |
|
||||
*/ |
|
||||
public function addResourceFile($resource_id, $file_ids) |
|
||||
{ |
|
||||
foreach ($file_ids as $file_id) { |
|
||||
$resource_file_array[] = [ |
|
||||
'resource_id' => $resource_id, |
|
||||
'file_id' => $file_id, |
|
||||
]; |
|
||||
} |
|
||||
$ResourceFile = new ResourceFile(); |
|
||||
$ResourceFile->saveAll($resource_file_array); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,23 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
|
||||
|
class ResourceOperationLog extends Model |
||||
|
{ |
||||
|
// // 记录操作日志
|
||||
|
// protected static function logOperation($resource, $operationType)
|
||||
|
// {
|
||||
|
// echo 123;
|
||||
|
// die;
|
||||
|
// $log->resource_id = $resource->id;
|
||||
|
// $log->resource_type = 'resource';
|
||||
|
// $log->operation_type = $operationType;
|
||||
|
// $log->user_id = session('user_id'); // 获取当前用户ID,假设用户ID保存在session中
|
||||
|
// $log->details = json_encode($resource->toArray());
|
||||
|
// $log->ip_address = Request::ip();
|
||||
|
// $log->user_agent = Request::server('HTTP_USER_AGENT');
|
||||
|
// $log->save();
|
||||
|
// }
|
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
|
||||
|
class Share extends Model |
||||
|
{ |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service; |
||||
|
|
||||
|
class FileService |
||||
|
{ |
||||
|
/** |
||||
|
* @param $resource 资源对象 |
||||
|
* @param $accredit_files 授权素材数组 |
||||
|
* 添加 授权素材 |
||||
|
*/ |
||||
|
public function addAccreditFile($resource, $accredit_files) |
||||
|
{ |
||||
|
foreach ($accredit_files as $accredit_file) { |
||||
|
$accredit_file['type'] = 2; |
||||
|
$accredit_file_array[] = $accredit_file; |
||||
|
} |
||||
|
// 资源素材关联 系统会自动插入
|
||||
|
$resource->accreditFiles()->saveAll($accredit_file_array); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $resource 资源对象 |
||||
|
* @param $files 素材 |
||||
|
* 添加 素材 |
||||
|
*/ |
||||
|
public function addFile($resource, $files, $file_input_mode) |
||||
|
{ |
||||
|
foreach ($files as $file) { |
||||
|
$file['type'] = 1; |
||||
|
$file['file_input_mode'] = $file_input_mode; |
||||
|
$file_array[] = $file; |
||||
|
} |
||||
|
// 资源素材关联 系统会自动插入
|
||||
|
$resource->files()->saveAll($file_array); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $files 素材数组 |
||||
|
* 获得一组素材的大小 |
||||
|
*/ |
||||
|
public function getFileSize($files) |
||||
|
{ |
||||
|
$size = 0; |
||||
|
foreach ($files as $file) { |
||||
|
$size += $file['size']; |
||||
|
} |
||||
|
return $size; |
||||
|
} |
||||
|
} |
||||
@ -1,31 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
declare(strict_types=1); |
|
||||
|
|
||||
namespace app\service; |
|
||||
|
|
||||
class FileSystemService extends \think\Service |
|
||||
{ |
|
||||
/** |
|
||||
* 注册服务 |
|
||||
* |
|
||||
* @return mixed |
|
||||
*/ |
|
||||
public function register() |
|
||||
{ |
|
||||
//
|
|
||||
//手动修改的
|
|
||||
echo 'FileSystemService-register()'; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 执行服务 |
|
||||
* |
|
||||
* @return mixed |
|
||||
*/ |
|
||||
public function boot() |
|
||||
{ |
|
||||
//手动修改的
|
|
||||
echo 'FileSystemService-boot()'; |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,83 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
use app\model\Resource; |
||||
|
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 |
||||
|
* @return array |
||||
|
* 获取一个文件夹下的所有文件夹 和资源 |
||||
|
*/ |
||||
|
public function getResourceDirAndResource($resource_dir_id) |
||||
|
{ |
||||
|
$resource_list = ResourceDir::where('parent_id', $resource_dir_id) |
||||
|
->order('type', 'desc') //文件夹放在上面
|
||||
|
->order('create_time', 'desc') |
||||
|
->select(); |
||||
|
return $resource_list; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $resource 资源对象 |
||||
|
* @param $resource_dir_id 移入/移出的文件夹id |
||||
|
* @param $resource_dir_id method + 移入 - 移除 |
||||
|
* @return array |
||||
|
* 将一资源移入或者移出文件夹 文件夹修改资源数量 和 资源大小 |
||||
|
*/ |
||||
|
public function changeDirSizeAndCount($resource, $resource_dir_id, $method = '+') |
||||
|
{ |
||||
|
|
||||
|
//资源还不属于文件夹
|
||||
|
if ($resource_dir_id == 0) { |
||||
|
return true; |
||||
|
} |
||||
|
$resource_dir = ResourceDir::find($resource_dir_id); |
||||
|
if ($method === '+') { |
||||
|
ResourceDir::whereIn('id', $resource_dir['id_path'])->inc('resource_size', $resource->file_size) |
||||
|
->inc('resource_count', 1) |
||||
|
->update(); |
||||
|
} elseif ($method === '-') { |
||||
|
ResourceDir::whereIn('id', $resource_dir['id_path'])->dec('resource_size', $resource->file_size) |
||||
|
->dec('resource_count', 1) |
||||
|
->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)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,177 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
use app\model\Resource; |
||||
|
use app\model\ResourceDir; |
||||
|
use app\model\Share; |
||||
|
|
||||
|
class ResourceService extends Service |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @param $param 前端提交的参数 |
||||
|
* @return array |
||||
|
* 添加资源 |
||||
|
*/ |
||||
|
public function addResource($param): array |
||||
|
{ |
||||
|
try { |
||||
|
// event(new ResourceEvent($this::find(1), 'add'));
|
||||
|
//插入资源表
|
||||
|
$resource = Resource::create($param); |
||||
|
|
||||
|
//存在授权文件 插入授权文件表
|
||||
|
if ($param['copyright_type'] == 2 && isset($param['accredit_files'])) { |
||||
|
(new FileService())->addAccreditFile($resource, $param['accredit_files']); |
||||
|
} |
||||
|
//插入素材表
|
||||
|
(new FileService())->addFile($resource, $param['files'], $param['file_input_mode']); |
||||
|
//添加资源的后置操作
|
||||
|
$this->afterAddResource($resource); |
||||
|
} catch (\Exception $e) { |
||||
|
throw new ModelException($e->getMessage()); |
||||
|
} |
||||
|
|
||||
|
return dataReturn($this->sucCode, $this->addMsg, $resource->id); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $resource 资源对象 |
||||
|
* @return array |
||||
|
* 添加资源后的的操作 |
||||
|
*/ |
||||
|
public function afterAddResource($resource) |
||||
|
{ |
||||
|
//根据素材 更新资源大小 数量
|
||||
|
$file_size_and_count = $this->getFileSizeAndCount($resource); |
||||
|
$resource->file_size = $file_size_and_count['file_size']; |
||||
|
$resource->file_count = $file_size_and_count['file_count']; |
||||
|
//todo 还有状态的啥的
|
||||
|
$resource->save(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @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 更新parent_id
|
||||
|
$resource->parent_id = $to_id; |
||||
|
$resource->save(); |
||||
|
|
||||
|
//3 更新 新的文件夹 以及所有父文件夹 大小和数量
|
||||
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
||||
|
} |
||||
|
return dataReturn($this->sucCode, $this->moveMsg); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @param $ids 需要复制的资源ids |
||||
|
* @param $to_id 文件夹id |
||||
|
* @return array |
||||
|
* 移动操作 |
||||
|
*/ |
||||
|
public function copyTo($ids, $to_id) |
||||
|
{ |
||||
|
foreach ($ids as $resource_id) { |
||||
|
//复制需要 需要复制里面所有的素材
|
||||
|
$resource = Resource::with(['files', 'accredit_files'])->find($resource_id); |
||||
|
|
||||
|
//1.创建一个新资源
|
||||
|
$this->copyResource($resource->toArray(), $to_id); |
||||
|
|
||||
|
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
|
||||
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
||||
|
} |
||||
|
return dataReturn($this->sucCode, $this->copyMsg); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @param $resource 资源数组(因为前台创建是数组 保持一致) |
||||
|
* @param $to_id 复制到的文件夹id |
||||
|
* @return array |
||||
|
* 复制一个资源数据 |
||||
|
*/ |
||||
|
public function copyResource($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; |
||||
|
|
||||
|
//关联表去掉关联字段
|
||||
|
if (isset($resource['files'])) { |
||||
|
foreach ($resource['files'] as $key => $file) { |
||||
|
unset($resource['files'][$key]['id']); |
||||
|
unset($resource['files'][$key]['resource_id']); //去掉原来的关联关系
|
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (isset($resource['accredit_files'])) { |
||||
|
foreach ($resource['accredit_files'] as $key => $file) { |
||||
|
unset($resource['accredit_files'][$key]['id']); |
||||
|
unset($resource['accredit_files'][$key]['resource_id']); //去掉原来的关联关系
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//重新创建资源
|
||||
|
return $this->addResource($resource); |
||||
|
} |
||||
|
/** |
||||
|
* @param $resource 资源对象 |
||||
|
* @return array |
||||
|
* 获取一个资源的大小(所有素材大小的总和) 和包含的素材数量 |
||||
|
*/ |
||||
|
public function getFileSizeAndCount($resource) |
||||
|
{ |
||||
|
$size = 0; |
||||
|
$count = 0; |
||||
|
foreach ($resource->files as $file) { |
||||
|
if (isset($file['size'])) { |
||||
|
$size += $file['size']; |
||||
|
} |
||||
|
$count++; |
||||
|
} |
||||
|
return [ |
||||
|
'file_size' => $size, |
||||
|
'file_count' => $count, |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $param 提交参数 |
||||
|
* @param $user_id 用户id |
||||
|
* |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function addResourceShare($param, $user_id) |
||||
|
{ |
||||
|
$param['user_id'] = $user_id; |
||||
|
$param['code'] = $this->getResourceShareInfo(); //获取访问链接和密码
|
||||
|
dd($param); |
||||
|
$resource = Share::create($param); |
||||
|
} |
||||
|
|
||||
|
public function getResourceShareInfo() |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service; |
||||
|
|
||||
|
class Service |
||||
|
{ |
||||
|
// msg
|
||||
|
public $moveMsg; |
||||
|
public $copyMsg; |
||||
|
public $getMsg; |
||||
|
public $addMsg; |
||||
|
public $updateMsg; |
||||
|
public $delMsg; |
||||
|
|
||||
|
// 成功编码
|
||||
|
public $sucCode = 0; |
||||
|
// 软删除数据
|
||||
|
public $delData; |
||||
|
// 错误编码
|
||||
|
public $errorCode = '50011'; |
||||
|
|
||||
|
public $listCacheKey; |
||||
|
public $detailCacheKey; |
||||
|
public $className; |
||||
|
|
||||
|
|
||||
|
protected $hidden = [ |
||||
|
'password', 'seller_id' |
||||
|
]; |
||||
|
|
||||
|
public function __construct($data = []) |
||||
|
{ |
||||
|
$this->delData = [ |
||||
|
'is_del' => 2, |
||||
|
'delete_time' => time(), |
||||
|
'update_time' => time(), |
||||
|
]; |
||||
|
$this->className = $this->getClassName(); |
||||
|
$this->listCacheKey = $this->className . '_cache_list'; |
||||
|
$this->detailCacheKey = $this->className . '_cache_detail'; |
||||
|
$this->moveMsg = lang('移动成功'); |
||||
|
$this->copyMsg = lang('复制成功'); |
||||
|
$this->getMsg = lang('获取成功'); |
||||
|
$this->addMsg = lang('添加成功'); |
||||
|
$this->updateMsg = lang('修改成功'); |
||||
|
$this->delMsg = lang('删除成功'); |
||||
|
// parent::__construct($data);
|
||||
|
} |
||||
|
|
||||
|
public function getClassName(): string |
||||
|
{ |
||||
|
$reflection = new \ReflectionClass($this); |
||||
|
return $reflection->getShortName(); |
||||
|
} |
||||
|
} |
||||
@ -1,22 +0,0 @@ |
|||||
<?php |
|
||||
// application/service/UserService.php
|
|
||||
namespace app\service; |
|
||||
|
|
||||
use think\Service; |
|
||||
|
|
||||
class UserService extends Service |
|
||||
{ |
|
||||
// public function register()
|
|
||||
// {
|
|
||||
// // 注册服务时执行的逻辑,例如绑定到容器
|
|
||||
// $this->app->bind('userService', function () {
|
|
||||
// return new UserService();
|
|
||||
// });
|
|
||||
// }
|
|
||||
|
|
||||
public function doSomething() |
|
||||
{ |
|
||||
// 服务的具体方法
|
|
||||
return 'Service is working'; |
|
||||
} |
|
||||
} |
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue