Role.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\bug\controller;
  3. use app\bug\model\RoleAction;
  4. use think\App;
  5. use think\facade\Validate;
  6. class Role extends Base
  7. {
  8. protected $noLogin=[];
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->model=new \app\bug\model\Role();
  13. }
  14. /**
  15. * @param status
  16. * @param role_name
  17. * @param level
  18. * @param page
  19. * @param size
  20. * @return \think\response\Json
  21. * @throws \think\exception\DbException
  22. */
  23. public function RoleList(){
  24. $post =$this->request->param(["status"=>"","role_name"=>"","createrId"=>"","level"=>"","page"=>1,
  25. "size"=>15],"post","trim");
  26. $condition=[];
  27. isset($post['status'])&&$post['status']!=="" ? $condition[]=['status',"=",$post['status']]:"";
  28. isset($post['level'])&&$post['level']!=="" ? $condition[]=['level',"=",$post['level']]:"";
  29. isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['role_name',"like","%".$post['role_name']."%"]:"";
  30. isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["createrId","=",$post['createrId']]:"";
  31. $list =$this->model->with(["role_action"])->where($condition)->order("id","desc")
  32. ->paginate(["list_rows"=>$post['size'],"page"=>$post['page']]);
  33. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  34. }
  35. /**
  36. * @param role_name
  37. * @param level
  38. * @param action
  39. * @param action_data
  40. * @param role_name
  41. * @return \think\response\Json
  42. * @throws \think\exception\DbException
  43. */
  44. public function RoleAdd(){
  45. $post =$this->request->param(["role_name"=>"","role_code"=>"","level"=>"","remark"=>"","action"=>[],"action_data"=>[],
  46. "private_data"=>[]],"post","trim");
  47. $valid=Validate::rule([
  48. "role_name|角色名称"=>"require|max:255|unique:app\bug\model\Role,role_name",
  49. "level|角色等级"=>"require|number|egt:0",
  50. "action|角色权限"=>"require|array",
  51. ]);
  52. if($valid->check($post)==false)return error($valid->getError());
  53. $this->model->startTrans();
  54. try {
  55. $list = [
  56. "role_name"=>$post['role_name'],
  57. "role_code"=>$post['role_code'],
  58. "status"=>1,
  59. 'creater'=>$this->uname,
  60. 'createrId'=>$this->uid,
  61. "level"=>$post['level'],
  62. "desc"=>$post['remark'],
  63. "item"=>"",
  64. ];
  65. $role = \app\bug\model\Role::create($list);
  66. if($role->isEmpty())throw new \Exception('角色创建失败');
  67. $role_action = [
  68. "role_id"=>$role->id,
  69. "action_conllect"=>$post['action'],
  70. "action_data"=>$post['action_data'],
  71. "private_data"=>$post['private_data'],
  72. "status"=>1,
  73. ];
  74. $roleaction=RoleAction::create($role_action);
  75. if($roleaction->isEmpty())throw new \Exception("权限录入失败");
  76. $this->model->commit();
  77. return success("角色创建成功");
  78. }catch (\Exception $e){
  79. $this->model->rollback();
  80. return error($e->getMessage());
  81. }
  82. }
  83. /**
  84. * @param roleid
  85. * @return \think\response\Json
  86. * @throws \think\exception\DbException
  87. */
  88. public function RoleInfo(){
  89. $roleid =$this->request->post("roleid/d");
  90. if($roleid=="")return error('roleid不能为空');
  91. $info =$this->model->with(["role_action"])->findOrEmpty($roleid);
  92. if($info->isEmpty())return error("未找到对应的数据");
  93. return success("获取成功",$info);
  94. }
  95. /**
  96. * @param role_name
  97. * @param level
  98. * @param action
  99. * @param action_data
  100. * @param roleid
  101. * @return \think\response\Json
  102. * @throws \think\exception\DbException
  103. */
  104. public function RoleSave(){
  105. $post =$this->request->param(['role_name'=>'','roleid'=>'','role_code'=>'','level'=>'','remark'=>'','action'=>[],'action_data'=>[],
  106. 'private_data'=>[]],'post','trim');
  107. $valid=Validate::rule([
  108. 'roleid|角色id'=>'require|number',
  109. 'role_name|角色名称'=>'require|max:255|unique:app\bug\model\Role,role_name,'.$post['roleid'],
  110. 'level|角色等级'=>'require|number|egt:0',
  111. 'action|角色权限'=>'require|array',
  112. 'action_data|数据权限'=>'array',
  113. 'private_data|私有权限'=>'array',
  114. ]);
  115. if($valid->check($post)==false)return error($valid->getError());
  116. $info =$this->model->findOrEmpty($post['roleid']);
  117. if($info->isEmpty())return error("未找到对应的数据");
  118. $action = RoleAction::where("role_id",$post['roleid'])->findOrEmpty();
  119. $this->model->startTrans();
  120. try {
  121. $info->role_name= $post['role_name'];
  122. $info->level= $post['level'];
  123. $info->desc= $post['role_name'];
  124. $info->role_code= $post['role_code'];
  125. $role=$info->save();
  126. if($role){
  127. $action->role_id= $post['roleid'];
  128. $action->action_conllect= $post['action'];
  129. $action->action_data= $post['action_data'];
  130. $action->private_data= $post['private_data'];
  131. $roleaction= $action->save();
  132. if($roleaction==false)throw new \Exception("权限更新失败");
  133. }else throw new \Exception('角色更新失败');
  134. $this->model->commit();
  135. return success("角色更新成功");
  136. }catch (\Exception $e){
  137. $this->model->rollback();
  138. return error($e->getMessage());
  139. }
  140. }
  141. /**
  142. * @return \think\response\Json
  143. * @throws \think\exception\DbException
  144. */
  145. public function RoleStatus(){
  146. $post =$this->request->param(["roleid"=>"","status"=>""],"post","trim");
  147. $valid=Validate::rule([
  148. 'roleid|角色id'=>'require|number',
  149. 'status|角色等级'=>'require|number|in:0,1',
  150. ]);
  151. if($valid->check($post)==false)return error($valid->getError());
  152. $info =$this->model->findOrEmpty($post['roleid']);
  153. if($info->isEmpty())return error('未找到对应的数据');
  154. $action = RoleAction::where('role_id',$post['roleid'])->findOrEmpty();
  155. $statusCn=$this->model->status_cn[$post['status']];
  156. $this->model->startTrans();
  157. try {
  158. $info->status= $post['status'];
  159. $re = $info->save();
  160. if($re==false)throw new \Exception( $statusCn.'失败');
  161. if($action->isEmpty()==false){
  162. $action->status=$post['status'];
  163. $acStatus= $action->save();
  164. if($acStatus==false)throw new \Exception( $statusCn.'失败');
  165. }
  166. $this->model->commit();
  167. return success($statusCn.'成功');
  168. }catch (\Exception $e){
  169. $this->model->rollback();
  170. return error($e->getMessage());
  171. }
  172. }
  173. /**
  174. * @return \think\response\Json
  175. * @throws \think\exception\DbException
  176. */
  177. public function RoleAll(){
  178. return success("获取成功",$this->model->with(['role_action'])->select());
  179. }
  180. }