CarQuotationPay.js 8.39 KB
// packageEquity/pages/carQuotation/CarQuotationPay/CarQuotationPay.js
import { getOrderDetail, updateOrderStatus, getQuotationInfoById, createPayOrder } from '../../../api'
import { createQrcodeImage } from '~/api/api'
import qrcode from '~/utils/lib/wxqrcode'
// const imgUrl = {
//   wechat: '/packageEquity/images/pay/wechat.png'
// }
// import { setWebSocket } from '~/store/index'
let time = null
const navStyle = 'background:transparent;box-shadow:none;'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navStyle,
    payItems: [
      { id: 1, name: '微信支付', icon: 'wxPay' },
      { id: 3, name: 'POS机', icon: 'posPay' },
    ],
    orderCode: '',
    wxqrcode: '',
    posqrcode: '',
    orderDetail: {},
    popPayShow: true,
    payType: {
      1: {
        name: '微信支付',
        imgUrl: '/packageEquity/images/wechat.png'
      },
      3: {
        name: 'pos机支付',
        imgUrl: '/packageEquity/images/posMachine.png'
      }
    },
    selectPayType: 1
  },
  back() {
    wx.$router.back()
  },
  async changePayType(e) {
    console.log(e)
    // pos支付
    let { posqrcode } = this.data
    console.log('pos支付')
    let { value } = e.currentTarget.dataset
    console.log(value)
    //  createQRCode(text) {
    this.setData({
      selectPayType: value
    })
    if (value == 3 && !posqrcode) {
      let { orderCode } = this.data
      let result = await createPayOrder({
        orderCode,
        payType: 3
      })
      console.log(result)
      let { code, data, message } = result
      if (code == 0) {
        // console.log(text)
        let size = 0
        try {
          const info = wx.getSystemInfoSync()
          let scale = info.windowWidth / 750 //不同屏幕下QRcode的适配比例;设计稿是750宽
          let width = 300 * scale
          size = width
        } catch (e) {
          size = 150
        }
        let _img = qrcode.createQrCodeImg(data.outTradeNo, {
          size: parseInt(size)
        })
        this.setData({
          posqrcode: _img
        })
      }
    }
  },
  // 生成小程序码
  createWxQrcode() {
    let _this = this
    let oParams = {}
    oParams.appId = 'wx06d1198e751435d1'
    oParams.qrcodeType = 'QR_WXA_UNLIMIT'
    oParams.path = 'packagesPayment/pages/payment/index'
    oParams.scene = `orderCode=${_this.data.orderCode}&workWxSessionId=${wx.getStorageSync('dj_boutique_sessionId')}`
    oParams.channelType = 1
    // B端小程序调用接口生成领券码时,传参userId用uuid,类型由int变string
    createQrcodeImage(oParams).then(res => {
      console.log(res.data)
      console.log(res.code)
      _this.setData({
        wxqrcode: res.data
      })
      // 轮询订单状态
    })
    this.getDetail()
  },
  createQRCode(text) {
    console.log(text)
    let size = 0
    try {
      const info = wx.getSystemInfoSync()
      let scale = info.windowWidth / 750 //不同屏幕下QRcode的适配比例;设计稿是750宽
      let width = 300 * scale
      size = width
    } catch (e) {
      size = 150
    }
    let _img = qrcode.createQrCodeImg(text, {
      size: parseInt(size)
    })
    this.setData({
      qr: _img
    })
  },
  // getOrderDetail() {
  //   // console.log(wx.$router, 'wx.$router')
  //   let _this = this
  //   let { orderCode } = this.data
  //   _this.getDetail(orderCode)
  // },
  getDetail() {
    let { orderCode,orderDetail } = this.data
    // console.log(orderCode)
    let _this = this
    getOrderDetail({
      orderCode
    })
      .then(res => {
        let { code, message, data } = res
        // data.orderTime = wx.$util.tsFormatTime(data.orderTime, 'Y-M-D h:m:s')
        data.orderTime = data.orderTime ? wx.$util.tsFormatTime(data.orderTime, 'Y年M月D日 h:m:s') : ''
        // data.paySuccessDate = data.paySuccessDate ? wx.$util.tsFormatTime(data.paySuccessDate, 'Y-M-D h:m:s') : ''
        let paySuccessDate = data?.paySuccessDate || new Date()
        data.paySuccessDate = wx.$util.tsFormatTime(paySuccessDate, 'Y年M月D日 h:m:s')
        if (code == 0) {
          this.setData({
            orderDetail: {
              ...orderDetail,
              ...data.customer,
              ...data
              // ...data.payOrder
            }
          })
          wx.$util.hideLoading()
          // console.log(data.payOrder?.payStatus, 'payorder.paystatus')
          let { payOrder, status } = data
          if (payOrder?.payStatus == 2 || status == 4) {
            wx.$util.showToast('支付成功')
            if (time) clearInterval(time)
            //  通知并跳转成功页面
            _this.changeOrderStatus()
          }
        } else {
          wx.showModal({
            title: '温馨提示',
            content: message,
            showCancel: false,
            confirmText: '刷新',
            confirmColor: '#347BFF',
            success(res) {
              if (res.confirm) {
                _this.getDetail(orderCode)
              }
            }
          })
        }
      })
      .finally(() => {
        if (time) clearInterval(time)
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        const url = `${currentPage.route}`
        if ('packageEquity/pages/carQuotation/CarQuotationPay/CarQuotationPay' == url) {
          time = setInterval(() => {
            _this.getDetail(orderCode)
          }, 5 * 1000)
        }
      })
  },
  changeOrderStatus() {
    let { orderId, orderCode, orderDetail, id } = this.data
    wx.$router.redirectTo('CarQuotationDetail', { orderId, orderCode, orderDetail, id })
    // updateOrderStatus({ id, orderStatus: 3 })
    //   .then(result => {
    //   })
    //   .catch(err => {})
  },
  getQuotationInfoById() {
    let { id } = this.data
    getQuotationInfoById({ id }).then(res => {
      let { code, data } = res
      console.log(res)
      let { orderId, payId, merchantAddress, merchantName4s, quotationGoodsInfoList } = data
      // quotationGoodsInfoList
      if (quotationGoodsInfoList&&quotationGoodsInfoList.length) {
        quotationGoodsInfoList.forEach(item=>{
          if (item.position) {
            const fieldMap = {
              "4大件": "燃油车4大件服务",
              "3电": "新能源3电服务",
              "6大件": "新能源6大件服务",
              // 在这里可以添加更多的替换规则
            };
            // 如果输入字段在字段映射中有匹配项,则进行替换,否则保持不变
            fieldMap[item.position]&&( item.position = fieldMap[item.position])
          }
          if (item.quotationGoodsServiceItemsRelationDTOList&&item.quotationGoodsServiceItemsRelationDTOList.length) {
            item.serviceItems = item.quotationGoodsServiceItemsRelationDTOList.reduce((prev,subItm,index)=>{
              let unit = index == 0 ? '':'/'
              prev += unit+subItm.serviceName
              return prev
            },'')
          }
        }) 
      }
      console.log(quotationGoodsInfoList,'quotationGoodsInfoList');
      this.setData({
        // || '230311133649896'
        // orderCode: orderCode || '230310104652929',
        orderCode: payId,
        // orderCode: '230310104652929',
        merchantAddress,
        merchantName4s,
        orderId,
        ['orderDetail.quotationGoodsInfoList']:quotationGoodsInfoList
        // quotationGoodsInfoList
        // id
      })
      this.createWxQrcode()
    })
  },
  onRoute(options) {
    // console.log(options,'options');
    // console.log(options = 19)
    let { data, id } = options
    // id = 150
    // orderCode: 230724111753590
    this.setData({ id })
    this.getQuotationInfoById()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // setWebSocket()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // clearInterval(time)
    // if (time) clearInterval(time)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (time) clearInterval(time)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onPageScroll({scrollTop}){
     this.setData({
      navStyle:scrollTop > 20 ? '' : navStyle
    })
  }
})