Files
FastapiAdmin/frontend/app/src/composables/useGlobalDialog.ts
T
zhangtao 50eac1872b chore: 初始化项目基础结构与配置
添加了nvm配置、静态资源、项目配置文件、工具类、api模块、状态管理、国际化、路由配置等基础代码,搭建了uniapp前端项目的初始框架。
2026-08-10 22:23:32 +08:00

107 lines
2.8 KiB
TypeScript

import type { DialogOptions, DialogResult } from '@wot-ui/ui/components/wd-dialog/types'
import { defineStore } from 'pinia'
export type GlobalDialogOptions = DialogOptions & {
success?: (res: DialogResult) => void
fail?: (res: DialogResult) => void
}
interface GlobalDialog {
dialogOptions: GlobalDialogOptions | null
currentPage: string
}
type DialogType = NonNullable<DialogOptions['type']>
function isButtonPropsObject(value: unknown): value is Record<string, any> {
return value !== null && CommonUtil.isObj(value)
}
function normalizeButtonProps(props: unknown, text?: string) {
if (props === null) {
return null
}
if (isButtonPropsObject(props)) {
return {
...props,
...(text ? { text } : {}),
}
}
if (CommonUtil.isString(props) || text) {
return {
text: text || props,
}
}
if (props === undefined) {
return {}
}
return props
}
function withDefaultTypeOptions(option: GlobalDialogOptions, type?: DialogType): GlobalDialogOptions {
const next: GlobalDialogOptions = {
...option,
...(type ? { type } : {}),
}
if (next.showCancelButton === undefined) {
if (next.type === 'alert') {
next.showCancelButton = false
}
else if (next.type === 'confirm' || next.type === 'prompt') {
next.showCancelButton = true
}
}
return next
}
function normalizeDialogOptions(option: GlobalDialogOptions, type?: DialogType): GlobalDialogOptions {
const next = withDefaultTypeOptions(option, type)
next.confirmButtonProps = normalizeButtonProps(next.confirmButtonProps, next.confirmButtonText) as DialogOptions['confirmButtonProps']
if (next.showCancelButton === false) {
next.cancelButtonProps = null
}
else if (next.showCancelButton === true || next.cancelButtonProps !== undefined || next.cancelButtonText) {
next.cancelButtonProps = normalizeButtonProps(next.cancelButtonProps, next.cancelButtonText) as DialogOptions['cancelButtonProps']
}
return next
}
function normalizeOption(option: GlobalDialogOptions | string, type?: DialogType): GlobalDialogOptions {
return normalizeDialogOptions(CommonUtil.isString(option) ? { title: option } : option, type)
}
export const useGlobalDialog = defineStore('global-Dialog', {
state: (): GlobalDialog => ({
dialogOptions: null,
currentPage: '',
}),
actions: {
show(option: GlobalDialogOptions | string, type?: DialogType) {
this.currentPage = getCurrentPath()
this.dialogOptions = normalizeOption(option, type)
},
alert(option: GlobalDialogOptions | string) {
this.show(option, 'alert')
},
confirm(option: GlobalDialogOptions | string) {
this.show(option, 'confirm')
},
prompt(option: GlobalDialogOptions | string) {
this.show(option, 'prompt')
},
close() {
this.dialogOptions = null
this.currentPage = ''
},
},
})