OrderPopup.js 6.74 KB
// components/OrderPopup/OrderPopup.js
import qrcode from '~/utils/lib/wxqrcode'
import api from '../../api/api.js'
Component({
  /**
   * 组件的属性列表
   */
  options: {
    styleIsolation: 'shared', // 自定义覆盖vant样式 
  },
  properties: {
    show: Boolean,
    orderType: {
      type: String,
      observer: function (n) {
        if (n == '0') {
          api
            .getConfigList({
              confGroup: 'SMARTCARLIFE_NOT_UPGRADE_LABEL'
            })
            .then(res => {
              let list = []
              let { code, data } = res
              if (code == 0) {
                list = data
              }
              this.setData({
                shortcutList: list
              })
            })
        }
      }
    },
    price: String,
    qrCode: {
      type: String,
      observer: function (n) {
        if (n) {
          this.createQRCode(n)
        }
      }
    },
    orderCode: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    shortcutList: [],
    orderTypeList: [
      {
        name: '旧单补录',
        tips: '以旧的下单时间为客户补录生成新工单',
        active: true,
        value: 6
      },
      {
        name: '新增一单',
        tips: '根据当前时间为客户新建一笔新工单',
        value: 4
      }
    ],
    textareaValue: '',
    routerBack: null,
    outTradeNo: '',
    wxqrcode: '',
    payVoucherList: []
  },

  /** 组件生命周期 */
  lifetimes: {
    attached: function () {
      // 支付方式为微信
      if (this.data.orderType == 1) {
        this.createWxQrcode()
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noScroll() { },
    close() {
      let { routerBack } = this.data
      if (routerBack) {
        wx.navigateBack()
        return
      }
      this.setData({
        show: false
      })
    },
    blur(e) {
      console.log(e)
      let { value } = e.detail
      let { shortcutList } = this.data
      value = value.substring(0, 30)
      if (!value) {
        shortcutList.map(i => (i.active = false))
      }
      this.setData({
        textareaValue: value,
        shortcutList,
        focus: false
      })
    },
    chooseShortcut(e) {
      let { item, index } = e.currentTarget.dataset
      let { textareaValue, shortcutList } = this.data
      if (shortcutList[index].active) return
      textareaValue += item.confValue
      textareaValue = textareaValue.substring(0, 30)
      this.setData({
        textareaValue,
        [`shortcutList[${index}].active`]: true,
        focus: true
      })
    },
    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
      })
    },
    input(e) {
      let { value } = e.detail
      let val = value.replace(/[\u4E00-\u9FA5]/g, '')
      this.setData({
        outTradeNo: val
      })
    },
    // 上传付款凭证
    upLoad(event) {
      let _this = this
      let { payVoucherList } = _this.data
      const { file } = event.detail
      let tempFiles = file?.url
      let imgType = tempFiles.split('.')[1]
      let type = imgType == 'unknown' ? 'jpg' : imgType
      wx.getFileSystemManager().readFile({
        filePath: tempFiles, //选择图片返回的相对路径
        encoding: 'base64', //编码格式
        success(_res) {
          wx.$util.showLoading('上传中...')
          api
            .uploadImg({
              file: `${type}@${_res.data}`
            })
            .then(img => {
              wx.$util.hideLoading()
              console.log(img)
              let { code, data, message } = img
              if (code == 0) {
                payVoucherList.push({ url: data })
                _this.setData({
                  payVoucherList
                })
              } else {
                wx.$util.showToast(message)
              }
            })
        }
      })
    },
    // 删除上传
    deletePayVoucher(e) {
      let { index } = e?.detail
      let _this = this
      let { payVoucherList } = _this.data
      payVoucherList.splice(index, 1)
      this.setData({
        payVoucherList
      })
    },
    changeOrderSource(e) {
      console.log(e)
      let { index } = e.currentTarget.dataset
      let { orderTypeList } = this.data
      orderTypeList.map(i => (i.active = false))
      orderTypeList[index].active = true
      this.setData({
        orderTypeList
      })
    },
    confirm(e) {
      let { orderType, outTradeNo, payVoucherList, textareaValue, orderTypeList, confirmStatus } = this.data
      // debugger
      // console.log(wx.$router)
      // 做防抖
      if (confirmStatus) return
      this.setData({
        confirmStatus: true
      })
      setTimeout(() => {
        this.setData({
          confirmStatus: false
        })
      }, 1000)
      console.log(thisPage, 'thisPage');
      let thisPage = wx.$router.getPageInstance()
      if (orderType == 2 || orderType == 5) {
        if (payVoucherList.length && outTradeNo) {
          thisPage.setData({
            outTradeNo,
            payVoucherList
          })
          thisPage.createPayOrder()
        } else {
          wx.$util.showToast('交易流水号或付款凭证不能为空!')
        }
      } else if (orderType == 0) {
        if (textareaValue) {
          console.log(textareaValue)
          thisPage.installOnly(textareaValue)
        } else {
          wx.$util.showToast('请输入内容!')
        }
      } else {
        orderTypeList.map(i => {
          if (i.active) {
            return console.log(i.value)
            console.log(thisPage.keepGoing)
            return
            thisPage.keepGoing(i.value)
          }
        })
      }
    },

    // 生成小程序码
    createWxQrcode() {
      let _this = this
      let oParams = {}
      oParams.appId = 'wx06d1198e751435d1'
      oParams.qrcodeType = 'QR_WXA_UNLIMIT'
      oParams.path = 'packagesPayment/pages/payment/index'
      // oParams.path = 'packagesPayment/pages/QRPayment/QRPayment'
      // oParams.scene = `merchantId=111`
      oParams.scene = `orderCode=${_this.data.orderCode}&workWxSessionId=${wx.getStorageSync('dj_boutique_sessionId')}`
      oParams.channelType = 1
      // B端小程序调用接口生成领券码时,传参userId用uuid,类型由int变string
      api.createQrcodeImage(oParams).then(res => {
        console.log(res.data)
        console.log(res.code)
        _this.setData({
          wxqrcode: res.data
        })
      })
    }
  }
})