Ai_GirlFriend/xunifriend_RaeeC/extend/fast/Easemob.php

168 lines
6.0 KiB
PHP
Raw Normal View History

2026-01-31 19:15:41 +08:00
<?php
namespace fast;
use think\Config;
use Easemob\Auth;
use Easemob\User;
use Easemob\Message;
use app\common\model\UserHuanxin as UserHuanxinModel;
use app\common\model\User as UserModel;
use Easemob\Attachment ;
class Easemob
{
protected $app_key = "1102251210209648#xuni";
protected $client_id = "YXA6LeNLEmsvRmaIhLCr26VQMw";
protected $client_secret = "YXA6v8mFR5m3LNRVy-WlIvzYxK0OrAQ";
protected $user_password = 'xuni123';//密码全部默认
public function creatuser($user_info)
{
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$user = new User($auth);
try {
// 注册单个用户
$data = [
'username' => $user_info['user_number'],
'password' => $this->user_password,
'nickname' => $user_info['nickname'],
];
$add_info = $user->create($data);
//更新换新表token
if (!isset($add_info['code'])){
UserHuanxinModel::create_userinfo($user_info['id'],$add_info);
}else{
return ['code'=>'0','msg'=>$add_info['error_description']??'信息错误'];
}
}catch (\Exception $e){
return ['code'=>'0','msg'=>$e->getMessage()??'信息错误'];
}
return ['code'=>'200','data'=>$add_info,'msg'=>'成功'];
}
//获取用户token
public function getUserToken($username,$user_id){
$user_huanxin = UserHuanxinModel::get(['user_id'=>$user_id]);
if (empty($user_huanxin)) return ['code'=>'0','msg'=>'环信用户不存在'];
if ($user_huanxin['hx_expires_in'] <= time()-60){
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$result = $auth->getUserToken($username,$this->user_password,60*60*24*60);
if (!empty($result)){
//更新环信用户表token
UserHuanxinModel::update_userinfo($user_id,$result);
}
}else{
$result = [
'access_token'=>$user_huanxin['hx_access_token'],
'expires_in'=>$user_huanxin['hx_expires_in'],
];
}
return $result;
}
// 获取用户信息
public function getUser($username)
{
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$user = new User($auth);
return $user->show($username);
}
//发送消息
public function sendMessage($from_user_id,$to_user_id,$send_type,$content,$uuid,$url,$length=0,$thumb_uuid=''){
//查询环信用户表
$from_user_info = UserModel::get($from_user_id);
$to_user_info = UserModel::get($to_user_id);
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$message = new Message($auth);
switch ($send_type){
case 'text':
$result = $message->text('users',[$to_user_info['user_number']],['msg'=>$content],$from_user_info['user_number']);
break;
case 'image':
//$data = $this->uploadFile($content);
$msg = array(
'uuid' => $uuid,
'url' => $url.'/'.$uuid,
'size' => array(
'width' => 36,
'height' => 36,
),
);
$result = $message->image('users',[$to_user_info['user_number']],$msg,$from_user_info['user_number']);
break;
case 'audio':
// $data = $this->uploadFile($content);
// var_dump($data);
// die;
$msg = array(
'uuid' => $uuid,
'url' => $url.'/'.$uuid,
'length'=>$length,
);
$result = $message->audio('users',[$to_user_info['user_number']],$msg,$from_user_info['user_number']);
break;
case 'video':
//$data = $this->uploadFile($content);
$msg = array(
'uuid' => $uuid, // 成功上传视频文件返回的UUID
'url' => $url.'/'.$uuid, // 成功上传视频文件后返回的secret
'thumb'=>$url.'/'.$thumb_uuid,
'thumb_uuid'=>$thumb_uuid,
);
$result = $message->video('users',[$to_user_info['user_number']],$msg,$from_user_info['user_number']);
break;
case 'file':
//$data = $this->uploadFile($content);
$msg = array(
'uuid' => $uuid, // 获取上传文件成功返回的uuid
'secret' => $url.'/'.$uuid, // 获取上传文件成功返回的secret
);
$result = $message->file('users',[$to_user_info['user_number']],$msg,$from_user_info['user_number']);
break;
}
}
//传附件
public function uploadFile($file_path){
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$Attachment = new Attachment($auth);
return $Attachment->uploadFile(dirname(__DIR__, 2).'/public/'.$file_path);
}
/*
* 获取在线用户
*/
public function getOnlineUsers($user_ids){
$user_ids = explode(',',$user_ids);
$user_lists = UserModel::whereIn('id',$user_ids)->field('id,user_number')->select();
$user_nums = [];
foreach ($user_lists as $v){
$user_nums[]=$v['user_number'];
}
$auth = new Auth($this->app_key, $this->client_id, $this->client_secret);
$User = new User($auth);
$result = $User->isUsersOnline($user_nums);
foreach ($user_lists as $v){
$v['is_online'] = false;
foreach ($result as $kk=>$vv){
if (!$v['user_number']) continue;
if ($v['user_number'] == $kk){
$v['is_online'] = true;
}
}
}
return $user_lists;
}
}