23 lines
1.6 KiB
TypeScript
23 lines
1.6 KiB
TypeScript
import { computed, getCurrentInstance, reactive, ref, type Ref } from 'vue'
|
|||
|
|
import { ElMessageBox } from 'element-plus'
|
||
|
|
import { useUnsavedWorkRegistration } from '@/core/unsaved-work/useUnsavedWorkRegistration'
|
||
|
|
|
||
|
|
/** All dialog close paths use requestClose; the shared registry also protects route/tab exits. */
|
||
|
|
export function useFleetDraftGuard(key: string, open: Ref<boolean>, model: object, busy: Ref<boolean>, uncertain: Ref<boolean>, close: () => void) {
|
||
|
|
const baseline = ref(''), checking = ref(false)
|
||
|
|
const snapshot = computed(() => JSON.stringify(model))
|
||
|
|
const registration = useUnsavedWorkRegistration({ key: `${key}:${getCurrentInstance()?.uid}`, label: computed(() => '拼车表单'), isOpen: computed(() => open.value || uncertain.value), model: reactive({ form: model, uncertain }), requiredPermission: computed(() => null) })
|
||
|
|
function capture() { baseline.value = snapshot.value; if (!uncertain.value) registration.captureBaseline() }
|
||
|
|
async function requestClose() {
|
||
|
|
if (busy.value || checking.value) return
|
||
|
|
if (snapshot.value !== baseline.value || uncertain.value) {
|
||
|
|
checking.value = true
|
||
|
|
try { await ElMessageBox.confirm(uncertain.value ? '上次提交结果尚未确定,原请求将保留。关闭后可重新打开并核对或重试,是否关闭?' : '表单有未保存内容,确认放弃本次修改?', '关闭表单', { customClass: 'admin-density-confirm', confirmButtonText: '确认关闭', cancelButtonText: '继续填写' }) }
|
||
|
|
catch { return }
|
||
|
|
finally { checking.value = false }
|
||
|
|
}
|
||
|
|
close()
|
||
|
|
}
|
||
|
|
return { capture, requestClose, dirty: computed(() => snapshot.value !== baseline.value) }
|
||
|
|
}
|