Group.php 9.0 KB

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