146 lines
4.6 KiB
JavaScript
146 lines
4.6 KiB
JavaScript
import { ref, computed } from 'vue';
|
|
import { onShow, onUnload } from "@dcloudio/uni-app";
|
|
import { getChatStatus } from "@/api/corp/im.js";
|
|
import { getDoctorByDoctorId } from '@/api/doctor/doctor';
|
|
import { toast } from '@/utils/widget';
|
|
import dayjs from 'dayjs';
|
|
|
|
import useChatOrder from './chat-order-hook';
|
|
|
|
|
|
/**
|
|
* 获取聊天室当前进行中业务hook
|
|
* @param {string聊天群组ID} groupID
|
|
*/
|
|
export default function useChatBusiness(groupID) {
|
|
const groupInfo = ref({});
|
|
|
|
const assistant = computed(() => {
|
|
if (groupInfo.value.assistant?.memberAccount) {
|
|
return { assistantId: groupInfo.value.assistant.memberAccount, name: groupInfo.value.assistant.name }
|
|
}
|
|
return null;
|
|
})
|
|
|
|
const doctor = computed(() => {
|
|
// 优先返回从API获取的详细医生信息
|
|
if (doctorInfo.value) {
|
|
return doctorInfo.value;
|
|
}
|
|
// 如果详细信息还未加载,返回基本信息
|
|
if (groupInfo.value.doctor?.memberAccount) {
|
|
return {
|
|
doctorId: groupInfo.value.doctor.memberAccount,
|
|
name: groupInfo.value.doctor.name,
|
|
avatar: groupInfo.value.doctor.avatar
|
|
}
|
|
}
|
|
return null;
|
|
})
|
|
|
|
const patient = computed(() => {
|
|
if (groupInfo.value.patient?.memberAccount) {
|
|
return { openId: groupInfo.value.patient.memberAccount, name: groupInfo.value.patient.name, patientId: groupInfo.value.patient.patientId }
|
|
}
|
|
return null;
|
|
})
|
|
const chatMember = computed(() => {
|
|
const res = {
|
|
assistant: { name: '医生助理', avatar: '/static/assistant.png' }
|
|
};
|
|
if (assistant.value) {
|
|
res[assistant.value.assistantId] = { name: `医生助理${assistant.value.name}`, avatar: '/static/assistant.png' };
|
|
}
|
|
if (doctor.value) {
|
|
|
|
res[doctor.value.doctorId] = { name: `医生${doctor.value.name || ''}`, avatar: doctor.value.avatar || '/static/home/doctor.svg' };
|
|
}
|
|
if (patient.value) {
|
|
res[patient.value.openId] = { name: '我' || `患者${patient.value.name}`, avatar: '/static/center/user-avatar.png' };
|
|
}
|
|
return res;
|
|
})
|
|
|
|
const chatRoomBusiness = computed(() => groupInfo.value.groupBusiness || {});
|
|
const isPending = computed(() => groupInfo.value.status === 'active');
|
|
const isClosed = computed(() => !isPending.value);
|
|
const doctorInfo = ref(null) // 医生信息
|
|
|
|
const orderId = computed(() => {
|
|
return chatRoomBusiness.value.businessType === 'consultation' ? chatRoomBusiness.value.businessId : ''
|
|
})
|
|
|
|
const { currentOrder, chatRoomStatus, countdown, getCurrentOrder } = useChatOrder(orderId)
|
|
|
|
const chatRoomCountDown = ref('') // 会话倒计时
|
|
const showCountdown = computed(() => {
|
|
const isPendingOrder = currentOrder.value && chatRoomStatus.value.isPending && orderId.value == currentOrder.value.orderId;
|
|
const isRefill = chatRoomBusiness.value.businessType === 'refill_prescription'
|
|
return chatRoomCountDown.value && (isPendingOrder || isRefill)
|
|
})
|
|
|
|
async function getChatBusiness() {
|
|
if (!groupID.value) return;
|
|
const res = await getChatStatus(groupID.value);
|
|
if (res && res.success) {
|
|
groupInfo.value = res.data;
|
|
setCountdown()
|
|
getDoctor()
|
|
} else {
|
|
await toast('获取聊天室业务失败')
|
|
uni.navigateBack()
|
|
}
|
|
}
|
|
|
|
function setCountdown() {
|
|
if (isPending.value && groupInfo.value.expireTime && dayjs(groupInfo.value.expireTime).isAfter(dayjs())) {
|
|
if (getApp().chatRoomCountDownTimer) clearInterval(getApp().chatRoomCountDownTimer);
|
|
getApp().chatRoomCountDownTimer = setInterval(() => {
|
|
const now = dayjs();
|
|
const endTime = dayjs(groupInfo.value.expireTime);
|
|
const diff = endTime.diff(now, 'second');
|
|
if (diff <= 0) {
|
|
clearCountdown();
|
|
getChatBusiness()
|
|
return;
|
|
}
|
|
// 计算剩余时分秒
|
|
const hours = Math.floor(diff / 3600);
|
|
const minutes = Math.floor((diff % 3600) / 60);
|
|
const seconds = diff % 60;
|
|
|
|
const pad = n => n.toString().padStart(2, '0');
|
|
chatRoomCountDown.value = `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`.replace(/^00\:/, '');
|
|
}, 1000)
|
|
} else {
|
|
clearCountdown()
|
|
}
|
|
}
|
|
|
|
function clearCountdown() {
|
|
if (getApp().chatRoomCountDownTimer) {
|
|
clearInterval(getApp().chatRoomCountDownTimer);
|
|
getApp().chatRoomCountDownTimer = null;
|
|
}
|
|
chatRoomCountDown.value = ''
|
|
}
|
|
|
|
async function getDoctor() {
|
|
const doctorId = groupInfo.value.doctor?.memberAccount;
|
|
if (doctorId && (!doctorInfo.value || doctorInfo.value.doctorId !== doctorId)) {
|
|
const res = await getDoctorByDoctorId(doctorId)
|
|
doctorInfo.value = res && res.data ? res.data : null;
|
|
}
|
|
}
|
|
|
|
onUnload(() => {
|
|
clearCountdown()
|
|
})
|
|
|
|
onShow(() => {
|
|
getChatBusiness()
|
|
})
|
|
|
|
|
|
return { chatRoomBusiness, getChatBusiness, currentOrder, chatRoomStatus, countdown, getCurrentOrder, chatRoomCountDown, showCountdown, doctorInfo, isClosed, chatMember }
|
|
} |