Role.php 7.0 KB

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