Before using this extension need to log instant messaging (IM) to create a console application, configuration managers, access to critical information app_id, Key, etc.
Please review and familiarize yourself with instant messaging (IM) server API, REST API interface list
A cloud Tencent IM API (tp5 usually placed extend directory)
<?php
namespace tencentyun\im;
/**
-
API Tencent IM
* /
class IM {
Private $ sdkappid; // create application when instant messaging IM console allocated SDKAppID
Private $ identifier The; // App administrator account must be a
private $ key; // private key is
private $ usersig; / / signature
private $ random; // random
private $ postfix; // url parameters
public function the __construct (sdkappid $, $ identifier, Key $) {
$ this-> = $ sdkappid sdkappid;
$ this-> = $ identifier identifier;
this- $> = $ Key Key;
$ this-> USERSIG = $ this-> genSig ($ identifier The);
$ this-> Random = (int) $ this-> nonce_str ();
$ this-> = postfix '? '.. $ this-> identifier' sdkappid = '.. $ this-> sdkappid' & identifier = & usersig = '.. $ this-> usersig' & random = '. $ this-> random.' & contenttype = json ';
}
/**- Base64 encode url for the
- '+' => '*', '/' => '-', '=' => '_'
- @param string $ string encoded data needs
- After @return string base64 encoded string, else return 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);
}
/** - For base64 decode url of
- '+' => '*', '/' => '-', '=' => '_'
- @param string $ base64 string to be decoded base64
- Decoding data @return string, else return 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;
}
/** - Sig generated using HMAC SHA256 field contents, after base64 encoded
- @param $ identifier user name, utf-8 encoded
- @param $ curr_time current generation of sig unix timestamp
- @param $ expire expiration date, in seconds
- @param $ base64_userbuf base64 encoded userbuf
- @param $ userbuf_enabled whether to open 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));
}
/** - Generating a signature.
- @param $ identifier user account
- @param int $ expire expiration time, in seconds, default 180 days
- userbuf after @param $ userbuf base64 encoded
- @param $ userbuf_enabled whether to open userbuf
- @return string signature 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);
}
/** - Generating a signature
- @param $ identifier user account
- @param int $ expire expiration time, in seconds, default 180 days
- @return string signature string
- @throws \Exception
/
public function genSig($identifier, $expire=6060*24) {
return $this->__genSig($identifier, $expire, '', false);
}
/** - With userbuf generated signature.
- @param $ identifier user account
- @param int $ expire expiration time, in seconds, default 180 days
- @param string $ userbuf user data
- @return string signature string
- @throws \Exception
*/
public function genSigWithUserBuf($identifier, $expire, $userbuf) {
return $this->__genSig($identifier, $expire, $userbuf, true);
}
/** - Verify the signature.
- @param string $ sig signature content
- @param string $ identifier need to verify a user name, utf-8 encoded
- @param int $ init_time generation time of return, unix timestamp
- @param int $ expire_time return period, in seconds
- @param string $ userbuf return of user data
- @param string $ error_msg error message when the failure
- @Return boolean whether the validation is successful
- @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;
}
}
/** - With userbuf verify the signature.
- @param string $ sig signature content
- @param string $ identifier need to verify a user name, utf-8 encoded
- @param int $ init_time generation time of return, unix timestamp
- @param int $ expire_time return period, in seconds
- @param string $ error_msg error message when the failure
- @Return boolean whether the validation is successful
- @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);
}
/** - Verify signatures
- @param string $ sig signature content
- @param string $ identifier need to verify a user name, utf-8 encoded
- @param int $ init_time generation time of return, unix timestamp
- @param int $ expire_time return period, in seconds
- @param string $ userbuf return of user data
- @param string $ error_msg error message when the failure
- @Return boolean whether the validation is successful
- @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);
}
/****账号管理***/
/** - Creating IM users
- [set_user description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-23
- @param [type] $ uid [description] user id (user name)
- @param [type] $ nickname [description] Nickname
- @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;
}
/** - Check if the user exists
- [verify_user description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-24
- @param [array] $ uids [description] user id (user name), for example: [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;
}
/** - delete users
- [del_user description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-24
- @param [array] $ uids [description] user id (user name), for example: [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;
}
/** - An interface adapted to present a user account App login state (e.g. USERSIG) failure.
- For example, developers determine a malicious user after the account, you can call this interface to the user's current login state failure, so users log into the instant messaging (IM) fail to use history UserSig.
- [kick_user description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-24
- @param [type] $ uid [description] user id (user name)
- @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;
}
/****单聊消息管理***/
/** - Single chat message single
- [push_msg description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-24
- @param [type] $ From_Account [description] message sender Identifier (account number for specifying the sending side message) transmitted transmission information indicates that an administrator empty
- @param [type] $ To_Account [description] message recipient Identifier
- @param [type] $ MsgBody [description] message body
- @param [type] $ SyncOtherMachine [description] 1: The message is synchronized to the line terminal and the roaming From_Account; 2: messages are not synchronized to 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 single message
- [batch_sendmsg description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @param [type] $ To_Account [description] The recipient list [ 'user1', 'user2']
- @param [type] $ MsgBody [description] message body
- @param [type] $ From_Account [description] Sender uid
- @param integer $ SyncOtherMachine [description] 1: The message is synchronized to the line terminal and the roaming From_Account; 2: messages are not synchronized to 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;
}
/** - The withdrawal of single chat messages
- [msg_withdraw description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @param [type] $ From_Account [description] message sender UserID
- @param [type] $ To_Account [description] message recipient UserID
- Uniquely identifies @param [type] $ MsgKey [description] messages to be withdrawn
- @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;
}
/** - User status inquiry
- [query_state description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @param [type] $ uids [description], for example, set user 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;
}
/****用户资料管理***/
/** - Obtaining user information
- [portrait_get description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-31
- @param [type] $ uids [description] user id
- @return [type] [Description]
* /
public function portrait_get ($ UIDs) {
$ URL = ' https://console.tim.qq.com/v4/profile/portrait_get'.$this->postfix ;
the foreach ($ Key $ = aS UIDs> $ value) {
$ UIDs [Key $] = (String) $ value;
}
// Add friend authentication
// AllowType_Type_NeedConfirm: go through their own acknowledgment to add friends
// AllowType_Type_AllowAny: allow anyone add yourself as a friend
// AllowType_Type_DenyAny: allow anyone to add yourself as a friend
// location
// length must not exceed 16 bytes, the recommended usage is as follows:
// App locally defined set of digital mapping to names
// actual background save that four types of digital uint32_t
// first of which represents a uint32_t country
// second uint32_t used to represent the province
// third uint32_t used to represent the city
// fourth uint32_t used to represent the county
$ = Data [
'To_Account' => $ UIDs,
'TagList' => [
'Tag_Profile_IM_Nick', // nickname
'Tag_Profile_IM_Gender', // not set Gender_Type_Female female gender Gender_Type_Unknown Gender_Type_Male men
'Tag_Profile_IM_BirthDay', // birthday recommended usage: 20,190,419
'Tag_Profile_IM_Location', // location
'Tag_Profile_IM_SelfSignature', / / signature
'Tag_Profile_IM_AllowType', // add friends authentication
'Tag_Profile_IM_Language', // language
'Tag_Profile_IM_Image', // avatar the URL
'Tag_Profile_IM_MsgSettings', // message disposed Bit0: set to 0 indicating the received message, the received message is not set to 1
'Tag_Profile_IM_AdminForbidType', // add friends to identify AdminForbid_Type_None administrator has disabled allowed AdminForbid_Type_SendOut ban
'Tag_Profile_IM_Level', // grade
'Tag_Profile_IM_Role', // role
]
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$info = $this->http_request($url,$data);
$info = json_decode($info,true);
return $info;
}
/** - Set user data
- [portrait_set description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @param [type] $ From_Account [description] Identifier of the data needs to be set
- @param [type] $ ProfileItem [description] Data array of objects to be set by the user, each object in the array contains the Tag and 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;
}
/****好友管理***/
/** - add friend
- [friend_add description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @param [type] $ From_Account [description] need for UserID Add Friend
- @param [type] $ To_Account [description] friends UserID
- @param [type] $ AddSource [description] plus friends plus friends source field source keyword is Android, the sources add friends fields are: AddSource_Type_Android
- @param [type] $ Remark [description] friend Remarks
- @param [type] $ GroupName [description] group information
- PS information when @param [type] $ AddWording [description] is formed friendship
- @param [type] $ AddType [description] Add Friend way Add_Type_Single represent one way to add friends add friends Add_Type_Both represent two-way
- @param integer $ ForceAddFlags [description] administrator mandatory labeling add friends: 1 indicates a mandatory add friends, add friends 0 represents the conventional way
- @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;
}
/** - delete friend
- [friend_delete description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-26
- @param [type] $ From_Account [description] initiating user
- @param [type] $ To_Account [description] is an array of deleted user
- @param string $ DeleteType [description] type Delete_Type_Both two-way one-way delete delete 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;
}
/** - Blacklist
- [black_list_add description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-26
- @param [type] $ From_Account [description] initiating user
- @param [type] $ To_Account [description] array of user objects
- @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;
}
/** - Delete blacklist
- [black_list_delete description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-26
- @param [type] $ From_Account [description] initiating user
- @param [type] $ To_Account [description] array of user objects
- @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 request
- [http_request description]
- @Author read lengthy heaven and earth
- @DateTime 2019-02-21
- @param [type] $ url [description] Request Address
- @param [type] $ data [description] Data
- @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-bit random number string pure
- [nonce_str description]
- @Author read lengthy heaven and earth
- @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;
}
}
?>The introduction of two IM API, written communication interface
<?php
namespace app\api\controller;
use app\common\controller\Base;
use think\Validate;
use think\Db;
use tencentyun\im\im as Tim;
/**
- Communication
* /
class Im the extends Base {
/ **- Single Message
- [send_msg description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @return [type] [Description]
* /
public send_msg function () {
$ INPUT post_data = ( 'POST.');
$ config = config ( 'Site');
$ Tim new new IM = ($ config [ 'SdkAppid'] , $ config [ 'Identifier'], $ config [ 'im_appkey']);
// the MsgType message object currently supported include: TIMTextElem (text message), TIMFaceElem (expression message), TIMLocationElem (location message)
$ msgBody [ 'the MsgType '] = $ post_data [' the MsgType '];
IF ($ post_data [' the MsgType '] ==' TIMTextElem ') {
$ msgBody [' MsgContent '] = [' the Text '=> $ post_data [' TXT ']];
ELSEIF} ($ post_data [ 'the MsgType'] == 'TIMFaceElem') {
$ msgBody [ 'MsgContent'] = [ 'Index' => $ post_data [ 'Index'], 'the Data' => $ post_data [ 'the 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 single message
- [batch_sendmsg description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @return [type] [Description]
* /
public batch_sendmsg function () {
$ INPUT post_data = ( 'POST.');
$ config = config ( 'Site');
$ Tim new new IM = ($ config [ 'SdkAppid'] , $ config [ 'Identifier'], $ config [ 'im_appkey']);
// the MsgType message object currently supported include: TIMTextElem (text message), TIMFaceElem (expression message), TIMLocationElem (location message)
$ msgBody [ 'the MsgType '] = $ post_data [' the MsgType '];
IF ($ post_data [' the MsgType '] ==' TIMTextElem ') {
$ msgBody [' MsgContent '] = [' the Text '=> $ post_data [' TXT ']];
ELSEIF} ($ post_data [ 'the MsgType'] == 'TIMFaceElem') {
$ msgBody [ 'MsgContent'] = [ 'Index' => $ post_data [ 'Index'], 'the Data' => $ post_data [ 'the 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);
}
/** - The withdrawal of single chat message
- [msg_withdraw description]
- @Author read lengthy heaven and earth
- @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);
}
/** - Queries users online
- [query_state description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @return [type] [Description]
* /
public query_state function () {
$ INPUT post_data = ( 'POST.');
$ ARR = the explode ( ',', $ post_data [ 'UIDs']);
IF (empty ($ ARR)) {
$ RET = returnArr (2, '', 'Get querying user fails');!
the else {}
IF (COUNT ($ ARR)> 500) {
$ RET = returnArr (. 3,' ',' up to the query 500 user ');!
} the else {
$ config = config (' Site ');
$ IM = new new Tim ($ config [' SdkAppid '], $ config [' Identifier '], $ config [' im_appkey ']) ;
$ = $ to im- info> query_state ($ ARR);
IF ($ info [ 'the ErrorCode'] == 0) {
$ returnArr RET = (. 1, $ info [ 'the QueryResult'], 'query succeeds!');
the else {}
$ returnArr RET = (0, '', $ info [ 'the ErrorInfo']);
}
}
}
return json($ret);
}
/** - Obtaining user information
- [portrait_get description]
- @Author read lengthy heaven and earth
- @DateTime 2019-12-25
- @return [type] [Description]
* /
public portrait_get function () {
$ INPUT post_data = ( 'POST.');
$ ARR = the explode ( ',', $ post_data [ 'UIDs']);
IF (empty ($ ARR)) {
$ RET = returnArr (2, '', 'Get querying user fails');!
the else {}
IF (COUNT ($ ARR)> 100) {
$ RET = returnArr (. 3,' ',' up to the query 100 user ');!
} the else {
$ config = config (' Site ');
$ IM = new new Tim ($ config [' SdkAppid '], $ config [' Identifier '], $ config [' im_appkey ']) ;
$ = $ to im- info> portrait_get ($ ARR);
IF ($ info [ 'the ErrorCode'] == 0) {
$ returnArr RET = (. 1, $ info [ 'UserProfileItem'], 'query succeeds!');
the else {}
$ returnArr RET = (0, '', $ info [ 'the ErrorInfo']);
}
}
}
return json($ret);
}
/** - Set user data
- [portrait_set description]
- @Author read lengthy heaven and earth
- @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);
}
/** - add friend
- [friend_add description]
- @Author read lengthy heaven and earth
- @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);
}
/** - delete friend
- [friend_delete description]
- @Author read lengthy heaven and earth
- @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);
}
/** - add to blacklist
- [black_list_add description]
- @Author read lengthy heaven and earth
- @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);
}
/** - Delete blacklist
- [black_list_delete description]
- @Author read lengthy heaven and earth
- @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);
}
}
?>