89 lines
3.8 KiB
TypeScript
89 lines
3.8 KiB
TypeScript
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
|
|
import { getDoctorWebConsultationPolicy } from '@/api/app'
|
|
import useUserStore from '@/stores/modules/user'
|
|
import feedback from '@/utils/feedback'
|
|
import {
|
|
doctorWebConsultationMessage,
|
|
doctorWebConsultationReminder,
|
|
hasDoctorWebConsultationExpired,
|
|
isConsultationDoctor,
|
|
isDoctorWebConsultationBlocked,
|
|
normalizeDoctorWebConsultationPolicy
|
|
} from '@/utils/doctor-web-consultation'
|
|
|
|
// 多个列表、公告及聊天窗口共用请求;配置更新无需重新构建前端。
|
|
let pending: Promise<void> | undefined
|
|
let lastAttemptAt = 0
|
|
const REFRESH_MS = 60 * 1000
|
|
const policyLoadFailed = ref(false)
|
|
|
|
export function useDoctorWebConsultation() {
|
|
const userStore = useUserStore()
|
|
const now = ref(Date.now())
|
|
let timer: ReturnType<typeof setInterval> | undefined
|
|
const updateClock = () => { now.value = Date.now() + (userStore.serverTimeOffsetMs || 0) }
|
|
const policy = computed(() => normalizeDoctorWebConsultationPolicy(userStore.userInfo.web_consultation_policy))
|
|
|
|
const loadPolicy = (force = false): Promise<void> => {
|
|
if (pending) return pending
|
|
if (!force && lastAttemptAt > 0 && Date.now() - lastAttemptAt < REFRESH_MS) return Promise.resolve()
|
|
lastAttemptAt = Date.now()
|
|
pending = getDoctorWebConsultationPolicy().then(data => {
|
|
const next = normalizeDoctorWebConsultationPolicy(data)
|
|
if (!next) throw new Error('网页面诊时间配置无效')
|
|
userStore.userInfo.web_consultation_policy = next
|
|
userStore.serverTimeOffsetMs = next.server_time * 1000 - Date.now()
|
|
policyLoadFailed.value = false
|
|
updateClock()
|
|
}).catch(() => {
|
|
// 保留最近一次有效配置,后端仍逐次校验实际截止时间。
|
|
policyLoadFailed.value = true
|
|
}).finally(() => { pending = undefined })
|
|
return pending
|
|
}
|
|
const refreshConsultationPolicy = () => loadPolicy(true)
|
|
const refreshOnFocus = () => {
|
|
updateClock()
|
|
void refreshConsultationPolicy()
|
|
}
|
|
const refreshOnVisible = () => {
|
|
if (document.visibilityState !== 'hidden') refreshOnFocus()
|
|
}
|
|
|
|
// 登录响应和配置刷新响应均提供服务器时刻,避免本机时区/时钟影响规则。
|
|
watch(() => userStore.serverTimeOffsetMs, updateClock, { immediate: true })
|
|
|
|
onMounted(() => {
|
|
updateClock()
|
|
void refreshConsultationPolicy()
|
|
timer = setInterval(() => {
|
|
updateClock()
|
|
void loadPolicy()
|
|
}, 1000)
|
|
window.addEventListener('focus', refreshOnFocus)
|
|
document.addEventListener('visibilitychange', refreshOnVisible)
|
|
})
|
|
onUnmounted(() => {
|
|
clearInterval(timer)
|
|
window.removeEventListener('focus', refreshOnFocus)
|
|
document.removeEventListener('visibilitychange', refreshOnVisible)
|
|
})
|
|
|
|
const isDoctor = computed(() => isConsultationDoctor(userStore.userInfo))
|
|
const webConsultationExpired = computed(() => hasDoctorWebConsultationExpired(policy.value, now.value))
|
|
const consultationReminder = computed(() => doctorWebConsultationReminder(policy.value, now.value))
|
|
const webConsultationMessage = computed(() => doctorWebConsultationMessage(policy.value))
|
|
const webConsultationBlocked = computed(() => isDoctorWebConsultationBlocked(userStore.userInfo, policy.value, now.value))
|
|
const ensureWebConsultationAllowed = () => {
|
|
updateClock()
|
|
if (!webConsultationBlocked.value) return true
|
|
feedback.msgWarning(webConsultationMessage.value)
|
|
return false
|
|
}
|
|
|
|
return {
|
|
isDoctor, consultationReminder, webConsultationExpired, webConsultationBlocked,
|
|
webConsultationMessage, ensureWebConsultationAllowed, policyLoadFailed, refreshConsultationPolicy
|
|
}
|
|
}
|