chargeDetail.js 6.36 KB
import { equityApi } from "../../../../api/api"
const baseUrl = 'https://img02.glsx.com.cn/weapp/resource/equity-h5-new/mini/'
Page({

    data: {
        curIndex:0,
        checkedItem: 0,
        isOpen:false,
        selectedItem:null,
        imageUrl:baseUrl,
        listData:[]
    },
    bindInput(e){
        this.setData({
            rechageNum:e.detail.value
        })
    },
    changeMumberType(e) {
        let {selectedItem,listData} = this.data
        const {index} = e.currentTarget.dataset
        selectedItem = listData[index].denominationList[0]
        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() {
      const SID = wx.$util.getSessionId()
      if (!SID) {
        return
      }
        equityApi.checkOpenMember().then(res => {
          let flag = false
          if(res.code==0&&res.data){
            flag = true
          }
          this.setData({
            isOpen: flag
          })
          wx.$eventbus.off('EQUITYGOODS')
        })
    },
    getChargeDetail(query){
        let para ={
            brandName:query.brandName,
            supplier:query.supplier
        }
        equityApi.chargeData(para).then(res => {
            let {selectedItem,curIndex,checkedItem,richText} = this.data
            if(!res){
              wx.redirectTo({
                url: "/packageEquity/pages/allEquity/allEquity?showToast=1",
              });
              return
            }
            if(res.length){
                res.forEach(item=>{
                    item.denominationList.map(el=>{
                        if(el.noMemberPrice){
                            el.diffPraice = wx.$util.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"')
            this.setData({
                listData:res,
                query,
                selectedItem,
                checkedItem,
                curIndex,
                richText,
                showLogin:false
            })
          })
    },
    toCoupons(){
          wx.$router.go('packageMine.order.index', {
            title:'我的订单'
          })
    },
    toOpenPage(){
        this.modalOnCancel()
        let str = `?memberType=${1}` + `&index=${0}` + `&item=${null}`+`&page=business&getPackage=1`
        wx.navigateTo({
          url: '/packageEquity/pages/open/open'+str
        })
        // wx.navigateTo({
        //     url: '/packageEquity/pages/open/open?page=business'
        //   })
    },
    toMemberPage(){
      this.toOpenPage()
    },
    toRecharge(){
        const isLogin = this.checkLogin()
        if(!isLogin){
          return
        }
        if(!this.data.rechageNum){
            wx.$util.showToast('请输入充值账号')
            return
        }
        const { noMemberPrice, salesPrice } = this.data.selectedItem;
        let diffPrice = 0
        if (noMemberPrice) {
            diffPrice = wx.$util.decimal(noMemberPrice - salesPrice, 2);
        }
        this.setData({
            showModal:true,
            diffPrice
        })

    },
    modalOnCancel(){
        this.setData({
            showModal:false,
        })
    },
    comfirmBuyItem(){
        wx.$util.showLoading()
        let para = {
            supplier: this.data.query.supplier
        }
        equityApi.getOrderCode(para).then(res=>{
            this.getPayParam(res)
            this.modalOnCancel()
        })
    },
     // 获取支付参数
  getPayParam(orderCode) {
    if (!orderCode) {
      return
    }
    const { selectedItem,rechageNum } = this.data
    let para = {
      orderCode,
      goodsId: selectedItem.id,
      rechargeNumber:rechageNum,
      goodsQuality: 1,
    //   payMannerId: 19,
      paySource: 3
    }
    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() {
        //跳转商品详情
        // wx.$util.showToast('支付成功')
        let {query,selectedItem,rechageNum} = self.data
        let obj = {
          ...query,
          ...selectedItem,
          rechageNum,
        }
        const temp = JSON.stringify(obj)
        wx.navigateTo({
          url:'/packageEquity/pages/chargeWait/chargeWait?query='+temp
        })
      },
      fail() {

      }
    })
  },
   // 设置标题名称
   setNaviTitle(title) {
    wx.setNavigationBarTitle({
      title
    })
  },
    onLoad(options){
      const query =options?JSON.parse(decodeURIComponent(options.param)):this.data.query
      this.getChargeDetail(query)
      this.setNaviTitle(query.brandName)
      wx.$eventbus.on('EQUITYGOODS',(data)=>{
        this.toCheckMember()
      })
    },
    onShow(){
        this.toCheckMember()
    },
    // 检测是否已经登录
   checkLogin(){
    const sessionId = wx.$util.getSessionId()
    let flag = true
    if(!sessionId){
    flag = false
    this.setData({
      showLogin:true
    })
    }
    return flag
  },
  loginCallBack(data){
    if(data){
      this.toCheckMember()
    }
  }
})