chargeDetail.js 10.3 KB
import { IMG_URL } from '~/config';
import USERAPI from "~/api/usersApi/index";
Page({
  data: {
    IMGURL: IMG_URL,
    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 = wx.p.format.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 = wx.p.format.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.cache.storageApi.getSessionId()
    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, isGH } = this.data
      if (!res) {
        // wx.redirectTo({
        //   url: '/packageShopmall/packageEquity/pages/allEquity/allEquity?showToast=1'
        // })
        setTimeout(() => {
          wx.p.router.back(e => {
            e.onLoad({ showToast: 1 })
          })
        }, 500)
        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 = wx.p.format.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,
        isGH,
        showComponent: true
      }, () => {
        if (options.source == 'GH') {
          this.handleEquityOfGH(options.couponsItem)
        }
      })
    })
  },
  toCoupons() {
    wx.navigateTo({
      url: '/packageMine/pages/order/index/index'
    })
  },
  toOpenPage() {
    this.modalOnCancel()
    wx.navigateTo({
      url: '/packageSaveMoney/pages/open/open?page=business'
    })
  },
  toMemberPage() {
    this.toOpenPage()
  },
  toRecharge() {
    const isLogin = this.checkLogin()
    if (!isLogin) {
      return
    }
    if (!this.data.rechageNum) {
      wx.p.wxApi.showToast('请输入充值账号')
      return
    }
    if (this.data.isGameUse) return this.comfirmBuyItem()
    // if(this.data.isGH)return this.toRechargeFill(this.data.rechageNum)
    const { noMemberPrice, salesPrice } = this.data.selectedItem
    let diffPrice = 0
    if (noMemberPrice) {
      diffPrice = wx.p.format.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
    }
    this.orderCode = orderCode
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    const { selectedItem, rechageNum, isGameUse, coupons } = this.data
    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,curIndex,listData } = self.data
        // 设置埋点参数
        const tracker = {
          item_type: 7,
          view: 3,
          id: selectedItem.id
        }
        const firstLevel = listData[curIndex]
        self.orderTime = Number(timeStamp)
        self.order_total_price = selectedItem.salesPrice
        self.pay_time = new Date().getTime()
        self.trade_name = `${firstLevel.productName}${selectedItem.denomination}`
        self.trade_classification = firstLevel.goodsCategory
        self.addTrack(tracker)
        //跳转商品详情
        let obj = {
          ...query,
          ...selectedItem,
          rechageNum,
          isGH: self.data.isGH
        }
        const temp = JSON.stringify(obj)
        wx.navigateTo({
          url: '/packageSaveMoney/pages/chargeWait/chargeWait?query=' + temp
        })
      },
      fail() { }
    })
  },
  // 设置标题名称
  setNaviTitle(title) {
    wx.setNavigationBarTitle({
      title
    })
  },
  onLoad(options) {
    this.data.isGameUse = options.inType == 'game'
    this.data.isGH = options.source == 'GH'
    const query = options ? options.param : this.data.query
    this.getChargeDetail(query, options)
    this.setNaviTitle(query.brandName)
    if (options.inType == 'game') return
    wx.p.eventbus.on('EQUITYGOODS', data => {
      this.toCheckMember()
    })
  },
  onShow() {
    this.toCheckMember()
  },
  // 处理广汇权益明细进入的卡券
  handleEquityOfGH(couponsItem) {
    let { selectedItem } = this.data
    selectedItem.payCouponsPrice = wx.p.format.decimal(selectedItem.lineationPrice - (Number(couponsItem.denomination)), 2)
    this.setData({
      selectedItem,
      coupons: couponsItem
    })
  },
  // 检测是否已经登录
  checkLogin() {
    const sessionId = wx.p.cache.storageApi.getSessionId()
    let flag = true
    if (!sessionId) {
      flag = false
      this.setData({
        showLogin: true,
        showComponent: false
      })
    }
    return flag
  },
  loginCallBack(data) {
    if (data) {
      this.toCheckMember()
    }
  },
  // 广汇赠送权益直充
  // toRechargeFill(phone) {
  //   wx.p.wxApi.showLoading()
  //   const { productId, orderCode, productName, discount } = this.data.ghOption
  //   wx.p.api.equityApi.toRechargeByPhone({ orderCode, phone, productId }).then(res => {
  //     if (res) {
  //       const obj = {
  //         brandName: productName,
  //         rechageNum: phone,
  //         denomination: discount,
  //         isGH: true
  //       }
  //       const temp = JSON.stringify(obj)
  //       wx.navigateTo({
  //         url: '/packageSaveMoney/pages/chargeWait/chargeWait?query=' + temp
  //       })
  //     }
  //   })
  // },
  // 2023-12版本 添加权益下单支付成功埋点
  /**添加埋点开始 */
  /**
   * 商品-活动id, 
   * 物品类型item_type  1 商品  3活动,
   * view 动作类型  3下单
   * */
  async addTrack(query) {
    const {
      id,
      item_type,
      view
    } = query
    const userInfo = wx.p.cache.userInfo.getUserInfo() || {},
      {
        openId
      } = await USERAPI.getWXUserInfo()
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let data = {
      open_id: openId || null,
      item_type,
      customer_phone: userInfo.phone || null,
      action_type: view,
      item_id: id,
      business_type: 1,
      add_way: 103,
      merchant_id: merchantId,
      customer_name: userInfo.nickName || null,
      master_order_no: this.orderCode,
      sub_order_no: this.orderCode,
      order_time: new Date(this.orderTime).format('yyyy-MM-dd hh:mm:ss'),
      order_total_price: this.order_total_price || 0,
      pay_method: "微信支付",
      pay_time: new Date(this.pay_time).format('yyyy-MM-dd hh:mm:ss') || '',
      status: '已支付',
      trade_pay_price: this.order_total_price || 0,
      trade_name: this.trade_name || '',
      trade_classification: this.trade_classification || '',
      association_classify_first_name:this.trade_classification || ''
    }
    this.firstEnter = false
    const jsonList = [JSON.stringify(data)]
    const res = await wx.p.api.shopmallApi.trackHttp({
      jsonList
    })
  },
})