fix: 弹出弹窗逻辑修改

This commit is contained in:
Tim
2025-08-19 21:48:48 +08:00
parent e9458f5419
commit fb89c9fb25
4 changed files with 194 additions and 90 deletions

View File

@@ -1,42 +0,0 @@
import { ref } from 'vue'
const state = ref({
visible: false,
title: '',
message: '',
resolve: null,
reject: null,
})
export const useConfirm = () => {
const confirm = (title, message) => {
state.value.title = title
state.value.message = message
state.value.visible = true
return new Promise((resolve, reject) => {
state.value.resolve = resolve
state.value.reject = reject
})
}
const onConfirm = () => {
if (state.value.resolve) {
state.value.resolve(true)
}
state.value.visible = false
}
const onCancel = () => {
if (state.value.reject) {
state.value.reject(false)
}
state.value.visible = false
}
return {
confirm,
onConfirm,
onCancel,
state,
}
}

View File

@@ -0,0 +1,52 @@
// composables/useConfirm.ts
import { ref } from 'vue'
// 全局单例SPA 下即可Nuxt/SSR 下见文末“SSR 提醒”)
const visible = ref(false)
const title = ref('')
const message = ref('')
let resolver: ((ok: boolean) => void) | null = null
function reset() {
visible.value = false
title.value = ''
message.value = ''
resolver = null
}
export function useConfirm() {
/**
* 打开确认框,返回 Promise<boolean>
* - 确认 => resolve(true)
* - 取消/关闭 => resolve(false)
* 若并发调用,以最后一次为准(更简单直观)
*/
const confirm = (t: string, m: string) => {
title.value = t
message.value = m
visible.value = true
return new Promise<boolean>((resolve) => {
resolver = resolve
})
}
const onConfirm = () => {
resolver?.(true)
reset()
}
const onCancel = () => {
resolver?.(false)
reset()
}
return {
visible,
title,
message,
confirm,
onConfirm,
onCancel,
}
}