Group.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\BaseController;
  4. use app\enterprise\model\{User,Group as GroupModel,GroupUser,Message};
  5. use think\Exception;
  6. use think\facade\Db;
  7. use app\common\controller\Upload;
  8. class Group extends BaseController
  9. {
  10. protected $setting=['manage' => 0, 'invite' => 1, 'nospeak' => 0];
  11. // 获取联系人列表
  12. public function getAllUser(){
  13. $param=$this->request->param();
  14. $user_ids=isset($param['user_ids'])?$param['user_ids']:[];
  15. $groupId=$param['group_id'] ?? '';
  16. $group_id='';
  17. if($groupId){
  18. $group_id=explode('-',$groupId)[1];
  19. }
  20. $data=User::getAllUser([['status','=',1],['user_id','<>',$this->userInfo['user_id']]],$user_ids,$this->uid,$group_id);
  21. return success('',$data);
  22. }
  23. // 获取群成员
  24. public function groupUserList()
  25. {
  26. $param = $this->request->param();
  27. try {
  28. $group_id = explode('-', $param['group_id'])[1];
  29. $data = GroupUser::getGroupUser(['group_id' => $group_id]);
  30. return success('', $data);
  31. } catch (Exception $e) {
  32. return error($e->getMessage());
  33. }
  34. }
  35. // 获取群基本信息
  36. public function groupInfo()
  37. {
  38. $param = $this->request->param();
  39. try {
  40. $jm='qr';
  41. $groupId=$param['group_id'] ?? '';
  42. $groupInfo = explode('-', $groupId);
  43. $group_id=$groupInfo[1];
  44. $group=GroupModel::find($group_id)->toArray();
  45. $userList=User::matchUser($group,false,'owner_id');
  46. $userCount=GroupUser::where(['group_id'=>$group_id])->count();
  47. $userInfo=$userList[$group['owner_id']];
  48. $expire=time()+7*86400;
  49. $token=urlencode(authcode($this->uid.'-'.$group_id,'ENCODE', $jm,7*86400));
  50. $qrUrl=request()->domain().'/scan/g/'.$token;
  51. $group['id']=$groupId;
  52. $group['qrUrl']=$qrUrl;
  53. $group['qrExpire']=date('m月d日',$expire);
  54. $group['userInfo']=$userInfo;
  55. $group['ownerName']=$userInfo['realname'];
  56. $group['groupUserCount']=$userCount;
  57. $group['displayName']=$group['name'];
  58. $group['avatar']=avatarUrl($group['avatar'],$group['name'],$group['group_id'],120);
  59. $group['setting']=$group['setting']?json_decode($group['setting'],true):['manage' => 0, 'invite' => 1, 'nospeak' => 0];
  60. $group['isJoin']=GroupUser::where(['group_id'=>$group_id,'user_id'=>$this->uid])->value('role') ?: 0;
  61. return success('', $group);
  62. } catch (Exception $e) {
  63. return error($e->getMessage());
  64. }
  65. }
  66. // 修改团队名称
  67. public function editGroupName()
  68. {
  69. $param = $this->request->param();
  70. $group_id = explode('-', $param['id'])[1];
  71. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$this->userInfo['user_id']])->value('role');
  72. if($role>2){
  73. return warning('你没有操作权限,只有群主和群管理员才可以修改!');
  74. }
  75. GroupModel::where(['group_id' => $group_id])->update(['name' => $param['displayName'],'name_py'=>pinyin_sentence($param['displayName'])]);
  76. $param['editUserName'] = $this->userInfo['realname'];
  77. $action='editGroupName';
  78. event('GroupChange', ['action' => $action, 'group_id' => $group_id, 'param' => $param]);
  79. wsSendMsg($group_id, $action, $param, 1);
  80. return success('修改成功');
  81. }
  82. // 添加群成员
  83. public function addGroupUser(){
  84. $param = $this->request->param();
  85. $uid=$this->userInfo['user_id'];
  86. $group_id = explode('-', $param['id'])[1];
  87. $user_ids=$param['user_ids'];
  88. $groupUserCount=GroupUser::where(['group_id'=>$group_id,'status'=>1])->count();
  89. if((count($user_ids) + $groupUserCount) > $this->chatSetting['groupUserMax'] && $this->chatSetting['groupUserMax']!=0){
  90. return warning("人数不能超过".$this->chatSetting['groupUserMax']."人!");
  91. }
  92. $data=[];
  93. try{
  94. foreach($user_ids as $k=>$v){
  95. $data[]=[
  96. 'group_id'=>$group_id,
  97. 'user_id'=>$v,
  98. 'role'=>3,
  99. 'invite_id'=>$uid
  100. ];
  101. }
  102. $groupUser=new GroupUser;
  103. $groupUser->saveAll($data);
  104. $url=GroupModel::setGroupAvatar($group_id);
  105. wsSendMsg($group_id,"addGroupUser",['group_id'=>$param['id'],'avatar'=>$url],1);
  106. return success('添加成功');
  107. }catch(Exception $e){
  108. return error($e->getMessage());
  109. }
  110. }
  111. // 设置管理员
  112. public function setManager(){
  113. $param = $this->request->param();
  114. $uid=$this->userInfo['user_id'];
  115. $group_id = explode('-', $param['id'])[1];
  116. $user_id=$param['user_id'];
  117. $role=$param['role'];
  118. if(!GroupUser::checkAuth(['group_id'=>$group_id,'user_id'=>$uid])){
  119. return warning('您没有操作权限!');
  120. }
  121. $groupUser=GroupUser::where(['group_id'=>$group_id,'user_id'=>$user_id])->find();
  122. if($groupUser){
  123. $groupUser->role=$role;
  124. $groupUser->save();
  125. wsSendMsg($group_id,"setManager",['group_id'=>$param['id']],1);
  126. return success('设置成功');
  127. }else{
  128. return warning('设置失败!');
  129. }
  130. }
  131. // 添加群聊
  132. public function add(){
  133. $param = $this->request->param();
  134. $uid=$this->userInfo['user_id'];
  135. $user_ids=$param['user_ids'];
  136. if($this->chatSetting['groupChat']==0){
  137. return warning("您没有创建群聊的权限!");
  138. }
  139. if(count($user_ids)>$this->chatSetting['groupUserMax'] && $this->chatSetting['groupUserMax']!=0){
  140. return warning("人数不能超过".$this->chatSetting['groupUserMax']."人!");
  141. }
  142. if(count($user_ids)<=1){
  143. return warning("请至少选择两人!");
  144. }
  145. // 将自己也加入群聊
  146. $user_ids[]=$this->userInfo['user_id'];
  147. Db::startTrans();
  148. $setting=$this->setting;
  149. try{
  150. $create=[
  151. 'create_user'=>$uid,
  152. 'owner_id'=>$uid,
  153. 'name'=>"群聊",
  154. 'name_py'=>"qunliao",
  155. 'setting'=>json_encode($setting),
  156. ];
  157. $name=$param['name'] ?? '';
  158. if($name){
  159. $create['name']=$name;
  160. $create['name_py']=pinyin_sentence($name);
  161. }
  162. $group=new GroupModel();
  163. $group->save($create);
  164. $group_id=$group->group_id;
  165. $data=[];
  166. sort($user_ids);
  167. foreach($user_ids as $k=>$v){
  168. $info=[
  169. 'user_id'=>$v,
  170. 'invite_id'=>$uid,
  171. 'status'=>1,
  172. 'role'=>3,
  173. 'group_id'=>$group_id
  174. ];
  175. if($v==$uid){
  176. $info['invite_id']=0;
  177. $info['role']=1;
  178. }
  179. $data[]=$info;
  180. }
  181. $groupUser=new GroupUser();
  182. $groupUser->saveAll($data);
  183. $url=GroupModel::setGroupAvatar($group_id);
  184. $groupInfo=[
  185. 'displayName'=>$create['name'],
  186. 'owner_id'=>$create['owner_id'],
  187. 'role'=>3,
  188. 'name_py'=>$create['name_py'],
  189. 'id'=>'group-'.$group_id,
  190. 'avatar'=>avatarUrl($url,$create['name'],$group_id,120),
  191. 'is_group'=>1,
  192. 'lastContent'=>$this->userInfo['realname'].' 创建了群聊',
  193. 'lastSendTime'=>time()*1000,
  194. 'index'=>"[2]群聊",
  195. 'is_notice'=>1,
  196. 'is_top'=>0,
  197. 'setting'=>$setting,
  198. ];
  199. Message::create([
  200. 'from_user'=>$uid,
  201. 'to_user'=>$group_id,
  202. 'content'=>str_encipher('创建了群聊'),
  203. 'type'=>'event',
  204. 'is_group'=>1,
  205. 'is_read'=>1,
  206. 'is_last'=>1,
  207. 'chat_identify'=>'group-'.$group_id
  208. ]);
  209. wsSendMsg($user_ids, 'addGroup', $groupInfo);
  210. Db::commit();
  211. $groupInfo['role']=1;
  212. return success('',$groupInfo);
  213. }catch(Exception $e){
  214. Db::rollback();
  215. return error($e->getMessage());
  216. }
  217. }
  218. // 移除成员
  219. public function removeUser(){
  220. $param = $this->request->param();
  221. $uid=$this->userInfo['user_id'];
  222. $group_id = explode('-', $param['id'])[1];
  223. $user_id=$param['user_id'];
  224. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->value('role');
  225. if($role>2 && $user_id!=$uid){
  226. return warning('您没有操作权限!');
  227. }
  228. $groupUser=GroupUser::where(['group_id'=>$group_id,'user_id'=>$user_id])->find();
  229. if(($groupUser && $groupUser['role']>$role) || $user_id==$uid){
  230. GroupUser::destroy($groupUser->id);
  231. }else{
  232. return warning('您的权限不够!');
  233. }
  234. $url=GroupModel::setGroupAvatar($group_id);
  235. wsSendMsg($group_id,"removeUser",['group_id'=>$param['id'],'avatar'=>$url,'user_id'=>$user_id],1);
  236. return success('删除成功');
  237. }
  238. // 解散团队
  239. public function removeGroup(){
  240. $param = $this->request->param();
  241. $uid=$this->userInfo['user_id'];
  242. $group_id = explode('-', $param['id'])[1];
  243. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->value('role');
  244. if($role>1){
  245. return warning('您没有操作权限!');
  246. }
  247. Db::startTrans();
  248. try{
  249. // 删除团队成员
  250. GroupUser::where(['group_id'=>$group_id])->delete();
  251. // 删除团队
  252. GroupModel::destroy($group_id);
  253. wsSendMsg($group_id,"removeGroup",['group_id'=>$param['id']],1);
  254. Db::commit();
  255. return success('');
  256. }catch(Exception $e){
  257. Db::rollback();
  258. return error($e->getMessage());
  259. }
  260. }
  261. // 设置公告
  262. public function setNotice(){
  263. $param = $this->request->param();
  264. $uid=$this->userInfo['user_id'];
  265. $group_id = explode('-', $param['id'])[1];
  266. if($param['notice']==''){
  267. return warning('请输入内容!');
  268. }
  269. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->value('role');
  270. // if($role>2){
  271. // return warning('您没有操作权限!');
  272. // }
  273. GroupModel::update(['notice'=>$param['notice']],['group_id'=>$group_id]);
  274. wsSendMsg($group_id,"setNotice",['group_id'=>$param['id'],'notice'=>$param['notice']],1);
  275. return success('');
  276. }
  277. // 群聊设置
  278. public function groupSetting(){
  279. $param = $this->request->param();
  280. $uid=$this->userInfo['user_id'];
  281. $group_id = explode('-', $param['id'])[1];
  282. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->value('role');
  283. if($role!=1){
  284. return warning('您没有操作权限!');
  285. }
  286. $setting=json_encode($param['setting']);
  287. GroupModel::update(['setting'=>$setting],['group_id'=>$group_id]);
  288. wsSendMsg($group_id,"groupSetting",['group_id'=>$param['id'],'setting'=>$param['setting']],1);
  289. return success('');
  290. }
  291. //生成群聊头像
  292. protected function setGroupAvatar($group_id){
  293. $userList=GroupUser::where('group_id',$group_id)->limit(9)->column('user_id');
  294. $userList=User::where('user_id','in',$userList)->select()->toArray();
  295. $imgList=[];
  296. $dirPath=app()->getRootPath().'public/temp';
  297. foreach($userList as $k=>$v){
  298. if($v['avatar']){
  299. $imgList[]=avatarUrl($v['avatar'],$v['realname'],$v['user_id']);
  300. }else{
  301. $imgList[]=circleAvatar($v['realname'],80,$v['user_id'],1,$dirPath);
  302. }
  303. }
  304. $groupId='group_'.$group_id;
  305. $path=$dirPath.'/'.$groupId.'.jpg';
  306. $a = getGroupAvatar($imgList,1,$path);
  307. $url='';
  308. if($a){
  309. $upload=new Upload();
  310. $newPath=$upload->uploadLocalAvatar($path,[],$groupId);
  311. if($newPath){
  312. GroupModel::where('group_id',$group_id)->update(['avatar'=>$newPath]);
  313. $url=avatarUrl($newPath);
  314. }
  315. }
  316. // 删除目录下的所有文件
  317. $files = glob($dirPath . '/*'); // 获取目录下所有文件路径
  318. foreach ($files as $file) {
  319. if (is_file($file)) { // 如果是文件则删除
  320. unlink($file);
  321. }
  322. }
  323. return $url;
  324. }
  325. // 加入群
  326. public function joinGroup(){
  327. $param = $this->request->param();
  328. $uid=$this->userInfo['user_id'];
  329. $group_id = explode('-', $param['group_id'])[1];
  330. $inviteUid=$param['inviteUid'] ?? '';
  331. $groupUserCount=GroupUser::where(['group_id'=>$group_id,'status'=>1])->count();
  332. $groupUser=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->find();
  333. if($groupUser){
  334. return warning('您已经加入该群!');
  335. }
  336. if(($groupUserCount+1) > $this->chatSetting['groupUserMax'] && $this->chatSetting['groupUserMax']!=0){
  337. return warning("人数不能超过".$this->chatSetting['groupUserMax']."人!");
  338. }
  339. try{
  340. $data=[
  341. 'group_id'=>$group_id,
  342. 'user_id'=>$uid,
  343. 'role'=>3,
  344. 'invite_id'=>$inviteUid
  345. ];
  346. GroupUser::create($data);
  347. $url=GroupModel::setGroupAvatar($group_id);
  348. $action='joinGroup';
  349. event('GroupChange', ['action' => $action, 'group_id' => $group_id, 'param' => $param]);
  350. wsSendMsg($group_id,"addGroupUser",['group_id'=>$param['group_id'],'avatar'=>$url],1);
  351. return success('加入成功');
  352. }catch(Exception $e){
  353. return error($e->getMessage());
  354. }
  355. }
  356. // 更换群主
  357. public function changeOwner()
  358. {
  359. $user_id = $this->request->param('user_id');
  360. $id = $this->request->param('id');
  361. $group_id = explode('-', $id)[1];
  362. $uid=$this->userInfo['user_id'];
  363. $group=GroupModel::where('group_id',$group_id)->find();
  364. if(!$group){
  365. return warning('群组不存在');
  366. }
  367. $user=User::where('user_id',$user_id)->find();
  368. if(!$user){
  369. return warning('用户不存在');
  370. }
  371. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$uid])->value('role');
  372. if($role>1){
  373. return warning('您没有操作权限!');
  374. }
  375. Db::startTrans();
  376. try{
  377. GroupUser::where('group_id',$group_id)->where('user_id',$user_id)->update(['role'=>1]);
  378. GroupUser::where('group_id',$group_id)->where('user_id',$group->owner_id)->update(['role'=>3]);
  379. $group->owner_id=$user_id;
  380. $group->save();
  381. wsSendMsg($group_id,"changeOwner",['group_id'=>'group-'.$group_id,'user_id'=>$user_id],1);
  382. Db::commit();
  383. return success('转让成功');
  384. }catch (\Exception $e){
  385. Db::rollback();
  386. return warning('更换失败');
  387. }
  388. }
  389. }