2025-07-01 19:39:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
use app\common\logic\ResourceLogic;
|
|
|
|
|
use app\common\validate\ResourceValidate;
|
|
|
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
|
|
|
use app\common\model\Resource;
|
|
|
|
|
use app\common\utils\Tree;
|
|
|
|
|
use support\Request;
|
2025-07-01 19:39:13 +08:00
|
|
|
|
|
|
|
|
class ResourceService
|
|
|
|
|
{
|
2025-08-14 20:08:57 +08:00
|
|
|
|
|
|
|
|
use CrudServiceTrait;
|
|
|
|
|
|
2025-07-01 19:39:13 +08:00
|
|
|
/**
|
2025-08-14 20:08:57 +08:00
|
|
|
* 指定当前要操作的模型
|
2025-07-01 19:39:13 +08:00
|
|
|
*/
|
2025-08-14 20:08:57 +08:00
|
|
|
protected string $modelClass = Resource::class;
|
2025-07-01 19:39:13 +08:00
|
|
|
|
|
|
|
|
/**
|
2025-08-14 20:08:57 +08:00
|
|
|
* 验证器
|
2025-07-01 19:39:13 +08:00
|
|
|
*/
|
2025-08-14 20:08:57 +08:00
|
|
|
protected $validator = ResourceValidate::class;
|
2025-07-01 19:39:13 +08:00
|
|
|
|
|
|
|
|
/**
|
2025-08-14 20:08:57 +08:00
|
|
|
* 获取验证器
|
2025-07-01 19:39:13 +08:00
|
|
|
*/
|
2025-08-14 20:08:57 +08:00
|
|
|
public function getValidator()
|
2025-07-01 19:39:13 +08:00
|
|
|
{
|
2025-08-14 20:08:57 +08:00
|
|
|
return new $this->validator;
|
2025-07-01 19:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
|
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
2025-07-01 19:39:13 +08:00
|
|
|
{
|
2025-08-14 20:08:57 +08:00
|
|
|
$p = $request->all();
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
switch ($scene) {
|
|
|
|
|
case 'default':
|
|
|
|
|
$config['where']['system_id'] = ['in', [0, $p['system_id']]];
|
|
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
$config['order']['sort'] = 'asc';
|
|
|
|
|
$config['order']['id'] = 'asc';
|
|
|
|
|
break;
|
|
|
|
|
case 'menuList':
|
2025-12-04 17:54:40 +08:00
|
|
|
if(isset($p['is_home']) && $p['is_home']==1){
|
|
|
|
|
$config['where']['is_home'] = 1;
|
|
|
|
|
}
|
2025-08-14 20:08:57 +08:00
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
$config['where']['type'] = ['!=', 'permission'];
|
|
|
|
|
$config['order']['sort'] = 'asc';
|
|
|
|
|
$config['order']['id'] = 'asc';
|
|
|
|
|
break;
|
|
|
|
|
case 'menuTreeList':
|
2025-08-19 13:47:31 +08:00
|
|
|
$config['where']['system_id'] = ['in', [0, $p['system_id']]];
|
2025-08-14 20:08:57 +08:00
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
$config['order']['sort'] = 'asc';
|
|
|
|
|
$config['order']['id'] = 'asc';
|
|
|
|
|
break;
|
|
|
|
|
case 'index':
|
|
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
$config['order']['sort'] = 'asc';
|
|
|
|
|
$config['order']['id'] = 'asc';
|
|
|
|
|
break;
|
|
|
|
|
case 'show':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
$config['where']['status'] = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'store':
|
|
|
|
|
break;
|
|
|
|
|
case 'update':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 'destroy':
|
|
|
|
|
$config['where']['id'] = $p['id'];
|
|
|
|
|
break;
|
2025-07-01 19:39:13 +08:00
|
|
|
}
|
2025-08-14 20:08:57 +08:00
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function serviceMenuList(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'menuList');
|
|
|
|
|
$result = $this->logicListAll($config);
|
|
|
|
|
return $this->postProcess('MenuList', $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function serviceMenuTreeList(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'menuTreeList');
|
|
|
|
|
$result = (new ResourceLogic())->logicListAllResources($request, $config);
|
|
|
|
|
return $this->postProcess('MenuTreeList', $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-09-18 06:46:15 +00:00
|
|
|
protected function afterServiceDefault($data)
|
2025-08-14 20:08:57 +08:00
|
|
|
{
|
|
|
|
|
$data = Tree::buildMenuTreeWithReference($data->toArray());
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function afterServiceMenuList($data)
|
|
|
|
|
{
|
|
|
|
|
$data = Tree::buildMenuTree($data->toArray());
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function afterServiceMenuTreeList($data)
|
|
|
|
|
{
|
|
|
|
|
$data = Tree::buildMenuTree($data->toArray());
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function beforeServiceStore($data, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data['parent_id'] = $data['parent_id'] != '' ? $data['parent_id'] : 0;
|
|
|
|
|
$data['system_id'] = $data['system_id'] != '' ? $data['system_id'] : 0;
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function beforeServiceUpdate($data, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data['parent_id'] = $data['parent_id'] != '' ? $data['parent_id'] : 0;
|
|
|
|
|
$data['system_id'] = $data['system_id'] != '' ? $data['system_id'] : 0;
|
|
|
|
|
return $data;
|
2025-07-01 19:39:13 +08:00
|
|
|
}
|
|
|
|
|
}
|