|
@@ -8,6 +8,7 @@ use app\model\UserRoleModel;
|
|
|
use app\model\UserModel;
|
|
use app\model\UserModel;
|
|
|
use think\facade\Config;
|
|
use think\facade\Config;
|
|
|
use think\facade\Request;
|
|
use think\facade\Request;
|
|
|
|
|
+use think\facade\Cookie;
|
|
|
|
|
|
|
|
class UserRole extends BaseController
|
|
class UserRole extends BaseController
|
|
|
{
|
|
{
|
|
@@ -16,6 +17,15 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function getPermissions()
|
|
public function getPermissions()
|
|
|
{
|
|
{
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'permissions')) {
|
|
|
|
|
+ return json_error([], '没有查看权限配置的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$permissions = Config::get('permission.permissions', []);
|
|
$permissions = Config::get('permission.permissions', []);
|
|
|
return json_success($permissions, '获取权限配置成功');
|
|
return json_success($permissions, '获取权限配置成功');
|
|
|
}
|
|
}
|
|
@@ -25,13 +35,21 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function list()
|
|
public function list()
|
|
|
{
|
|
{
|
|
|
- $merchantId = $this->getMerchantId();
|
|
|
|
|
- $list = UserRoleModel::getRoleList($merchantId);
|
|
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'list')) {
|
|
|
|
|
+ return json_error([], '没有查看角色列表的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $list = UserRoleModel::getRoleList($loginInfo['merchant_id']);
|
|
|
|
|
|
|
|
return json_success([
|
|
return json_success([
|
|
|
'list' => $list,
|
|
'list' => $list,
|
|
|
'total' => count($list)
|
|
'total' => count($list)
|
|
|
- ]);
|
|
|
|
|
|
|
+ ], '获取角色列表成功');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -39,19 +57,26 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function detail()
|
|
public function detail()
|
|
|
{
|
|
{
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'detail')) {
|
|
|
|
|
+ return json_error([], '没有查看角色详情的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$id = $this->request->param('id', 0, 'intval');
|
|
$id = $this->request->param('id', 0, 'intval');
|
|
|
if (!$id) {
|
|
if (!$id) {
|
|
|
return json_error([], '角色ID不能为空');
|
|
return json_error([], '角色ID不能为空');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $merchantId = $this->getMerchantId();
|
|
|
|
|
-
|
|
|
|
|
- $role = UserRoleModel::getRoleById($id, $merchantId);
|
|
|
|
|
|
|
+ $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']);
|
|
|
if (!$role) {
|
|
if (!$role) {
|
|
|
return json_error([], '角色不存在');
|
|
return json_error([], '角色不存在');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return json_success($role);
|
|
|
|
|
|
|
+ return json_success($role, '获取角色详情成功');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -59,6 +84,15 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function create()
|
|
public function create()
|
|
|
{
|
|
{
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'create')) {
|
|
|
|
|
+ return json_error([], '没有创建角色的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$data = Request::only(['role_name', 'privileges']);
|
|
$data = Request::only(['role_name', 'privileges']);
|
|
|
|
|
|
|
|
if (empty($data['role_name'])) {
|
|
if (empty($data['role_name'])) {
|
|
@@ -69,13 +103,11 @@ class UserRole extends BaseController
|
|
|
return json_error([], '权限配置不能为空');
|
|
return json_error([], '权限配置不能为空');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $merchantId = $this->getMerchantId();
|
|
|
|
|
-
|
|
|
|
|
- if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId)) {
|
|
|
|
|
|
|
+ if (UserRoleModel::checkRoleExists($data['role_name'], $loginInfo['merchant_id'])) {
|
|
|
return json_error([], '角色名称已存在');
|
|
return json_error([], '角色名称已存在');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $data['merchant_id'] = $merchantId;
|
|
|
|
|
|
|
+ $data['merchant_id'] = $loginInfo['merchant_id'];
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
$role = UserRoleModel::createRole($data);
|
|
$role = UserRoleModel::createRole($data);
|
|
@@ -90,6 +122,15 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function update()
|
|
public function update()
|
|
|
{
|
|
{
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'update')) {
|
|
|
|
|
+ return json_error([], '没有编辑角色的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$id = Request::param('id', 0, 'intval');
|
|
$id = Request::param('id', 0, 'intval');
|
|
|
if (!$id) {
|
|
if (!$id) {
|
|
|
return json_error([], '角色ID不能为空');
|
|
return json_error([], '角色ID不能为空');
|
|
@@ -105,19 +146,17 @@ class UserRole extends BaseController
|
|
|
return json_error([], '权限配置不能为空');
|
|
return json_error([], '权限配置不能为空');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $merchantId = $this->getMerchantId();
|
|
|
|
|
-
|
|
|
|
|
- $role = UserRoleModel::getRoleById($id, $merchantId);
|
|
|
|
|
|
|
+ $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']);
|
|
|
if (!$role) {
|
|
if (!$role) {
|
|
|
return json_error([], '角色不存在');
|
|
return json_error([], '角色不存在');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId, $id)) {
|
|
|
|
|
|
|
+ if (UserRoleModel::checkRoleExists($data['role_name'], $loginInfo['merchant_id'], $id)) {
|
|
|
return json_error([], '角色名称已存在');
|
|
return json_error([], '角色名称已存在');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- UserRoleModel::updateRole($id, $merchantId, $data);
|
|
|
|
|
|
|
+ UserRoleModel::updateRole($id, $loginInfo['merchant_id'], $data);
|
|
|
return json_success([], '更新角色成功');
|
|
return json_success([], '更新角色成功');
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
return json_error([], '更新角色失败:' . $e->getMessage());
|
|
return json_error([], '更新角色失败:' . $e->getMessage());
|
|
@@ -129,14 +168,21 @@ class UserRole extends BaseController
|
|
|
*/
|
|
*/
|
|
|
public function delete()
|
|
public function delete()
|
|
|
{
|
|
{
|
|
|
|
|
+ $loginInfo = checkUserLogin();
|
|
|
|
|
+ if (!$loginInfo) {
|
|
|
|
|
+ return json_error([], '请先登录');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!checkPermission($loginInfo, 'role', 'delete')) {
|
|
|
|
|
+ return json_error([], '没有删除角色的权限');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$id = Request::param('id', 0, 'intval');
|
|
$id = Request::param('id', 0, 'intval');
|
|
|
if (!$id) {
|
|
if (!$id) {
|
|
|
return json_error([], '角色ID不能为空');
|
|
return json_error([], '角色ID不能为空');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $merchantId = $this->getMerchantId();
|
|
|
|
|
-
|
|
|
|
|
- $role = UserRoleModel::getRoleById($id, $merchantId);
|
|
|
|
|
|
|
+ $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']);
|
|
|
if (!$role) {
|
|
if (!$role) {
|
|
|
return json_error([], '角色不存在');
|
|
return json_error([], '角色不存在');
|
|
|
}
|
|
}
|
|
@@ -147,58 +193,18 @@ class UserRole extends BaseController
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$userCount = UserModel::where('user_role', $id)
|
|
$userCount = UserModel::where('user_role', $id)
|
|
|
- ->where('merchant_id', $merchantId)
|
|
|
|
|
|
|
+ ->where('merchant_id', $loginInfo['merchant_id'])
|
|
|
->count();
|
|
->count();
|
|
|
if ($userCount > 0) {
|
|
if ($userCount > 0) {
|
|
|
return json_error([], '该角色下还有用户,不能删除');
|
|
return json_error([], '该角色下还有用户,不能删除');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- UserRoleModel::deleteRole($id, $merchantId);
|
|
|
|
|
|
|
+ UserRoleModel::deleteRole($id, $loginInfo['merchant_id']);
|
|
|
return json_success([], '删除角色成功');
|
|
return json_success([], '删除角色成功');
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
return json_error([], '删除角色失败:' . $e->getMessage());
|
|
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;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|