// pages/goods/detail.js const app = getApp() Page({ data: { id: null, goods: { title: '', price: '0.00', marketPrice: '0.00', commission: '0.00', commission_rate: 0, sales: 0, shares: 0, images: [], tags: [], detailImages: [] }, current: 0, cartCount: 0, isFavorite: false, showShare: false, showPoster: false, posterUrl: '', loading: true, hotRecommendations: [], posterTemplates: [ { id: 1, name: '简约风格', desc: '清新简约,突出商品主图', preview: '/static/images/poster/template1.png' }, { id: 2, name: '活力主题', desc: '色彩明快,突出价格优势', preview: '/static/images/poster/template2.png' }, { id: 3, name: '高级黑金', desc: '高端大气,突出品质感', preview: '/static/images/poster/template3.png' } ], currentTemplate: 1, showPosterModal: false, showBackToTop: false }, onLoad(options) { const { id } = options this.setData({ id }) this.loadGoodsDetail() this.getCartCount() this.checkFavorite() this.loadHotRecommendations() }, onPageScroll(e) { // 页面滚动超过一屏时显示返回顶部按钮 const showBackToTop = e.scrollTop > wx.getSystemInfoSync().windowHeight if (showBackToTop !== this.data.showBackToTop) { this.setData({ showBackToTop }) } }, onShow() { this.getCartCount() }, // 加载商品详情 async loadGoodsDetail() { wx.showLoading({ title: '加载中' }) try { // TODO: 替换为实际的API调用 // 模拟数据 const goods = { title: 'iPhone 14 Pro Max', price: '9299.00', marketPrice: '9999.00', commission: '186.00', commission_rate: 2, sales: 8526, shares: 1253, images: [ '/static/images/goods/iphone-1.png', '/static/images/goods/iphone-2.png', '/static/images/goods/iphone-3.png' ], tags: ['官方正品', '全网最低价', '24期免息', '顺丰包邮'], detailImages: [ '/static/images/goods/detail-1.png', '/static/images/goods/detail-2.png', '/static/images/goods/detail-3.png' ] } this.setData({ goods, loading: false }) wx.hideLoading() } catch (error) { console.error('加载商品详情失败:', error) wx.hideLoading() wx.showToast({ title: '加载失败', icon: 'none' }) } }, // 轮播图切换 onSwiperChange(e) { this.setData({ current: e.detail.current }) }, // 预览图片 previewImage(e) { const { current } = e.currentTarget.dataset const { images } = this.data.goods wx.previewImage({ current, urls: images }) }, // 预览详情图片 previewDetailImage(e) { const { current } = e.currentTarget.dataset const { detailImages } = this.data.goods wx.previewImage({ current, urls: detailImages }) }, // 获取购物车数量 async getCartCount() { try { const cartList = wx.getStorageSync('cart') || [] const cartCount = cartList.reduce((sum, item) => sum + item.quantity, 0) this.setData({ cartCount }) } catch (error) { console.error('获取购物车数量失败:', error) } }, // 检查是否已收藏 checkFavorite() { try { const favorites = wx.getStorageSync('favorites') || [] const isFavorite = favorites.includes(this.data.id) this.setData({ isFavorite }) } catch (error) { console.error('检查收藏状态失败:', error) } }, // 切换收藏状态 async toggleFavorite() { try { if (checkNeedLogin()) { try { await doLogin() } catch (err) { console.error('登录失败:', err) wx.showToast({ title: '登录失败', icon: 'none' }) return } } const { id, isFavorite } = this.data let favorites = wx.getStorageSync('favorites') || [] if (isFavorite) { favorites = favorites.filter(item => item !== id) } else { favorites.push(id) } wx.setStorageSync('favorites', favorites) this.setData({ isFavorite: !isFavorite }) wx.showToast({ title: isFavorite ? '已取消收藏' : '已收藏', icon: 'success' }) } catch (error) { console.error('收藏操作失败:', error) wx.showToast({ title: '操作失败', icon: 'none' }) } }, // 加入购物车 addToCart() { try { const { id, goods } = this.data let cartList = wx.getStorageSync('cart') || [] const existItem = cartList.find(item => item.id === id) if (existItem) { existItem.quantity += 1 } else { cartList.push({ id, title: goods.title, price: goods.price, image: goods.images[0], quantity: 1 }) } wx.setStorageSync('cart', cartList) this.getCartCount() wx.showToast({ title: '已加入购物车', icon: 'success' }) } catch (error) { console.error('加入购物车失败:', error) wx.showToast({ title: '操作失败', icon: 'none' }) } }, // 立即购买 buyNow() { const { id } = this.data wx.navigateTo({ url: `/pages/order/confirm?goods_id=${id}&quantity=1` }) }, // 检查推客身份 checkPromoterStatus() { const userData = wx.getStorageSync('user_data') return userData && userData.is_promoter === 1 }, // 显示非推客提示弹窗 showPromoterTips() { return new Promise((resolve) => { wx.showModal({ title: '温馨提示', content: '您还不是推客,购买/分享商品将无法获得收益', confirmText: '去授权', cancelText: '暂不收益', success(res) { if (res.confirm) { // 跳转到推客授权页面 wx.navigateTo({ url: '/pages/promoter/auth' }) resolve(false) } else { resolve(true) } }, fail() { resolve(false) } }) }) }, // 显示分享面板 async showSharePanel() { if (!this.checkPromoterStatus()) { const continue_share = await this.showPromoterTips() if (!continue_share) return } this.setData({ showShare: true }) }, // 隐藏分享面板 hideSharePanel() { this.setData({ showShare: false }) }, // 加载热门推荐 async loadHotRecommendations() { try { // TODO: 替换为实际的API调用 const recommendations = [ { id: 1, title: '2023新款时尚潮流女装连衣裙', price: '299.00', sales: 1234, tag: '新品', image: '/static/images/goods/recommend1.png' }, { id: 2, title: '夏季薄款防晒衣女士外套', price: '199.00', sales: 856, tag: '热销', image: '/static/images/goods/recommend2.png' }, { id: 3, title: '高腰显瘦牛仔裤女装新款', price: '258.00', sales: 2341, tag: '爆款', image: '/static/images/goods/recommend3.png' }, { id: 4, title: '轻奢小香风名媛气质套装', price: '458.00', sales: 1567, tag: '推荐', image: '/static/images/goods/recommend4.png' }, { id: 5, title: '夏季新款韩版休闲运动套装', price: '228.00', sales: 987, tag: '特惠', image: '/static/images/goods/recommend5.png' }, { id: 6, title: '法式复古碎花连衣裙', price: '328.00', sales: 1876, tag: '流行', image: '/static/images/goods/recommend6.png' }, { id: 7, title: '时尚百搭小黑裙', price: '269.00', sales: 2156, tag: '经典', image: '/static/images/goods/recommend7.png' }, { id: 8, title: '轻薄透气运动鞋', price: '399.00', sales: 1432, tag: '新款', image: '/static/images/goods/recommend8.png' }, { id: 9, title: '高档真丝阔腿裤套装', price: '599.00', sales: 876, tag: '精选', image: '/static/images/goods/recommend9.png' } ] this.setData({ hotRecommendations: recommendations }) } catch (error) { console.error('加载推荐商品失败:', error) } }, // 跳转到商品详情 navigateToGoods(e) { const { id } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/goods/detail?id=${id}` }) }, // 生成分享海报 async generatePoster() { if (!this.checkPromoterStatus()) { const continue_share = await this.showPromoterTips() if (!continue_share) return } this.setData({ showPosterModal: true }) }, // 切换海报模板 switchTemplate(e) { const { templateId } = e.currentTarget.dataset this.setData({ currentTemplate: templateId }) }, // 确认生成海报 async confirmGeneratePoster() { wx.showLoading({ title: '生成中' }) try { const { goods, currentTemplate } = this.data // TODO: 替换为实际的海报生成API调用 const posterConfig = { template: currentTemplate, goods: { title: goods.title, price: goods.price, image: goods.images[0] } } // 模拟API调用 setTimeout(() => { this.setData({ posterUrl: '/static/images/poster/sample.png', showPosterModal: false, showPoster: true }) wx.hideLoading() }, 1000) } catch (error) { console.error('生成海报失败:', error) wx.hideLoading() wx.showToast({ title: '生成失败', icon: 'none' }) } }, // 关闭海报模态框 closePosterModal() { this.setData({ showPosterModal: false }) }, // 关闭海报预览 closePosterPreview() { this.setData({ showPoster: false, posterUrl: '' }) }, // 保存海报 async savePoster() { try { await wx.saveImageToPhotosAlbum({ filePath: this.data.posterUrl }) wx.showToast({ title: '保存成功', icon: 'success' }) this.hidePoster() } catch (error) { console.error('保存海报失败:', error) if (error.errMsg.includes('auth deny')) { wx.showModal({ title: '提示', content: '需要您授权保存图片到相册', success: (res) => { if (res.confirm) { wx.openSetting() } } }) } else { wx.showToast({ title: '保存失败', icon: 'none' }) } } }, // 分享给好友 onShareAppMessage() { const { goods } = this.data return { title: goods.title, imageUrl: goods.images[0], path: `/pages/goods/detail?id=${this.data.id}` } }, // 分享到朋友圈 onShareTimeline() { const { goods } = this.data return { title: goods.title, imageUrl: goods.images[0], query: `id=${this.data.id}` } }, // 返回顶部 scrollToTop() { wx.pageScrollTo({ scrollTop: 0, duration: 300 }) }, })