Files
digital_doctor/digital_doctor/application/api/logic/TychatLogic.php
2024-08-01 23:30:14 +08:00

229 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* description
* authorwh
* 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'=>'你好',
* ]
* authorwh
*/
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']['ticket'])){
$json = Tools::wss_json_fail($action, 'ticket参数错误');
return Gateway::sendToClient($client_id, $json);
}
$type = $data['items']['type'];//角色类型
$ticket = $data['items']['ticket'];//对话票据
$chat_msg = $data['items']['chat_msg'];//对话内容
//广播消息
//查询广播客户端id
$arr = Db::table(TabConf::$fa_device)
->where('ticket',$ticket)
->where('clientid','neq',$client_id)//不给自己发
->select();
if(empty($arr)){
$json = Tools::wss_json_fail($action, '未找到广播对象,请确认目标对象是否登录并绑定设备关系');
return Gateway::sendToClient($client_id, $json);
}
$username = $arr[0]['username'];
$clientid_arr = array_column($arr,'clientid');
//保存记录
$this->saveHistory($username,$chat_msg,$type,$ticket);
//向客户端发送消息
$json = Tools::wss_json_ok($action, $chat_msg);
Gateway::sendToAll($json,$clientid_arr);
//健康小洞察
$this->getHealthInsight2($clientid_arr,$chat_msg);
});
}
/**
* desc保存听译聊天记录
*
*/
private function saveHistory($username,$chat_content,$type,$ticket){
$data = [
'chat_msg'=>$chat_content,
'username'=>$username,
'type'=>$type,//聊天角色
'ticket'=>$ticket,//对话凭据
];
Db::table('fa_ty_chathistory')->insert($data);
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" => '']
],
* ]
* authorwh
*/
private function getHealthInsight2($clientid_arr,$chat_msg){
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,
];
$content = [
["role" => "user", "content" => $chat_msg]
];
$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]);
$json = Tools::wss_json_ok($action, 'ok',$answer_json_arr);
Gateway::sendToAll($json,$clientid_arr);
}
/**
* desc医生打开诊断窗口记录打开诊断窗口时调用该接口发送对话数据给客户端客户端拿本地登录的票据与该票据参数做相等匹配
* 相等表示同一个医生,否则提示切换医生登录账号
*
* 请求类型wss
* action: Tychat/openChatRoomWriteRecord
* items:[
* doctor_name //医生姓名
doctor_phone //医生电话
name //患者姓名
phone //患者电话
ticket //对话票据
* ]
* authorwh
*/
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);
}
$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($data);
//查询绑定的设备
$device = Db::table(TabConf::$fa_device)
->where('ticket',$data['items']['ticket'])
->find();
$client_id_arr = array_column($device,'clientid');
//向客户端发消息
$json = Tools::wss_json($action,'诊断医生已打开诊断窗口');
Gateway::sendToAll($json, $client_id_arr, [$client_id]);//不向自己发送
});
}
}