Role.php 7.1 KB

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