123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace app\bug\controller;
- use app\bug\model\RoleAction;
- use think\App;
- use think\facade\Validate;
- class Role extends Base
- {
- protected $noLogin=[];
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->model=new \app\bug\model\Role();
- }
- /**
- * @param status
- * @param role_name
- * @param level
- * @param page
- * @param size
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleList(){
- $post =$this->request->param(["status"=>"","role_name"=>"","createrId"=>"","level"=>"","page"=>1,
- "size"=>15],"post","trim");
- $condition=[];
- isset($post['status'])&&$post['status']!=="" ? $condition[]=['status',"=",$post['status']]:"";
- isset($post['level'])&&$post['level']!=="" ? $condition[]=['level',"=",$post['level']]:"";
- isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['role_name',"like","%".$post['role_name']."%"]:"";
- isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["createrId","=",$post['createrId']]:"";
- $list =$this->model->with(["role_action"])->where($condition)->order("id","desc")
- ->paginate(["list_rows"=>$post['size'],"page"=>$post['page']]);
- return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- /**
- * @param role_name
- * @param level
- * @param action
- * @param action_data
- * @param role_name
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleAdd(){
- $post =$this->request->param(["role_name"=>"","role_code"=>"","level"=>"","remark"=>"","action"=>[],"action_data"=>[],
- "private_data"=>[]],"post","trim");
- $valid=Validate::rule([
- "role_name|角色名称"=>"require|max:255|unique:app\bug\model\Role,role_name",
- "level|角色等级"=>"require|number|egt:0",
- "action|角色权限"=>"require|array",
- ]);
- if($valid->check($post)==false)return error($valid->getError());
-
- $this->model->startTrans();
- try {
- $list = [
- "role_name"=>$post['role_name'],
- "role_code"=>$post['role_code'],
- "status"=>1,
- 'creater'=>$this->uname,
- 'createrId'=>$this->uid,
- "level"=>$post['level'],
- "desc"=>$post['remark'],
- "item"=>"",
- ];
- $role = \app\bug\model\Role::create($list);
- if($role->isEmpty())throw new \Exception('角色创建失败');
- $role_action = [
- "role_id"=>$role->id,
- "action_conllect"=>$post['action'],
- "action_data"=>$post['action_data'],
- "private_data"=>$post['private_data'],
- "status"=>1,
- ];
- $roleaction=RoleAction::create($role_action);
- if($roleaction->isEmpty())throw new \Exception("权限录入失败");
-
- $this->model->commit();
- return success("角色创建成功");
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- }
- /**
- * @param roleid
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleInfo(){
- $roleid =$this->request->post("roleid/d");
- if($roleid=="")return error('roleid不能为空');
- $info =$this->model->with(["role_action"])->findOrEmpty($roleid);
- if($info->isEmpty())return error("未找到对应的数据");
- return success("获取成功",$info);
- }
- /**
- * @param role_name
- * @param level
- * @param action
- * @param action_data
- * @param roleid
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleSave(){
- $post =$this->request->param(['role_name'=>'','roleid'=>'','role_code'=>'','level'=>'','remark'=>'','action'=>[],'action_data'=>[],
- 'private_data'=>[]],'post','trim');
- $valid=Validate::rule([
- 'roleid|角色id'=>'require|number',
- 'role_name|角色名称'=>'require|max:255|unique:app\bug\model\Role,role_name,'.$post['roleid'],
- 'level|角色等级'=>'require|number|egt:0',
- 'action|角色权限'=>'require|array',
- 'action_data|数据权限'=>'array',
- 'private_data|私有权限'=>'array',
- ]);
- if($valid->check($post)==false)return error($valid->getError());
-
- $info =$this->model->findOrEmpty($post['roleid']);
- if($info->isEmpty())return error("未找到对应的数据");
- $action = RoleAction::where("role_id",$post['roleid'])->findOrEmpty();
- $this->model->startTrans();
- try {
- $info->role_name= $post['role_name'];
- $info->level= $post['level'];
- $info->desc= $post['role_name'];
- $info->role_code= $post['role_code'];
- $role=$info->save();
- if($role){
- $action->role_id= $post['roleid'];
- $action->action_conllect= $post['action'];
- $action->action_data= $post['action_data'];
- $action->private_data= $post['private_data'];
- $roleaction= $action->save();
- if($roleaction==false)throw new \Exception("权限更新失败");
- }else throw new \Exception('角色更新失败');
- $this->model->commit();
- return success("角色更新成功");
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- }
- /**
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleStatus(){
- $post =$this->request->param(["roleid"=>"","status"=>""],"post","trim");
- $valid=Validate::rule([
- 'roleid|角色id'=>'require|number',
- 'status|角色等级'=>'require|number|in:0,1',
- ]);
- if($valid->check($post)==false)return error($valid->getError());
- $info =$this->model->findOrEmpty($post['roleid']);
- if($info->isEmpty())return error('未找到对应的数据');
- $action = RoleAction::where('role_id',$post['roleid'])->findOrEmpty();
- $statusCn=$this->model->status_cn[$post['status']];
- $this->model->startTrans();
- try {
- $info->status= $post['status'];
- $re = $info->save();
- if($re==false)throw new \Exception( $statusCn.'失败');
- if($action->isEmpty()==false){
- $action->status=$post['status'];
- $acStatus= $action->save();
- if($acStatus==false)throw new \Exception( $statusCn.'失败');
- }
-
- $this->model->commit();
- return success($statusCn.'成功');
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- }
- /**
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function RoleAll(){
- return success("获取成功",$this->model->with(['role_action'])->select());
- }
- }
|