This commit is contained in:
Your Name
2026-03-20 18:00:19 +08:00
parent d765517b74
commit 18fee2e8f8
240 changed files with 976 additions and 280 deletions
@@ -0,0 +1,31 @@
<?php
namespace app\api\controller;
use app\api\logic\ChatNotifyLogic;
/**
* 聊天相关接口(C端-患者)
*/
class ChatController extends BaseApiController
{
public array $notNeedLogin = ['notifyOpen'];
/**
* 患者打开会话时通知医生
* 无需登录,通过参数传递
*/
public function notifyOpen()
{
$doctorId = (int) ($this->request->post('doctor_id') ?: $this->request->post('doctorId'));
$patientId = trim((string) ($this->request->post('patient_id') ?: $this->request->post('patientId') ?: ''));
$patientName = trim((string) ($this->request->post('patient_name') ?: $this->request->post('patientName') ?: '患者'));
if ($doctorId <= 0) {
return $this->fail('医生ID无效');
}
ChatNotifyLogic::addNotify($doctorId, $patientId ?: '0', $patientName);
return $this->success('已通知');
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use think\facade\Cache;
/**
* 聊天打开通知逻辑(患者打开会话时通知医生)
* 使用缓存存储,支持多消息队列
*/
class ChatNotifyLogic extends BaseLogic
{
const CACHE_PREFIX = 'chat_open_notify:';
const CACHE_TTL = 86400; // 24小时
const MAX_PER_DOCTOR = 50; // 每个医生最多保留条数
/**
* 添加患者打开会话通知
* @param int $doctorId 医生ID (admin_id)
* @param string $patientId 患者/诊单ID
* @param string $patientName 患者姓名
* @return bool
*/
public static function addNotify(int $doctorId, string $patientId, string $patientName): bool
{
$key = self::CACHE_PREFIX . $doctorId;
$item = [
'id' => uniqid('', true),
'type' => 'patient_opened_chat',
'doctor_id' => $doctorId,
'patient_id' => $patientId,
'patient_name' => $patientName,
'created_at' => time(),
];
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
array_unshift($list, $item);
$list = array_slice($list, 0, self::MAX_PER_DOCTOR);
Cache::set($key, $list, self::CACHE_TTL);
return true;
}
/**
* 添加面诊结束通知(医生完成接诊后通知对应医助)
* @param int $assistantId 医助ID (admin_id)
* @param string $patientName 患者姓名
* @param string $doctorName 医生姓名
* @param int $diagnosisId 诊单ID
* @return bool
*/
public static function addConsultationCompleteNotify(int $assistantId, string $patientName, string $doctorName, int $diagnosisId = 0): bool
{
$key = self::CACHE_PREFIX . $assistantId;
$item = [
'id' => uniqid('', true),
'type' => 'consultation_complete',
'assistant_id' => $assistantId,
'patient_name' => $patientName,
'doctor_name' => $doctorName,
'diagnosis_id' => $diagnosisId,
'created_at' => time(),
];
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
array_unshift($list, $item);
$list = array_slice($list, 0, self::MAX_PER_DOCTOR);
Cache::set($key, $list, self::CACHE_TTL);
return true;
}
/**
* 获取未读通知(医生/医助通用,按 admin_id 获取)
* @param int $adminId 管理员ID (医生或医助)
* @param bool $consume 是否消费(移除)已获取的
* @return array
*/
public static function getNotifies(int $adminId, bool $consume = true): array
{
$key = self::CACHE_PREFIX . $adminId;
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
if ($consume && !empty($list)) {
Cache::delete($key);
}
return $list;
}
}
+5 -5
View File
@@ -104,7 +104,7 @@ class DoctorLogic extends BaseLogic
$doctor = Admin::where('id', $doctorId)
->where('disable', 0)
->field(['id', 'name', 'account', 'qualification_images','avatar','specialty','license_no','qualification_images','enable_image_consult','enable_video_consult','enable_charge'])
->field(['id', 'name', 'account','experience', 'title','qualification_images','avatar','specialty','license_no','qualification_images','enable_image_consult','enable_video_consult','enable_charge'])
->find();
if (!$doctor) {
@@ -131,10 +131,10 @@ class DoctorLogic extends BaseLogic
'enable_image_consult' => $doctorData['enable_image_consult'] ?? 1,
'enable_video_consult' => $doctorData['enable_video_consult'] ?? 1,
'enable_charge' => $doctorData['enable_charge'] ?? 0,
'specialty' => '中医',
'title' => '主任医师',
'specialty' => $doctorData['specialty']??'中医',
'title' => $doctorData['title'],
'hospital' => '甄养堂互联网医院',
'experience' => '24年临床经验',
'experience' => $doctorData['experience']??'10+年临床经验',
'rating' => '4.9',
'patient_count' => $patientCount,
'papers' => 15,
@@ -171,7 +171,7 @@ class DoctorLogic extends BaseLogic
'patient_initials' => 'JS',
'patient_name' => 'James S.',
'rating' => 5,
'content' => '李医生对我慢性疲劳的治疗改变了我的生活。他在建议方剂之前足足听我倾诉了45分钟。真是一位大师级的人物。',
'content' => '对我慢性疲劳的治疗改变了我的生活。他在建议方剂之前足足听我倾诉了45分钟。真是一位大师级的人物。',
'create_time' => '2天前',
],
[