File size: 1,027 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ref } from 'vue'

const showConfirmModal = ref(false)
const confirmOptions = ref({
  title: '',
  message: '',
  confirmText: '继续',
  cancelText: '取消'
})
const confirmResolve = ref(null)

export function useConfirm() {
  const showConfirm = (title, message, confirmText = '继续', cancelText = '取消') => {
    return new Promise((resolve) => {
      confirmOptions.value = {
        title,
        message,
        confirmText,
        cancelText
      }
      confirmResolve.value = resolve
      showConfirmModal.value = true
    })
  }

  const handleConfirm = () => {
    showConfirmModal.value = false
    if (confirmResolve.value) {
      confirmResolve.value(true)
      confirmResolve.value = null
    }
  }

  const handleCancel = () => {
    showConfirmModal.value = false
    if (confirmResolve.value) {
      confirmResolve.value(false)
      confirmResolve.value = null
    }
  }

  return {
    showConfirmModal,
    confirmOptions,
    showConfirm,
    handleConfirm,
    handleCancel
  }
}