318 lines
12 KiB
PHP
318 lines
12 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/7/15} {16:14}
|
||
*/
|
||
|
||
namespace app\api\logic;
|
||
|
||
|
||
use app\api\controller\BaseWssApi;
|
||
use app\common\model\TabConf;
|
||
use GatewayWorker\Lib\Gateway;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\gpt\chat\ChatGPT;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
|
||
/**
|
||
* 听译聊天
|
||
* Class TychatLogic
|
||
* @package app\api\logic
|
||
*/
|
||
class TychatLogic extends BaseLogic
|
||
{
|
||
/**
|
||
* desc:听译-保存听译聊天记录,同时分发消息
|
||
*
|
||
* action:Tychat/saveChatHistory
|
||
* items:[
|
||
* 'chat_msg'=>'你好',
|
||
* ]
|
||
* author:wh
|
||
*/
|
||
function saveChatHistory($client_id,$data){
|
||
return Mmodel::catchJson(function ()use ($client_id,$data){
|
||
Tools::log_to_write_txt(['保存历史记录,入参'=>['域名'=>request()->controller().'/'.request()->action()]]);
|
||
$action = 'Tychat/saveChatHistory';
|
||
if(empty($data['items'])){
|
||
$json = Tools::wss_json_fail($action, 'items参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['chat_msg'])){
|
||
$json = Tools::wss_json($action, 550,'chat_msg参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['type'])){
|
||
$json = Tools::wss_json_fail($action, 'type参数错误,缺少角色类型:doc、user');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['say_token'])){
|
||
$json = Tools::wss_json_fail($action, 'say_token参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['ticket'])){
|
||
$json = Tools::wss_json_fail($action, 'ticket参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
|
||
if(empty($data['items']['hdruserbaseinfo_id'])){
|
||
$json = Tools::wss_json_fail($action, 'hdruserbaseinfo_id 参数错误,患者基本信息id必须。');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
$hdruserbaseinfo_id = $data['items']['hdruserbaseinfo_id'];
|
||
|
||
$type = $data['items']['type'];//角色类型
|
||
$ticket = $data['items']['ticket'];
|
||
$say_token = $data['items']['say_token'];//对话票据
|
||
|
||
//{\"role\":\"patient\",\"text\":\" get message\"}
|
||
$chat_msg_arr = $data['items']['chat_msg'];//对话内容 $chat_msg是个数组
|
||
|
||
|
||
//保存历史记录 $chat_msg是个数组
|
||
$data = [
|
||
'chat_msg'=>json_encode($chat_msg_arr, JSON_UNESCAPED_UNICODE),
|
||
'username'=>'',
|
||
'type'=>$type,//聊天角色
|
||
'say_token'=>$say_token,//对话凭据
|
||
'hdruserbaseinfo_id'=>$hdruserbaseinfo_id,
|
||
];
|
||
Tools::log_to_write_txt(['开始保存聊天记录'=>$data]);
|
||
Db::table(TabConf::$fa_ty_chathistory)
|
||
->data($data)
|
||
->insert();
|
||
Tools::log_to_write_txt(['结束保存聊天记录.']);
|
||
|
||
//广播消息
|
||
//查询广播客户端id
|
||
$arr = Db::table(TabConf::$fa_device)
|
||
->where('ticket',$ticket)
|
||
->where('clientid','neq',$client_id)//不给自己发
|
||
->find();
|
||
if(empty($arr)){
|
||
$json = Tools::wss_json_fail($action, '未找到广播对象,请确认目标对象是否登录并绑定设备关系');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($arr['clientid'])){
|
||
$json = Tools::wss_json_fail($action, '未找到广播对象,请确认目标对象是否登录并绑定设备关系!');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
$clientid = $arr['clientid'];
|
||
|
||
|
||
//向客户端发送消息
|
||
$json = Tools::wss_json_ok($action, $chat_msg_arr);
|
||
Tools::log_to_write_txt(['向客户端广播消息,入参:clientid='.$clientid=>$json]);
|
||
Gateway::sendToClient($clientid,$json);
|
||
|
||
Tools::log_to_write_txt(['开始健康小洞察'=>$chat_msg_arr]);
|
||
//健康小洞察
|
||
$this->getHealthInsight2($hdruserbaseinfo_id,$chat_msg_arr);
|
||
|
||
|
||
|
||
|
||
|
||
|
||
});
|
||
}
|
||
/**
|
||
* desc:保存听译聊天记录
|
||
*
|
||
*/
|
||
//private function saveHistory(string $username,string $chat_content,string $type,string $ticket){
|
||
// $data = [
|
||
// 'chat_msg'=>$chat_content,
|
||
// 'username'=>$username,
|
||
// 'type'=>$type,//聊天角色
|
||
// 'ticket'=>$ticket,//对话凭据
|
||
// ];
|
||
// Db::table('fa_ty_chathistory')
|
||
// ->data($data)
|
||
// ->insert();
|
||
//
|
||
//
|
||
// return Tools::set_ok();
|
||
//}
|
||
|
||
/**
|
||
* 查询聊天记录
|
||
*/
|
||
function getChatHistory(){
|
||
return Mmodel::catchJson(function (){
|
||
$ticket = input('ticket');
|
||
if(empty($ticket)){
|
||
return Tools::set_fail('ticket参数错误');
|
||
}
|
||
$list = Db::table('fa_ty_chathistory')
|
||
->where('ticket',$ticket)
|
||
->order('id desc')
|
||
->select();
|
||
return Tools::set_ok('查询成功',$list);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* desc:健康洞察
|
||
* 请求类型:wss
|
||
* action: Tychat/getHealthInsight2
|
||
* items:[
|
||
* username:'18922226666',
|
||
* content=>[
|
||
["role" => "user", "content" => '']
|
||
],
|
||
* ]
|
||
* author:wh
|
||
*/
|
||
private function getHealthInsight2($hdruserbaseinfo_id,$chat_msg_arr){
|
||
Tools::log_to_write_txt(['健康洞察,入参'=>['域名'=>request()->controller().'/'.request()->action()]]);
|
||
//$action = 'Tychat/getHealthInsight2';
|
||
|
||
$config = config('ai_health_insight_config');
|
||
$question = '';//input('question','');
|
||
//$content = [
|
||
// ["role" => "user", "content" => '']
|
||
//];
|
||
$chatobj = new ChatGPT();
|
||
$chatobj->url = $config['base_url'];
|
||
$chatobj->model = '';
|
||
$chatobj->apiKey = $config['APIKey'];
|
||
|
||
$answer_json_arr = [];
|
||
|
||
$config = [
|
||
'stream'=>false,
|
||
'chatId'=>Tools::getMillisecond(),
|
||
];
|
||
$content = [
|
||
["role" => "user", "content" => $chat_msg_arr['text']]
|
||
];
|
||
$chatobj->setBefore($content);
|
||
|
||
$chatobj->returnAnswer($question,$config,$answer_json_arr);
|
||
Tools::log_to_write_txt(['健康洞察,gpt请求参数'=>$chatobj->post_msg_body]);
|
||
////return json(Tools::set_ok('查询成功',$answer_json_arr));
|
||
Tools::log_to_write_txt(['健康洞察,出参'=>$answer_json_arr]);
|
||
|
||
|
||
$content_arr = json_decode($answer_json_arr[0],true);
|
||
if(empty($content_arr['choices'][0]['message']['content'])){
|
||
return;
|
||
}
|
||
$ctx_msg = $content_arr['choices'][0]['message']['content'];
|
||
|
||
//存储洞察内容
|
||
Db::table(TabConf::$fa_hdrhealth_insight)
|
||
->data([
|
||
'hdruserbaseinfo_id'=>$hdruserbaseinfo_id,
|
||
'content'=>$ctx_msg,
|
||
])
|
||
->insert();
|
||
|
||
//$json = Tools::wss_json_ok($action, 'ok',$answer_json_arr);
|
||
//Gateway::sendToClient($clientid,$json);
|
||
}
|
||
|
||
/**
|
||
* desc:医生打开诊断窗口记录,打开诊断窗口时调用该接口,发送对话数据给客户端,客户端拿本地登录的票据与该票据参数做相等匹配,
|
||
* 相等表示同一个医生,否则提示切换医生登录账号
|
||
*
|
||
* 请求类型:wss
|
||
* action: Tychat/openChatRoomWriteRecord
|
||
* items:[
|
||
* doctor_name //医生姓名
|
||
doctor_phone //医生电话
|
||
name //患者姓名
|
||
phone //患者电话
|
||
ticket //对话票据
|
||
* ]
|
||
* 【注意】医患对话只会一对一
|
||
* author:wh
|
||
*/
|
||
function openChatRoomWriteRecord($client_id,$data){
|
||
return Mmodel::catchJson(function () use ($client_id,$data){
|
||
Tools::log_to_write_txt(['打开诊断窗口记录,入参'=>$data]);
|
||
$action = 'Tychat/openChatRoomWriteRecord';
|
||
if(empty($data['items'])){
|
||
$json = Tools::wss_json_fail($action, 'items参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['doctor_name'])){
|
||
$json = Tools::wss_json_fail($action, 'doctor_name参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['doctor_phone'])){
|
||
$json = Tools::wss_json_fail($action, 'doctor_phone参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['name'])){
|
||
$json = Tools::wss_json_fail($action, 'name 参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['phone'])){
|
||
$json = Tools::wss_json_fail($action, 'phone 参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['ticket'])){
|
||
$json = Tools::wss_json_fail($action, 'ticket 参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
if(empty($data['items']['hdruserbaseinfo_id'])){
|
||
$json = Tools::wss_json_fail($action, 'hdruserbaseinfo_id 参数错误');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
$hdruserbaseinfo_id = $data['items']['hdruserbaseinfo_id'];
|
||
|
||
$upd_data = [
|
||
'doctor_name'=>$data['items']['doctor_name'],
|
||
'doctor_phone'=>$data['items']['doctor_phone'],
|
||
'name'=>$data['items']['name'],
|
||
'phone'=>$data['items']['phone'],
|
||
'ticket'=>$data['items']['ticket'],
|
||
];
|
||
Db::table(TabConf::$fa_hdropen_chat_room_record)
|
||
->insert($upd_data);
|
||
//【注意】医患对话只会一对一
|
||
////更新医生对话设备目标
|
||
Db::table(TabConf::$fa_device)
|
||
->where('clientid',$client_id)
|
||
->data([
|
||
'target_name'=>$data['items']['name'],
|
||
'target_phone'=>$data['items']['phone'],
|
||
])
|
||
->update();
|
||
|
||
//查询患者设备
|
||
$device = Db::table(TabConf::$fa_device)
|
||
->where('username',$data['items']['doctor_phone'])
|
||
->where('clientid','neq',$client_id)//不是自己
|
||
->select();//严格来说只有一条数据
|
||
if(empty($device)){
|
||
$json = Tools::wss_json_fail($action, '错误:当前医生没有对应患者设备');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
|
||
//医患只能一对一
|
||
if(count($device) > 1){
|
||
$json = Tools::wss_json_fail($action, '错误:当前医生存在多个患者设备,无法确认目标');
|
||
return Gateway::sendToClient($client_id, $json);
|
||
}
|
||
//更新患者设备的ticket为医生的ticket(保持在一个组)
|
||
Db::table(TabConf::$fa_device)
|
||
->where('clientid',$device[0]['clientid'])
|
||
->data([
|
||
'ticket'=>$data['items']['ticket'],
|
||
])
|
||
->update();
|
||
//向客户端发消息
|
||
$json = Tools::wss_json_ok($action,'诊断医生已打开诊断窗口',[
|
||
'hdruserbaseinfo_id'=>$hdruserbaseinfo_id,
|
||
]);
|
||
Tools::log_to_write_txt(['打开诊断窗口记录,出参,向客户端发消息。'=>$json,'device:'=>$device]);
|
||
Gateway::sendToClient($device[0]['clientid'],$json);
|
||
});
|
||
}
|
||
} |