Group.php 8.5 KB

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