Files
nl-mall-api/app/Service/RoleService.php
2025-05-12 16:13:46 +08:00

162 lines
4.2 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Service;
use App\BaseApp\BaseService;
use App\Models\RoleMenuRelationModel;
use App\Models\RoleModel;
use App\Service\common\RedisService;
use Exception;
use Illuminate\Support\Facades\DB;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 角色服务类
*/
class RoleService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->model = RoleModel::class;
$this->selectField = ['id', 'name', 'value', 'pid', 'desc', 'created_at'];
$this->queryField = ['name' => 'like', 'value' => 'like', 'pid' => '='];
$this->orderBy = ['name' => 'id', 'sort' => 'asc'];
}
/**
* 获取列表
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
return $this->getPageList();
}
/**
* 获取下拉列表
* @return mixed
*/
public function option(): mixed
{
return $this->getOption();
}
/**
* 根据角色ID获取菜单ID
* @param $roleId
* @return mixed
* @throws Exception
*/
public function getMenuIdsByRoleIds($roleId): mixed
{
if (empty($roleId)) $this->utils->errorThrow('请选择角色');
return RoleMenuRelationModel::where('role_id', $roleId)->pluck('menu_id');
}
/**
* 保存角色菜单关系
* @param $roleId
* @param $menuIds
* @return true
* @throws Exception
*/
public function saveRoleMenu($roleId, $menuIds): true
{
$menus = array_unique($menuIds);
// 查询角色当前绑定的菜单
$bindResources = RoleMenuRelationModel::where('role_id', $roleId)->pluck('menu_id');
// 开启事务
DB::beginTransaction();
try {
// 提取查询到的菜单ID
$existingResourceIds = $bindResources->toArray();
// 传值中不存在的菜单ID从数据库中删除
$menusToDelete = array_diff($existingResourceIds, $menus);
if (!empty($menusToDelete)) {
$delBindResources = RoleMenuRelationModel::where('role_id', $roleId)->whereIn('menu_id', array_values($menusToDelete))->delete();
if (!$delBindResources) {
$this->utils->errorThrow('更新失败!');
}
}
// 传值中存在但数据库中不存在的菜单ID新增到数据库
$menusToAdd = array_diff($menus, $existingResourceIds);
$insertData = [];
foreach ($menusToAdd as $menusId) {
$insertData[] = [
'role_id' => $roleId,
'menu_id' => $menusId,
'created_at' => time()
];
}
$userRoleBinding = RoleMenuRelationModel::insert($insertData);
if (!$userRoleBinding) {
$this->utils->errorThrow('更新失败!');
}
DB::commit();
RedisService::getInstance()->init(config('nl.redis.menu_key'))->del($roleId);
} catch (\Exception $e) {
DB::rollBack();
$this->utils->errorThrow($e->getMessage());
}
return true;
}
/**
* 获取数据详情
* @param $id
* @return mixed
* @throws Exception
*/
public function detail($id): mixed
{
return $this->getDetail($id);
}
/**
* 新增数据
* @param $params
* @return mixed
*/
public function create($params): mixed
{
return $this->insert($params);
}
/**
* 编辑数据
* @param $id
* @param $params
* @return mixed
* @throws Exception
*/
public function update($id, $params): mixed
{
return $this->save($id, $params);
}
/**
* 删除数据
* @param $id
* @return mixed|true
* @throws Exception
*/
public function delete($id): mixed
{
if (in_array($id, [1, 2])) {
$this->utils->errorThrow('管理员角色禁止删除');
}
return $this->del($id);
}
}