123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- // 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
- })
- },
- })
|