* @Date: 2026-04-29 02:32:33 * @LastEditors: YwxApp * @LastEditTime: 2026-07-23 18:17:36 * @Description: * @FilePath: \ywxapp_dev\app\backend\controller\Power.php * @CustomString: Copyright (c) 2026 YwxApp */ declare(strict_types=1); namespace app\backend\controller; use app\backend\service\PowerService; use app\backend\validate\Power as PowerValidate; use think\exception\ValidateException; use think\facade\Db; use think\facade\View; use think\Request; use think\Response; use ywxapp\controller\BackendBase; use ywxapp\model\BackendPower as PowerModel; use ywxapp\model\BackendRolePower; /** * Power 类 * * @author ywxapp */ class Power extends BackendBase { /** * Summary of needLogin * @var array */ protected $noNeedLogin = []; /** * Summary of needRight * @var array */ protected $noNeedVerify = []; /** * 控制器初始化 initialize * @return void */ protected function initialize() { $this->model = new PowerModel(); $this->view->assign('title', '权限管理'); } /** * 显示资源列表 * * @param \think\Request $request * @return \think\Response */ public function index() { $page = $this->request->param('page', 1); $size = $this->request->param('size', 100); if ($this->request->isAjax()) { $data = PowerModel::cache(false)->paginate([ 'page' => $page, 'list_rows' => $size, ]); $this->result->success($data->items()); } return view('index', [ 'name' => 'ThinkPHP', 'email' => 'thinkphp@qq.com' ]); return $this->view->fetch(); } /** * 创建权限 * * @param \think\Request $request * @return \think\Response */ public function create() { if ($this->request->isAjax()) { $power = PowerModel::cateTree(PowerModel::select()->toArray()); $this->result->success(['power' => $power]); } // } /** * 保存权限 * * @param \think\Request $request * @return \think\Response */ public function save() { if ($this->request->isPost()) { $params = $this->request->post(); validate(PowerValidate::class)->check($params); try { Db::transaction(function () use ($params) { $data = PowerModel::create($params); }); $this->result->success($params, '保存成功'); } catch (ValidateException $e) { $this->result->error('保存失败: ' . $e->getMessage(), 2, $params); } catch (\Throwable $th) { $this->result->error('保存失败: ' . $th->getMessage(), 1, $params); } } } /** * 显示编辑权限表单页. * * @param \think\Request $request * @return \think\Response */ public function edit($id = null) { $id = $this->request->param('id'); $power = PowerModel::find($id); if (! $power) { $this->result->error('权限不存在'); } if ($this->request->isAjax()) { $powers = PowerModel::cateTree(PowerModel::select()->toArray()); $this->result->success(['power' => $powers, 'info' => $power]); } //View::assign('power', $power); //return View::fetch(); } /** * 显示编辑权限表单页. * * @param \think\Request $request * @return \think\Response */ public function update($id = null) { $id = $id ? $id : $this->request->param('id'); if ($this->request->isAjax() && $this->request->isPut()) { $params = $this->request->param(); validate(PowerValidate::class)->check($params); try { $res = Db::transaction(function () use ($params, $id) { $power = PowerModel::find($id); if (! $power) { throw new ValidateException('权限不存在'); } $power->save($params); }); $this->result->success($res, '更新成功'); } catch (ValidateException $e) { $this->result->error('更新失败: ' . $e->getMessage()); } catch (\Throwable $th) { $this->result->error('更新失败: ' . $th->getMessage()); } } } /** * 显示回收站列表 * * @param \think\Request $request * @return \think\Response */ public function recyclebin() { $page = $this->request->param('page', 1); $size = $this->request->param('size', 15); if ($this->request->isAjax()) { $data = PowerModel::onlyTrashed()->paginate([ 'page' => $page, 'list_rows' => $size, ]); $this->result->success($data->items()); } View::assign('title', '回收站'); return View::fetch('power/index'); } /** * 删除权限 * * @param \think\Request $request * @return \think\Response */ 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('请选择要删除的数据'); } try { Db::transaction(function () use ($ids, $force) { if ($force) { PowerModel::onlyTrashed()->whereIn('id', $ids)->select()->each(function ($item) { $item->force()->delete(); }); } else { PowerModel::destroy($ids); } }); $this->result->success(); } catch (\think\exception\HttpResponseException $e) { throw $e; // 不要 return,不要吞掉! } catch (\Throwable $th) { $this->result->error('删除失败: ' . $th->getMessage()); } } } /** * 还原权限 * * @param \think\Request $request * @return \think\Response */ public function restore($ids = null) { if ($this->request->isAjax() && $this->request->isPut()) { $ids = $this->request->param('ids', ''); if (empty($ids)) { $this->result->error('请选择要还原的数据'); } $idsArray = explode(',', $ids); try { Db::transaction(function () use ($idsArray) { PowerModel::onlyTrashed()->whereIn('id', $idsArray)->select()->each(function ($item) { $item->restore(); }); }); $this->result->success(); } catch (\think\exception\HttpResponseException $e) { throw $e; // 不要 return,不要吞掉! } catch (\Throwable $th) { $this->result->error('还原失败: ' . $th->getMessage()); } } } // 更新角色权限 public function updateRolePowers($roleId): Response { $powerIds = $this->request->post('powerIds', []); Db::startTrans(); try { // 删除旧的角色权限关联 BackendRolePower::where('role_id', $roleId)->delete(); // 添加新的角色权限关联 if (! empty($powerIds)) { $rolePowerData = []; foreach ($powerIds as $powerId) { $rolePowerData[] = [ 'role_id' => $roleId, 'power_id' => $powerId, 'create_at' => date('Y-m-d H:i:s'), ]; } (new BackendRolePower())->saveAll($rolePowerData); } Db::commit(); return json(['code' => 200, 'message' => '权限分配成功']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 500, 'message' => '权限分配失败: ' . $e->getMessage()]); } } }