chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
/*
|
||||
* @Author: YwxApp <ywx@ywxapp.cn>
|
||||
* @Date: 2026-04-29 02:32:33
|
||||
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
||||
* @LastEditTime: 2026-07-21 23:20:59
|
||||
* @Description:
|
||||
* @FilePath: \ywxapp_dev\app\backend\controller\Role.php
|
||||
* @CustomString: Copyright (c) 2026 YwxApp
|
||||
*/
|
||||
|
||||
declare (strict_types = 1);
|
||||
namespace app\backend\controller;
|
||||
|
||||
use app\backend\validate\AdminRole as RoleValidate;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\Request;
|
||||
use ywxapp\model\BackendPower as PermissionModel;
|
||||
use ywxapp\model\BackendRole as RoleModel;
|
||||
use ywxapp\model\BackendRolePower as RolePowerModel;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Role 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Role 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 = RoleModel::field('id,name,title,status,description,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,title,status')->where('status', 1)->select();
|
||||
$this->result->success(['roles' => $roles]);
|
||||
}
|
||||
View::assign('title', '登录');
|
||||
return View::fetch('role/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据保存
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$params = $this->request->only(['name', 'title', 'status', 'description'], 'post');
|
||||
try {
|
||||
validate(RoleValidate::class)->check($params);
|
||||
Db::transaction(function () use ($params) {
|
||||
$data = RoleModel::create($params);
|
||||
});
|
||||
$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 = RoleModel::where('id', $id)->find();
|
||||
$this->result->success(['info' => $data]);
|
||||
}
|
||||
View::assign('title', '登录');
|
||||
return View::fetch('role/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据更新
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int|null $ids
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$params = $this->request->only(['id', 'name', 'title', 'status', 'description'], 'put');
|
||||
$info = RoleModel::find($params['id']);
|
||||
if (! $info) {
|
||||
$this->result->error('角色不存在', 404);
|
||||
}
|
||||
// 禁止编辑超级管理员角色
|
||||
if ($params['id'] == config('ywxapp.superAdmin', 1) && $info->name === 'superadmin') {
|
||||
$this->result->error('超级管理员角色不可编辑', 403);
|
||||
}
|
||||
$info->save($params);
|
||||
$this->result->success($info, "角色更新成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$info = RoleModel::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()) {
|
||||
$roles = RoleModel::onlyTrashed()
|
||||
->paginate([
|
||||
'page' => $page,
|
||||
'list_rows' => $size,
|
||||
]);
|
||||
$this->result->success($roles->items());
|
||||
}
|
||||
View::assign('title', '回收站');
|
||||
return View::fetch('role/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) {
|
||||
RoleModel::onlyTrashed()->whereIn('id', $idsArray)->select()->each(function ($item) {
|
||||
$item->permissions()->detach();
|
||||
$item->force()->delete();
|
||||
});
|
||||
} else {
|
||||
RoleModel::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->isPut()) {
|
||||
$ids = $this->request->param('ids', '');
|
||||
if (empty($ids)) {
|
||||
$this->result->error('请选择要恢复的数据');
|
||||
}
|
||||
$idsArray = explode(',', $ids);
|
||||
Db::startTrans();
|
||||
try {
|
||||
RoleModel::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());
|
||||
}
|
||||
}
|
||||
$this->result->error('请求错误! ' );
|
||||
}
|
||||
|
||||
|
||||
public function permission()
|
||||
{
|
||||
$id = $this->request->param('id', 0);
|
||||
if ($this->request->isGet()) {
|
||||
$permissions = PermissionModel::field('id,pid,name,code,type,status,sort')
|
||||
->where('status', 1)
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
$ownedPermissions = RolePowerModel::where('role_id', $id)->column('power_id');
|
||||
// 超级管理员角色:权限恒为 *(全部),前端展示为全部勾选且不可编辑
|
||||
$role = RoleModel::field('id,name')->find($id);
|
||||
$isSuper = $role && ($role->name === 'superadmin' || $id == config('ywxapp.superAdmin', 1));
|
||||
if ($isSuper) {
|
||||
$ownedPermissions = array_column($permissions, 'id');
|
||||
}
|
||||
$treeData = $this->buildTreeWithChecked($permissions, $ownedPermissions);
|
||||
$this->result->success($treeData, $isSuper ? '超级管理员拥有全部权限(*)' : '获取成功');
|
||||
}
|
||||
|
||||
if ($this->request->isAjax() && $this->request->isPut()) {
|
||||
// 禁止修改超级管理员权限
|
||||
$role = RoleModel::field('id,name')->find($id);
|
||||
if ($role && ($role->name === 'superadmin' || $id == config('ywxapp.superAdmin', 1))) {
|
||||
$this->result->error('超级管理员权限不可修改', 403);
|
||||
}
|
||||
$permissionIds = $this->request->param('permissions/a', []);
|
||||
try {
|
||||
Db::startTrans();
|
||||
RolePowerModel::where('role_id', $id)->delete();
|
||||
if (!empty($permissionIds)) {
|
||||
$batchData = [];
|
||||
foreach ($permissionIds as $permissionId) {
|
||||
$batchData[] = [
|
||||
'role_id' => $id,
|
||||
'power_id' => $permissionId,
|
||||
];
|
||||
}
|
||||
// 批量插入,提升性能
|
||||
RolePowerModel::insertAll($batchData);
|
||||
}
|
||||
Db::commit();
|
||||
$this->result->success([], '权限分配成功');
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->result->error('保存失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function buildTreeWithChecked($items, $checkedIds, $parentId = 0)
|
||||
{
|
||||
$tree = [];
|
||||
foreach ($items as $item) {
|
||||
if ($item['pid'] == $parentId) {
|
||||
$isChecked = in_array($item['id'], $checkedIds);
|
||||
$children = $this->buildTreeWithChecked($items, $checkedIds, $item['id']);
|
||||
$item['spread'] = true;
|
||||
$item['checked'] = $isChecked;
|
||||
$item['children'] = $children;
|
||||
$tree[] = $item;
|
||||
// $tree[] = [
|
||||
// 'id' => $item['id'],
|
||||
// 'title' => $item['name'],
|
||||
// 'spread' => true,
|
||||
// 'checked' => $isChecked, // 设置选中状态
|
||||
// 'children' => $children,
|
||||
// ];
|
||||
}
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user