feat : role
This commit is contained in:
@@ -118,7 +118,7 @@ class RoleMenuService extends BaseService
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$menuId = $this->request->input('menu_id');
|
||||
$menuId = (int)$this->request->input('menu_id');
|
||||
$url = $this->request->input('menu_url');
|
||||
$oldUrlInfo = $this->adminMenuModel->getMenuByUrl($url);
|
||||
$menuInfo = $this->adminMenuModel->where('id',$menuId)->first();
|
||||
@@ -213,32 +213,40 @@ class RoleMenuService extends BaseService
|
||||
if (!$res) throw new AdminException('路由不存在');
|
||||
$res = $res->toArray();
|
||||
|
||||
//闭包函数获取子集
|
||||
$res = function() use($res,$menuId) {
|
||||
$children = $this->adminMenuModel->where('parent_id',$menuId)->select(['type','value','title','id'])->get();
|
||||
$res['permissionList'] = [];
|
||||
if (!empty($children)) {
|
||||
$children = $children->toArray();
|
||||
if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
foreach ($children as $one)
|
||||
{
|
||||
$res['permissionList'][] = [
|
||||
'id' => $one['id'],
|
||||
'label' => $one['title'],
|
||||
'value' => $one['value'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
};
|
||||
//获取子集
|
||||
$res = $this->getChildren($res,$menuId);
|
||||
|
||||
return $this->return->success('success',['info' => $res]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $res
|
||||
* @param $menuId
|
||||
* @return array
|
||||
*/
|
||||
private function getChildren(&$res,$menuId): array
|
||||
{
|
||||
$children = $this->adminMenuModel->where('parent_id',$menuId)->get(['type','value','title','id']);
|
||||
$res['permission_list'] = [];
|
||||
if (!empty($children)) {
|
||||
$children = $children->toArray();
|
||||
if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
foreach ($children as $one)
|
||||
{
|
||||
$res['permission_list'][] = [
|
||||
'id' => $one['id'],
|
||||
'label' => $one['title'],
|
||||
'value' => $one['value'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建缓存
|
||||
* @return array
|
||||
|
||||
@@ -10,12 +10,18 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\User;
|
||||
|
||||
use App\Cache\Redis\Admin\MenuCache;
|
||||
use App\Cache\Redis\Admin\RoleCache;
|
||||
use App\Constants\Admin\AuthCode;
|
||||
use App\Exception\AdminException;
|
||||
use App\Model\AdminRole;
|
||||
use App\Model\AdminRoleMenu;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Exception;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class RoleService extends BaseService
|
||||
{
|
||||
@@ -25,6 +31,24 @@ class RoleService extends BaseService
|
||||
#[Inject]
|
||||
protected AdminRole $adminRoleModel;
|
||||
|
||||
/**
|
||||
* @var AdminRoleMenu
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminRoleMenu $adminRoleMenuModel;
|
||||
|
||||
/**
|
||||
* @var RoleCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected RoleCache $roleCache;
|
||||
|
||||
/**
|
||||
* @var MenuCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected MenuCache $menuCache;
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
* @var array|string[]
|
||||
@@ -37,7 +61,7 @@ class RoleService extends BaseService
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$limit = $this->request->input('limit', 10);
|
||||
$limit = (int)$this->request->input('limit', 10);
|
||||
|
||||
$list = $this->adminRoleModel->paginate($limit,$this->field)->toArray();
|
||||
|
||||
@@ -45,9 +69,15 @@ class RoleService extends BaseService
|
||||
}
|
||||
|
||||
|
||||
public function add()
|
||||
/**
|
||||
* 添加角色
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function add(): array
|
||||
{
|
||||
$name = $this->request->input('name');
|
||||
$name = $this->request->input('role_name');
|
||||
|
||||
if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在');
|
||||
|
||||
@@ -56,17 +86,29 @@ class RoleService extends BaseService
|
||||
$model = new AdminRole();
|
||||
|
||||
$model->name = $name;
|
||||
$model->status = $this->request->input('status', 1);
|
||||
$model->remark = $this->request->input('remark', '');
|
||||
$model->status = $this->request->input('role_status', 1);
|
||||
$model->remark = $this->request->input('role_remark', '');
|
||||
|
||||
if (!$model->save()) throw new Exception('添加失败');
|
||||
|
||||
//todo 添加角色权限
|
||||
$menuIdArr = explode(',', $this->request->input('menu_ids'));
|
||||
if (!$model->save()) throw new Exception('添加失败-角色错误');
|
||||
|
||||
//添加角色权限
|
||||
if (!empty($this->request->input('menu_ids')))
|
||||
{
|
||||
$menuIdArr = explode(',', $this->request->input('menu_ids'));
|
||||
$insertArr = [];
|
||||
foreach ($menuIdArr as $menuId) {
|
||||
$insertArr[] = [
|
||||
'role_id' => $model->id,
|
||||
'menu_id' => $menuId,
|
||||
];
|
||||
}
|
||||
|
||||
if (!(new AdminRoleMenu)->insert($insertArr)) throw new Exception('添加失败-权限组错误');
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
|
||||
$this->roleCache->getRoleCache($model->id);
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
throw new AdminException($e->getMessage());
|
||||
@@ -76,26 +118,50 @@ class RoleService extends BaseService
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
* 修改角色
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$id = $this->request->input('id');
|
||||
$id = (int)$this->request->input('role_id');
|
||||
|
||||
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在');
|
||||
|
||||
$name = $this->request->input('role_name');
|
||||
$oldInfo = $this->adminRoleModel->getInfoByName($name);
|
||||
if ($oldInfo->id != $id) throw new AdminException('角色已存在');
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$info->name = $this->request->input('name');
|
||||
$info->status = $this->request->input('status', 1);
|
||||
$info->remark = $this->request->input('remark', '');
|
||||
$info->name = $name;
|
||||
$info->status = $this->request->input('role_status', 1);
|
||||
$info->remark = $this->request->input('role_remark', '');
|
||||
|
||||
if (!$info->save()) throw new Exception('修改失败');
|
||||
if (!$info->save()) throw new Exception('修改失败-角色错误');
|
||||
|
||||
//todo 删除权限 添加角色权限
|
||||
//删除权限 添加角色权限
|
||||
if (!empty($this->request->input('menu_ids'))) {
|
||||
//todo 判断数据一致 是否修改
|
||||
$this->adminRoleMenuModel->where('role_id', $info->id)->delete();
|
||||
|
||||
$menuIdArr = explode(',', $this->request->input('menu_ids'));
|
||||
$insertArr = [];
|
||||
foreach ($menuIdArr as $menuId) {
|
||||
$insertArr[] = [
|
||||
'role_id' => $info->id,
|
||||
'menu_id' => $menuId,
|
||||
];
|
||||
}
|
||||
|
||||
if (!(new AdminRoleMenu)->insert($insertArr)) throw new Exception('修改失败-权限组错误');
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
|
||||
$this->roleCache->delRoleCache($info->id);
|
||||
$this->roleCache->getRoleCache($info->id);
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
throw new AdminException($e->getMessage());
|
||||
@@ -111,20 +177,24 @@ class RoleService extends BaseService
|
||||
*/
|
||||
public function changeStatus(): array
|
||||
{
|
||||
$id = $this->request->input('id');
|
||||
$id = (int)$this->request->input('role_id');
|
||||
if ($id == AuthCode::SUPERADMIN) throw new AdminException('超级管理员不可关闭');
|
||||
|
||||
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在');
|
||||
|
||||
$info->status = $this->request->input('status', 0);
|
||||
$info->status = $this->request->input('role_status', 0);
|
||||
|
||||
if (!$info->save()) throw new Exception('修改失败');
|
||||
if (!$info->save()) throw new AdminException('修改失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
* 详情角色
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \RedisException
|
||||
*/
|
||||
public function details(): array
|
||||
{
|
||||
@@ -132,6 +202,14 @@ class RoleService extends BaseService
|
||||
$res = $this->adminRoleModel->where('id',$roleId)->first($this->field);
|
||||
if (!$res) throw new AdminException('角色不存在');
|
||||
|
||||
return $this->return->success('success',['info' => $res->toArray()]);
|
||||
$data = $this->roleCache->getRoleCache((int)$roleId);
|
||||
|
||||
$menuList = $this->menuCache->getMenu();
|
||||
|
||||
return $this->return->success('success',[
|
||||
'info' => $res->toArray(),
|
||||
'role_arr' => $data['role_arr'],
|
||||
'role_list' => $data['role_list'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user