封装发送消息

This commit is contained in:
2023-03-10 17:17:20 +08:00
parent d8ba69f9b6
commit 16a0225cc6
3 changed files with 57 additions and 69 deletions
+51 -3
View File
@@ -11,6 +11,7 @@ use App\Model\UserPatient;
use App\Model\UserPharmacist;
use App\Utils\Log;
use Extend\TencentIm\Account;
use Extend\TencentIm\Message;
use Extend\TencentIm\Profile;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Redis\Redis;
@@ -72,9 +73,8 @@ class ImService extends BaseService
$account = new Account();
// 查询账号导入状态
$res = $account->checkAccountStatus($user_patient['user_id']);
$res = $account->checkAccountStatus($user['user_id']);
if (!$res){
// 创建单个账号
$account->createAccount($user_id,$user_name,$avatar);
}
@@ -83,7 +83,7 @@ class ImService extends BaseService
if ($user['user_type'] == 2){
// 检测用户资料
$profile = new Profile();
$result = $profile->getOneAccountPortraitList("123456");
$result = $profile->getOneAccountPortraitList($user['user_id']);
if (!empty($result)){
// 获取订单医生医院
$params = array();
@@ -164,4 +164,52 @@ class ImService extends BaseService
$redis->hSet($redis_key,$hash_key,json_encode($content,JSON_UNESCAPED_UNICODE));
}
/**
* 发送文本消息
* @param string $from_user_id 发送者id
* @param string $to_user_id 接受者id
* @param string $content 内容
* @param string $order_inquiry_id 订单id
* @return void
* @throws GuzzleException
*/
public function sendTextMessage(string $from_user_id,string $to_user_id,string $content,string $order_inquiry_id): void
{
if (!empty($from_user_id)){
// 检测并创建发送者账号
$this->createAccount($from_user_id);
}
// 检测并创建接收者账号
$this->createAccount($to_user_id);
// 医生给患者发送消息
$message = new Message();
$arg = array();
if (!empty($from_user_id)){
$arg['From_Account'] = $from_user_id; // 发送方user_id 如系统发送,无需填写
}
$arg['To_Account'] = $to_user_id; // 接收方user_id
$arg['SendMsgControl'] = ['NoUnread'];
$arg['MsgBody'] = [
[
"MsgType" => "TIMTextElem",
"MsgContent" => [
"Text" => $content,
],
]
];
// 自定义消息
$cloud_custom_data = array();
$cloud_custom_data['order_inquiry_id'] = $order_inquiry_id;
$cloud_custom_data['is_system'] = 1;
$arg['CloudCustomData'] = json_encode($cloud_custom_data,JSON_UNESCAPED_UNICODE);
$message->sendMessage($arg);
}
}