存客宝应用接口初始化
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\administrator;
|
||||
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class AddAdministratorController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 检查账号是否已存在
|
||||
*
|
||||
* @param string $account
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function chekAdminIsExist(string $account)
|
||||
{
|
||||
$exists = AdministratorModel::where('account', $account)->count() > 0;
|
||||
|
||||
if ($exists) {
|
||||
throw new \Exception('账号已存在', 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'account' => 'require|regex:^[a-zA-Z0-9]+$|/\S+/',
|
||||
'username' => 'require|/\S+/',
|
||||
'password' => 'require|/\S+/',
|
||||
'permissionIds' => 'require|array',
|
||||
], [
|
||||
'account.require' => '账号不能为空',
|
||||
'account.regex' => '账号只能用数字或者字母或者数字字母组合',
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
'permissionIds.require' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有权限修改
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function checkPermission(): self
|
||||
{
|
||||
if ($this->getAdminInfo('id') != AdministratorModel::MASTER_ID) {
|
||||
throw new \Exception('您没有权限添加管理员', 403);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存管理员权限
|
||||
*
|
||||
* @param int $adminId 管理员ID
|
||||
* @param array $permissionIds 权限ID数组
|
||||
* @return bool
|
||||
*/
|
||||
protected function savePermissions(int $adminId, array $permissionIds)
|
||||
{
|
||||
$record = AdministratorPermissionsModel::where('adminId', $adminId)->find();
|
||||
|
||||
$permissionData = [
|
||||
'ids' => is_array($permissionIds) ? implode(',', $permissionIds) : $permissionIds
|
||||
];
|
||||
|
||||
if ($record) {
|
||||
return $record->save([
|
||||
'permissions' => json_encode($permissionData),
|
||||
]);
|
||||
} else {
|
||||
return AdministratorPermissionsModel::create([
|
||||
'adminId' => $adminId,
|
||||
'permissions' => json_encode($permissionData),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加管理员信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return AdministratorModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function addAdministrator(array $params): AdministratorModel
|
||||
{
|
||||
$result = AdministratorModel::create(array_merge($params, ['password' => md5($params['password'])]));
|
||||
|
||||
if (!$result) {
|
||||
throw new \Exception('添加管理员失败', 401);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加管理员
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only(['account', 'username', 'password', 'permissionIds']);
|
||||
|
||||
$this->dataValidate($params);
|
||||
$this->checkPermission()->chekAdminIsExist($params['account']);
|
||||
|
||||
Db::startTrans();
|
||||
$admin = $this->addAdministrator($params);
|
||||
|
||||
// 保存权限
|
||||
if (!empty($params['permissionIds'])) {
|
||||
$this->savePermissions($admin->id, $params['permissionIds']);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?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 library\ResponseHelper;
|
||||
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') != AdministratorModel::MASTER_ID) {
|
||||
throw new \Exception('您没有权限删除管理员', 403);
|
||||
}
|
||||
|
||||
// 不能删除超级管理员账号
|
||||
if ($adminId == AdministratorModel::MASTER_ID) {
|
||||
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 ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\administrator;
|
||||
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class GetAdministratorDetailController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 查询管理员信息,关联权限表
|
||||
*
|
||||
* @param int $adminId
|
||||
* @return AdministratorModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getAdministrator(int $adminId): AdministratorModel
|
||||
{
|
||||
$admin = AdministratorModel::alias('a')
|
||||
->field([
|
||||
'a.id', 'a.account', 'a.username', 'a.status', 'a.authId', 'a.createTime createdAt', 'a.lastLoginTime lastLogin',
|
||||
'p.permissions'
|
||||
])
|
||||
->leftJoin('administrator_permissions p', 'a.id = p.adminId')
|
||||
->where('a.id', $adminId)
|
||||
->find();
|
||||
|
||||
if (!$admin) {
|
||||
throw new \Exception('管理员不存在', 404);
|
||||
}
|
||||
|
||||
return $admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析权限数据
|
||||
*
|
||||
* @param string|null $permission
|
||||
* @return array
|
||||
*/
|
||||
protected function parsePermissions(?string $permission): array
|
||||
{
|
||||
$permissionIds = [];
|
||||
|
||||
if (!empty($permission)) {
|
||||
$permissions = json_decode($permission, true);
|
||||
$permissions = is_array($permissions) ? $permissions : json_decode($permissions, true);
|
||||
|
||||
if (isset($permissions['ids'])) {
|
||||
$permissionIds = is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
|
||||
$permissionIds = array_map('intval', $permissionIds);
|
||||
}
|
||||
}
|
||||
|
||||
return $permissionIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权限ID获取角色名称
|
||||
*
|
||||
* @param int $authId
|
||||
* @return string
|
||||
*/
|
||||
protected function getRoleName($authId): string
|
||||
{
|
||||
switch ($authId) {
|
||||
case 1:
|
||||
return '超级管理员';
|
||||
case 2:
|
||||
return '项目管理员';
|
||||
case 3:
|
||||
return '客户管理员';
|
||||
default:
|
||||
return '普通管理员';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详细信息
|
||||
*
|
||||
* @param int $id 管理员ID
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index($id)
|
||||
{
|
||||
try {
|
||||
$admin = $this->getAdministrator($id);
|
||||
$roleName = $this->getRoleName($admin->authId);
|
||||
$permissionIds = $this->parsePermissions($admin->permissions);
|
||||
|
||||
return ResponseHelper::success(
|
||||
array_merge($admin->toArray(), [
|
||||
'roleName' => $roleName,
|
||||
'permissions' => $permissionIds,
|
||||
'lastLogin' => $admin->lastLogin ? date('Y-m-d H:i', $admin->lastLogin) : '从未登录',
|
||||
'createdAt' => date('Y-m-d H:i', $admin->createdAt),
|
||||
])
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
<?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 library\ResponseHelper;
|
||||
use think\Controller;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class GetAdministratorListController extends Controller
|
||||
{
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function makeWhere(array $params = []): array
|
||||
{
|
||||
$where = [];
|
||||
|
||||
// 如果有搜索关键词
|
||||
if (!empty($keyword = $this->request->param('keyword/s', ''))) {
|
||||
$where[] = ['account|username', 'like', "%{$keyword}%"];
|
||||
}
|
||||
|
||||
return array_merge($params, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员列表
|
||||
*
|
||||
* @param array $where 查询条件
|
||||
* @return \think\Paginator 分页对象
|
||||
*/
|
||||
protected function getAdministratorList(array $where): \think\Paginator
|
||||
{
|
||||
$query = AdministratorModel::alias('a')
|
||||
->field([
|
||||
'a.id', 'a.account', 'a.username', 'a.status', 'a.authId', 'a.createTime createdAt', 'a.lastLoginTime', 'a.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(int $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,
|
||||
'account' => $item->account,
|
||||
'username' => $item->username,
|
||||
'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) : '从未登录',
|
||||
'role' => $this->getRoleName($item->authId),
|
||||
'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 ResponseHelper::success(
|
||||
[
|
||||
'list' => $this->makeReturnedResult($result),
|
||||
'total' => $result->total(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\administrator;
|
||||
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class UpdateAdministratorController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 更新管理员信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function udpateAdministrator(array $params): void
|
||||
{
|
||||
$admin = AdministratorModel::find($params['id']);
|
||||
|
||||
if (!$admin) {
|
||||
throw new \Exception('管理员不存在', 404);
|
||||
}
|
||||
|
||||
if (!empty($params['password'])) {
|
||||
$params['password'] = md5($params['password']);
|
||||
}
|
||||
|
||||
if (!$admin->save($params)) {
|
||||
throw new \Exception('记录更新失败', 402);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require|regex:/^[1-9]\d*$/',
|
||||
'account' => 'require|regex:^[a-zA-Z0-9]+$|/\S+/',
|
||||
'username' => 'require|/\S+/',
|
||||
'password' => '/\S+/',
|
||||
'permissionIds' => 'array',
|
||||
], [
|
||||
'id.require' => '缺少必要参数',
|
||||
'account.require' => '账号不能为空',
|
||||
'account.regex' => '账号只能用数字或者字母或者数字字母组合',
|
||||
'username.require' => '用户名不能为空',
|
||||
'permissionIds.array' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有权限修改
|
||||
*
|
||||
* @param int $adminId
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
protected function checkPermission(int $adminId, array $params): self
|
||||
{
|
||||
$currentAdminId = $this->getAdminInfo('id');
|
||||
|
||||
if ($currentAdminId != AdministratorModel::MASTER_ID && $currentAdminId != $adminId) {
|
||||
throw new \Exception('您没有权限修改其他管理员', 403);
|
||||
}
|
||||
|
||||
if ($params['id'] != AdministratorModel::MASTER_ID && empty($params['permissionIds'])) {
|
||||
throw new \Exception('请至少分配一种权限', 403);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存管理员权限
|
||||
*
|
||||
* @param int $adminId
|
||||
* @param array $permissionIds
|
||||
* @return bool
|
||||
*/
|
||||
protected function savePermissions(int $adminId, array $permissionIds)
|
||||
{
|
||||
$record = AdministratorPermissionsModel::where('adminId', $adminId)->find();
|
||||
|
||||
$permissionData = [
|
||||
'ids' => is_array($permissionIds) ? implode(',', $permissionIds) : $permissionIds
|
||||
];
|
||||
|
||||
if ($record) {
|
||||
return $record->save([
|
||||
'permissions' => json_encode($permissionData),
|
||||
]);
|
||||
} else {
|
||||
return AdministratorPermissionsModel::create([
|
||||
'adminId' => $adminId,
|
||||
'permissions' => json_encode($permissionData),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新管理员信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only(['id', 'account', 'username', 'password', 'permissionIds']);
|
||||
|
||||
// 被修改的管理员id
|
||||
$adminId = $params['id'] ?? 0;
|
||||
|
||||
$this->dataValidate($params)->checkPermission($adminId, $params);
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
$this->udpateAdministrator($params);
|
||||
|
||||
// 如果当前是超级管理员(ID为1),并且修改的不是自己,则更新权限
|
||||
if ($this->getAdminInfo('id') == AdministratorModel::MASTER_ID
|
||||
&& $this->getAdminInfo('id') != $adminId
|
||||
&& !empty($params['permissionIds'])
|
||||
) {
|
||||
$this->savePermissions($adminId, $params['permissionIds']);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user