Group.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;
  5. //管理员权限分组
  6. class Group extends Base
  7. {
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. }
  12. /**
  13. * @return \think\response\Json
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. * @throws \think\exception\DbException
  18. */
  19. public function GroupList(){
  20. $post =$this->post;
  21. $token = isset($post['token']) ? trim($post['token']) : "";
  22. $page = isset($post['page']) ? intval($post['page']): 1;
  23. $size = isset($post['size']) ? intval($post['size']):10;
  24. $condition=['is_del'=>0];
  25. $count = Db::name("role_group")->where($condition)->count();
  26. $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
  27. $list = Db::name("role_group")->where($condition)->page($page,$size)->order('addtime','desc')->select()->toArray();
  28. $data =[];
  29. foreach ($list as $key=>$val) {
  30. $userlist = [];
  31. if ($val['group_user'] != "") {
  32. $cond = ['id' => explode(",",$val['group_user'])];
  33. $user = GetUserlist($token, $cond);
  34. if ($user['code'] == 0 && !empty($user['data'])) {
  35. foreach ($user['data'] as $v) {
  36. $userlist[] = isset($v['nickname']) ?$v['nickname']:"";
  37. }
  38. }
  39. }
  40. $val['userlist']= $userlist;
  41. $data[] = $val;
  42. }
  43. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  44. }
  45. /**
  46. * @return \think\response\Json
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public function GroupAll(){
  53. $post =$this->post;
  54. $token = isset($post['token']) ? trim($post['token']) : "";
  55. if($token==""){
  56. return error_show(101,'token不能为空');
  57. }
  58. $condition=[];
  59. isset($post['name']) && $post['name']!=="" ? $condition[]= ['group_name',"like","%{$post['name']}%"] : "";
  60. isset($post['status']) && $post['status']!=="" ? $condition[] = ['status',"=",$post['status']] : "";
  61. $list = Db::name("role_group")->where($condition)->select();
  62. $data =[];
  63. foreach ($list as $key=>$val) {
  64. $userlist = [];
  65. if ($val['group_user'] != "") {
  66. $cond = ['id' => explode(",",$val['group_user'])];
  67. $user = GetUserlist($token, $cond);
  68. if ($user['code'] == 0 && !empty($user['data'])) {
  69. foreach ($user['data'] as $v) {
  70. $userlist[] = isset($v['nickname']) ?$v['nickname']:"";
  71. }
  72. }
  73. }
  74. $val['userlist']= $userlist;
  75. $data[] = $val;
  76. }
  77. return app_show(0,"获取成功",$data);
  78. }
  79. /**@param id
  80. * @return \think\response\Json
  81. * @throws \think\exception\DbException
  82. */
  83. public function GroupInfo(){
  84. $post =$this->post;
  85. $token = isset($post['token']) ? trim($post['token']) : "";
  86. if($token==""){
  87. return error_show(101,'token不能为空');
  88. }
  89. $id = isset($post['id']) ? intval($post['id']) : "";
  90. if($id==""){
  91. return error_show(1002,"用户组id不能为空");
  92. }
  93. $group = Db::name("role_group")->where("id","=",$id)->find();
  94. if(!$group){
  95. return error_show(1003,"未找到对应的数据");
  96. }
  97. $userlist=[];
  98. if($group['group_user']!=""){
  99. $cond = ['id' => explode(",",$group['group_user'])];
  100. $user = GetUserlist($token, $cond);
  101. if ($user['code'] == 0&& !empty($user['data'])) {
  102. foreach ($user['data'] as $v) {
  103. $userlist[] = isset($v['nickname']) ?$v['nickname']:"";
  104. }
  105. }
  106. }
  107. $group['userlist'] = $userlist;
  108. return app_show(0,"获取成功",$group);
  109. }
  110. /**
  111. * @return \think\response\Json
  112. * @throws \think\exception\DbException
  113. */
  114. public function GroupAdd(){
  115. $post =$this->post;
  116. $token = isset($post['token']) ? trim($post['token']) : "";
  117. if($token==""){
  118. return error_show(101,'token不能为空');
  119. }
  120. $groupname = isset($post['name']) ? trim($post['name']):"";
  121. if($groupname==""){
  122. return error_show(1002,"用户组名不能为空");
  123. }
  124. $groupuser = isset($post['group_user']) ? trim($post['group_user']):"";
  125. if($groupuser==""){
  126. return error_show(1002,"组成员不能为空");
  127. }
  128. $marke = isset($post['group_remark']) ? trim($post['group_remark']):"";
  129. $list = ['group_name'=>$groupname];
  130. $select = Db::name("role_group")->where($list)->find();
  131. if($select){
  132. return error_show(1003,"用户组名称已存在");
  133. }
  134. $data= [
  135. 'group_name'=>$groupname,
  136. 'group_user'=>$groupuser,
  137. 'status'=>1,
  138. 'group_remark'=>$marke,
  139. "addtime"=>date("Y-m-d H:i:s"),
  140. "updatetime"=>date("Y-m-d H:i:s")
  141. ];
  142. $in = Db::name("role_group")->save($data);
  143. // $orde = ["order_code"=>$groupuser,"status"=>$in['status'],"action_remark"=>'',"action_type"=>"create"];
  144. // ActionLog::logAdd($token,$orde,'role_group',1,$orde);
  145. return $in ? app_show(0,"新建成功"): error_show(1005,"新建失败");
  146. }
  147. /**
  148. * @return \think\response\Json
  149. * @throws \think\exception\DbException
  150. */
  151. public function GroupSave(){
  152. $post =$this->post;
  153. $token = isset($post['token']) ? trim($post['token']) : "";
  154. if($token==""){
  155. return error_show(101,'token不能为空');
  156. }
  157. $groupid = isset($post['id']) ? intval($post['id']):"";
  158. if($groupid==""){
  159. return error_show(1002,"用户组id不能为空");
  160. }
  161. $groupname = isset($post['name']) ? trim($post['name']):"";
  162. if($groupname==""){
  163. return error_show(1002,"用户组名不能为空");
  164. }
  165. $groupuser = isset($post['group_user']) ? trim($post['group_user']):"";
  166. if($groupuser==""){
  167. return error_show(1002,"组成员不能为空");
  168. }
  169. $marke = isset($post['group_remark']) ? trim($post['group_remark']):"";
  170. $list = ['group_name'=>$groupname];
  171. $select =Db::name("role_group")->where($list)->find();
  172. if($select && $select['id']!=$groupid){
  173. return error_show(1003,"用户组名称已存在");
  174. }
  175. $data= [
  176. "id"=>$groupid,
  177. 'group_name'=>$groupname,
  178. 'group_user'=>$groupuser,
  179. 'group_remark'=>$marke,
  180. "updatetime"=>date("Y-m-d H:i:s")
  181. ];
  182. $in = Db::name("role_group")->save($data);
  183. // $orde = ["order_code"=>$groupname,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  184. // ActionLog::logAdd($token,$orde,'role_group',0,$orde);
  185. return $in ? app_show(0,"更新成功"): error_show(1005,"更新失败");
  186. }
  187. /**
  188. * @return \think\response\Json
  189. * @throws \think\exception\DbException
  190. */
  191. public function GroupStatus(){
  192. $post =$this->post;
  193. $token = isset($post['token']) ? trim($post['token']) : "";
  194. if($token==""){
  195. return error_show(101,'token不能为空');
  196. }
  197. $groupid = isset($post['id']) ? intval($post['id']):"";
  198. if($groupid==""){
  199. return error_show(1002,"用户组id不能为空");
  200. }
  201. $status = isset($post['status']) ? intval($post['status']) : "";
  202. if($status===""){
  203. return error_show(1001,'status不能为空');
  204. }
  205. if(!in_array($status,[0,1])){
  206. return error_show(1001,'status参数非法');
  207. }
  208. $data= [
  209. "id"=>$groupid,
  210. "status"=>$status,
  211. "updatetime"=>date("Y-m-d H:i:s")
  212. ];
  213. $in = Db::name("role_group")->save($data);
  214. // $orde = ["order_code"=>$groupid,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  215. // ActionLog::logAdd($token,$orde,'role_group',$status,$orde);
  216. return $in ? app_show(0,"更新成功"): error_show(1005,"更新失败");
  217. }
  218. }