| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- declare (strict_types = 1);
- namespace app\controller;
- use app\BaseController;
- use app\model\UserRoleModel;
- use app\model\UserModel;
- use think\facade\Config;
- use think\facade\Request;
- class UserRole extends BaseController
- {
- /**
- * 获取所有权限配置
- */
- public function getPermissions()
- {
- $permissions = Config::get('permission.permissions', []);
- return json_success($permissions, '获取权限配置成功');
- }
-
- /**
- * 获取角色列表
- */
- public function list()
- {
- $merchantId = $this->getMerchantId();
- $list = UserRoleModel::getRoleList($merchantId);
-
- return json_success([
- 'list' => $list,
- 'total' => count($list)
- ]);
- }
-
- /**
- * 获取角色详情
- */
- public function detail()
- {
- $id = $this->request->param('id', 0, 'intval');
- if (!$id) {
- return json_error([], '角色ID不能为空');
- }
-
- $merchantId = $this->getMerchantId();
-
- $role = UserRoleModel::getRoleById($id, $merchantId);
- if (!$role) {
- return json_error([], '角色不存在');
- }
-
- return json_success($role);
- }
-
- /**
- * 创建角色
- */
- public function create()
- {
- $data = Request::only(['role_name', 'privileges']);
-
- if (empty($data['role_name'])) {
- return json_error([], '角色名称不能为空');
- }
-
- if (empty($data['privileges']) || !is_array($data['privileges'])) {
- return json_error([], '权限配置不能为空');
- }
-
- $merchantId = $this->getMerchantId();
-
- if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId)) {
- return json_error([], '角色名称已存在');
- }
-
- $data['merchant_id'] = $merchantId;
-
- try {
- $role = UserRoleModel::createRole($data);
- return json_success(['id' => $role->id], '创建角色成功');
- } catch (\Exception $e) {
- return json_error([], '创建角色失败:' . $e->getMessage());
- }
- }
-
- /**
- * 更新角色
- */
- public function update()
- {
- $id = Request::param('id', 0, 'intval');
- if (!$id) {
- return json_error([], '角色ID不能为空');
- }
-
- $data = Request::only(['role_name', 'privileges']);
-
- if (empty($data['role_name'])) {
- return json_error([], '角色名称不能为空');
- }
-
- if (empty($data['privileges']) || !is_array($data['privileges'])) {
- return json_error([], '权限配置不能为空');
- }
-
- $merchantId = $this->getMerchantId();
-
- $role = UserRoleModel::getRoleById($id, $merchantId);
- if (!$role) {
- return json_error([], '角色不存在');
- }
-
- if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId, $id)) {
- return json_error([], '角色名称已存在');
- }
-
- try {
- UserRoleModel::updateRole($id, $merchantId, $data);
- return json_success([], '更新角色成功');
- } catch (\Exception $e) {
- return json_error([], '更新角色失败:' . $e->getMessage());
- }
- }
-
- /**
- * 删除角色
- */
- public function delete()
- {
- $id = Request::param('id', 0, 'intval');
- if (!$id) {
- return json_error([], '角色ID不能为空');
- }
-
- $merchantId = $this->getMerchantId();
-
- $role = UserRoleModel::getRoleById($id, $merchantId);
- if (!$role) {
- return json_error([], '角色不存在');
- }
-
- $superAdminRoleId = Config::get('permission.super_admin_role_id', 1);
- if ($id == $superAdminRoleId) {
- return json_error([], '不能删除超级管理员角色');
- }
-
- $userCount = UserModel::where('user_role', $id)
- ->where('merchant_id', $merchantId)
- ->count();
- if ($userCount > 0) {
- return json_error([], '该角色下还有用户,不能删除');
- }
-
- try {
- UserRoleModel::deleteRole($id, $merchantId);
- return json_success([], '删除角色成功');
- } catch (\Exception $e) {
- return json_error([], '删除角色失败:' . $e->getMessage());
- }
- }
-
- /**
- * 获取商户ID
- */
- private function getMerchantId()
- {
- $token = Request::cookie('auth_token');
- if (!$token) {
- throw new \Exception('未登录');
- }
-
- $payload = $this->verifyToken($token);
- if (!$payload) {
- throw new \Exception('登录已过期');
- }
-
- return $payload['merchant_id'] ?? 0;
- }
-
- /**
- * 验证Token
- */
- private function verifyToken($token)
- {
- try {
- $parts = explode('.', $token);
- if (count($parts) != 3) {
- return false;
- }
-
- $payload = json_decode(base64_decode($parts[1]), true);
-
- if ($payload['exp'] < time()) {
- return false;
- }
-
- return $payload;
- } catch (\Exception $e) {
- return false;
- }
- }
- }
|