功能(小程序):同步用户体验流程到小程序
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const STORAGE_KEY = 'YUMI_BUY_NOW_CONTEXT'
|
||||
|
||||
function createBuyNowContext(productId) {
|
||||
return {
|
||||
mode: 'buyNow',
|
||||
productId: String(productId || ''),
|
||||
createdAt: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
function saveBuyNowContext(productId) {
|
||||
const context = createBuyNowContext(productId)
|
||||
wx.setStorageSync(STORAGE_KEY, context)
|
||||
return context
|
||||
}
|
||||
|
||||
function readBuyNowContext(maxAgeMs = 5 * 60 * 1000) {
|
||||
const context = wx.getStorageSync(STORAGE_KEY)
|
||||
if (!context || context.mode !== 'buyNow' || !context.productId) {
|
||||
return null
|
||||
}
|
||||
if (Date.now() - Number(context.createdAt || 0) > maxAgeMs) {
|
||||
return null
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
function clearBuyNowContext() {
|
||||
wx.removeStorageSync(STORAGE_KEY)
|
||||
}
|
||||
|
||||
function consumeBuyNowContext(maxAgeMs = 5 * 60 * 1000) {
|
||||
const context = readBuyNowContext(maxAgeMs)
|
||||
clearBuyNowContext()
|
||||
return context
|
||||
}
|
||||
|
||||
function isBuyNowCartItem(item, productId) {
|
||||
return Boolean(productId) && String(item && item.productId || '') === String(productId)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
STORAGE_KEY,
|
||||
clearBuyNowContext,
|
||||
consumeBuyNowContext,
|
||||
isBuyNowCartItem,
|
||||
readBuyNowContext,
|
||||
saveBuyNowContext
|
||||
}
|
||||
+45
-13
@@ -6,6 +6,17 @@ const fileApi = require('../api/file')
|
||||
const { DEFAULT_PRODUCT_IMAGE } = require('./config')
|
||||
const { CACHE_TTL } = require('./config')
|
||||
const { splitCsv } = require('./format')
|
||||
const { showRecoveryModal } = require('./recovery')
|
||||
|
||||
function handlePreviewRecoveryAction(action) {
|
||||
if (!action || action.key !== 'products') {
|
||||
return
|
||||
}
|
||||
wx.switchTab({
|
||||
url: '/pages/products/index',
|
||||
fail() {}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: urlCache supports file.js by holding or executing the url cache page or API role.
|
||||
@@ -91,24 +102,45 @@ async function resolveLegacyImage(value, fallbackImage) {
|
||||
* Purpose: previewImagesSafely opens wx.previewImage with a user-facing fallback when signed URLs expire or preview fails.
|
||||
* Usage: pass the current image URL and full URL list from image tap handlers instead of calling wx.previewImage directly.
|
||||
*/
|
||||
function previewImagesSafely(current, urls) {
|
||||
function previewImagesSafely(current, urls, options) {
|
||||
const config = options || {}
|
||||
const list = (Array.isArray(urls) ? urls : [current]).filter(Boolean)
|
||||
if (!current || !list.length) {
|
||||
wx.showToast({
|
||||
return showRecoveryModal({
|
||||
title: '图片暂不可预览',
|
||||
icon: 'none'
|
||||
message: config.emptyMessage || '没有可预览的图片,请返回作品列表查看其他款式。',
|
||||
actions: [
|
||||
{ key: 'dismiss', text: '知道了' },
|
||||
{ key: 'products', text: '看作品' }
|
||||
]
|
||||
}).then(action => {
|
||||
handlePreviewRecoveryAction(action)
|
||||
return action
|
||||
})
|
||||
return
|
||||
}
|
||||
wx.previewImage({
|
||||
current,
|
||||
urls: list,
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '图片预览失败,请稍后重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
wx.previewImage({
|
||||
current,
|
||||
urls: list,
|
||||
success: () => resolve(null),
|
||||
fail: () => {
|
||||
showRecoveryModal({
|
||||
title: '图片预览失败',
|
||||
message: config.failMessage || '图片链接可能已过期,请重试;仍失败时可以返回作品列表重新进入。',
|
||||
actions: [
|
||||
{ key: 'retry', text: '重试' },
|
||||
{ key: 'products', text: '看作品' }
|
||||
]
|
||||
}).then(action => {
|
||||
if (action && action.key === 'retry') {
|
||||
previewImagesSafely(current, list, config).then(resolve)
|
||||
return
|
||||
}
|
||||
handlePreviewRecoveryAction(action)
|
||||
resolve(action)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
const assert = require('assert')
|
||||
|
||||
const recovery = require('./recovery')
|
||||
|
||||
const checkoutState = recovery.buildRecoveryState({
|
||||
title: '下单失败',
|
||||
message: new Error('库存不足'),
|
||||
fallbackMessage: '订单创建失败',
|
||||
actions: [
|
||||
{ key: 'retry', text: '重试下单' },
|
||||
{ key: 'orders', text: '查看订单' },
|
||||
{ key: 'contact', text: '联系客服' },
|
||||
{ key: 'cart', text: '返回购物车' }
|
||||
]
|
||||
})
|
||||
|
||||
assert.strictEqual(checkoutState.visible, true)
|
||||
assert.strictEqual(checkoutState.title, '下单失败')
|
||||
assert.strictEqual(checkoutState.message, '库存不足')
|
||||
assert.strictEqual(checkoutState.actions.length, 4)
|
||||
assert.strictEqual(checkoutState.actions[0].key, 'retry')
|
||||
assert.strictEqual(checkoutState.actions[0].primary, true)
|
||||
assert.strictEqual(checkoutState.actions[3].route, '/pages/cart/index')
|
||||
|
||||
const emptyState = recovery.clearRecoveryState()
|
||||
assert.strictEqual(emptyState.visible, false)
|
||||
assert.deepStrictEqual(emptyState.actions, [])
|
||||
|
||||
const normalized = recovery.normalizeRecoveryMessage(null, '图片预览失败')
|
||||
assert.strictEqual(normalized, '图片预览失败')
|
||||
@@ -96,10 +96,14 @@ function isWhitelistedPath(url) {
|
||||
'/product/get/',
|
||||
'/product/list/',
|
||||
'/comment/list/product/',
|
||||
'/nail-artist/profile/',
|
||||
'/nail-artist/',
|
||||
'/shop/get/',
|
||||
'/shop/trust-profile/',
|
||||
'/shop/list/public',
|
||||
'/shop/list/simple',
|
||||
'/tag/list/all',
|
||||
'/tag/list/filter-options',
|
||||
'/file/get-signed-url/'
|
||||
].some(path => String(url || '').includes(path))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user