功能:新增微信小程序界面

This commit is contained in:
xiaowang
2026-04-01 15:18:35 +08:00
parent bb2d8c4410
commit 7fa67d0511
92 changed files with 7463 additions and 0 deletions
+301
View File
@@ -0,0 +1,301 @@
const cartApi = require('../../api/cart')
const productApi = require('../../api/product')
const productLikeApi = require('../../api/product-like')
const shopApi = require('../../api/shop')
const wishlistApi = require('../../api/wishlist')
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const { resolveImageList } = require('../../utils/file')
const { DEFAULT_PRODUCT_IMAGE } = require('../../utils/config')
const { formatPrice, getProductTitle, splitCsv } = require('../../utils/format')
const { runWithLoading } = require('../../utils/loading')
function parseTimeRange(businessHours) {
if (!businessHours) {
return null
}
const matched = String(businessHours).replace(/\s+/g, '').match(/(\d{1,2}:\d{2}).*?(\d{1,2}:\d{2})/)
if (!matched) {
return null
}
return {
start: matched[1],
end: matched[2]
}
}
function estimateEarliestBookable(businessHours) {
const range = parseTimeRange(businessHours)
if (!range) {
return businessHours || '以门店安排为准'
}
const next = new Date(Date.now() + 30 * 60 * 1000)
const openTime = new Date(next)
const closeTime = new Date(next)
const startParts = range.start.split(':').map(Number)
const endParts = range.end.split(':').map(Number)
openTime.setHours(startParts[0], startParts[1], 0, 0)
closeTime.setHours(endParts[0], endParts[1], 0, 0)
if (next <= openTime) {
return `今天 ${range.start}`
}
if (next >= closeTime) {
return `明天 ${range.start}`
}
const month = String(next.getMonth() + 1).padStart(2, '0')
const date = String(next.getDate()).padStart(2, '0')
const hour = String(next.getHours()).padStart(2, '0')
const minute = String(next.getMinutes()).padStart(2, '0')
return `${month}-${date} ${hour}:${minute}`
}
Page({
data: {
loading: true,
loadError: '',
productId: '',
product: null,
productImages: [],
shopInfo: null,
isFavorite: false,
isLiked: false,
artistCountText: '门店安排',
earliestBookableText: '以门店安排为准'
},
onLoad(options) {
this.setData({
productId: options && options.id ? options.id : ''
})
this.loadProductDetail()
},
async onPullDownRefresh() {
await this.loadProductDetail()
wx.stopPullDownRefresh()
},
async loadProductDetail() {
const productId = this.data.productId
if (!productId) {
this.setData({
loading: false,
loadError: '作品 ID 不存在'
})
return
}
this.setData({
loading: true,
loadError: ''
})
try {
const product = await productApi.getProductById(productId)
const productImages = await resolveImageList(product.imageFileIds, DEFAULT_PRODUCT_IMAGE)
const shopInfo = product.shopId ? await shopApi.getShopById(product.shopId) : null
const artistCount = typeof product.availableBookableArtistCount === 'number'
? product.availableBookableArtistCount
: splitCsv(product.bookableArtists).length
const formattedProduct = {
...product,
displayTitle: getProductTitle(product),
priceText: `¥${formatPrice(product.price)}`,
descriptionText: product.description || '店家还没有补充作品说明。'
}
this.setData({
product: formattedProduct,
productImages,
shopInfo,
artistCountText: artistCount > 0 ? `${artistCount} 位可预约` : (product.bookableArtistRestricted ? '暂时没有' : '门店安排'),
earliestBookableText: estimateEarliestBookable(shopInfo && shopInfo.businessHours)
})
if (auth.isLoggedIn()) {
this.loadInteractionStatus()
} else {
this.setData({
isFavorite: false,
isLiked: false
})
}
} catch (error) {
this.setData({
loadError: error.message || '作品详情加载失败'
})
} finally {
this.setData({
loading: false
})
}
},
async loadInteractionStatus() {
const product = this.data.product
if (!product || !product.id) {
return
}
try {
const [isFavorite, isLiked] = await Promise.all([
wishlistApi.checkWishlist(product.id),
productLikeApi.checkLikeStatus(product.id)
])
this.setData({
isFavorite: Boolean(isFavorite),
isLiked: Boolean(isLiked)
})
} catch (error) {}
},
handleBack() {
navigation.goBack('/pages/products/index')
},
handlePreviewImage(event) {
const current = event.currentTarget.dataset.url
if (!current) {
return
}
wx.previewImage({
current,
urls: this.data.productImages
})
},
handleOpenShop() {
const shopInfo = this.data.shopInfo
if (!shopInfo || !shopInfo.id) {
return
}
navigation.navigate('/pages/shop-detail/index', {
id: shopInfo.id,
returnTo: navigation.getCurrentPageUrl()
})
},
async handleToggleFavorite() {
const product = this.data.product
if (!product || !product.id || !navigation.ensureLogin('收藏作品')) {
return
}
try {
const nextFavorite = !this.data.isFavorite
if (this.data.isFavorite) {
await runWithLoading(() => wishlistApi.removeFromWishlist(product.id))
product.collectCount = Math.max(0, Number(product.collectCount || 0) - 1)
} else {
await runWithLoading(() => wishlistApi.addToWishlist(product.id))
product.collectCount = Number(product.collectCount || 0) + 1
}
this.setData({
isFavorite: nextFavorite,
product
})
wx.showToast({
title: nextFavorite ? '已加入收藏' : '已取消收藏',
icon: 'none'
})
} catch (error) {
wx.showToast({
title: error.message || '收藏操作失败',
icon: 'none'
})
}
},
async handleToggleLike() {
const product = this.data.product
if (!product || !product.id || !navigation.ensureLogin('点赞作品')) {
return
}
try {
const nextLiked = !this.data.isLiked
if (this.data.isLiked) {
await runWithLoading(() => productLikeApi.unlikeProduct(product.id))
product.likeCount = Math.max(0, Number(product.likeCount || 0) - 1)
} else {
await runWithLoading(() => productLikeApi.likeProduct(product.id))
product.likeCount = Number(product.likeCount || 0) + 1
}
this.setData({
isLiked: nextLiked,
product
})
wx.showToast({
title: nextLiked ? '已点赞' : '已取消点赞',
icon: 'none'
})
} catch (error) {
wx.showToast({
title: error.message || '点赞操作失败',
icon: 'none'
})
}
},
async handleAddToCart() {
const product = this.data.product
if (!product || !product.id || !navigation.ensureLogin('加购')) {
return
}
try {
await runWithLoading(() => cartApi.addToCart({
productId: product.id,
quantity: 1
}))
await getApp().updateCartBadge()
wx.showToast({
title: '已加入购物车',
icon: 'none'
})
} catch (error) {
wx.showToast({
title: error.message || '加入购物车失败',
icon: 'none'
})
}
},
async handleBuyNow() {
const product = this.data.product
if (!product || !product.id || !navigation.ensureLogin('预约下单')) {
return
}
try {
await runWithLoading(() => cartApi.addToCart({
productId: product.id,
quantity: 1
}))
await getApp().updateCartBadge()
wx.switchTab({
url: '/pages/cart/index'
})
} catch (error) {
wx.showToast({
title: error.message || '操作失败',
icon: 'none'
})
}
}
})
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
}
@@ -0,0 +1,88 @@
<navigation-bar title="{{product ? product.displayTitle : '作品详情'}}" color="#2d2430" background="#fff7f8" bindback="handleBack"></navigation-bar>
<view class="page-shell detail-page">
<view wx:if="{{loadError}}" class="page-content">
<view class="page-card empty-card">
<text class="empty-title">暂时无法查看作品详情</text>
<text class="empty-desc">{{loadError}}</text>
<view class="ghost-btn" bindtap="handleBack">返回上一页</view>
</view>
</view>
<block wx:elif="{{product}}">
<view class="media-box">
<swiper class="media-swiper" indicator-dots autoplay circular>
<swiper-item wx:for="{{productImages}}" wx:key="*this">
<image class="media-image" src="{{item}}" mode="aspectFill" data-url="{{item}}" bindtap="handlePreviewImage"></image>
</swiper-item>
</swiper>
<view class="media-mask">
<view class="media-badges">
<view class="chip">{{product.approximateDuration || '时长待确认'}}</view>
<view class="soft-chip">{{artistCountText}}</view>
</view>
</view>
</view>
<view class="page-content detail-content">
<view class="page-card info-card">
<view class="detail-head">
<text class="section-kicker">作品详情</text>
<view class="product-title">{{product.displayTitle}}</view>
<view class="product-price">{{product.priceText}}</view>
<view class="product-brief">适合先看作品风格、门店信息和可预约情况,再决定是否立即下单。</view>
</view>
<view class="summary-strip detail-summary">
<view class="summary-metric" bindtap="handleOpenShop">
<text class="summary-label">所属门店</text>
<text class="summary-value detail-summary-value">{{shopInfo ? shopInfo.shopName : '待绑定'}}</text>
<text class="summary-note">点击查看门店详情</text>
</view>
<view class="summary-metric">
<text class="summary-label">可约美甲师</text>
<text class="summary-value detail-summary-value">{{artistCountText}}</text>
<text class="summary-note">预约前可在购物车继续选择</text>
</view>
<view class="summary-metric">
<text class="summary-label">最早可约</text>
<text class="summary-value detail-summary-value">{{earliestBookableText}}</text>
<text class="summary-note">以门店营业时间为准</text>
</view>
</view>
<view wx:if="{{shopInfo}}" class="shop-box" bindtap="handleOpenShop">
<view class="shop-box-head">
<view class="shop-name">{{shopInfo.shopName}}</view>
<view class="shop-link">查看门店</view>
</view>
<view class="shop-meta">{{shopInfo.address || '门店地址待补充'}}</view>
<view class="shop-meta">{{shopInfo.businessHours || '营业时间待补充'}}</view>
</view>
<view class="actions-row">
<view class="action-pill {{isLiked ? 'active' : ''}}" bindtap="handleToggleLike">
点赞 {{product.likeCount || 0}}
</view>
<view class="action-pill {{isFavorite ? 'active' : ''}}" bindtap="handleToggleFavorite">
收藏 {{product.collectCount || 0}}
</view>
</view>
<view wx:if="{{product.tagInfos && product.tagInfos.length}}" class="tags-wrap">
<view wx:for="{{product.tagInfos}}" wx:key="id" class="tag-chip">{{item.tagName}}</view>
</view>
<view class="desc-block">
<view class="section-title">款式说明</view>
<view class="desc-text">{{product.descriptionText}}</view>
</view>
</view>
</view>
<view class="footer-bar">
<view class="footer-btn secondary" bindtap="handleAddToCart">加入购物车</view>
<view class="footer-btn primary" bindtap="handleBuyNow">立即预约</view>
</view>
</block>
</view>
@@ -0,0 +1,193 @@
.detail-page {
padding-bottom: calc(env(safe-area-inset-bottom) + 160rpx);
}
.media-box {
position: relative;
overflow: hidden;
border-radius: 0 0 40rpx 40rpx;
}
.media-swiper,
.media-image {
width: 100%;
height: 640rpx;
}
.media-mask {
position: absolute;
left: 24rpx;
right: 24rpx;
bottom: 28rpx;
}
.media-badges {
display: flex;
flex-wrap: wrap;
gap: 12rpx;
}
.detail-content {
margin-top: -48rpx;
}
.info-card {
padding: 32rpx 30rpx;
}
.detail-head {
padding-bottom: 10rpx;
}
.product-title {
margin-top: 16rpx;
font-size: 42rpx;
font-weight: 700;
line-height: 1.4;
color: #2d2430;
}
.product-price {
margin-top: 12rpx;
font-size: 38rpx;
font-weight: 700;
color: #d5536f;
}
.product-brief {
margin-top: 14rpx;
font-size: 24rpx;
line-height: 1.8;
color: #8d7a84;
}
.detail-summary {
margin-top: 24rpx;
}
.detail-summary-value {
font-size: 26rpx;
line-height: 1.5;
}
.shop-box {
margin-top: 28rpx;
padding: 24rpx;
border-radius: 24rpx;
background: linear-gradient(135deg, #fff8fa 0%, #fff1f5 100%);
}
.shop-box-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16rpx;
}
.shop-name {
font-size: 30rpx;
font-weight: 700;
color: #2d2430;
}
.shop-link {
font-size: 24rpx;
color: #c6597d;
font-weight: 700;
}
.shop-meta {
margin-top: 10rpx;
font-size: 24rpx;
color: #8d7a84;
line-height: 1.7;
}
.actions-row {
display: flex;
gap: 16rpx;
margin-top: 28rpx;
}
.action-pill {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 180rpx;
height: 68rpx;
padding: 0 24rpx;
border-radius: 999rpx;
background: #fbf5f7;
color: #7a5262;
font-size: 24rpx;
font-weight: 600;
}
.action-pill.active {
background: #fde8ef;
color: #c6597d;
}
.tags-wrap {
display: flex;
flex-wrap: wrap;
gap: 12rpx;
margin-top: 24rpx;
}
.tag-chip {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 10rpx 18rpx;
border-radius: 999rpx;
background: #fbecf1;
color: #c6597d;
font-size: 22rpx;
}
.desc-block {
margin-top: 28rpx;
}
.desc-text {
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.8;
color: #5d4b55;
}
.footer-bar {
position: fixed;
left: 24rpx;
right: 24rpx;
bottom: calc(env(safe-area-inset-bottom) + 20rpx);
z-index: 20;
display: flex;
gap: 16rpx;
padding: 18rpx;
border-radius: 30rpx;
background: rgba(255, 255, 255, 0.96);
box-shadow: 0 18rpx 50rpx rgba(122, 67, 88, 0.12);
}
.footer-btn {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
height: 88rpx;
border-radius: 999rpx;
font-size: 28rpx;
font-weight: 700;
}
.footer-btn.secondary {
background: #fbf5f7;
color: #7a5262;
}
.footer-btn.primary {
background: linear-gradient(135deg, #ff8cb0 0%, #c6597d 100%);
color: #ffffff;
}