ai-c/pages/gift-shop/gift-shop.js
2026-02-04 18:12:02 +08:00

154 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const api = require('../../utils/api')
const config = require('../../config/index')
Page({
data: {
statusBarHeight: 44,
navBarHeight: 44,
totalNavHeight: 88,
lovePoints: 0,
isLoggedIn: false,
gifts: [],
giftsLoading: false,
giftsError: '',
skeletonGifts: Array.from({ length: 6 }).map((_, idx) => ({ id: idx + 1 }))
},
getGiftImageUrl(url) {
if (!url) return ''
if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('data:')) return url
// 兼容所有相对路径
if (url.startsWith('/')) {
const baseUrl = String(config.API_BASE_URL || '').replace(/\/api$/, '')
return baseUrl + url
}
return url
},
async onLoad() {
// 计算导航栏高度
const systemInfo = wx.getSystemInfoSync()
const statusBarHeight = systemInfo.statusBarHeight || 44
const menuButton = wx.getMenuButtonBoundingClientRect()
const navBarHeight = menuButton.height + (menuButton.top - statusBarHeight) * 2
const totalNavHeight = statusBarHeight + navBarHeight
this.setData({
statusBarHeight,
navBarHeight,
totalNavHeight
})
// 礼物商城无需登录即可访问
this.loadGifts()
// 检查登录状态并加载爱心值
const app = getApp()
const isLoggedIn = app.globalData.isLoggedIn
this.setData({ isLoggedIn })
if (isLoggedIn) {
this.loadLovePoints()
}
},
onShow() {
const app = getApp()
this.setData({ isLoggedIn: app.globalData.isLoggedIn })
// 仅在登录状态下刷新爱心值
if (app.globalData.isLoggedIn) {
this.loadLovePoints()
}
},
// 加载爱心值
async loadLovePoints() {
try {
const res = await api.loveExchange.getOptions()
if (res.success) {
this.setData({
lovePoints: res.data.current_love_points || 0
})
}
} catch (error) {
console.error('加载爱心值失败:', error)
}
},
async loadGifts() {
if (this.data.giftsLoading) return
this.setData({
giftsLoading: true,
giftsError: ''
})
try {
// 使用 shop.getItems 获取 exchange_items 数据
const res = await api.shop.getItems()
if (res.success) {
const gifts = (res.data || []).map(gift => ({
id: gift.id,
name: gift.name,
// 兼容后端返回 image 或 image_url
image: this.getGiftImageUrl(gift.image_url || gift.image),
// 兼容后端返回 price 或 love_cost
love_cost: gift.price || gift.love_cost,
stock: gift.stock,
sold_count: gift.sold_count,
category: gift.category
}))
this.setData({ gifts })
} else {
this.setData({ giftsError: res.error || '获取礼品失败' })
}
} catch (error) {
console.error('加载礼品失败:', error)
this.setData({ giftsError: error.message || '获取礼品失败' })
} finally {
this.setData({ giftsLoading: false })
}
},
// 返回上一页
goBack() {
wx.navigateBack()
},
// 查看明细
onViewDetails() {
wx.navigateTo({
url: '/pages/love-transactions/love-transactions'
})
},
// 点击礼品卡片
onGiftTap() {
wx.showModal({
title: '联系客服',
content: '请添加客服mmj20259999',
confirmText: '复制',
success: (res) => {
if (res.confirm) {
wx.setClipboardData({
data: 'mmj20259999',
success: () => {
wx.showToast({ title: '已复制', icon: 'success' })
}
})
}
}
})
},
// 下拉刷新
onPullDownRefresh() {
this.loadLovePoints()
this.loadGifts()
setTimeout(() => {
wx.stopPullDownRefresh()
}, 1000)
}
})