|
@@ -15,12 +15,25 @@ class Easemob extends Api
|
|
|
protected $noNeedRight = ['*'];
|
|
|
|
|
|
protected $appKey;
|
|
|
+ protected $redis;
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
|
//初始化配置
|
|
|
$easemob_config = config('easemob');
|
|
|
$this->appKey = $easemob_config['appkey'];
|
|
|
+
|
|
|
+ //redis
|
|
|
+ $redis = new Redis();
|
|
|
+ $redisconfig = config("redis");
|
|
|
+ $redis->connect($redisconfig["host"], $redisconfig["port"]);
|
|
|
+ if ($redisconfig['redis_pwd']) {
|
|
|
+ $redis->auth($redisconfig['redis_pwd']);
|
|
|
+ }
|
|
|
+ if($redisconfig['redis_selectdb'] > 0){
|
|
|
+ $redis->select($redisconfig['redis_selectdb']);
|
|
|
+ }
|
|
|
+ $this->redis = $redis;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -28,12 +41,19 @@ class Easemob extends Api
|
|
|
* https://docs-im-beta.easemob.com/document/server-side/callback_configurations.html
|
|
|
*
|
|
|
* 群组和聊天室操作:
|
|
|
+ *
|
|
|
* muc:kick 踢出聊天室
|
|
|
* {"callId":"1101231101159883#demo_1215088856588617164","channel_channel":"1101231101159883#demo_9@easemob.com","eventType":"chat","channel_user":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","chat_type":"muc","security":"83f9462c7769df1c979fd9dea88e7400","is_downgrade":false,"content_type":"muc:kick","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","reason":"chatroom kick offline user","is_chatroom":true,"operation":"kick","status":{"description":"","error_code":"ok"}},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync71","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"9","msg_id":"1215088856588617164","timestamp":1700474720591}
|
|
|
+ *
|
|
|
* muc:absence 有成员离开了聊天室
|
|
|
* {"callId":"1101231101159883#demo_1215088856710252128","channel_channel":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","eventType":"chat","channel_user":"admin@easemob.com","chat_type":"muc","security":"ec7d29ea415ea5930c2236089dff3bc2","is_downgrade":false,"content_type":"muc:absence","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","is_chatroom":true,"operation":"absence"},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync108","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"231181574537218","msg_id":"1215088856710252128","timestamp":1700474720619}
|
|
|
+ *
|
|
|
* muc:leave 成员主动退出聊天室
|
|
|
*
|
|
|
+ *
|
|
|
+ * 用户登入
|
|
|
+ * {"callId":"1101231101159883#demo_927e28f2-0e88-4a58-855b-c8d26949ea4f","reason":"login","security":"6b793f812b5ed74f62d3a95d9ea48c8f","os":"android","ip":"182.37.138.94:39768","host":"msync@ebs-ali-beijing-msync60","session_id":"1215076057950983584","appkey":"1101231101159883#demo","user":"1101231101159883#demo_2@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837","version":"4.1.2","timestamp":1700471740680,"status":"online"}
|
|
|
+ *
|
|
|
* 用户登出
|
|
|
* {"callId":"1101231101159883#demo_ee82832b-3455-472d-9024-3706b1e519a7","reason":"logout","security":"6266f2a31bc3b603d8458f3d39a49d47","os":"android","ip":"182.37.138.94:39768","host":"msync@ebs-ali-beijing-msync60","session_id":"1215076057950983584","appkey":"1101231101159883#demo","user":"1101231101159883#demo_2@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837","version":"4.1.2","timestamp":1700472505682,"status":"offline"}
|
|
|
*
|
|
@@ -50,20 +70,102 @@ class Easemob extends Api
|
|
|
|
|
|
//解析数据格式
|
|
|
|
|
|
+ //用户登入
|
|
|
+ if(isset($input['reason']) && $input['reason'] == 'login' && isset($input['status']) && $input['status'] == 'online'){
|
|
|
+
|
|
|
+ $user = $input['user'];
|
|
|
+ $uid = $this->get_easemob_uid($user);//用户主键id
|
|
|
+
|
|
|
+ //开始处理
|
|
|
+ $loginToday = Db::name('user')->where('id',$uid)->whereTime('onlinetime', 'today')->find();
|
|
|
+ if ($loginToday){
|
|
|
+ \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$uid]);
|
|
|
+ }else{
|
|
|
+ \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$uid]);
|
|
|
+ }
|
|
|
+ exit;
|
|
|
+ }
|
|
|
+
|
|
|
//用户登出
|
|
|
//用户登出(被其他设备踢掉)
|
|
|
if(isset($input['reason']) && ( $input['reason'] == 'logout' || $input['status'] == 'replaced') && isset($input['status']) && $input['status'] == 'offline'){
|
|
|
- //登出
|
|
|
+
|
|
|
$user = $input['user'];
|
|
|
$uid = $this->get_easemob_uid($user);//用户主键id
|
|
|
|
|
|
//开始处理
|
|
|
+ // 更新用户在线状态为离线
|
|
|
+ \app\common\model\User::update(["is_online"=>0],["id"=>$uid]);
|
|
|
+ \app\common\model\User::update(["is_live"=>0],["id"=>$uid]);
|
|
|
+
|
|
|
+ $livingUserPartyId = $this->redis->hGet("livingUser", $uid);
|
|
|
+ if ($livingUserPartyId) {
|
|
|
+ // 扣除在线用户在房间情况
|
|
|
+ $this->redis->hDel("livingUser", $uid);
|
|
|
+ $this->redis->HDel("online_" . $livingUserPartyId, $uid);
|
|
|
+ $this->redis->zRem("party_user_".$livingUserPartyId,$uid); //新加的
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取用户所在群信息
|
|
|
+ $groupids = $this->getJoindGroupList($uid);
|
|
|
+// \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
|
|
|
+ if($groupids["ErrorCode"] == 0) {
|
|
|
+ $grouplist = $groupids["GroupIdList"];
|
|
|
+ $groupsites = [];
|
|
|
+ if($grouplist) foreach($grouplist as $k => $v) {
|
|
|
+ // 获取群组消息
|
|
|
+ $groupInfo = $this->getGroupInfo($v["GroupId"]);
|
|
|
+
|
|
|
+ if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
|
|
|
+
|
|
|
+ // 踢出用户在线组
|
|
|
+ $this->redis->HDel("online_".$v["GroupId"],$uid);
|
|
|
+ $this->redis->zRem("party_user_".$v["GroupId"],$uid); //新加的
|
|
|
+
|
|
|
+ // 更新麦位前四
|
|
|
+ $this->updatePosition($uid,$v["GroupId"]);
|
|
|
+
|
|
|
+ // 取消排麦
|
|
|
+ $this->cancelLineup($uid,$v["GroupId"]);
|
|
|
+
|
|
|
+ // 房间状态变更
|
|
|
+ $partyInfo = $this->redis->get("party_".$v["GroupId"]);
|
|
|
+ if($partyInfo) {
|
|
|
+ $partyInfo = json_decode($partyInfo,true);
|
|
|
+ $memCount = count($this->redis->hGetAll("online_".$v["GroupId"]));
|
|
|
+ if($memCount <= 0) {
|
|
|
+ $partyInfo["is_online"] = 0;
|
|
|
+ $this->redis->set("party_".$v["GroupId"],json_encode($partyInfo));
|
|
|
+ \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $liveInfo = $this->redis->get("live_".$v["GroupId"]);
|
|
|
+ if($liveInfo) {
|
|
|
+ $liveInfo = json_decode($liveInfo,true);
|
|
|
+ $memCount = count($this->redis->hGetAll("online_".$v["GroupId"]));
|
|
|
+ if($memCount <= 0) {
|
|
|
+ $liveInfo["is_online"] = 0;
|
|
|
+ $this->redis->set("live_".$v["GroupId"],json_encode($liveInfo));
|
|
|
+ \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 强制下麦
|
|
|
+ print_r($this->downSite($uid,$groupsites));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ exit;
|
|
|
}
|
|
|
|
|
|
|
|
|
- //
|
|
|
- if(isset($input['chat_type']) && $input['chat_type'] == 'muc' && isset($input['eventType']) && $input['eventType'] == 'chat' ){
|
|
|
+ //群组和聊天室操作:
|
|
|
+ //muc:kick 踢出聊天室
|
|
|
+ //muc:absence 有成员离开了聊天室
|
|
|
+ //muc:leave 成员主动退出聊天室
|
|
|
|
|
|
+ if(isset($input['chat_type']) && $input['chat_type'] == 'muc' && isset($input['eventType']) && $input['eventType'] == 'chat'){
|
|
|
|
|
|
$room_id = '';
|
|
|
$is_chatroom = false;
|
|
@@ -95,7 +197,7 @@ class Easemob extends Api
|
|
|
//用户
|
|
|
$uid = 0;
|
|
|
if(isset($input['from'])){
|
|
|
- $uid = $this->get_easemob_uid($user);
|
|
|
+ $uid = $this->get_easemob_uid($input['from']);
|
|
|
}
|
|
|
|
|
|
if($uid && $room_id && $is_chatroom && $operation && $error_code){
|
|
@@ -107,6 +209,209 @@ class Easemob extends Api
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取用户所加入的聊天室
|
|
|
+ */
|
|
|
+ public function getJoindGroupList($user_id) {
|
|
|
+ $random = rand(10000000,99999999);
|
|
|
+ $usersig = $this->usersig("administrator");
|
|
|
+ // 获取配置信息
|
|
|
+ $config = config("tencent_im");
|
|
|
+ $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_joined_group_list";
|
|
|
+ $url .= "?sdkappid=".$config["sdkappid"];
|
|
|
+ $url .= "&identifier=administrator";
|
|
|
+ $url .= "&usersig=".$usersig;
|
|
|
+ $url .= "&random=".$random;
|
|
|
+ $url .= "&contenttype=json";
|
|
|
+
|
|
|
+ $tencentObj = new tencentim($url);
|
|
|
+ $data = [];
|
|
|
+ $data["Member_Account"] = $user_id;
|
|
|
+ $data["WithHugeGroups"] = 1;
|
|
|
+ $data["GroupType"] = 'AVChatRoom';
|
|
|
+ $data["ResponseFilter"] = ["GroupBaseInfoFilter" => ['GroupId'] ];
|
|
|
+ $groupInfo = $tencentObj->toSend($data);
|
|
|
+// \app\common\model\Test::update(["content"=>json_encode($groupInfo)],["id"=>1]);
|
|
|
+ return $groupInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取群组信息
|
|
|
+ */
|
|
|
+ public function getGroupInfo($party_id='0') {
|
|
|
+ $party_id = !empty($party_id) ? $party_id : $this->request->param('part_id',0);
|
|
|
+ $random = rand(10000000,99999999);
|
|
|
+ $usersig = $this->usersig("administrator");
|
|
|
+ // 获取配置信息
|
|
|
+ $config = config("tencent_im");
|
|
|
+ $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_info";
|
|
|
+ $url .= "?sdkappid=".$config["sdkappid"];
|
|
|
+ $url .= "&identifier=administrator";
|
|
|
+ $url .= "&usersig=".$usersig;
|
|
|
+ $url .= "&random==".$random;
|
|
|
+ $url .= "&contenttype=json";
|
|
|
+ $tencentObj = new tencentim($url);
|
|
|
+ $data = [];
|
|
|
+ $data["GroupIdList"] = [(string)$party_id];
|
|
|
+ $data["ResponseFilter"] = [
|
|
|
+ "GroupBaseInfoFilter" => ["GroupId","Type","Name","FaceUrl","Owner_Account","MemberNum"],
|
|
|
+ "MemberInfoFilter" => ["Account","Role"],
|
|
|
+ "AppDefinedDataFilter_Group" => ["roomInfo"],
|
|
|
+ ];
|
|
|
+ $groupInfo = $tencentObj->toSend($data);
|
|
|
+ $this->success('获取成功',$groupInfo['GroupInfo']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新麦位前四
|
|
|
+ */
|
|
|
+ public function updatePosition($user_id,$party_id) {
|
|
|
+ // 获取用户头像
|
|
|
+ $userInfo = \app\common\model\User::field("avatar")->where(["id"=>$user_id])->find();
|
|
|
+ $userAvatar = isset($userInfo["avatar"])?$userInfo["avatar"]:"";
|
|
|
+
|
|
|
+ // 更新下麦时间
|
|
|
+ $update = [];
|
|
|
+ $update["offsite_time"] = time();
|
|
|
+ $update["status"] = 2;
|
|
|
+ \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
|
|
|
+
|
|
|
+ //
|
|
|
+ $redis = new Redis();
|
|
|
+ $redisconfig = config("redis");
|
|
|
+ $redis->connect($redisconfig["host"], $redisconfig["port"]);
|
|
|
+ if ($redisconfig['redis_pwd']) {
|
|
|
+ $redis->auth($redisconfig['redis_pwd']);
|
|
|
+ }
|
|
|
+ if($redisconfig['redis_selectdb'] > 0){
|
|
|
+ $redis->select($redisconfig['redis_selectdb']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $room_type = Db::name('party')->where('id',$party_id)->value('room_type');
|
|
|
+ $redisData = $redis->get($room_type."_".$party_id);
|
|
|
+
|
|
|
+ if(!$redisData){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ $partyInfo = json_decode($redisData,true);
|
|
|
+
|
|
|
+ // 遍历已有头像
|
|
|
+ $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
|
|
|
+ if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $userAvatar) unset($partyInfo["party_user"][$k]);
|
|
|
+
|
|
|
+ $redis->set($room_type."_".$party_id,json_encode($partyInfo));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消排麦
|
|
|
+ */
|
|
|
+ public function cancelLineUp($user_id,$party_id) {
|
|
|
+ if (!$party_id || !$user_id) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $redis = new Redis();
|
|
|
+ $redisconfig = config("redis");
|
|
|
+ $redis->connect($redisconfig["host"], $redisconfig["port"]);
|
|
|
+ if ($redisconfig['redis_pwd']) {
|
|
|
+ $redis->auth($redisconfig['redis_pwd']);
|
|
|
+ }
|
|
|
+ if($redisconfig['redis_selectdb'] > 0){
|
|
|
+ $redis->select($redisconfig['redis_selectdb']);
|
|
|
+ }
|
|
|
+ $data=unserialize($redis->hGet("party_lineup",$party_id));
|
|
|
+ // 更改红点排麦数量
|
|
|
+ $num = 0;
|
|
|
+ $send = false;
|
|
|
+ if($data) foreach($data as $k => $v) {
|
|
|
+ if($v["user_id"] == $user_id) {
|
|
|
+ $send = true;
|
|
|
+ unset($data[$k]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $redis->hSet("party_lineup",$party_id,serialize($data));
|
|
|
+
|
|
|
+ if($send) {
|
|
|
+ is_array($data) && $num = count($data);
|
|
|
+
|
|
|
+ $random = rand(10000000,99999999);
|
|
|
+ $usersig = $this->usersig("administrator");
|
|
|
+ // 获取配置信息
|
|
|
+ $config = config("tencent_im");
|
|
|
+ $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
|
|
|
+ $url .= "?sdkappid=".$config["sdkappid"];
|
|
|
+ $url .= "&identifier=administrator";
|
|
|
+ $url .= "&usersig=".$usersig;
|
|
|
+ $url .= "&random=".$random;
|
|
|
+ $url .= "&contenttype=json";
|
|
|
+ $tencentObj = new tencentim($url);
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ $data["GroupId"] = $party_id;
|
|
|
+ $data["Random"] = rand(1000000,9999999);
|
|
|
+ $message = [
|
|
|
+ "message" => json_encode(["type"=>23,"content"=>$num]),
|
|
|
+ "version" => "1.0",
|
|
|
+ "action" => 301,
|
|
|
+ "command" => ""
|
|
|
+ ];
|
|
|
+ $data["MsgBody"][] = [
|
|
|
+ "MsgType" => "TIMCustomElem",
|
|
|
+ "MsgContent" => [
|
|
|
+ "Data"=> json_encode($message)
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ $tencentObj->toSend($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 强制下线
|
|
|
+ */
|
|
|
+ public function downSite($user_id,$groupsites) {
|
|
|
+ $random = rand(10000000,99999999);
|
|
|
+ $usersig = $this->usersig("administrator");
|
|
|
+ // 获取配置信息
|
|
|
+ $config = config("tencent_im");
|
|
|
+ $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
|
|
|
+ $url .= "?sdkappid=".$config["sdkappid"];
|
|
|
+ $url .= "&identifier=administrator";
|
|
|
+ $url .= "&usersig=".$usersig;
|
|
|
+ $url .= "&random=".$random;
|
|
|
+ $url .= "&contenttype=json";
|
|
|
+ $tencentObj = new tencentim($url);
|
|
|
+
|
|
|
+ // 先更新下麦时间
|
|
|
+ $update = [];
|
|
|
+ $update["offsite_time"] = time();
|
|
|
+ $update["status"] = 2;
|
|
|
+ \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
|
|
|
+
|
|
|
+ // 循环房间
|
|
|
+ if($groupsites) foreach($groupsites as $k => $v) {
|
|
|
+ // 循环座位
|
|
|
+ $data = [];
|
|
|
+ foreach($v["AppDefinedData"] as $m => $n) {
|
|
|
+ // 解析字段信息
|
|
|
+ $siteInfo = json_decode($n["Value"],true);
|
|
|
+ if($m != 0 && isset($siteInfo["status"]) && $siteInfo["status"] == 1 && isset($siteInfo["status"]) && $siteInfo["user"] == $user_id) {
|
|
|
+ $data["AppDefinedData"][] = [
|
|
|
+ "Key" => $n["Key"],
|
|
|
+ "Value" => '{"mute":false,"status":0,"user":""}',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!empty($data)) {
|
|
|
+ $data["GroupId"] = $v["GroupId"];
|
|
|
+ return $tencentObj->toSend($data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
//输入: 1101231101159883#demo_9@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837
|
|
|
//输出: 9
|
|
|
|