Files
JayuCloud/JC-ArtList-WeChat/utils/product.js
T

52 lines
2.0 KiB
JavaScript

const { formatPrice, getProductTitle } = require('./format')
const { resolveImageList } = require('./file')
const { DEFAULT_PRODUCT_IMAGE } = require('./config')
/**
* Purpose: getProductTags supports product.js by holding or executing the get product tags page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getProductTags(product) {
if (Array.isArray(product && product.tagInfos)) {
return product.tagInfos
.map(tag => tag && tag.tagName)
.filter(Boolean)
.slice(0, 3)
}
return []
}
/**
* Purpose: buildProductCard supports product.js by holding or executing the build product card page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
async function buildProductCard(product) {
const images = await resolveImageList(product && product.imageFileIds, DEFAULT_PRODUCT_IMAGE)
return {
...product,
displayTitle: getProductTitle(product),
priceText: `¥${formatPrice(product && product.price)}`,
metaText: (product && product.approximateDuration) || '支持预约',
shopNameText: product && product.shopName ? `所属店铺:${product.shopName}` : '',
coverUrl: images[0],
imageUrls: images,
tagsPreview: getProductTags(product),
statsText: `${Number((product && product.likeCount) || 0)}赞 · ${Number((product && product.collectCount) || 0)}藏`
}
}
/**
* Purpose: buildProductCards supports product.js by holding or executing the build product cards page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
async function buildProductCards(products) {
const list = Array.isArray(products) ? products : []
return Promise.all(list.map(item => buildProductCard(item)))
}
module.exports = {
buildProductCard,
buildProductCards
}