215 lines
7.2 KiB
PHP
215 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Constants\DoctorTitleCode;
|
|
use App\Exception\BusinessException;
|
|
use App\Model\Hospital;
|
|
use App\Model\User;
|
|
use App\Model\UserDoctor;
|
|
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;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class ImService extends BaseService
|
|
{
|
|
/**
|
|
* 检测并创建账号
|
|
* @param string $user_id
|
|
* @return bool
|
|
* @throws GuzzleException
|
|
*/
|
|
public function createAccount(string $user_id): bool
|
|
{
|
|
try {
|
|
$params = array();
|
|
$params['user_id'] = $user_id;
|
|
$user = User::getOne($params);
|
|
if (empty($user)){
|
|
throw new BusinessException("用户数据错误");
|
|
}
|
|
|
|
if ($user['user_type'] == 1){
|
|
// 患者
|
|
$params = array();
|
|
$params['user_id'] = $user['user_id'];
|
|
$user_patient = UserPatient::getOne($params);
|
|
if (empty($user_patient)){
|
|
throw new BusinessException("患者数据错误");
|
|
}
|
|
|
|
$avatar = addAliyunOssWebsite($user_patient['avatar']);
|
|
$user_name = $user_patient['user_name'];
|
|
}elseif ($user['user_type'] == 2){
|
|
// 医生
|
|
$params = array();
|
|
$params['user_id'] = $user['user_id'];
|
|
$user_doctor = UserDoctor::getOne($params);
|
|
if (empty($user_doctor)){
|
|
throw new BusinessException("医生数据错误");
|
|
}
|
|
|
|
$avatar = addAliyunOssWebsite($user_doctor['avatar']);
|
|
$user_name = $user_doctor['user_name'];
|
|
}elseif ($user['user_type'] == 3){
|
|
// 药师
|
|
$params = array();
|
|
$params['user_id'] = $user['user_id'];
|
|
$user_pharmacist = UserPharmacist::getOne($params);
|
|
if (empty($user_pharmacist)){
|
|
throw new BusinessException("药师数据错误");
|
|
}
|
|
|
|
$avatar = addAliyunOssWebsite($user_pharmacist['avatar']);
|
|
$user_name = $user_pharmacist['user_name'];
|
|
}
|
|
|
|
$account = new Account();
|
|
// 查询账号导入状态
|
|
$res = $account->checkAccountStatus($user['user_id']);
|
|
if (!$res){
|
|
// 创建单个账号
|
|
$account->createAccount($user_id,$user_name,$avatar);
|
|
}
|
|
|
|
// 医生检测并设置资料
|
|
if ($user['user_type'] == 2){
|
|
// 检测用户资料
|
|
$profile = new Profile();
|
|
$result = $profile->getOneAccountPortraitList($user['user_id']);
|
|
if (!empty($result)){
|
|
// 获取订单医生医院
|
|
$params = array();
|
|
$params['hospital_id'] = $user_doctor['hospital_id'];
|
|
$hospital = Hospital::getOne($params);
|
|
if (empty($hospital)){
|
|
throw new BusinessException("医生医院数据错误");
|
|
}
|
|
|
|
// 设置用户资料
|
|
$arg = array();
|
|
// 性别
|
|
if (in_array('Tag_Profile_IM_Gender',$result)){
|
|
$arg['Tag_Profile_IM_Gender'] = sexToImSex($user_doctor['sex']);
|
|
}
|
|
|
|
// 医院
|
|
if (in_array('Tag_Profile_Custom_Hname',$result)){
|
|
$arg['Tag_Profile_Custom_Hname'] = $hospital['hospital_name'];
|
|
}
|
|
|
|
// 职称
|
|
if (in_array('Tag_Profile_Custom_Hname',$result)){
|
|
$arg['Tag_Profile_Custom_Title'] = $user_doctor['doctor_title'] ?? DoctorTitleCode::getMessage($user_doctor['doctor_title']);
|
|
}
|
|
|
|
if (!empty($arg)){
|
|
$profile->setProfile($user_doctor['user_id'],$arg);
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 验证回调签名
|
|
* @param string $request_time 签名时间戳
|
|
* @param string $sign 签名
|
|
* @return bool
|
|
*/
|
|
public function validateSign(string $request_time,string $sign): bool
|
|
{
|
|
$token = config('im.token');
|
|
if (empty($token)){
|
|
throw new BusinessException("Im Token Config Error");
|
|
}
|
|
|
|
$sys_sign = hash("sha256", $token . $request_time);
|
|
if ($sign != $sys_sign){
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 添加最近联系人会话记录缓存
|
|
* @param string $doctor_id 医生id
|
|
* @param string $inquiry_type 订单类型(自定义字段 1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)
|
|
* @param string $patient_user_id 患者用户id
|
|
* @param array $content 内容
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function addRecentContactRecordCache(string $doctor_id,string $inquiry_type,string $patient_user_id,array $content): void
|
|
{
|
|
$redis_key = "recentContact" . $doctor_id . $inquiry_type;
|
|
|
|
$hash_key = $patient_user_id;
|
|
|
|
$redis = $this->container->get(Redis::class);
|
|
|
|
$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);
|
|
}
|
|
} |