Group.php 9.0 KB

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