Group.php 7.2 KB

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