points-mall.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import memberService from '../../utils/member.js';
  2. Page({
  3. data: {
  4. memberInfo: {},
  5. pointsProducts: [],
  6. selectedProduct: null,
  7. showExchangeModal: false,
  8. coupons: []
  9. },
  10. onLoad() {
  11. this.loadData();
  12. // 添加数据变化监听器
  13. this.memberDataListener = (memberInfo) => {
  14. this.loadData();
  15. };
  16. memberService.addListener(this.memberDataListener);
  17. },
  18. onUnload() {
  19. // 移除监听器
  20. if (this.memberDataListener) {
  21. memberService.removeListener(this.memberDataListener);
  22. }
  23. },
  24. onShow() {
  25. // 每次显示页面时刷新数据
  26. this.loadData();
  27. // 清理过期积分
  28. memberService.cleanExpiredPoints();
  29. },
  30. loadData() {
  31. const memberInfo = memberService.getUserMemberInfo();
  32. const pointsProducts = memberService.POINTS_PRODUCTS;
  33. const coupons = memberService.getAvailableCoupons();
  34. this.setData({
  35. memberInfo,
  36. pointsProducts,
  37. coupons
  38. });
  39. },
  40. // 选择商品兑换
  41. selectProduct(e) {
  42. const productId = e.currentTarget.dataset.id;
  43. const product = this.data.pointsProducts.find(p => p.id === productId);
  44. this.setData({
  45. selectedProduct: product,
  46. showExchangeModal: true
  47. });
  48. },
  49. // 关闭兑换弹窗
  50. closeExchangeModal() {
  51. this.setData({
  52. showExchangeModal: false,
  53. selectedProduct: null
  54. });
  55. },
  56. // 确认兑换
  57. confirmExchange() {
  58. const { selectedProduct } = this.data;
  59. if (!selectedProduct) return;
  60. wx.showLoading({
  61. title: '兑换中...'
  62. });
  63. const result = memberService.exchangePoints(selectedProduct.id);
  64. wx.hideLoading();
  65. if (result.success) {
  66. wx.showToast({
  67. title: '兑换成功',
  68. icon: 'success'
  69. });
  70. // 刷新数据
  71. this.loadData();
  72. this.closeExchangeModal();
  73. // 显示获得的优惠券信息
  74. setTimeout(() => {
  75. wx.showModal({
  76. title: '兑换成功',
  77. content: `恭喜您获得${result.coupon.name},有效期${Math.ceil((new Date(result.coupon.expireDate) - new Date()) / (1000 * 60 * 60 * 24))}天`,
  78. showCancel: false
  79. });
  80. }, 1500);
  81. } else {
  82. wx.showToast({
  83. title: result.message,
  84. icon: 'none'
  85. });
  86. }
  87. },
  88. // 查看积分明细
  89. viewPointsHistory() {
  90. wx.navigateTo({
  91. url: '../points-history/points-history'
  92. });
  93. },
  94. // 查看我的优惠券
  95. viewMyCoupons() {
  96. wx.navigateTo({
  97. url: '../my-coupons/my-coupons'
  98. });
  99. },
  100. // 返回上一页
  101. goBack() {
  102. wx.navigateBack();
  103. }
  104. });