<?php
namespace app\controller\admin;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;
use think\facade\Request;
class Role extends Backend
{
protected $noNeedAuth = ['getMenuIds'];
public $model;
public function _initialize()
{
$this->model = new \app\model\admin\Role();
}
public function add()
{
Db::startTrans();
try {
$post = CommonFun::filterPostData($this->request->post());
$roleInfo = $this->model->getByName($post['name']);
if ($roleInfo) throw new \Exception('角色名已存在');
$menuIds = explode(',', $post['menu_ids']);
unset($post['menu_ids']);
$saveMenu = $this->model->save($post);
if (!$saveMenu) throw new \Exception('保存角色基本信息失败');
$saveAllData = [];
foreach ($menuIds as $menu_id) {
$saveAllData[] = [
'admin_role_id' => $this->model->id,
'admin_menu_id' => $menu_id,
];
}
$menu = new \app\model\admin\menu\Role();
$saveAllMenu = $menu->saveAll($saveAllData);
if (!$saveAllMenu) throw new \Exception('保存角色权限失败');
Db::commit();
return $this->success('操作成功');
} catch (\Exception $e) {
Db::rollback();
return $this->error('数据库异常,操作失败');
}
}
public function edit()
{
$id = $this->request->param('id');
$postData = Request::only(['id', 'name', 'menu_ids']);
$post = CommonFun::filterPostData($postData);
$roleInfo = $this->model->getByName($post['name']);
if ($roleInfo && ($roleInfo['id'] != $id)) {
return $this->error('角色名已存在');
}
Db::startTrans();
try {
$menuIds = explode(',', $post['menu_ids']);
unset($post['menu_ids']);
$updateRes = $this->model->where('id', '=', $id)->update($post);
if(!is_numeric($updateRes)) throw new \Exception('保存角色基本信息失败');
$delRes = \app\model\admin\menu\Role::where('admin_role_id', '=', $id)->delete();
if(!is_numeric($delRes)) throw new \Exception('删除角色权限失败');
$saveAllData = [];
foreach ($menuIds as $menu_id) {
$saveAllData[] = [
'admin_role_id' => $id,
'admin_menu_id' => $menu_id,
];
}
$menu = new \app\model\admin\menu\Role();
$result[] = $menu->saveAll($saveAllData);
Db::commit();
return $this->success('操作成功');
} catch (\Exception $e) {
Db::rollback();
return $this->error('数据库异常,操作失败');
}
}
public function trueDel()
{
$ids = (string)$this->request->param('ids');
Db::startTrans();
try {
$idsArr = explode(',', $ids);
$roles = $this->model->onlyTrashed()->where('id', 'in', $ids)->select();
foreach ($roles as $key => $item) {
$delRes = $item->force()->delete();
if(!$delRes) throw new \Exception('角色删除失败');
}
$delRes = \app\model\admin\menu\Role::where('admin_role_id', 'in', $idsArr)->delete();
if(!is_numeric($delRes)) throw new \Exception('角色权限删除失败');
$delRes = \app\model\admin\role\User::where('admin_role_id', 'in', $idsArr)->delete();
if(!is_numeric($delRes)) throw new \Exception('角色用户删除失败');
Db::commit();
return $this->success('操作成功');
} catch (\Exception $e) {
Db::rollback();
return $this->error('数据库异常,操作失败');
}
}
public function getMenuIds()
{
$id = $this->request->param('id');
$menuIds = \app\model\admin\menu\Role::where('admin_role_id', '=', $id)->column('admin_menu_id');
$auth = [];
foreach ($menuIds as $menuId) {
$hasChild = \app\model\admin\Menu::where('pid', '=', $menuId)->find() ? true : false;
if (!$hasChild) {
$auth[] = $menuId;
}
}
return $this->success('获取成功', $auth);
}
}