Files
yuyin-php/application/api/model/Tencent.php

387 lines
15 KiB
PHP
Raw Normal View History

2025-08-08 14:45:40 +08:00
<?php
namespace app\api\model;
use think\Db;
use think\Model;
use think\facade\Env;
// use Tencent\TLSSigAPI;
use Tencent\TLSSigAPIv2;
class Tencent extends Model
{
//获取腾讯IM UserSig信息
public function tencent_user_sig_info($uid){
// $config = model('admin/Config')->get_system_config();
// $tencentyun_im_appid = $config['tencentyun_im_appid'];
// $tencentyun_im_public_key = $config['tencentyun_im_public_key'];
// $tencentyun_im_private_key = $config['tencentyun_im_private_key'];
// $tencent = new \Tencent\TLSSigAPI();
// $tencent->SetAppid($tencentyun_im_appid);
// $tencent->SetPrivateKey($tencentyun_im_private_key);
// $user_sig = $tencent->genSig($uid);
// return $user_sig;
$config = model('admin/Config')->get_uncache_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$tencentyun_im_key = $config['tencentyun_im_key'];
$tencent = new \Tencent\TLSSigAPIv2($tencentyun_im_appid, $tencentyun_im_key);
$user_sig = $tencent->genUserSig($uid);
return $user_sig;
}
//查询账号
public function account_check($uid){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/im_open_login_svc/account_check?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'CheckItem' => array(
array(
'UserID' => $uid, //用户id
),
),
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
dump($reslut);exit;
}
//拉入黑名单
public function black_list_add($uid, $receive_uid){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/sns/black_list_add?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'From_Account' => $uid,
'To_Account' => $receive_uid,
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
if($reslut['ActionStatus'] == 'OK'){
return ['code' => 200, 'msg' => '添加成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => $reslut['ErrorCode'], 'data' => null];
}
}
//移除黑名单
public function black_list_delete($uid, $receive_uid){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/sns/black_list_delete?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'From_Account' => $uid,
'To_Account' => $receive_uid,
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
if($reslut['ActionStatus'] == 'OK'){
return ['code' => 200, 'msg' => '移除成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => $reslut['ErrorCode'], 'data' => null];
}
}
//发送群内系统消息
public function send_group_system_notification($rid, $message_data){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/group_open_http_svc/send_group_system_notification?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'GroupId' => $rid,
'Content' => $message_data,
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
}
//发送群内普通消息
public function send_group_msg($rid, $message_data){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$random = time().rand(111,999);
$postUrl = 'https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'GroupId' => $rid,
'Random' => $random,
'MsgBody' => array(
array(
'MsgType' => 'TIMTextElem',
'MsgContent' => array(
'Text' => $message_data,
),
),
),
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
// $txt = date('Y-m-d H:i:s').'测试';
// error_log($txt, 3, 'a.txt');
return $reslut;
}
//创建群组
public function create_group($rid){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/group_open_http_svc/create_group?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
// 'Owner_Account' => 'administrator',
'Type' => 'AVChatRoom',
'GroupId' => strval($rid),
'Name' => strval($rid)
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
if($reslut['ErrorCode'] == 10021){
return ['code' => 200, 'msg' => '添加成功', 'data' => null];
}
if($reslut['ActionStatus'] == 'OK'){
return ['code' => 200, 'msg' => '添加成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => $reslut['ErrorCode'], 'data' => null];
}
}
//指定用户发送消息给注册用户
public function user_sendmsg($uid, $receive_uid, $message, $machine_type = 2){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/openim/sendmsg?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'SyncOtherMachine' => $machine_type,
'From_Account' => strval($uid),
'To_Account' => strval($receive_uid),
'MsgRandom' => time(),
'MsgBody' => array(
array(
'MsgType' => 'TIMTextElem',
'MsgContent' => array(
'Text' => $message,
),
),
),
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
return $reslut;
}
//获取APP中的所有群组
public function get_appid_group_list(){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/group_open_http_svc/get_appid_group_list?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'Limit' => 20,
'Next' => 0,
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
}
public function txt(){
$postUrl = 'https://app.yayinyy.com/api/Agora/get_sstoken';
$curlPost = array(
'code' => '44863d01cd628e583efeb3b9eda510fd',
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
return $reslut;
}
//腾讯IM请求封装方法
public function tencent_post_url($postUrl, $curlPost){
$headerArray =array(
"Content-type:application/json",
"Accept:application/json",
);
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
// curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$data = curl_exec($ch);//运行curl
curl_close($ch);
if ($data) {
$data = json_decode($data, true);
}
// dump($data);exit;
return $data;
}
//指定用户发送自定义消息给注册用户
public function user_custom_sendmsg($uid, $receive_uid, $message, $machine_type = 2){
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/openim/sendmsg?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'SyncOtherMachine' => $machine_type,
'From_Account' => strval($uid),
'To_Account' => strval($receive_uid),
'MsgRandom' => time(),
'MsgBody' => array(
array(
'MsgType' => 'TIMCustomElem',
'MsgContent' => array(
// 'Text' => $message,
'Data' => $message,
// 'Desc' => 'notification',
),
),
),
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
return $reslut;
}
//获取用户在线状态
public function query_user_online_status($to_uid_arr)
{
foreach($to_uid_arr as $key => $v) {
$to_uid_arr[$key] = strval($v);
}
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$postUrl = 'https://console.tim.qq.com/v4/openim/query_online_status?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'To_Account' => $to_uid_arr,
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
return $reslut;
}
//主动审核接口
public function content_moderation($type_name, $content){
return ['code' => 200, 'msg' => '通过审核', 'data' => null];
$config = model('admin/Config')->get_system_config();
$tencentyun_im_appid = $config['tencentyun_im_appid'];
$im_admin = 'administrator';
$admin_sig = $this->tencent_user_sig_info($im_admin);
$rand = rand(111111111,9999999999);
$random = time().rand(111,999);
$postUrl = 'https://console.tim.qq.com/v4/im_msg_audit/content_moderation?sdkappid='.$tencentyun_im_appid.'&identifier='.$im_admin.'&usersig='.$admin_sig.'&random='.$rand.'&contenttype=json';
$curlPost = array(
'AuditName' => 'C2C', //表明送审策略取值C2C/Group/UserInfo/GroupInfo/GroupMemberInfo/RelationChain
'ContentType' => $type_name, //送审类型取值Text/Image/Audio/Video。
'Content' => $content //送审内容最大限制8KB当审核文件时填对应 URL。其中图片审核最大不超过5MB
);
$curlPost = json_encode($curlPost);
$reslut = $this->tencent_post_url($postUrl, $curlPost);
if($reslut['ActionStatus'] == 'OK'){
if($reslut['Result'] == 'Pass'){
return ['code' => 200, 'msg' => '通过审核', 'data' => null];
}else{
return ['code' => 201, 'msg' => '该内容不过审', 'data' => null];
}
}else{
return ['code' => 201, 'msg' => $reslut['ErrorCode'], 'data' => null];
}
// $txt = date('Y-m-d H:i:s').'测试';
// error_log($txt, 3, 'a.txt');
// return $reslut;
}
}