const DEFAULT_ROUTES = { cart: '/pages/cart/index', orders: '/pages/orders/index', products: '/pages/products/index', profile: '/pages/profile/index' } function normalizeRecoveryMessage(error, fallbackMessage) { if (typeof error === 'string' && error.trim()) { return error.trim() } if (error && error.message) { return String(error.message).trim() } return fallbackMessage || '操作失败,请稍后重试' } function normalizeRecoveryActions(actions) { return (Array.isArray(actions) ? actions : []) .filter(action => action && action.key && action.text) .map((action, index) => ({ key: action.key, text: action.text, route: action.route || DEFAULT_ROUTES[action.key] || '', primary: action.primary !== undefined ? Boolean(action.primary) : index === 0 })) } function buildRecoveryState(options) { const config = options || {} return { visible: true, title: config.title || '操作失败', message: normalizeRecoveryMessage(config.message || config.error, config.fallbackMessage), actions: normalizeRecoveryActions(config.actions) } } function clearRecoveryState() { return { visible: false, title: '', message: '', actions: [] } } function showRecoveryModal(options) { const state = buildRecoveryState(options) const primaryAction = state.actions.find(action => action.primary) || state.actions[0] const secondaryAction = state.actions.find(action => action !== primaryAction) return new Promise(resolve => { wx.showModal({ title: state.title, content: state.message, confirmText: primaryAction ? primaryAction.text : '知道了', cancelText: secondaryAction ? secondaryAction.text : '取消', showCancel: Boolean(secondaryAction), success: result => { if (result.confirm) { resolve(primaryAction || null) return } resolve(secondaryAction || null) }, fail: () => resolve(null) }) }) } module.exports = { DEFAULT_ROUTES, normalizeRecoveryMessage, buildRecoveryState, clearRecoveryState, showRecoveryModal }