角色管理
This commit is contained in:
parent
eef3f5d49b
commit
d9a9613bfb
|
|
@ -7,6 +7,8 @@ namespace App\Controller\Admin;
|
|||
|
||||
use App\Annotation\Auth;
|
||||
use App\Model\Menu as mModel;
|
||||
use App\Model\Dept as dModel;
|
||||
use App\Model\Role as rModel;
|
||||
use App\Utils\Param;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\DeleteMapping;
|
||||
|
|
@ -14,6 +16,8 @@ use Hyperf\HttpServer\Annotation\GetMapping;
|
|||
use Hyperf\HttpServer\Annotation\PostMapping;
|
||||
use Hyperf\HttpServer\Annotation\PutMapping;
|
||||
use App\Request\Menu as mRequest;
|
||||
use App\Request\Role as rRequest;
|
||||
use App\Request\Dept as dRequest;
|
||||
|
||||
#[Controller(prefix: "admin")]
|
||||
class System extends Base
|
||||
|
|
@ -134,4 +138,57 @@ class System extends Base
|
|||
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
||||
}
|
||||
|
||||
// 角色列表
|
||||
#[GetMapping(path: "role/list")]
|
||||
#[Auth(auth: "role:list")]
|
||||
public function roleList()
|
||||
{
|
||||
$role_name = $this->request->input("role_name", "");
|
||||
return $this->success("菜单列表", rModel::list($this->account['belong_id'], $this->account['account_type'], $role_name));
|
||||
}
|
||||
|
||||
// 角色选择
|
||||
#[GetMapping(path: "role/option")]
|
||||
#[Auth(needAuth: false)]
|
||||
public function roleOption()
|
||||
{
|
||||
return $this->success("菜单列表", rModel::options($this->account['belong_id'], $this->account['account_type']));
|
||||
}
|
||||
|
||||
// 添加角色
|
||||
#[PostMapping(path: "role/add")]
|
||||
#[Auth(auth: "role:add")]
|
||||
public function roleAdd()
|
||||
{
|
||||
$request = $this->container->get(rRequest::class);
|
||||
$request->scene('add')->validateResolved();
|
||||
$data = Param::only(['role_name' => '', 'menus' => [], 'remark', 'status' => 1, 'rank', "checked_menus"]);
|
||||
$data['belong_id'] = $this->account['belong_id'];
|
||||
$data['account_type'] = $this->account['account_type'];
|
||||
$res = rModel::add($data);
|
||||
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
||||
}
|
||||
|
||||
// 修改角色
|
||||
#[PutMapping(path: "role/edit")]
|
||||
#[Auth(auth: "role:edit")]
|
||||
public function roleEdit()
|
||||
{
|
||||
$request = $this->container->get(rRequest::class);
|
||||
$request->scene('edit')->validateResolved();
|
||||
$data = Param::only(['role_id' => '', 'role_name' => '', 'menus' => [], 'remark', 'status' => 1, 'rank', "checked_menus"]);
|
||||
$res = rModel::edit($data);
|
||||
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
||||
}
|
||||
|
||||
// 删除角色
|
||||
#[DeleteMapping(path: "role/del")]
|
||||
#[Auth(auth: "role:del")]
|
||||
public function roleDel()
|
||||
{
|
||||
$ids = $this->request->input("ids", "");
|
||||
if (!$ids) return $this->error("请选择要删除的角色");
|
||||
$res = rModel::del($ids);
|
||||
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
||||
/**
|
||||
* @property int $role_id
|
||||
* @property string $role_name
|
||||
|
|
@ -34,4 +36,97 @@ class Role extends Model
|
|||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['role_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'status' => 'integer', 'rank' => 'integer'];
|
||||
|
||||
public static function list(int $belong_id, int $account_type, string $role_name)
|
||||
{
|
||||
$model = self::where('del_flag', 0)
|
||||
->where('belong_id', $belong_id)
|
||||
->where('account_type', $account_type);
|
||||
if ($role_name != '') {
|
||||
$model = $model->where("role_name","like", "%$role_name%");
|
||||
}
|
||||
return $model->select(["role_id","role_name","status","remark","create_time","rank","checked_menus"])
|
||||
->get()->each(function ($item){
|
||||
$item['menus'] = RoleMenu::getMenu($item['role_id']);
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
public static function options(int $belong_id, int $account_type)
|
||||
{
|
||||
return self::where('del_flag', 0)
|
||||
->where('belong_id', $belong_id)
|
||||
->where('account_type', $account_type)
|
||||
->orderByDesc("rank")
|
||||
->select(["role_id","role_name"])
|
||||
->get()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function add(array $data)
|
||||
{
|
||||
$data['create_time'] = date("Y-m-d H:i:s");
|
||||
$data['del_flag'] = 0;
|
||||
$menus = $data['menus'];
|
||||
unset($data['menus']);
|
||||
Db::beginTransaction();
|
||||
$role_id = self::insertGetId($data);
|
||||
if (!$role_id) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
$role_menu = [];
|
||||
foreach ($menus as $menu_id) {
|
||||
$role_menu[] = ['role_id' => $role_id, 'menu_id' => $menu_id];
|
||||
}
|
||||
if (!empty($role_menu)) {
|
||||
$res = RoleMenu::insert($role_menu);
|
||||
if (!$res) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function edit(array $data)
|
||||
{
|
||||
$data['update_time'] = date("Y-m-d H:i:s");
|
||||
$menus = $data['menus'];
|
||||
unset($data['menus']);
|
||||
Db::beginTransaction();
|
||||
$res = self::where("role_id", $data['role_id'])->where('del_flag', 0)->update($data);
|
||||
if (!$res) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
// 删除之前的
|
||||
$res1 = RoleMenu::where('role_id', $data['role_id'])->delete();
|
||||
if (!$res1) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
$role_menu = [];
|
||||
foreach ($menus as $menu_id) {
|
||||
$role_menu[] = ['role_id' => $data['role_id'], 'menu_id' => $menu_id];
|
||||
}
|
||||
// 重新添加
|
||||
if (!empty($role_menu)) {
|
||||
if (!RoleMenu::insert($role_menu)) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
||||
/**
|
||||
* @property int $role_id
|
||||
* @property int $menu_id
|
||||
|
|
@ -24,4 +26,12 @@ class RoleMenu extends Model
|
|||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['role_id' => 'integer', 'menu_id' => 'integer'];
|
||||
|
||||
public static function getMenu($role_id)
|
||||
{
|
||||
return Db::table("role_menu")
|
||||
->leftJoin('menu', 'menu.menu_id', '=', 'role_menu.menu_id')
|
||||
->where('role_menu.role_id', $role_id)
|
||||
->select(["menu.menu_id","menu.title"])->get()->toArray();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace App\Request;
|
||||
|
||||
use Hyperf\Validation\Request\FormRequest;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
*/
|
||||
class Dept extends FormRequest
|
||||
{
|
||||
protected array $scenes = [
|
||||
'add' => ['pid', 'dept_name'],
|
||||
'edit' => ['dept_id', 'pid', 'dept_name'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'dept_id' => 'required',
|
||||
'pid' => 'required',
|
||||
'dept_name' => 'required'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'dept_id.required' => '部门ID必填!',
|
||||
'pid.required' => '上级ID必选!',
|
||||
'dept_name.required' => '部门名称必填!'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Request;
|
||||
|
||||
use Hyperf\Validation\Request\FormRequest;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
*/
|
||||
class Role extends FormRequest
|
||||
{
|
||||
protected array $scenes = [
|
||||
'add' => ['role_name'],
|
||||
'edit' => ['role_id', 'role_name'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'role_id' => 'required',
|
||||
'role_name' => 'required'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'role_id.required' => '角色ID必填!',
|
||||
'role_name.required' => '角色名称必填!'
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue