Files
hyperf_service/app/Service/Admin/User/RoleService.php
2024-11-12 11:02:38 +08:00

216 lines
6.0 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
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\Constants\Common\RoleCode;
use App\Exception\ErrException;
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
{
/**
* @var AdminRole $adminRoleModel
*/
#[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[]
*/
private array $field = ['id as role_id', 'name', 'remark', 'status', 'create_time'];
/**
* 列表
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$list = $this->adminRoleModel->paginate($limit,$this->field)->toArray();
return $this->return->success('success',['list' => $list]);
}
/**
* 添加角色
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function add(): array
{
$name = $this->request->input('role_name');
if ($this->adminRoleModel->getInfoByName($name)) throw new ErrException('角色已存在');
Db::beginTransaction();
try {
$model = new AdminRole();
$model->name = $name;
$model->status = $this->request->input('role_status', 1);
$model->remark = $this->request->input('role_remark', '');
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 ErrException($e->getMessage());
}
return $this->return->success();
}
/**
* 修改角色
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function edit(): array
{
$id = (int)$this->request->input('role_id');
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new ErrException('角色不存在');
$name = $this->request->input('role_name');
$oldInfo = $this->adminRoleModel->getInfoByName($name);
if ($oldInfo->id != $id) throw new ErrException('角色已存在');
Db::beginTransaction();
try {
$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 (!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 ErrException($e->getMessage());
}
return $this->return->success();
}
/**
* 修改状态
* @return array
* @throws Exception
*/
public function changeStatus(): array
{
$id = (int)$this->request->input('role_id');
if ($id == RoleCode::SUPER_ADMIN) throw new ErrException('超级管理员不可关闭');
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new ErrException('角色不存在');
$info->status = $this->request->input('role_status', 0);
if (!$info->save()) throw new ErrException('修改失败');
return $this->return->success();
}
/**
* 详情角色
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function details(): array
{
$roleId = $this->request->input('role_id');
$res = $this->adminRoleModel->where('id',$roleId)->first($this->field);
if (!$res) throw new ErrException('角色不存在');
$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'],
]);
}
}