229 lines
6.3 KiB
PHP
229 lines
6.3 KiB
PHP
<?php
|
|
/*
|
|
* @Author: YwxApp <ywx@ywxapp.cn>
|
|
* @Date: 2026-04-29 02:32:33
|
|
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
|
* @LastEditTime: 2026-07-21 23:21:38
|
|
* @Description:
|
|
* @FilePath: \ywxapp_dev\app\backend\controller\Rule.php
|
|
* @CustomString: Copyright (c) 2026 YwxApp
|
|
*/
|
|
|
|
declare (strict_types = 1);
|
|
namespace app\backend\controller;
|
|
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
use think\facade\Request;
|
|
use ywxapp\controller\BackendBase;
|
|
use ywxapp\model\MemberRule as RuleModel;
|
|
use app\backend\validate\UserRule as RuleValidate;
|
|
use think\exception\ValidateException;
|
|
use ywxapp\model\MemberGroupRule;
|
|
|
|
/**
|
|
* Rule 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Rule extends BackendBase
|
|
{
|
|
/**
|
|
* Summary of needLogin
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
|
|
/**
|
|
* Summary of needRight
|
|
* @var array
|
|
*/
|
|
protected $noNeedVerify = [];
|
|
|
|
/**
|
|
* 控制器初始化 initialize
|
|
* @return void
|
|
*/
|
|
protected function initialize() {}
|
|
|
|
/**
|
|
* 显示资源列表
|
|
*
|
|
* @return \think\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$size = $this->request->param('size', 15);
|
|
if ($this->request->isAjax()) {
|
|
$data = RuleModel::cache(false)->paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($data->items());
|
|
}
|
|
View::assign('title', '登录');
|
|
return View::fetch();
|
|
}
|
|
|
|
/**
|
|
* 创建权限
|
|
*
|
|
* @return \think\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$power = RuleModel::cateTree(RuleModel::select()->toArray());
|
|
$this->result->success(['power' => $power]);
|
|
}
|
|
//
|
|
}
|
|
|
|
/**
|
|
* 保存权限
|
|
*
|
|
* @return \think\Response
|
|
*/
|
|
public function save()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post();
|
|
Db::startTrans();
|
|
try {
|
|
validate(RuleValidate::class)->check($params);
|
|
$data = RuleModel::create($params);
|
|
Db::commit();
|
|
$this->result->success($params, '保存成功');
|
|
} catch (ValidateException $e) {
|
|
Db::rollback();
|
|
$this->result->error('保存失败: ' . $e->getMessage(), 2, $params);
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
$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 = RuleModel::find($id);
|
|
if (! $power) {
|
|
$this->result->error('权限不存在');
|
|
}
|
|
if ($this->request->isAjax()) {
|
|
$powers = RuleModel::cateTree(RuleModel::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();
|
|
Db::startTrans();
|
|
try {
|
|
validate(RuleValidate::class)->check($params);
|
|
|
|
$power = RuleModel::find($id);
|
|
if (! $power) {
|
|
throw new ValidateException('权限不存在');
|
|
}
|
|
$power->save($params);
|
|
|
|
Db::commit();
|
|
$this->result->success($power, '更新成功');
|
|
} catch (ValidateException $e) {
|
|
Db::rollback();
|
|
$this->result->error('验证失败: ' . $e->getMessage());
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->result->error('位置错误: ' . $e->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 = RuleModel::onlyTrashed()->paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($data->items());
|
|
}
|
|
View::assign('title', '回收站');
|
|
return View::fetch('rule/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) {
|
|
RuleModel::onlyTrashed()->whereIn('id', $ids)->select()->each(function ($item) {
|
|
$item->force()->delete();
|
|
});
|
|
} else {
|
|
RuleModel::destroy($ids);
|
|
}
|
|
});
|
|
$this->result->success();
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e; // 不要 return,不要吞掉!
|
|
} catch (\Exception $e) {
|
|
$this->result->error('删除失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取角色权限
|
|
|
|
public function rolePowers($roleId)
|
|
{
|
|
$powerIds = MemberGroupRule::where('gid', $roleId)->column('rid');
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'message' => 'success',
|
|
'data' => $powerIds,
|
|
]);
|
|
}
|
|
}
|