add
This commit is contained in:
110
app/admin/controller/sys/Role.php
Normal file
110
app/admin/controller/sys/Role.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\admin\model\SysMenu;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use think\facade\Db;
|
||||
|
||||
class Role extends Backend
|
||||
{
|
||||
protected array $noNeedLogin = ['*'];
|
||||
|
||||
public function list()
|
||||
{
|
||||
$prams = $this->request->param();
|
||||
$data = DatabaseRoute::paginateDb('sys_role', function ($query) {
|
||||
return $query;
|
||||
}, $prams['page'], $prams['limit']);
|
||||
$this->n_success(['page' => $data]);
|
||||
}
|
||||
|
||||
private function saveRoleMenu($roleId, $params)
|
||||
{
|
||||
if (!empty($params['menu_id_list'])) {
|
||||
Db::name('sys_role_menu')->where([
|
||||
'role_id' => $roleId
|
||||
])->delete();
|
||||
|
||||
foreach ($params['menu_id_list'] as $menuId) {
|
||||
Db::name('sys_role_menu')->insert([
|
||||
'role_id' => $roleId,
|
||||
'menu_id' => $menuId
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
|
||||
$id = Db::name('sys_role')->insert([
|
||||
'role_name' => $params['role_name'],
|
||||
'create_time' => getNormalDate(),
|
||||
'remark' => $params['remark'] ?? ''
|
||||
]);
|
||||
|
||||
$this->saveRoleMenu($id, $params);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
Db::name('sys_role')->where([
|
||||
'role_id' => $params['role_id']
|
||||
])->update([
|
||||
'role_name' => $params['role_name'],
|
||||
'remark' => $params['remark'] ?? ''
|
||||
]);
|
||||
|
||||
$this->saveRoleMenu($params['role_id'], $params);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
Db::name('sys_role')->where([
|
||||
['role_id', 'in', $params]
|
||||
])->delete();
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function selects()
|
||||
{
|
||||
|
||||
$this->n_success(['list' => convertToCamelCase(array_map(function ($item) {
|
||||
$item['role_id'] = (string)$item['role_id'];
|
||||
return $item;
|
||||
}, Db::connect(get_slave_connect_name())->name('sys_role')->select()->toArray()))]);
|
||||
}
|
||||
|
||||
|
||||
// 角色信息
|
||||
public function info()
|
||||
{
|
||||
$get = $this->request->get();
|
||||
if(empty($get['roleId'])) {
|
||||
$this->error('roleId 不能为空');
|
||||
}
|
||||
$db = Db::connect(get_slave_connect_name());
|
||||
|
||||
$role = $db->name('sys_role')->where(['role_id' => $get['roleId']])->find();
|
||||
if (!$role) {
|
||||
$this->error('角色不存在');
|
||||
}
|
||||
$role = apiconvertToCamelCase($role);
|
||||
$menuIdList = $db->name('sys_role_menu')->where(['role_id' => $get['roleId']])->column('menu_id');
|
||||
|
||||
$role['menuIdList'] = $menuIdList;
|
||||
$this->n_success(['role' => $role]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user