9 changed files with 225 additions and 16 deletions
-
63app/controller/ResourceController.php
-
56app/controller/ResourceDirController.php
-
24app/exception/BaseException.php
-
9app/model/AccreditFile.php
-
1app/model/File.php
-
16app/model/Resource.php
-
46app/model/ResourceDir.php
-
11app/validate/ResourceValidate.php
-
15route/app.php
@ -0,0 +1,56 @@ |
|||
<?php |
|||
|
|||
namespace app\controller; |
|||
|
|||
use app\model\Resource; |
|||
use app\model\ResourceDir; |
|||
use app\validate\ResourceValidate; |
|||
use think\facade\Db; |
|||
use think\facade\Lang; |
|||
use think\exception\ValidateException; |
|||
use think\Request; |
|||
|
|||
/** |
|||
* 文件夹使用的也是Resource 视为类型不同的文件(资源) |
|||
*/ |
|||
class ResourceDirController |
|||
{ |
|||
/** |
|||
* 文件夹 首页 |
|||
*/ |
|||
public function index() |
|||
{ |
|||
echo 123; |
|||
die; |
|||
} |
|||
|
|||
/** |
|||
* 创建文件夹 |
|||
*/ |
|||
public function save(ResourceDir $resource_dir, Request $request) |
|||
{ |
|||
if ($request->param('type') != 2) { |
|||
return jsonReturn(-1, '仅支持创建文件夹'); |
|||
} |
|||
// 数据验证
|
|||
try { |
|||
validate(ResourceValidate::class)->scene('save_dir')->check($request->param()); |
|||
} catch (ValidateException $e) { |
|||
return jsonReturn(-1, $e->getError()); |
|||
} |
|||
|
|||
//保存到数据库
|
|||
Db::startTrans(); |
|||
try { |
|||
// 新增文件夹
|
|||
$res = $resource_dir->addResourceDir($request->param()); |
|||
|
|||
Db::commit(); |
|||
} catch (\Exception $e) { |
|||
Db::rollback(); |
|||
return jsonReturn(-5, $e->getMessage()); |
|||
} |
|||
|
|||
return json($res); |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
<?php |
|||
|
|||
namespace app\exception; |
|||
|
|||
use think\Exception; |
|||
|
|||
class BaseException extends Exception |
|||
{ |
|||
protected $errCode = 5000; |
|||
|
|||
protected $errMsg = '系统错误'; |
|||
|
|||
public function __construct($msg = null, $code = null) |
|||
{ |
|||
if (!$msg) { |
|||
$msg = $this->errMsg; |
|||
} |
|||
if (!$code) { |
|||
$code = $this->errCode; |
|||
} |
|||
|
|||
parent::__construct($msg, $code); |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
<?php |
|||
|
|||
namespace app\model; |
|||
|
|||
use app\exception\ModelException; |
|||
use think\facade\Db; |
|||
|
|||
class ResourceDir extends Model |
|||
{ |
|||
protected $table = 'zl_resource'; |
|||
|
|||
/** |
|||
* @param $param 前端提交的参数 |
|||
* @return array |
|||
* 添加文件夹 |
|||
*/ |
|||
public function addResourceDir($param): array |
|||
{ |
|||
try { |
|||
$resource = self::create($param); |
|||
} catch (\Exception $e) { |
|||
throw new ModelException($e->getMessage()); |
|||
} |
|||
return dataReturn($this->sucCode, $this->addMsg, $resource->id); |
|||
} |
|||
|
|||
/** |
|||
* @param $param 递归获取一个文件夹下所有ids |
|||
* @return array |
|||
* 添加文件夹 |
|||
*/ |
|||
public function getFolderIds($folderId) |
|||
{ |
|||
$folderIds = []; |
|||
// 获取直接子文件夹
|
|||
$sub_folders = Resource::where('parent_id', $folderId) |
|||
->where('type', 2) |
|||
->select(); |
|||
foreach ($sub_folders as $subfolder) { |
|||
$folderIds[] = $subfolder['id']; |
|||
$folderIds = array_merge($folderIds, $this->getFolderIds($subfolder['id'])); |
|||
} |
|||
|
|||
return $folderIds; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue