226 lines
6.1 KiB
JavaScript
226 lines
6.1 KiB
JavaScript
|
|
import { fetchUserCoupons } from '../../../services/coupon/index';
|
|||
|
|
|
|||
|
|
Page({
|
|||
|
|
data: {
|
|||
|
|
status: 0,
|
|||
|
|
list: [
|
|||
|
|
{
|
|||
|
|
text: '可使用',
|
|||
|
|
key: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '已使用',
|
|||
|
|
key: 1,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '已失效',
|
|||
|
|
key: 2,
|
|||
|
|
},
|
|||
|
|
],
|
|||
|
|
|
|||
|
|
couponList: [],
|
|||
|
|
loading: false,
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onLoad() {
|
|||
|
|
this.init();
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onShow() {
|
|||
|
|
// 页面显示时刷新数据,确保从领券中心返回后显示最新的优惠券
|
|||
|
|
this.fetchList();
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
init() {
|
|||
|
|
this.fetchList();
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
fetchList(status = this.data.status) {
|
|||
|
|
this.setData({ loading: true });
|
|||
|
|
|
|||
|
|
// 状态映射:前端页面状态 -> API状态
|
|||
|
|
// 前端: 0=可使用, 1=已使用, 2=已失效
|
|||
|
|
// API: 1=未使用, 2=已使用, 3=已过期
|
|||
|
|
const statusMap = {
|
|||
|
|
0: 1, // 可使用 -> 未使用(API状态1)
|
|||
|
|
1: 2, // 已使用 -> 已使用(API状态2)
|
|||
|
|
2: 3, // 已失效 -> 已过期(API状态3)
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const apiStatus = statusMap[status] || 1;
|
|||
|
|
|
|||
|
|
fetchUserCoupons(apiStatus)
|
|||
|
|
.then((result) => {
|
|||
|
|
console.log('获取用户优惠券成功:', result);
|
|||
|
|
console.log('API返回的原始数据:', JSON.stringify(result.data, null, 2));
|
|||
|
|
|
|||
|
|
// 检查第一个优惠券的数据结构
|
|||
|
|
if (result.data && result.data.length > 0) {
|
|||
|
|
const firstCoupon = result.data[0];
|
|||
|
|
console.log('第一个优惠券数据:', firstCoupon);
|
|||
|
|
console.log('优惠券详情:', firstCoupon.coupon);
|
|||
|
|
console.log('优惠券面值:', firstCoupon.coupon?.value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 转换API数据格式为页面所需格式
|
|||
|
|
const couponList = this.transformCouponData(result.data || [], status);
|
|||
|
|
|
|||
|
|
this.setData({
|
|||
|
|
couponList,
|
|||
|
|
loading: false
|
|||
|
|
});
|
|||
|
|
})
|
|||
|
|
.catch((error) => {
|
|||
|
|
console.error('获取用户优惠券失败:', error);
|
|||
|
|
wx.showToast({
|
|||
|
|
title: '获取优惠券失败',
|
|||
|
|
icon: 'none'
|
|||
|
|
});
|
|||
|
|
this.setData({
|
|||
|
|
couponList: [],
|
|||
|
|
loading: false
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 转换API数据格式
|
|||
|
|
transformCouponData(apiData, status) {
|
|||
|
|
const statusMap = {
|
|||
|
|
0: 'default', // 可使用
|
|||
|
|
1: 'useless', // 已使用
|
|||
|
|
2: 'disabled' // 已失效
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return apiData.map((item, index) => {
|
|||
|
|
const coupon = item.coupon || item;
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
key: `coupon_${coupon.id || index}`,
|
|||
|
|
title: coupon.name || '优惠券',
|
|||
|
|
type: coupon.type || 1,
|
|||
|
|
value: coupon.value, // 直接使用原始值,让ui-coupon-card组件处理
|
|||
|
|
currency: this.getCurrency(coupon.type),
|
|||
|
|
tag: this.getCouponTag(coupon.type),
|
|||
|
|
desc: this.getCouponDesc(coupon),
|
|||
|
|
timeLimit: this.formatTimeLimit(coupon, status),
|
|||
|
|
status: statusMap[status] || 'default',
|
|||
|
|
};
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 格式化优惠券金额
|
|||
|
|
formatCouponValue(coupon) {
|
|||
|
|
console.log('formatCouponValue 输入参数:', coupon);
|
|||
|
|
console.log('优惠券类型:', coupon?.type, '面值:', coupon?.value);
|
|||
|
|
|
|||
|
|
if (!coupon || coupon.value === undefined) {
|
|||
|
|
console.log('优惠券数据无效,返回0');
|
|||
|
|
return '0';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 直接返回原始值,让ui-coupon-card组件处理单位转换
|
|||
|
|
// ui-coupon-card会自动将满减券除以100(分转元),折扣券除以10(85转8.5折)
|
|||
|
|
if (coupon.type === 1) { // 满减券
|
|||
|
|
console.log('满减券,返回原始值:', coupon.value);
|
|||
|
|
return coupon.value.toString(); // 返回原始分值,ui-coupon-card会除以100
|
|||
|
|
} else if (coupon.type === 2) { // 折扣券
|
|||
|
|
console.log('折扣券,返回原始值:', coupon.value);
|
|||
|
|
return coupon.value.toString(); // 返回原始值,ui-coupon-card会除以10
|
|||
|
|
} else if (coupon.type === 3) { // 免邮券
|
|||
|
|
console.log('免邮券,返回0');
|
|||
|
|
return '0';
|
|||
|
|
}
|
|||
|
|
console.log('未知类型,返回0');
|
|||
|
|
return '0';
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 获取货币符号
|
|||
|
|
getCurrency(type) {
|
|||
|
|
if (type === 1) return '¥';
|
|||
|
|
if (type === 2) return '折';
|
|||
|
|
if (type === 3) return '';
|
|||
|
|
return '¥';
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 获取优惠券标签
|
|||
|
|
getCouponTag(type) {
|
|||
|
|
if (type === 1) return '满减券';
|
|||
|
|
if (type === 2) return '折扣券';
|
|||
|
|
if (type === 3) return '免邮券';
|
|||
|
|
return '优惠券';
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 获取优惠券描述
|
|||
|
|
getCouponDesc(coupon) {
|
|||
|
|
if (!coupon) {
|
|||
|
|
return '优惠券';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (coupon.type === 1) { // 满减券
|
|||
|
|
if (coupon.min_amount > 0) {
|
|||
|
|
return `满${(coupon.min_amount / 100).toFixed(0)}元可用`;
|
|||
|
|
}
|
|||
|
|
return '无门槛使用';
|
|||
|
|
} else if (coupon.type === 2) { // 折扣券
|
|||
|
|
if (coupon.min_amount > 0) {
|
|||
|
|
return `满${(coupon.min_amount / 100).toFixed(0)}元可用`;
|
|||
|
|
}
|
|||
|
|
return '全场商品可用';
|
|||
|
|
} else if (coupon.type === 3) { // 免邮券
|
|||
|
|
return '全场包邮';
|
|||
|
|
}
|
|||
|
|
return '优惠券';
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 格式化时间限制
|
|||
|
|
formatTimeLimit(coupon, status) {
|
|||
|
|
if (status === 1) { // 已使用
|
|||
|
|
return `已于${this.formatDate(coupon.updated_at || new Date())}使用`;
|
|||
|
|
} else if (status === 2) { // 已失效
|
|||
|
|
return `已于${this.formatDate(coupon.end_time)}过期`;
|
|||
|
|
} else { // 可使用
|
|||
|
|
return `${this.formatDate(coupon.end_time)}前有效`;
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 格式化日期
|
|||
|
|
formatDate(dateStr) {
|
|||
|
|
const date = new Date(dateStr);
|
|||
|
|
const year = date.getFullYear();
|
|||
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|||
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|||
|
|
return `${year}.${month}.${day}`;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
tabChange(e) {
|
|||
|
|
const { value } = e.detail;
|
|||
|
|
|
|||
|
|
this.setData({ status: value });
|
|||
|
|
this.fetchList(value);
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
goCouponCenterHandle() {
|
|||
|
|
wx.navigateTo({
|
|||
|
|
url: '/pages/coupon/coupon-center/index',
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onPullDownRefresh_(e) {
|
|||
|
|
// 安全获取callback
|
|||
|
|
const callback = e && e.detail && e.detail.callback;
|
|||
|
|
|
|||
|
|
this.setData(
|
|||
|
|
{
|
|||
|
|
couponList: [],
|
|||
|
|
},
|
|||
|
|
() => {
|
|||
|
|
this.fetchList();
|
|||
|
|
// 确保callback存在且是函数
|
|||
|
|
if (callback && typeof callback === 'function') {
|
|||
|
|
callback();
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
);
|
|||
|
|
},
|
|||
|
|
});
|