264 lines
8.1 KiB
PHP
264 lines
8.1 KiB
PHP
<?php
|
|
/*
|
|
* @Author: YwxApp <ywx@ywxapp.cn>
|
|
* @Date: 2026-04-29 02:32:33
|
|
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
|
* @LastEditTime: 2026-07-21 22:26:23
|
|
* @Description:
|
|
* @FilePath: \ywxapp_dev\app\backend\controller\Admin.php
|
|
* @CustomString: Copyright (c) 2026 YwxApp
|
|
*/
|
|
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\backend\controller;
|
|
|
|
use app\backend\validate\Admin as AdminValidate;
|
|
use think\db\exception\DbException;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
use think\Request;
|
|
use ywxapp\model\BackendAdmin as AdminsModel;
|
|
use ywxapp\model\BackendRole as RoleModel;
|
|
use ywxapp\controller\BackendBase;
|
|
|
|
/**
|
|
* Backend 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Admin extends BackendBase
|
|
{
|
|
/**
|
|
* Summary of needLogin
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
|
|
/**
|
|
* Summary of needRight
|
|
* @var array
|
|
*/
|
|
protected $noNeedVerify = [];
|
|
|
|
/**
|
|
* 控制器初始化 initialize
|
|
* @return void
|
|
*/
|
|
protected function initialize()
|
|
{}
|
|
|
|
/**
|
|
* 获取用户列表
|
|
*/
|
|
public function index()
|
|
{
|
|
$page = $this->request->param('page/d', 1);
|
|
$limit = $this->request->param('limit/d', 15);
|
|
if ($this->request->isAjax()) {
|
|
$admins = AdminsModel::with(['roles' =>
|
|
function($query) {
|
|
$query->field('id,name,status');
|
|
}])
|
|
->field('id,account,nickname,email,mobile,status,create_at,update_at')
|
|
->page($page, $limit)
|
|
->select();
|
|
$this->result->success($admins);
|
|
}
|
|
View::assign('title', '登录');
|
|
return View::fetch();
|
|
}
|
|
|
|
/**
|
|
* 数据创建
|
|
*
|
|
* @param Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$roles = RoleModel::field('id,name,status')->where('status', 1)->select();
|
|
$this->result->success(['roles' => $roles]);
|
|
}
|
|
View::assign('title', '登录');
|
|
return View::fetch('admin/index');
|
|
}
|
|
|
|
/**
|
|
* 数据保存
|
|
*
|
|
* @param Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function save()
|
|
{
|
|
$params = $this->request->only(['account', 'nickname', 'password', 'confirmpass', 'mobile', 'email', 'role_ids', 'status'], 'post');
|
|
$roleIds = $this->request->param('role_ids/a', []);
|
|
try {
|
|
validate(AdminValidate::class)->check($params);
|
|
Db::transaction(function () use ($params, $roleIds) {
|
|
$data = AdminsModel::create($params);
|
|
$data->roles()->saveAll($roleIds);
|
|
});
|
|
$this->result->success();
|
|
} catch (ValidateException $e) {
|
|
$this->result->error($e->getMessage());
|
|
} catch (DbException $e) {
|
|
$this->result->error($e->getMessage(), 2);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 数据编辑
|
|
*
|
|
* @param Request $request
|
|
* @param int|null $ids
|
|
* @return \think\Response
|
|
*/
|
|
public function edit( $id = null)
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$data = AdminsModel::with(['roles' =>
|
|
function($query) {
|
|
$query->field('id,name,status');
|
|
}])
|
|
->field('id,account,nickname,email,mobile,status')
|
|
->where('id', $id)
|
|
->find();
|
|
$roles = RoleModel::field('id,name,status')->where('status', 1)->select();
|
|
$this->result->success(['info' => $data, 'roles' => $roles]);
|
|
}
|
|
View::assign('title', '登录');
|
|
return View::fetch('admin/index');
|
|
}
|
|
|
|
/**
|
|
* 数据更新
|
|
*
|
|
* @param Request $request
|
|
* @param int|null $ids
|
|
* @return \think\Response
|
|
*/
|
|
public function update()
|
|
{
|
|
$data = $this->request->only(['id', 'account', 'nickname', 'password', 'confirmpass', 'mobile', 'email', 'status','role_ids'], 'put');
|
|
$roleIds = $this->request->put('role_ids/a', []);
|
|
$info = AdminsModel::find($data['id']);
|
|
if (! $info) {
|
|
$this->result->error('用户不存在',404);
|
|
}
|
|
// 超级管理员账号(id=config superAdmin)必须始终保留超级管理员角色,禁止被降权
|
|
if ((int)$data['id'] === (int)config('ywxapp.superAdmin', 1)
|
|
&& !in_array((int)config('ywxapp.superAdmin', 1), array_map('intval', $roleIds), true)) {
|
|
$this->result->error('超级管理员必须保留超级管理员角色', 403);
|
|
}
|
|
$info->save($data);
|
|
$info->roles()->sync($roleIds);
|
|
$this->result->success($info,"用户更新成功");
|
|
}
|
|
|
|
/**
|
|
* 获取详情
|
|
*/
|
|
public function read()
|
|
{
|
|
$info = AdminsModel::with(['roles', 'permissions'])
|
|
->find($id);
|
|
if (! $info) {
|
|
$this->result->error('用户不存在',404);
|
|
}
|
|
$this->result->success($info,"用户更新成功");
|
|
}
|
|
|
|
/**
|
|
* 数据回收站
|
|
*
|
|
* @param Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function recyclebin()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$size = $this->request->param('size', 15);
|
|
if ($this->request->isAjax()) {
|
|
$admins = AdminsModel::onlyTrashed()->with(['roles'])
|
|
->paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($admins->items());
|
|
}
|
|
View::assign('title', '回收站');
|
|
return View::fetch('admin/index');
|
|
}
|
|
|
|
/**
|
|
* 数据删除
|
|
*/
|
|
public function delete()
|
|
{
|
|
if ($this->request->isAjax() && $this->request->isDelete()) {
|
|
$ids = $this->request->param('ids', '');
|
|
$force = $this->request->param('force', false);
|
|
if (empty($ids)) {
|
|
$this->result->error('请选择要删除的数据');
|
|
}
|
|
$idsArray = explode(',', $ids);
|
|
// 禁止删除超级管理员账号
|
|
if (in_array((int)config('ywxapp.superAdmin', 1), array_map('intval', $idsArray), true)) {
|
|
$this->result->error('超级管理员账号不可删除', 403);
|
|
}
|
|
try {
|
|
Db::transaction(function () use ($idsArray, $force) {
|
|
if ($force) {
|
|
AdminsModel::onlyTrashed()->whereIn('id', $idsArray)->select()->each(function ($item) {
|
|
$item->roles()->detach();
|
|
$item->force()->delete();
|
|
});
|
|
} else {
|
|
AdminsModel::destroy($idsArray);
|
|
}
|
|
});
|
|
$this->result->success();
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e; // 不要 return,不要吞掉!
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('批量删除管理员失败', [
|
|
'exception' => $e->__toString(),
|
|
'admin_ids' => $idsArray,
|
|
]);
|
|
$this->result->error('删除失败: ' . ($e->getMessage() ?: $e->__toString()), 500);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function restore( $ids = '')
|
|
{
|
|
if ($this->request->isAjax() && $this->request->isPost()) {
|
|
$ids = $this->request->param('ids', '');
|
|
if (empty($ids)) {
|
|
$this->result->error('请选择要恢复的数据');
|
|
}
|
|
$idsArray = explode(',', $ids);
|
|
Db::startTrans();
|
|
try {
|
|
AdminsModel::withTrashed()
|
|
->where('id', 'in', $idsArray)
|
|
->select()
|
|
->each(function ($item) {
|
|
$item->restore();
|
|
});
|
|
Db::commit();
|
|
$this->result->success('恢复成功');
|
|
} catch (DbException $e) {
|
|
Db::rollback();
|
|
$this->result->error('恢复失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|