功能(小程序):同步用户体验流程到小程序
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
|
||||
}
|
||||
Reference in New Issue
Block a user