Group.php 8.9 KB

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