超级总管 - 管理员列表返工
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace app\superadmin\controller;
|
||||
|
||||
use app\superadmin\model\Administrator as AdminModel;
|
||||
use app\superadmin\model\AdministratorPermissions;
|
||||
use think\Controller;
|
||||
use app\superadmin\model\Administrator as AdminModel;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
<?php
|
||||
namespace app\superadmin\controller;
|
||||
|
||||
use think\Controller;
|
||||
use app\superadmin\model\Administrator;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* 管理员登录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
$account = $this->request->post('account');
|
||||
$password = $this->request->post('password');
|
||||
|
||||
if (empty($account) || empty($password)) {
|
||||
return json(['code' => 400, 'msg' => '账号和密码不能为空']);
|
||||
}
|
||||
|
||||
$admin = Administrator::login($account, $password);
|
||||
|
||||
if (!$admin) {
|
||||
return json(['code' => 401, 'msg' => '账号或密码错误']);
|
||||
}
|
||||
|
||||
// 更新登录信息
|
||||
$admin->lastLoginTime = time();
|
||||
$admin->lastLoginIp = $this->request->ip();
|
||||
$admin->save();
|
||||
|
||||
// 设置登录Cookie,有效期24小时
|
||||
cookie('admin_id', $admin->id, 86400);
|
||||
cookie('admin_token', $this->createToken($admin), 86400);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '登录成功',
|
||||
'data' => [
|
||||
'id' => $admin->id,
|
||||
'name' => $admin->name,
|
||||
'account' => $admin->account,
|
||||
'token' => cookie('admin_token')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建登录令牌
|
||||
* @param AdministratorController $admin
|
||||
* @return string
|
||||
*/
|
||||
private function createToken($admin)
|
||||
{
|
||||
$data = $admin->id . '|' . $admin->account;
|
||||
return md5($data . 'cunkebao_admin_secret');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\administrator;
|
||||
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
|
||||
use app\common\model\Menu as MenuModel;
|
||||
use think\Controller;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class GetAdministratorListController extends Controller
|
||||
{
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function makeWhere(array $params = []): array
|
||||
{
|
||||
$where = [['deleteTime', '=', 0]];
|
||||
|
||||
// 如果有搜索关键词
|
||||
if (!empty($keyword = $this->request->param('keyword/s', ''))) {
|
||||
$where[] = ['account|name', 'like', "%{$keyword}%"];
|
||||
}
|
||||
|
||||
return array_merge($params, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员列表
|
||||
*
|
||||
* @param array $where 查询条件
|
||||
* @param int $page 页码
|
||||
* @param int $limit 每页数量
|
||||
* @return \think\Paginator 分页对象
|
||||
*/
|
||||
protected function getAdministratorList(array $where): \think\Paginator
|
||||
{
|
||||
$query = AdministratorModel::alias('a')
|
||||
->field(
|
||||
'id, account, name, status, authId, createTime createdAt, lastLoginTime, lastLoginIp'
|
||||
);
|
||||
|
||||
foreach ($where as $key => $value) {
|
||||
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
|
||||
$query->whereExp('', $value[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$query->where($key, $value);
|
||||
}
|
||||
|
||||
return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权限ID获取角色名称
|
||||
*
|
||||
* @param int $authId 权限ID
|
||||
* @return string
|
||||
*/
|
||||
protected function getRoleName($authId): string
|
||||
{
|
||||
switch ($authId) {
|
||||
case 1:
|
||||
return '超级管理员';
|
||||
case 2:
|
||||
return '项目管理员';
|
||||
case 3:
|
||||
return '客户管理员';
|
||||
default:
|
||||
return '普通管理员';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员权限
|
||||
*
|
||||
* @param int $adminId
|
||||
* @return array
|
||||
*/
|
||||
protected function _getPermissions(int $adminId): array
|
||||
{
|
||||
$record = AdministratorPermissionsModel::where('adminId', $adminId)->find();
|
||||
|
||||
if (!$record || empty($record->permissions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$permissions = $record->permissions ? json_decode($record->permissions, true) : [];
|
||||
|
||||
if (isset($permissions['ids']) && !empty($permissions['ids'])) {
|
||||
return is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过菜单的id获取菜单的名字
|
||||
*
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
protected function getMenusNameByIds(array $ids): array
|
||||
{
|
||||
return MenuModel::whereIn('id', $ids)->column('title');
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权限ID获取权限列表
|
||||
*
|
||||
* @param int $authId 权限ID
|
||||
* @return array
|
||||
*/
|
||||
protected function getPermissions($authId): array
|
||||
{
|
||||
$ids = $this->_getPermissions($authId);
|
||||
|
||||
if ($ids) {
|
||||
return $this->getMenusNameByIds($ids);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建返回数据
|
||||
*
|
||||
* @param \think\Paginator $list
|
||||
* @return array
|
||||
*/
|
||||
protected function makeReturnedResult(\think\Paginator $list): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($list->items() as $item) {
|
||||
$section = [
|
||||
'id' => $item->id,
|
||||
'username' => $item->account,
|
||||
'name' => $item->name,
|
||||
'role' => $this->getRoleName($item->authId),
|
||||
'status' => $item->status,
|
||||
'lastLogin' => !empty($item->lastLoginTime) ? date('Y-m-d H:i:s', $item->lastLoginTime) : '从未登录',
|
||||
'permissions' => $this->getPermissions($item->id)
|
||||
];
|
||||
|
||||
array_push($result, $section);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员列表
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$where = $this->makeWhere();
|
||||
$result = $this->getAdministratorList($where);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'list' => $this->makeReturnedResult($result),
|
||||
'total' => $result->total(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user