Group.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User raingad@foxmail.com
  5. * Date 2022/12/14 17:24
  6. */
  7. namespace app\manage\controller;
  8. use app\BaseController;
  9. use app\enterprise\model\{User as UserModel,GroupUser,Group as GroupModel};
  10. use think\facade\Db;
  11. class Group extends BaseController
  12. {
  13. // 获取群聊列表
  14. public function index()
  15. {
  16. $map = [];
  17. $model=new GroupModel();
  18. $param = $this->request->param();
  19. //搜索关键词
  20. if ($keyword = $this->request->param('keywords')) {
  21. $model = $model->whereLike('name|name_py', '%' . $keyword . '%');
  22. }
  23. // 排序
  24. $order='group_id DESC';
  25. if ($param['order_field'] ?? '') {
  26. $order = orderBy($param['order_field'],$param['order_type'] ?? 1);
  27. }
  28. $list = $this->paginate($model->where($map)->order($order));
  29. if ($list) {
  30. $data = $list->toArray()['data'];
  31. $userList=UserModel::matchUser($data,true,'owner_id',120);
  32. foreach($data as $k=>$v){
  33. $data[$k]['avatar']=avatarUrl($v['avatar'],$v['name'],$v['group_id'],120);
  34. $data[$k]['owner_id_info']=$userList[$v['owner_id']] ?? [];
  35. }
  36. }
  37. return success('', $data, $list->total(), $list->currentPage());
  38. }
  39. // 更换群主
  40. public function changeOwner()
  41. {
  42. $group_id = $this->request->param('group_id');
  43. $user_id = $this->request->param('user_id');
  44. $group=GroupModel::where('group_id',$group_id)->find();
  45. if(!$group){
  46. return warning('群组不存在');
  47. }
  48. $user=UserModel::where('user_id',$user_id)->find();
  49. if(!$user){
  50. return warning('用户不存在');
  51. }
  52. Db::startTrans();
  53. try{
  54. GroupUser::where('group_id',$group_id)->where('user_id',$user_id)->update(['role'=>1]);
  55. GroupUser::where('group_id',$group_id)->where('user_id',$group->owner_id)->update(['role'=>3]);
  56. $group->owner_id=$user_id;
  57. $group->save();
  58. wsSendMsg($group_id,"changeOwner",['group_id'=>'group-'.$group_id,'user_id'=>$user_id],1);
  59. Db::commit();
  60. return success('保存成功');
  61. }catch (\Exception $e){
  62. Db::rollback();
  63. return warning('更换失败');
  64. }
  65. }
  66. // 解散群聊
  67. public function del()
  68. {
  69. $group_id = $this->request->param('group_id');
  70. $group=GroupModel::where('group_id',$group_id)->find();
  71. if(!$group){
  72. return warning('群组不存在');
  73. }
  74. Db::startTrans();
  75. try{
  76. // 删除团队成员
  77. GroupUser::where('group_id',$group_id)->delete();
  78. // 删除团队
  79. GroupModel::destroy($group_id);
  80. wsSendMsg($group_id,"removeGroup",['group_id'=>'group-'.$group_id],1);
  81. Db::commit();
  82. return success('解散成功');
  83. }catch (\Exception $e){
  84. Db::rollback();
  85. return warning('解散失败');
  86. }
  87. }
  88. // 添加群成员
  89. public function addGroupUser(){
  90. $param = $this->request->param();
  91. $uid=$this->userInfo['user_id'];
  92. $group_id = $param['group_id'];
  93. $group=GroupModel::where('group_id',$group_id)->find();
  94. if(!$group){
  95. return warning('群组不存在');
  96. }
  97. $user_ids=$param['user_ids'];
  98. $data=[];
  99. try{
  100. foreach($user_ids as $k=>$v){
  101. $data[]=[
  102. 'group_id'=>$group_id,
  103. 'user_id'=>$v,
  104. 'role'=>3,
  105. 'invite_id'=>$uid
  106. ];
  107. }
  108. $groupUser=new GroupUser;
  109. $groupUser->saveAll($data);
  110. $url=GroupModel::setGroupAvatar($group_id);
  111. wsSendMsg($group_id,"addGroupUser",['group_id'=>"group-".$group_id,'avatar'=>$url],1);
  112. return success('添加成功');
  113. }catch(\Exception $e){
  114. return error($e->getMessage());
  115. }
  116. }
  117. // 删除群成员
  118. public function delGroupUser(){
  119. $param = $this->request->param();
  120. $group_id = $param['group_id'];
  121. $group=GroupModel::where('group_id',$group_id)->find();
  122. if(!$group){
  123. return warning('群组不存在');
  124. }
  125. $user_id=$param['user_id'];
  126. $groupUser=GroupUser::where(['group_id'=>$group_id,'user_id'=>$user_id])->find();
  127. if($groupUser){
  128. $groupUser->delete();
  129. wsSendMsg($group_id,"removeUser",['group_id'=>'group-'.$group_id],1);
  130. return success('删除成功');
  131. }else{
  132. return warning('删除失败!');
  133. }
  134. }
  135. // 设置管理员
  136. public function setManager(){
  137. $param = $this->request->param();
  138. $group_id = $param['group_id'];
  139. $group=GroupModel::where('group_id',$group_id)->find();
  140. if(!$group){
  141. return warning('群组不存在');
  142. }
  143. $user_id=$param['user_id'];
  144. $role=$param['role'];
  145. $groupUser=GroupUser::where(['group_id'=>$group_id,'user_id'=>$user_id])->find();
  146. if($groupUser){
  147. $groupUser->role=$role;
  148. $groupUser->save();
  149. wsSendMsg($group_id,"setManager",['group_id'=>'group-'.$group_id],1);
  150. return success('设置成功');
  151. }else{
  152. return warning('设置失败!');
  153. }
  154. }
  155. }