chargeDetail.js 7.24 KB
import {
  decimal
} from '~/utils/format'
Page({
  data: {
    curIndex: 0,
    checkedItem: 0,
    isOpen: false,
    selectedItem: null,
    listData: [],
    isGameUse: false,
    coupons: null,
    showCoupons: false,
  },
  // 获取当前商品绑定的优惠券并且处理价格
  getCouponsItem(e) {
    const couponsItem = e.detail
    let {
      selectedItem
    } = this.data
    selectedItem.payCouponsPrice = decimal(selectedItem.lineationPrice - (Number(couponsItem.denomination)), 2)
    this.setData({
      selectedItem,
      coupons: couponsItem
    })
  },
  toSelectCoupons() {
    const isLogin = this.checkLogin()
    if (!isLogin) {
      return
    }
    if (this.data.isGH) return
    this.setData({
      showCoupons: true
    })
  },
  bindInput(e) {
    this.setData({
      rechageNum: e.detail.value
    })
  },
  changeMumberType(e) {
    let {
      selectedItem,
      listData,
      coupons
    } = this.data
    const {
      index
    } = e.currentTarget.dataset
    selectedItem = listData[index].denominationList[0]
    if (coupons) {
      selectedItem.payCouponsPrice = decimal(selectedItem.lineationPrice - (Number(coupons.denomination)), 2)
    }
    this.setData({
      curIndex: index,
      selectedItem,
      checkedItem: 0
    })
  },
  checkedMumberType(e) {
    const {
      item,
      index
    } = e.currentTarget.dataset
    let richText = decodeURIComponent(item.buysNotes)
    richText = richText.replace(/\<span/g, '<span class="rich_text"')
    this.setData({
      checkedItem: index,
      selectedItem: item,
      richText
    })
  },
  toCheckMember() {
    if (this.data.isGameUse) return
    const SID = wx.p.storage.get('sessionId')
    if (!SID) {
      return
    }
    wx.p.api.equityApi.checkOpenMember().then(res => {
      let flag = false
      if (res.code == 0 && res.data) {
        flag = true
      }
      this.setData({
        isOpen: flag
      })
      wx.p.eventbus.off('EQUITYGOODS')
    })
  },
  getChargeDetail(query, options) {
    let para = {
      brandName: query.brandName,
      supplier: query.supplier
    }
    if (this.data.isGameUse) {
      para.id = query.id
    }
    wx.p.api.equityApi.chargeData(para).then(res => {
      let {
        selectedItem,
        curIndex,
        checkedItem,
        richText,
        isGameUse,
        coupons
      } = this.data
      if (!res) {
        setTimeout(() => {
          wx.p.router.back()
        }, 1500)
        return
      }
      if (res.length) {
        let brandLogo = res[0].brandLogo
        query.icon = query.icon ? query.icon : brandLogo
        res.forEach(item => {
          item.denominationList.map(el => {
            if (el.noMemberPrice) {
              el.diffPraice = decimal(el.noMemberPrice - el.salesPrice, 2)
            } else {
              el.diffPraice = 0
            }
            return el
          })
        })
      }
      if (!query.id) {
        selectedItem = res[0].denominationList[0]
      } else {
        for (let i = 0; i < res.length; i++) {
          if (res[i].productName == query.productName) {
            curIndex = i
          }
          for (let j = 0; j < res[i].denominationList.length; j++) {
            if (res[i].denominationList[j].id == query.id) {
              checkedItem = j
              selectedItem = res[i].denominationList[j]
            }
          }
        }
      }
      richText = decodeURIComponent(selectedItem.buysNotes)
      richText = richText.replace(/\<span/g, '<span class="rich_text"')
      if (isGameUse) {
        selectedItem.payCouponsPrice = selectedItem.lineationPrice
      }
      this.setData({
        listData: res,
        query,
        selectedItem,
        checkedItem,
        curIndex,
        richText,
        showLogin: false,
        isGameUse,
        showComponent: true
      }, () => {
        if (options.source == 'GH') {
          this.handleEquityOfGH(options.couponsItem)
        }
      })
    })
  },
  toCoupons() {
    wx.p.router.go('/packageMine/pages/order/index/index')
  },
  toOpenPage() {
    this.modalOnCancel()
    wx.p.router.go('/packageEquity/pages/open/open?page=business')
  },
  toMemberPage() {
    this.toOpenPage()
  },
  async toRecharge() {
    const loginStatus = !wx.p.isLogin
    if (loginStatus) return
    if (!this.data.rechageNum) {
      wx.p.wxApi.showToast('请输入充值账号')
      return
    }
    if (this.data.isGameUse) return this.comfirmBuyItem()
    const {
      noMemberPrice,
      salesPrice
    } = this.data.selectedItem
    let diffPrice = 0
    if (noMemberPrice) {
      diffPrice = decimal(noMemberPrice - salesPrice, 2)
    }
    this.setData({
      showModal: true,
      diffPrice
    })
  },
  modalOnCancel() {
    this.setData({
      showModal: false
    })
  },
  comfirmBuyItem() {
    wx.p.wxApi.showLoading()
    let para = {
      supplier: this.data.query.supplier
    }
    wx.p.api.equityApi.getOrderCode(para).then(res => {
      this.getPayParam(res)
      this.modalOnCancel()
    })
  },
  // 获取支付参数
  getPayParam(orderCode) {
    if (!orderCode) {
      return
    }
    const {
      selectedItem,
      rechageNum,
      isGameUse,
      coupons
    } = this.data, {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let para = {
      orderCode,
      goodsId: selectedItem.id,
      rechargeNumber: rechageNum,
      goodsQuality: 1,
      //   payMannerId: 19,
      paySource: 3,
      merchantId
    }
    if (isGameUse && coupons) {
      para.usedCouponIds = [coupons.id]
    }
    wx.p.api.equityApi.payGoodsOrder(para).then(res => {
      wx.hideLoading()
      this.handlePay(res)
    })
  },
  // 处理支付
  handlePay(payData) {
    const {
      timeStamp,
      nonceStr,
      packages,
      signType,
      paySign
    } = payData
    let self = this
    wx.requestPayment({
      timeStamp: timeStamp,
      nonceStr: nonceStr,
      package: packages,
      signType: signType,
      paySign: paySign,
      success() {
        //跳转商品详情
        let {
          query,
          selectedItem,
          rechageNum
        } = self.data
        wx.p.router.go('/packageEquity/pages/chargeWait/chargeWait', {
          query: {
            ...query,
            ...selectedItem,
            rechageNum,
          }
        })
      },
      fail() {}
    })
  },
  // 设置标题名称
  setNaviTitle(title) {
    wx.setNavigationBarTitle({
      title
    })
  },
  onLoad(options) {
    const query = options.param
    this.getChargeDetail(query, options)
    this.setNaviTitle(query.brandName)
    wx.p.eventbus.on('EQUITYGOODS', data => {
      this.toCheckMember()
    })
  },
  onShow() {
    this.toCheckMember()
  },
  // 处理广汇权益明细进入的卡券
  handleEquityOfGH(couponsItem) {
    let {
      selectedItem
    } = this.data
    selectedItem.payCouponsPrice = decimal(selectedItem.lineationPrice - (Number(couponsItem.denomination)), 2)
    this.setData({
      selectedItem,
      coupons: couponsItem
    })
  },
  // 检测是否已经登录
  checkLogin() {
    const sessionId = wx.p.storage.get('sessionId')
    let flag = true
    if (!sessionId) {
      flag = false
      this.setData({
        showLogin: true,
        showComponent: false
      })
    }
    return flag
  },
  loginCallBack(data) {
    if (data) {
      this.toCheckMember()
    }
  }
})