闫增涛
2025-03-27 96b0b8bb1597d825004f5a8da0728c48b6ce7dba
pages/home/home.js
@@ -39,6 +39,7 @@
      value: false
    },
    loading: true,
    showStateMenu: false, // 新年公告弹窗
  },
  onShow() {
    this.setData({
@@ -50,8 +51,19 @@
      this.getShoppingCartList()
    }
    this.init();
    // const stateMenu = wx.getStorageSync('stateMenu')
    // if (stateMenu == 'true') {
    //   this.setData({
    //     showStateMenu: true
    //   })
    //   wx.setStorageSync('stateMenu', 'false')
    // }
  },
  closeStateMenu() {
    this.setData({
      showStateMenu: false
    })
  },
  onLoad() {
    this.getBannerList()
    const systInfo = wx.getSystemInfoSync();
@@ -65,17 +77,17 @@
    // 首页测试登录功能,后续注释
    // 检查登录状态
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        // 如果不是第一次登录,会执行回调
        if (data) {
          this.init();
          this.registe()
        } else {
          this.init();
        }
      })
    }
    // if (!token) {
    //   loginInfo(app, (data) => {
    //     // 如果不是第一次登录,会执行回调
    //     if (data) {
    //       this.init();
    //       this.registe()
    //     } else {
    //       this.init();
    //     }
    //   })
    // }
  },
  onReachBottom() {},
@@ -520,57 +532,66 @@
      })
    })
  },
  async addCart(e) {
  addCart(e) {
    const {
      info
    } = e.currentTarget.dataset;
    if (wx.getStorageSync(app.config.tokenKey)) {
      if (info.isbuy) {
        wx.showToast({
          title: "该商品已购买",
          icon: 'none',
          duration: 1000
    const token = wx.getStorageSync(app.config.tokenKey)
    if (token) {
      this.addCartFun(info)
    } else {
      if (!token) {
        loginInfo(app, (data) => {
          // 如果不是第一次登录,会执行回调
          if (data) {
            this.addCartFun(info)
            this.registe()
          } else {
            this.addCartFun(info)
          }
        })
      } else {
        let query = {
          start: 0,
          size: 999,
          filterList: [],
          searchList: []
        }
        try {
          if (
            this.data.shoppingCartGetId.includes(
              info.defaultSaleMethodId
            )
          ) {
            wx.showToast({
              title: "该商品已在购物车,请勿重复添加",
              icon: 'none',
      }
    }
  },
  // 剥离加入购物车方法登陆前后公用
  addCartFun(info) {
    if (info.isbuy) {
      wx.showToast({
        title: "该商品已购买",
        icon: 'none',
        duration: 1000
      })
    } else {
      try {
        if (
          this.data.shoppingCartGetId.includes(
            info.defaultSaleMethodId
          )
        ) {
          wx.showToast({
            title: "该商品已在购物车,请勿重复添加",
            icon: 'none',
            duration: 1000
          })
        } else {
          let query = {
            requests: [{
              saleMethodId: info.defaultSaleMethodId,
              storeEventId: null,
              agentCode: '电子书'
            }]
          }
          app.MG.store.addShoppingCart(query).then(res => {
            if (res) wx.showToast({
              title: "添加成功",
              icon: 'success',
              duration: 1000
            })
          } else {
            let query = {
              requests: [{
                saleMethodId: info.defaultSaleMethodId,
                storeEventId: null,
                agentCode: '电子书'
              }]
            }
            const addRes = await app.MG.store.addShoppingCart(query)
            if (addRes) {
              wx.showToast({
                title: "添加成功",
                icon: 'success',
                duration: 1000
              })
            }
            this.getShoppingCartList()
          }
        } catch (error) {
          console.error('出错了:', error)
          })
        }
      } catch (error) {
        console.error('出错了:', error)
      }
    }
  },