request->userInfo; $list = UserRoleModel::getRoleList($userInfo['merchant_id']); return json_success([ 'list' => $list, 'total' => count($list) ], '获取角色列表成功'); } /** * 获取角色详情 */ public function detail() { $userInfo = $this->request->userInfo; $id = Request::param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } return json_success($role, '获取角色详情成功'); } /** * 创建角色 */ public function create() { $userInfo = $this->request->userInfo; // 获取请求数据 $data = Request::only(['role_name', 'privileges']); $data['merchant_id'] = $userInfo['merchant_id']; // 使用验证器进行字段验证 try { $validate = validate(UserRoleValidate::class); if (!$validate->scene('create')->check($data)) { return json_error([], $validate->getError()); } } catch (\think\exception\ValidateException $e) { return json_error([], $e->getMessage()); } // 额外的业务逻辑验证 // 检查角色名称是否已存在 if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'])) { return json_error([], '角色名称已存在'); } try { $role = UserRoleModel::createRole($data); return json_success(['id' => $role->id], '创建角色成功'); } catch (\Exception $e) { return json_error([], '创建角色失败:' . $e->getMessage()); } } /** * 更新角色 */ public function update() { $userInfo = $this->request->userInfo; $id = Request::post('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } // 获取请求中提供的所有可更新字段 $requestData = Request::post(); $allowedFields = ['role_name', 'privileges']; $updateData = []; // 只处理请求中存在且允许更新的字段 foreach ($allowedFields as $field) { if (array_key_exists($field, $requestData)) { $updateData[$field] = $requestData[$field]; } } if (empty($updateData)) { return json_error([], '没有要更新的数据'); } // 使用验证器进行字段验证 $validate = new UserRoleValidate(); // 只验证传入的字段 if (!$validate->only(array_keys($updateData))->check($updateData)) { return json_error([], $validate->getError()); } // 额外的业务逻辑验证 // 检查角色名称是否重复 if (isset($updateData['role_name'])) { if (UserRoleModel::checkRoleExists($updateData['role_name'], $userInfo['merchant_id'], $id)) { return json_error([], '角色名称已存在'); } } try { UserRoleModel::updateRole($id, $userInfo['merchant_id'], $updateData); return json_success([], '更新角色成功'); } catch (\Exception $e) { return json_error([], '更新角色失败:' . $e->getMessage()); } } /** * 删除角色 */ public function delete() { $userInfo = $this->request->userInfo; $id = Request::param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } $userCount = UserModel::where('user_role', $id) ->where('merchant_id', $userInfo['merchant_id']) ->count(); if ($userCount > 0) { return json_error([], '该角色下还有用户,不能删除'); } try { UserRoleModel::deleteRole($id, $userInfo['merchant_id']); return json_success([], '删除角色成功'); } catch (\Exception $e) { return json_error([], '删除角色失败:' . $e->getMessage()); } } /** * 验证输入数据 */ protected function validateInput(array $data, $scene = '') { $validate = new UserRoleValidate(); // 执行场景验证 if (!$validate->scene($scene)->check($data)) { return $validate->getError(); } return ""; } }