50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
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
|
|
}
|