使用本擴展前需要登錄 即時通信 IM 控制檯 創建應用,配置管理員、獲取 app_id、Key 等關鍵信息
更多請查看並熟讀 即時通信 IM 服務端API , REST API 接口列表
一 騰訊雲IM API(tp5通常放在extend目錄下)
<?php
namespace tencentyun\im;
/**
-
騰訊IM API
*/
class im{
private $sdkappid; // 創建應用時即時通信 IM 控制檯分配的 SDKAppID
private $identifier; // 必須爲 App 管理員帳號
private $key; // 私鑰
private $usersig; // 簽名
private $random; // 隨機數
private $postfix; // url參數
public function __construct($sdkappid,$identifier,$key) {
$this->sdkappid = $sdkappid;
$this->identifier = $identifier;
$this->key = $key;
$this->usersig = $this->genSig($identifier);
$this->random = (int)$this->nonce_str();
$this->postfix = '?sdkappid='.$this->sdkappid.'&identifier='.$this->identifier.'&usersig='.$this->usersig.'&random='.$this->random.'&contenttype=json';
}
/**- 用於 url 的 base64 encode
- '+' => '*', '/' => '-', '=' => '_'
- @param string $string 需要編碼的數據
- @return string 編碼後的base64串,失敗返回false
- @throws \Exception
/
private function base64_url_encode($string) {
static $replace = Array('+' => '', '/' => '-', '=' => '_');
$base64 = base64_encode($string);
if ($base64 === false) {
throw new \Exception('base64_encode error');
}
return str_replace(array_keys($replace), array_values($replace), $base64);
}
/** - 用於 url 的 base64 decode
- '+' => '*', '/' => '-', '=' => '_'
- @param string $base64 需要解碼的base64串
- @return string 解碼後的數據,失敗返回false
- @throws \Exception
/
private function base64_url_decode($base64) {
static $replace = Array('+' => '', '/' => '-', '=' => '_');
$string = str_replace(array_values($replace), array_keys($replace), $base64);
$result = base64_decode($string);
if ($result == false) {
throw new \Exception('base64_url_decode error');
}
return $result;
}
/** - 使用 hmac sha256 生成 sig 字段內容,經過 base64 編碼
- @param $identifier 用戶名,utf-8 編碼
- @param $curr_time 當前生成 sig 的 unix 時間戳
- @param $expire 有效期,單位秒
- @param $base64_userbuf base64 編碼後的 userbuf
- @param $userbuf_enabled 是否開啓 userbuf
- @return string base64 後的 sig
*/
private function hmacsha256($identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled) {
$content_to_be_signed = "TLS.identifier:" . $identifier . "\n"
. "TLS.sdkappid:" . $this->sdkappid . "\n"
. "TLS.time:" . $curr_time . "\n"
. "TLS.expire:" . $expire . "\n";
if (true == $userbuf_enabled) {
$content_to_be_signed .= "TLS.userbuf:" . $base64_userbuf . "\n";
}
return base64_encode(hash_hmac( 'sha256', $content_to_be_signed, $this->key, true));
}
/** - 生成簽名。
- @param $identifier 用戶賬號
- @param int $expire 過期時間,單位秒,默認 180 天
- @param $userbuf base64 編碼後的 userbuf
- @param $userbuf_enabled 是否開啓 userbuf
- @return string 簽名字符串
- @throws \Exception
*/
private function __genSig($identifier, $expire, $userbuf, $userbuf_enabled) {
$curr_time = time();
$sig_array = Array(
'TLS.ver' => '2.0',
'TLS.identifier' => strval($identifier),
'TLS.sdkappid' => intval($this->sdkappid),
'TLS.expire' => intval($expire),
'TLS.time' => intval($curr_time)
);
$base64_userbuf = '';
if (true == $userbuf_enabled) {
$base64_userbuf = base64_encode($userbuf);
$sig_array['TLS.userbuf'] = strval($base64_userbuf);
}
$sig_array['TLS.sig'] = $this->hmacsha256($identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled);
if ($sig_array['TLS.sig'] === false) {
throw new \Exception('base64_encode error');
}
$json_str_sig = json_encode($sig_array);
if ($json_str_sig === false) {
throw new \Exception('json_encode error');
}
$compressed = gzcompress($json_str_sig);
if ($compressed === false) {
throw new \Exception('gzcompress error');
}
return $this->base64_url_encode($compressed);
}
/** - 生成簽名
- @param $identifier 用戶賬號
- @param int $expire 過期時間,單位秒,默認 180 天
- @return string 簽名字符串
- @throws \Exception
/
public function genSig($identifier, $expire=6060*24) {
return $this->__genSig($identifier, $expire, '', false);
}
/** - 帶 userbuf 生成簽名。
- @param $identifier 用戶賬號
- @param int $expire 過期時間,單位秒,默認 180 天
- @param string $userbuf 用戶數據
- @return string 簽名字符串
- @throws \Exception
*/
public function genSigWithUserBuf($identifier, $expire, $userbuf) {
return $this->__genSig($identifier, $expire, $userbuf, true);
}
/** - 驗證簽名。
- @param string $sig 簽名內容
- @param string $identifier 需要驗證用戶名,utf-8 編碼
- @param int $init_time 返回的生成時間,unix 時間戳
- @param int $expire_time 返回的有效期,單位秒
- @param string $userbuf 返回的用戶數據
- @param string $error_msg 失敗時的錯誤信息
- @return boolean 驗證是否成功
- @throws \Exception
*/
private function __verifySig($sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg) {
try {
$error_msg = '';
$compressed_sig = $this->base64_url_decode($sig);
$pre_level = error_reporting(E_ERROR);
$uncompressed_sig = gzuncompress($compressed_sig);
error_reporting($pre_level);
if ($uncompressed_sig === false) {
throw new \Exception('gzuncompress error');
}
$sig_doc = json_decode($uncompressed_sig);
if ($sig_doc == false) {
throw new \Exception('json_decode error');
}
$sig_doc = (array)$sig_doc;
if ($sig_doc['TLS.identifier'] !== $identifier) {
throw new \Exception("identifier dosen't match");
}
if ($sig_doc['TLS.sdkappid'] != $this->sdkappid) {
throw new \Exception("sdkappid dosen't match");
}
$sig = $sig_doc['TLS.sig'];
if ($sig == false) {
throw new \Exception('sig field is missing');
}
$init_time = $sig_doc['TLS.time'];
$expire_time = $sig_doc['TLS.expire'];
$curr_time = time();
if ($curr_time > $init_time+$expire_time) {
throw new \Exception('sig expired');
}
$userbuf_enabled = false;
$base64_userbuf = '';
if (isset($sig_doc['TLS.userbuf'])) {
$base64_userbuf = $sig_doc['TLS.userbuf'];
$userbuf = base64_decode($base64_userbuf);
$userbuf_enabled = true;
}
$sigCalculated = $this->hmacsha256($identifier, $init_time, $expire_time, $base64_userbuf, $userbuf_enabled);
if ($sig != $sigCalculated) {
throw new \Exception('verify failed');
}
return true;
} catch (\Exception $ex) {
$error_msg = $ex->getMessage();
return false;
}
}
/** - 帶 userbuf 驗證簽名。
- @param string $sig 簽名內容
- @param string $identifier 需要驗證用戶名,utf-8 編碼
- @param int $init_time 返回的生成時間,unix 時間戳
- @param int $expire_time 返回的有效期,單位秒
- @param string $error_msg 失敗時的錯誤信息
- @return boolean 驗證是否成功
- @throws \Exception
*/
public function verifySig($sig, $identifier, &$init_time, &$expire_time, &$error_msg) {
$userbuf = '';
return $this->__verifySig($sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg);
}
/** - 驗證簽名
- @param string $sig 簽名內容
- @param string $identifier 需要驗證用戶名,utf-8 編碼
- @param int $init_time 返回的生成時間,unix 時間戳
- @param int $expire_time 返回的有效期,單位秒
- @param string $userbuf 返回的用戶數據
- @param string $error_msg 失敗時的錯誤信息
- @return boolean 驗證是否成功
- @throws \Exception
*/
public function verifySigWithUserBuf($sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg) {
return $this->__verifySig($sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg);
}
/****賬號管理***/
/** - 創建IM用戶
- [set_user description]
- @Author 念天地之悠悠
- @DateTime 2019-12-23
- @param [type] $uid [description] 用戶id(用戶名)
- @param [type] $nickname [description] 用戶暱稱
- @param [type] $img_url [description] 頭像地址
*/
public function set_user($uid,$nickname,$img_url){
$url = 'https://console.tim.qq.com/v4/im_open_login_svc/account_import'.$this->postfix;
$list = ['Identifier'=>(string)$uid,'Nick'=>$nickname,'FaceUrl'=>$img_url];
$json = json_encode($list,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$json);
$info = json_decode($info,true);
return $info;
}
/** - 檢查用戶是否存在
- [verify_user description]
- @Author 念天地之悠悠
- @DateTime 2019-12-24
- @param [array] $uids [description] 用戶id(用戶名) 例如:[4,5]
- @return [type] [description]
*/
public function verify_user($uids){
$url = 'https://console.tim.qq.com/v4/im_open_login_svc/account_check'.$this->postfix;
$arr = [];
foreach ($uids as $key => $value) {
$arr[] = ['UserID'=>(string)$value];
}
$data = ['CheckItem'=>$arr];
$data = json_encode($data);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 刪除用戶
- [del_user description]
- @Author 念天地之悠悠
- @DateTime 2019-12-24
- @param [array] $uids [description] 用戶id(用戶名) 例如:[4,5]
- @return [type] [description]
*/
public function del_user($uids){
$url = 'https://console.tim.qq.com/v4/im_open_login_svc/account_delete'.$this->postfix;
$arr = [];
foreach ($uids as $key => $value) {
$arr[] = ['UserID'=>(string)$value];
}
$data = ['DeleteItem'=>$arr];
$data = json_encode($data);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 本接口適用於將 App 用戶帳號的登錄態(如 UserSig)失效。
- 例如,開發者判斷一個用戶爲惡意帳號後,可以調用本接口將該用戶當前的登錄態失效,這樣用戶使用歷史 UserSig 登錄即時通信 IM 會失敗。
- [kick_user description]
- @Author 念天地之悠悠
- @DateTime 2019-12-24
- @param [type] $uid [description] 用戶id(用戶名)
- @return [type] [description]
*/
public function kick_user($uid){
$url = 'https://console.tim.qq.com/v4/im_open_login_svc/kick'.$this->postfix;
$data = json_encode(['Identifier'=>(string)$uid]);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/****單聊消息管理***/
/** - 單發單聊消息
- [push_msg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-24
- @param [type] $From_Account [description] 消息發送方 Identifier(用於指定發送消息方帳號)傳空表示管理員發送信息
- @param [type] $To_Account [description] 消息接收方 Identifier
- @param [type] $MsgBody [description] 消息體
- @param [type] $SyncOtherMachine [description] 1:把消息同步到 From_Account 在線終端和漫遊上;2:消息不同步至 From_Account
-
@return [type] [description]
*/
public function push_msg($From_Account='',$To_Account,$MsgBody,$SyncOtherMachine=1){
$url = 'https://console.tim.qq.com/v4/openim/sendmsg'.$this->postfix;
$data = [
'SyncOtherMachine' => (int)$SyncOtherMachine,
'To_Account' => (string)$To_Account,
'MsgRandom' => (int)$this->nonce_str(8), // 隨機數
'MsgTimeStamp' => time(), // 時間戳
'MsgBody' => [$MsgBody],
];if (!empty($From_Account)) {
$data['From_Account'] = (string)$From_Account;
}
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 批量單發消息
- [batch_sendmsg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @param [type] $To_Account [description] 接收方列表 如['user1','user2']
- @param [type] $MsgBody [description] 消息體
- @param [type] $From_Account [description] 發送人uid
- @param integer $SyncOtherMachine [description] 1:把消息同步到 From_Account 在線終端和漫遊上;2:消息不同步至 From_Account
- @return [type] [description]
*/
public function batch_sendmsg($To_Account,$MsgBody,$From_Account,$SyncOtherMachine=2){
$url = 'https://console.tim.qq.com/v4/openim/batchsendmsg'.$this->postfix;
$data = [
'SyncOtherMachine' => $SyncOtherMachine,
"From_Account" => (string)$From_Account,
'To_Account' => $To_Account,
'MsgRandom' => (int)$this->nonce_str(8),
'MsgBody' => [$MsgBody]
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 撤回單聊消息
- [msg_withdraw description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @param [type] $From_Account [description] 消息發送方 UserID
- @param [type] $To_Account [description] 消息接收方 UserID
- @param [type] $MsgKey [description] 待撤回消息的唯一標識
- @return [type] [description]
*/
public function msg_withdraw($From_Account,$To_Account,$MsgKey){
$url = 'https://console.tim.qq.com/v4/openim/admin_msgwithdraw'.$this->postfix;
$data = [
'From_Account' => (string)$From_Account,
'To_Account' => (string)$To_Account,
'MsgKey' => $MsgKey
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 查詢用戶狀態
- [query_state description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @param [type] $uids [description] 用戶id集 例如 ['user1','user0']
- @return [type] [description]
*/
public function query_state($uids){
$url = 'https://console.tim.qq.com/v4/openim/querystate'.$this->postfix;
foreach ($uids as $key => $value) {
$uids[$key] = (string)$value;
}
$data = ['To_Account' => $uids];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/****用戶資料管理***/
/** - 獲取用戶信息
- [portrait_get description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $uids [description] 用戶id
- @return [type] [description]
*/
public function portrait_get($uids){
$url = 'https://console.tim.qq.com/v4/profile/portrait_get'.$this->postfix;
foreach ($uids as $key => $value) {
$uids[$key] = (string)$value;
}
// 加好友驗證方式
// AllowType_Type_NeedConfirm:需要經過自己確認才能添加自己爲好友
// AllowType_Type_AllowAny:允許任何人添加自己爲好友
// AllowType_Type_DenyAny:不允許任何人添加自己爲好友
// 所在地
// 長度不得超過16個字節,推薦用法如下:
// App 本地定義一套數字到地名的映射關係
// 後臺實際保存的是4個 uint32_t 類型的數字
// 其中第一個 uint32_t 表示國家
// 第二個 uint32_t 用於表示省份
// 第三個 uint32_t 用於表示城市
// 第四個 uint32_t 用於表示區縣
$data = [
'To_Account' => $uids,
'TagList' => [
'Tag_Profile_IM_Nick', // 暱稱
'Tag_Profile_IM_Gender', // 性別 Gender_Type_Unknown 未設置 Gender_Type_Female 女 Gender_Type_Male 男
'Tag_Profile_IM_BirthDay', // 生日 推薦用法:20190419
'Tag_Profile_IM_Location', // 所在地
'Tag_Profile_IM_SelfSignature', // 個性簽名
'Tag_Profile_IM_AllowType', // 加好友驗證方式
'Tag_Profile_IM_Language', // 語言
'Tag_Profile_IM_Image', // 頭像URL
'Tag_Profile_IM_MsgSettings', // 消息設置 Bit0:置0表示接收消息,置1則不接收消息
'Tag_Profile_IM_AdminForbidType', // 管理員禁止加好友標識 AdminForbid_Type_None允許 AdminForbid_Type_SendOut禁止
'Tag_Profile_IM_Level', // 等級
'Tag_Profile_IM_Role', // 角色
]
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 設置用戶資料
- [portrait_set description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @param [type] $From_Account [description] 需要設置該 Identifier 的資料
- @param [type] $ProfileItem [description] 待設置的用戶的資料對象數組,數組中每一個對象都包含了 Tag 和 Value
- @return [type] [description]
*/
public function portrait_set($From_Account,$ProfileItem){
$url = 'https://console.tim.qq.com/v4/profile/portrait_set'.$this->postfix;
$data = [
'From_Account' => (string)$From_Account,
'ProfileItem' => $ProfileItem
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/****好友管理***/
/** - 添加好友
- [friend_add description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @param [type] $From_Account [description] 需要爲該 UserID 添加好友
- @param [type] $To_Account [description] 好友的 UserID
- @param [type] $AddSource [description] 加好友來源字段 加好友來源的關鍵字是 Android,則加好友來源字段是:AddSource_Type_Android
- @param [type] $Remark [description] 好友備註
- @param [type] $GroupName [description] 分組信息
- @param [type] $AddWording [description] 形成好友關係時的附言信息
- @param [type] $AddType [description] 加好友方式 Add_Type_Single 表示單向加好友 Add_Type_Both 表示雙向加好友
- @param integer $ForceAddFlags [description] 管理員強制加好友標記:1表示強制加好友,0表示常規加好友方式
- @return [type] [description]
*/
public function friend_add($From_Account,$To_Account,$AddSource,$Remark,$GroupName,$AddWording='',$AddType='Add_Type_Both',$ForceAddFlags=1){
$url = 'https://console.tim.qq.com/v4/sns/friend_add'.$this->postfix;
$AddFriendItem['To_Account'] = $To_Account;
$AddFriendItem['AddSource'] = 'AddSourceType'.$AddSource;
if (!empty($Remark)) {
$AddFriendItem['Remark'] = $Remark;
}
if (!empty($GroupName)) {
$AddFriendItem['GroupName'] = $GroupName;
}
if (!empty($AddWording)) {
$AddFriendItem['AddWording'] = $AddWording;
}
$data = [
'From_Account' => (string)$From_Account,
'AddFriendItem' => [$AddFriendItem],
'AddType' => $AddType,
'ForceAddFlags' => $ForceAddFlags
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 刪除好友
- [friend_delete description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @param [type] $From_Account [description] 發起用戶
- @param [type] $To_Account [description] 被刪用戶數組
- @param string $DeleteType [description] 類型 Delete_Type_Both 雙向刪除 CheckResult_Type_Single 單向刪除
- @return [type] [description]
*/
public function friend_delete($From_Account,$To_Account,$DeleteType='Delete_Type_Both'){
$url = 'https://console.tim.qq.com/v4/sns/friend_delete'.$this->postfix;
$data = [
'From_Account' => (string)$From_Account,
'To_Account' => $To_Account,
'DeleteType' => $DeleteType
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 添加黑名單
- [black_list_add description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @param [type] $From_Account [description] 發起用戶
- @param [type] $To_Account [description] 對象用戶數組
- @return [type] [description]
*/
public function black_list_add($From_Account,$To_Account){
$url = 'https://console.tim.qq.com/v4/sns/black_list_add'.$this->postfix;
$data = [
'From_Account' => (string)$From_Account,
'To_Account' => $To_Account
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - 刪除黑名單
- [black_list_delete description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @param [type] $From_Account [description] 發起用戶
- @param [type] $To_Account [description] 對象用戶數組
- @return [type] [description]
*/
public function black_list_delete($From_Account,$To_Account){
$url = 'https://console.tim.qq.com/v4/sns/black_list_delete'.$this->postfix;
$data = [
'From_Account' => (string)$From_Account,
'To_Account' => $To_Account
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - curl請求
- [http_request description]
- @Author 念天地之悠悠
- @DateTime 2019-02-21
- @param [type] $url [description] 請求地址
- @param [type] $data [description] 數據
- @param array $headers [description]
- @return [type] [description]
*/
public function http_request($url,$data = null,$headers=array()){
$curl = curl_init();
if( count($headers) >= 1 ){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/** - 隨機32位字符串 純數字
- [nonce_str description]
- @Author 念天地之悠悠
- @DateTime 2019-12-23
- @return [type] [description]
*/
private function nonce_str($num=32){
$result = '';
$str = '0123456789';
for ($i = 0; $i < $num; $i++) {
$result .= $str[rand(0,9)];
}
return $result;
}
}
?>二 引入IM API,編寫通訊接口
<?php
namespace app\api\controller;
use app\common\controller\Base;
use think\Validate;
use think\Db;
use tencentyun\im\im as Tim;
/**
- 通訊
*/
class Im extends Base{
/**- 單發消息
- [send_msg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function send_msg(){
$post_data = input('post.');
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
// MsgType 目前支持的消息對象包括:TIMTextElem(文本消息),TIMFaceElem(表情消息),TIMLocationElem(位置消息)
$MsgBody['MsgType'] = $post_data['MsgType'];
if ($post_data['MsgType'] == 'TIMTextElem') {
$MsgBody['MsgContent'] = ['Text'=>$post_data['txt']];
}elseif ($post_data['MsgType'] == 'TIMFaceElem') {
$MsgBody['MsgContent'] = ['Index'=>$post_data['Index'],'Data'=>$post_data['Data']];
}elseif ($post_data['MsgType'] == 'TIMLocationElem') {
$MsgBody['MsgContent'] = [
'Desc' => $post_data['Desc'],
'Latitude' => $post_data['Latitude'],
'Longitude' => $post_data['Longitude']
];
}else{
return json(returnArr(2,'','消息類型錯誤!'));
}
$info = $im->push_msg($post_data['uid'],$post_data['To_Account'],$MsgBody,$post_data['SyncOtherMachine']);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['MsgKey'],'發送成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 批量單發消息
- [batch_sendmsg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function batch_sendmsg(){
$post_data = input('post.');
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
// MsgType 目前支持的消息對象包括:TIMTextElem(文本消息),TIMFaceElem(表情消息),TIMLocationElem(位置消息)
$MsgBody['MsgType'] = $post_data['MsgType'];
if ($post_data['MsgType'] == 'TIMTextElem') {
$MsgBody['MsgContent'] = ['Text'=>$post_data['txt']];
}elseif ($post_data['MsgType'] == 'TIMFaceElem') {
$MsgBody['MsgContent'] = ['Index'=>$post_data['Index'],'Data'=>$post_data['Data']];
}elseif ($post_data['MsgType'] == 'TIMLocationElem') {
$MsgBody['MsgContent'] = [
'Desc' => $post_data['Desc'],
'Latitude' => $post_data['Latitude'],
'Longitude' => $post_data['Longitude']
];
}else{
return json(returnArr(2,'','消息類型錯誤!'));
}
$users = explode(',', $post_data['To_Account']);
$info = $im->batch_sendmsg($users,$MsgBody,$post_data['uid']);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['MsgKey'],'發送成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 撤回單聊信息
- [msg_withdraw description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function msg_withdraw(){
$post_data = input('post.');
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->msg_withdraw($post_data['uid'],$post_data['To_Account'],$post_data['MsgKey']);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,'','撤回成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 查詢用戶在線狀態
- [query_state description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function query_state(){
$post_data = input('post.');
$arr = explode(',', $post_data['uids']);
if (empty($arr)) {
$ret = returnArr(2,'','獲取查詢用戶失敗!');
}else{
if (count($arr) > 500) {
$ret = returnArr(3,'','最多查詢500個用戶!');
}else{
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->query_state($arr);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['QueryResult'],'查詢成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
}
}
return json($ret);
}
/** - 獲取用戶資料
- [portrait_get description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function portrait_get(){
$post_data = input('post.');
$arr = explode(',', $post_data['uids']);
if (empty($arr)) {
$ret = returnArr(2,'','獲取查詢用戶失敗!');
}else{
if (count($arr) > 100) {
$ret = returnArr(3,'','最多查詢100個用戶!');
}else{
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->portrait_get($arr);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['UserProfileItem'],'查詢成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
}
}
return json($ret);
}
/** - 設置用戶資料
- [portrait_set description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function portrait_set(){
$post_data = input('post.');
$arr = json_decode($post_data['list'],true);
if (empty($arr)) {
$ret = returnArr(2,'','修改參數缺失!');
}else{
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->portrait_set($post_data['uid'],$arr);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,'','修改成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
}
return json($ret);
}
/** - 添加好友
- [friend_add description]
- @Author 念天地之悠悠
- @DateTime 2019-12-25
- @return [type] [description]
*/
public function friend_add(){
$post_data = input('post.');
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->friend_add($post_data['uid'],$post_data['To_Account'],$post_data['AddSource'],$post_data['Remark'],$post_data['GroupName'],$post_data['AddWording'],$post_data['AddType']);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,'','添加成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 刪除好友
- [friend_delete description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @return [type] [description]
*/
public function friend_delete(){
$post_data = input('post.');
$To_Account = explode(',', $post_data['To_Account']);
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->friend_delete($post_data['uid'],$To_Account);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['ResultItem'],'刪除好友成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 加入黑名單
- [black_list_add description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @return [type] [description]
*/
public function black_list_add(){
$post_data = input('post.');
$To_Account = explode(',', $post_data['To_Account']);
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->black_list_add($post_data['uid'],$To_Account);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['ResultItem'],'成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
/** - 刪除黑名單
- [black_list_delete description]
- @Author 念天地之悠悠
- @DateTime 2019-12-26
- @return [type] [description]
*/
public function black_list_delete(){
$post_data = input('post.');
$To_Account = explode(',', $post_data['To_Account']);
$config = config('site');
$im = new Tim($config['SdkAppid'],$config['Identifier'],$config['im_appkey']);
$info = $im->black_list_delete($post_data['uid'],$To_Account);
if ($info['ErrorCode'] == 0) {
$ret = returnArr(1,$info['ResultItem'],'成功!');
}else{
$ret = returnArr(0,'',$info['ErrorInfo']);
}
return json($ret);
}
}
?>