| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- declare (strict_types = 1);
- namespace app\controller;
- use app\BaseController;
- use app\model\UserRoleModel;
- use app\model\UserModel;
- use app\validate\UserRoleValidate;
- use think\facade\Config;
- use think\facade\Request;
- use think\facade\Cookie;
- class UserRole extends BaseController
- {
- /**
- * 获取角色列表
- */
- public function list()
- {
- $userInfo = $this->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 "";
- }
-
- }
|