45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { showConfirmDialog, type DialogOptions } from 'vant'
|
|
|
|
export type CrmConfirmKind = 'default' | 'danger'
|
|
|
|
export type BuildCrmConfirmInput = {
|
|
kind: CrmConfirmKind
|
|
title: string
|
|
message: string
|
|
confirmButtonText?: string
|
|
}
|
|
|
|
function escapeHtml(text: string): string {
|
|
return text
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
}
|
|
|
|
/** 方案 B:图标在上、标题居中(对齐 Demo),标题写入 message HTML */
|
|
function dangerMessageHtml(title: string, message: string): string {
|
|
return (
|
|
`<div class="mp-crm-dialog__warn"><span class="mp-crm-dialog__warn-icon">!</span></div>` +
|
|
`<div class="mp-crm-dialog__title">${escapeHtml(title)}</div>` +
|
|
`<div class="mp-crm-dialog__msg">${escapeHtml(message)}</div>`
|
|
)
|
|
}
|
|
|
|
export function buildCrmConfirmOptions(input: BuildCrmConfirmInput): DialogOptions {
|
|
const isDanger = input.kind === 'danger'
|
|
return {
|
|
title: isDanger ? '' : input.title,
|
|
message: isDanger ? dangerMessageHtml(input.title, input.message) : input.message,
|
|
allowHtml: isDanger,
|
|
className: isDanger ? 'mp-crm-dialog mp-crm-dialog--danger' : 'mp-crm-dialog',
|
|
confirmButtonText: input.confirmButtonText ?? (isDanger ? '确认删除' : '确认'),
|
|
confirmButtonColor: isDanger ? '#dc2626' : '#0a9f6d'
|
|
}
|
|
}
|
|
|
|
export function showCrmConfirmDialog(input: BuildCrmConfirmInput) {
|
|
return showConfirmDialog(buildCrmConfirmOptions(input))
|
|
}
|