Files
zt-closet-wxmini-frontend/miniprogram/components/v2-overlay/index.ts
T
2026-09-15 16:01:59 +08:00

89 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { logger } from '../../utils/logger'
// 日志模块名
const LOG = 'v2-overlay'
/**
* v2 统一玻璃弹层。
*
* 三类形态:
* modal 居中玻璃卡(确认/提示),buttons 决定按钮,事件 confirm/cancel
* actionsheet 底部动作表(选择),items 决定条目,事件 select/cancel
* toast 深色玻璃胶囊,duration 后自动关闭并抛出 close
*
* 页面侧只维护 visible 与文案数据,回调分支与原生 wx.showModal 一一对应。
*/
Component({
properties: {
visible: { type: Boolean, value: false },
type: { type: String, value: 'modal' },
title: { type: String, value: '' },
content: { type: String, value: '' },
// modal 按钮:[{ text, value, type: 'main' | 'normal' }]
buttons: { type: Array, value: [] },
// actionsheet 条目:['拍照', '从相册选择'] 或 [{ text, value }]
items: { type: Array, value: [] },
cancelText: { type: String, value: '取消' },
// toast 自动关闭时长(ms)
duration: { type: Number, value: 1800 },
},
observers: {
visible(v: boolean) {
if (!v) {
this.clearTimer()
return
}
if (this.data.type === 'toast') this.startTimer()
},
},
lifetimes: {
detached() {
this.clearTimer()
},
},
methods: {
startTimer() {
this.clearTimer()
const t = setTimeout(() => {
logger.debug(LOG, 'toast 自动关闭')
this.triggerEvent('close')
}, this.data.duration)
;(this as any).timer = t
},
clearTimer() {
const t = (this as any).timer
if (t) {
clearTimeout(t)
;(this as any).timer = null
}
},
onBtn(e: WechatMiniprogram.TouchEvent) {
const index = Number((e.currentTarget.dataset as any).index) || 0
const btn = (this.data.buttons[index] || {}) as any
logger.debug(LOG, 'modal 按钮点击, index=', index, 'value=', btn.value)
this.triggerEvent('confirm', { index, value: btn.value || '' })
},
onItem(e: WechatMiniprogram.TouchEvent) {
const index = Number((e.currentTarget.dataset as any).index) || 0
const raw = this.data.items[index] as any
const value = raw && typeof raw === 'object' ? raw.value || '' : raw
logger.debug(LOG, 'actionsheet 条目点击, index=', index, 'value=', value)
this.triggerEvent('select', { index, value })
},
onCancel() {
logger.debug(LOG, '弹层取消')
this.triggerEvent('cancel')
},
// 阻断蒙层下的页面滚动
noop() {},
},
})