xcx-shop/src/utils/page.ts
2025-10-15 22:04:21 +08:00

64 lines
1.8 KiB
TypeScript

import { closeToast, showLoadingToast, type ToastWrapperInstance } from 'vant'
interface ApiType {
api: <T, K>(data?: T) => Promise<{ code: string; msg: string; data: K }>
params?: object
}
export const useCreatePageData = (apis: ApiType[]) => {
return Promise.all(apis.map((item: ApiType) => item.api(item.params)))
}
// 提交loading
export const useSubmitLoading = (message = '提交中...') => {
showLoadingToast({ duration: 0, overlay: true, forbidClick: true, message })
}
useSubmitLoading.close = () => {
closeToast()
}
// 将日期转为数组
export const handleDateToArray = (date: Date) => {
return [date.getFullYear().toString(), (date.getMonth() + 1).toString().padStart(2, '0'), date.getDate().toString().padStart(2, '0')]
}
window.isBgWhite = ref(true)
export const isBgWhite = window.isBgWhite
// 获取loading
export const loadingData = reactive({ if: null as null | boolean, type: 'enter', show: false })
window.useLoading = (ob: { type: 'enter' | 'submit'; show: boolean } = { type: 'enter', show: true }) => {
loadingData.if ??= true
return Object.assign(loadingData, ob)
}
// 提示弹窗
export const tipDialogData = reactive({
if: null as null | boolean,
show: false,
showCloseIcon: false,
showCancelBtn: false,
cancelBtnName: '',
title: '',
html: '',
btnName: '',
callback: (() => {}) as (result: boolean) => void | Promise<any>,
})
window.useTipDialog = (
callback: (result: boolean) => void | Promise<any>,
config: {
title: string
html: string
btnName: string
showCloseIcon?: boolean
showCancelBtn?: boolean
cancelBtnName?: string
},
) => {
tipDialogData.showCancelBtn = false
Object.assign(tipDialogData, config)
tipDialogData.if ??= true
tipDialogData.show = true
tipDialogData.callback = callback
}