error("参数缺失!"); $this->sendMessageToUser($from_user,$to_user,$message); } /** * 发送消息给某人 */ //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json public function sendMessageToUser($from_user,$to_user,$message) { $random = rand(10000000,99999999); $usersig = $this->usersig("administrator"); // 获取配置信息 $config = config("tencent_im"); $url = "https://console.tim.qq.com/v4/openim/sendmsg"; $url .= "?sdkappid=".$config["sdkappid"]; $url .= "&identifier=administrator"; $url .= "&usersig=".$usersig; $url .= "&random=".$random; $url .= "&contenttype=json"; $tencentObj = new tencentim($url); $data = []; $data["SyncOtherMachine"] = 1; $data["From_Account"] = (string)$from_user; $data["To_Account"] = (string)$to_user; $data["MsgRandom"] = rand(1000000,9999999); $data["MsgTimeStamp"] = time(); $data["MsgBody"][] = [ "MsgType" => "TIMTextElem", "MsgContent" => [ "Text"=> $message ], ]; $res = $tencentObj->toSend($data); if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') { $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error'; return $error; } return true; } /** * 发送自定义消息给某人 */ public function sendCustomMessageToUser($from_user,$to_user,$message) { $random = rand(10000000,99999999); $usersig = $this->usersig("administrator"); // 获取配置信息 $config = config("tencent_im"); $url = "https://console.tim.qq.com/v4/openim/sendmsg"; $url .= "?sdkappid=".$config["sdkappid"]; $url .= "&identifier=administrator"; $url .= "&usersig=".$usersig; $url .= "&random=".$random; $url .= "&contenttype=json"; $tencentObj = new tencentim($url); $data = []; $data["SyncOtherMachine"] = 1;//1=消息同步至发送方,2=消息不同步至发送方 $data["From_Account"] = (string)$from_user; $data["To_Account"] = (string)$to_user; $data["MsgRandom"] = rand(1000000,9999999); $data["MsgTimeStamp"] = time(); $data["MsgBody"][] = [ "MsgType" => "TIMCustomElem", "MsgContent" => [ "Data" => json_encode($message), "Desc" => $message['name'], "Ext" => $message['name'], "Sound"=> '', ], ]; $data['CloudCustomData'] = $message['id']; $res = $tencentObj->toSend($data); if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') { $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error'; return $error; } return true; } //注册用户到im public function register($userid,$nickname,$avatar) { $random = rand(10000000,99999999); $usersig = $this->usersig("administrator"); //dump($usersig); // 获取配置信息 $config = config("tencent_im"); $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import"; $url .= "?sdkappid=".$config["sdkappid"]; $url .= "&identifier=administrator"; $url .= "&usersig=".$usersig; $url .= "&random=".$random; $url .= "&contenttype=json"; $tencentObj = new tencentim($url); $data = [ 'UserID' => $userid, 'Nick' => $nickname, 'FaceUrl' => $avatar, ]; $res = $tencentObj->toSend($data); if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') { $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error'; return $error; } return true; } //某个用户是否im在线 public function is_online($userid){ $random = rand(10000000,99999999); $usersig = $this->usersig("administrator"); //dump($usersig); // 获取配置信息 $config = config("tencent_im"); $url = "https://console.tim.qq.com/v4/openim/query_online_status"; $url .= "?sdkappid=".$config["sdkappid"]; $url .= "&identifier=administrator"; $url .= "&usersig=".$usersig; $url .= "&random=".$random; $url .= "&contenttype=json"; $tencentObj = new tencentim($url); $data = [ 'To_Account' => [$userid], ]; $res = $tencentObj->toSend($data); if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') { if( isset($res['QueryResult'][0]['To_Account']) && isset($res['QueryResult'][0]['Status']) && $res['QueryResult'][0]['To_Account'] == $userid && $res['QueryResult'][0]['Status'] == 'Online'){ return true; } } return false; } /** * 获取usersig签名-具体操作 */ public function usersig($user_id) { // 获取配置信息 $config = config("tencent_im"); $usersigObj = new getusersig($config["sdkappid"],$config["key"]); $usersig = $usersigObj->genUserSig($user_id); return $usersig; } }