超级总管 - 管理员全模块返工
This commit is contained in:
@@ -23,7 +23,7 @@ class AddAdministratorController extends BaseController
|
||||
*/
|
||||
protected function chekAdminIsExist(string $account)
|
||||
{
|
||||
$exists = AdministratorModel::where('account', $account)->where('deleteTime', 0)->count() > 0;
|
||||
$exists = AdministratorModel::where('account', $account)->count() > 0;
|
||||
|
||||
if ($exists) {
|
||||
throw new \Exception('账号已存在', 400);
|
||||
@@ -44,6 +44,11 @@ class AddAdministratorController extends BaseController
|
||||
'name' => 'require|/\S+/',
|
||||
'password' => 'require|/\S+/',
|
||||
'permissionIds' => 'require|array',
|
||||
], [
|
||||
'account.require' => '账号不能为空',
|
||||
'name.require' => '姓名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
'permissionIds.require' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\administrator;
|
||||
|
||||
use app\superadmin\controller\BaseController;
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class DeleteAdministratorController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 删除管理员
|
||||
*
|
||||
* @param int $adminId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteAdmin(int $adminId): void
|
||||
{
|
||||
$admin = AdministratorModel::where('id', $adminId)->find();
|
||||
|
||||
if (!$admin) {
|
||||
throw new \Exception('管理员不存在', 404);
|
||||
}
|
||||
|
||||
if (!$admin->delete()) {
|
||||
throw new \Exception('管理员删除失败', 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除管理员权限
|
||||
*
|
||||
* @param int $adminId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deletePermission(int $adminId): void
|
||||
{
|
||||
$permission = AdministratorPermissionsModel::where('adminId', $adminId)->find();
|
||||
|
||||
if (!$permission->delete()) {
|
||||
throw new \Exception('管理员权限移除失败', 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除账号的限制条件
|
||||
*
|
||||
* @param int $adminId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function canNotDeleteSelf(int $adminId)
|
||||
{
|
||||
// 不能删除自己的账号
|
||||
if ($this->getAdminInfo('id') == $adminId) {
|
||||
throw new \Exception('不能删除自己的账号', 403);
|
||||
}
|
||||
|
||||
// 只有超级管理员(ID为1)可以删除管理员
|
||||
if ($this->getAdminInfo('id') != 1) {
|
||||
throw new \Exception('您没有权限删除管理员', 403);
|
||||
}
|
||||
|
||||
// 不能删除超级管理员账号
|
||||
if ($adminId == 1) {
|
||||
throw new \Exception('不能删除超级管理员账号', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require|regex:/^[1-9]\d*$/',
|
||||
], [
|
||||
'id.regex' => '非法请求',
|
||||
'id.require' => '非法请求',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除管理员
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only('id');
|
||||
$adminId = $params['id'];
|
||||
|
||||
$this->dataValidate($params)->canNotDeleteSelf($adminId);
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
$this->deleteAdmin($adminId);
|
||||
$this->deletePermission($adminId);
|
||||
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功',
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => $e->getCode(),
|
||||
'msg' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,11 +22,10 @@ class GetAdministratorDetailController extends BaseController
|
||||
{
|
||||
$admin = AdministratorModel::alias('a')
|
||||
->field(
|
||||
'a.id, a.account username, a.name, a.status, a.authId, a.createTime createdAt, a.lastLoginTime lastLogin, p.permissions'
|
||||
'a.id, a.account, a.name, a.status, a.authId, a.createTime createdAt, a.lastLoginTime lastLogin, p.permissions'
|
||||
)
|
||||
->leftJoin('administrator_permissions p', 'a.id = p.adminId')
|
||||
->where('a.id', $adminId)
|
||||
->where('a.deleteTime', 0)
|
||||
->find();
|
||||
|
||||
if (!$admin) {
|
||||
@@ -98,7 +97,7 @@ class GetAdministratorDetailController extends BaseController
|
||||
'data' => array_merge($admin->toArray(), [
|
||||
'roleName' => $roleName,
|
||||
'permissions' => $permissionIds,
|
||||
'lastLogin' => !empty($admin->lastLogin) ? date('Y-m-d H:i', $admin->lastLogin) : '从未登录',
|
||||
'lastLogin' => $admin->lastLogin ? date('Y-m-d H:i', $admin->lastLogin) : '从未登录',
|
||||
'createdAt' => date('Y-m-d H:i', $admin->createdAt),
|
||||
])
|
||||
]);
|
||||
|
||||
@@ -20,7 +20,7 @@ class GetAdministratorListController extends Controller
|
||||
*/
|
||||
protected function makeWhere(array $params = []): array
|
||||
{
|
||||
$where = [['deleteTime', '=', 0]];
|
||||
$where = [];
|
||||
|
||||
// 如果有搜索关键词
|
||||
if (!empty($keyword = $this->request->param('keyword/s', ''))) {
|
||||
@@ -117,7 +117,7 @@ class GetAdministratorListController extends Controller
|
||||
* @param int $authId 权限ID
|
||||
* @return array
|
||||
*/
|
||||
protected function getPermissions($authId): array
|
||||
protected function getPermissions(int $authId): array
|
||||
{
|
||||
$ids = $this->_getPermissions($authId);
|
||||
|
||||
@@ -143,10 +143,11 @@ class GetAdministratorListController extends Controller
|
||||
'id' => $item->id,
|
||||
'username' => $item->account,
|
||||
'name' => $item->name,
|
||||
'role' => $this->getRoleName($item->authId),
|
||||
'status' => $item->status,
|
||||
'createdAt' => date('Y-m-d H:i:s', $item->createdAt),
|
||||
'lastLogin' => !empty($item->lastLoginTime) ? date('Y-m-d H:i:s', $item->lastLoginTime) : '从未登录',
|
||||
'permissions' => $this->getPermissions($item->id)
|
||||
'role' => $this->getRoleName($item->authId),
|
||||
'permissions' => $this->getPermissions($item->id),
|
||||
];
|
||||
|
||||
array_push($result, $section);
|
||||
|
||||
@@ -22,7 +22,7 @@ class UpdateAdministratorController extends BaseController
|
||||
*/
|
||||
protected function udpateAdministrator(array $params): void
|
||||
{
|
||||
$admin = AdministratorModel::where('deleteTime', 0)->find($params['id']);
|
||||
$admin = AdministratorModel::find($params['id']);
|
||||
|
||||
if (!$admin) {
|
||||
throw new \Exception('管理员不存在', 404);
|
||||
@@ -48,10 +48,15 @@ class UpdateAdministratorController extends BaseController
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require|regex:/^[1-9]\d*$/',
|
||||
'account' => 'require|/\S+/', // 这里做账号使用
|
||||
'account' => 'require|/\S+/',
|
||||
'name' => 'require|/\S+/',
|
||||
'password' => '/\S+/',
|
||||
'permissionIds' => 'require|array',
|
||||
], [
|
||||
'id.require' => '缺少必要参数',
|
||||
'account.require' => '账号不能为空',
|
||||
'name.require' => '姓名不能为空',
|
||||
'permissionIds.require' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
|
||||
Reference in New Issue
Block a user