belongsTo(User::class); } public function members() { return $this->hasMany(CommunityMember::class); } public function isApplied($user_id) { return $this->members()->where('user_id', $user_id)->where('status', 1)->count()?1:0; } public function links() { return $this->hasMany(CommunityGroupLink::class); } public function link() { return $this->hasOne(CommunityGroupLink::class); } public function complaints() { return $this->hasMany(CommunityComplaintHistory::class); } public function paasCommunityIds($paas_id) { $paas_community_ids = PaasCommunity::where('paas_id', $paas_id)->distinct('community_id')->pluck('community_id')->toArray(); return $paas_community_ids; } public function moment(){ return $this->hasMany(CommunityMoment::class); } public function topic(){ return $this->hasMany(CommunityTopic::class, 'community_id'); } /** * 创建网易云信群 * @return [type] [description] */ public function createIMGroup() { $owner_user = $this->user; if (empty($owner_user)) { return false; } if (empty($owner_user->IMUser)) { $result = $owner_user->createIMUser(); // throw new \Exception("没有创建网易云信账号 用户id:".$owner_user->id, 1); // return false; } $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $im_service->createGroup($this->title,$owner_user->id,[],$announcement='','',$msg='欢迎加入',$magree='0',$joinmode='0',$custom='0', $this->logo); $tid = 0; if ($result['code'] == 200) { $this->tid = $result['tid']; $this->save(); $tid = $result['tid']; }else{ return $tid=0; } return $tid; } /** * 修改网易云信群 */ public function updateIMGroup() { $owner_user = $this->user; if (empty($owner_user)) { return false; } if (empty($owner_user->IMUser)) { throw new \Exception("没有创建网易云信账号,用户id:".$owner_user->id, 1); return false; } $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $im_service->updateGroup($this->tid,$owner_user->id,$this->title,'',$this->intro,'0','', $this->logo); if ($result['code'] != 200) { return false; } return true; } /** * 邀请加入社群 * @param [type] $members 被邀请成员IM accid 数组 */ public function addIntoIMGroup($members) { try { //群主 $owner_user = $this->user; if (empty($owner_user)) { throw new \Exception("没有福恋账号", 1); } if (empty($owner_user->IMUser)) { throw new \Exception("没有创建网易云信账号,用户id:".$owner_user->id, 1); } $user_name = User::where('id', $members[0])->value('nickname'); //成员 $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $im_service->addIntoGroup($this->tid,$owner_user->id,$members,$magree='0',$msg='欢迎【'.$user_name.'】加入社群'); $result = $this->dealAddIM($result, $members); if (empty($result)) throw new \Exception("添加网易群成员失败", 1); return true; } catch (\Exception $e) { $this->getError($e, false); return false; } } /** * 邀请入群 * @param [type] $result [description] * @param [type] $members [description] * @return [type] [description] */ public function dealAddIM($result, $members) { if ($result['code'] != 200) { $reason = $result['msg']??$result['code']; CommunityMember::whereIn('user_id', $members)->where('community_id', $this->id)->update(['is_sync_im'=>-1, 'fail_reason'=>$reason]); return false; }else{ $accid = []; if (isset($result['faccid'])) {//部分同步失败 $accid = $result['faccid']['accid']; $reason = $result['faccid']['msg']; //修改失败状态和原因 CommunityMember::whereIn('user_id', $accid)->where('community_id', $this->id)->update(['is_sync_im'=>-1, 'fail_reason'=>$reason]); } //去除失败accid; $success_accid = array_diff($members,$accid); CommunityMember::whereIn('user_id', $success_accid)->where('community_id', $this->id)->update(['is_sync_im'=>1]); return true; } } public function kickFromIMGroup($owner, $member) { try { // $result = $this->groupInfo([$this->tid], 1); // dd($result); $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $result = $im_service->kickFromGroup($this->tid,$owner,$member); if ($result['code'] != 200) { throw new \Exception("网易IM踢人出群失败", 1); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 是否是IM群管理员 * @param [type] $accid [description] * @return boolean [description] */ public function isIMGroupManager($accid) { } /** * 任命IM群管理员 * @param [type] $members [description] */ public function addIMGroupManager($members) { try { $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $result = $im_service->addGroupManager($this->tid,$this->user_id,$members); if ($result['code'] != 200) { throw new \Exception("网易IM任命群管理员失败", 1); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 移除IM管理员 * @param [type] $members [description] * @return [type] [description] */ public function removeIMGroupManager($members) { try { $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $im_service->removeGroupManager($this->tid,$this->user_id,$members); if ($result['code'] != 200) { throw new \Exception("网易IM移除群管理员失败", 1); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function groupInfo($tids, $ope) { try { $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); $result = $im_service->queryGroup($tids, $ope); // dd($result); return $result; } catch (\Exception $e) { $this->getError($e); return false; } } }