Group.php 7.2 KB

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