uploadPayVoucher.js 2.58 KB
// packageStoreAssistant/packageActivity/pages/uploadPayVoucher/uploadPayVoucher.js
import {
  commonApi
} from '~/api/api'
import ActivityApi from '~/api/activityApi/index'
import environment from '~/utils/request/environment.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    success: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onRoute({
    subOrderCode,
    verificationVoucher
  }) {
    this.setData({
      subOrderCode,
      paymentVoucheUrl: verificationVoucher
    })
  },
  previewImage() {
    const urls = [this.data.paymentVoucheUrl]
    wx.previewImage({
      urls
    })
  },
  async upload() {
    const {
      tempFiles
    } = await wx.chooseMedia({
        count: 1,
        mediaType: ['image']
      }),
      [file] = tempFiles,
      _this = this
    wx.$api.showLoading('上传中...')
    try {
      wx.uploadFile({
        url: `${environment.domain}/rpc-scrm-entmicro-container/file/img?desFileSize=10`,
        // url: 'http://192.168.3.101:8186/rpc-scrm-entmicro-container/file/img?desFileSize=10',
        filePath: file.tempFilePath,
        name: 'file',
        formData: {
          desFileSize: 10
        },
        success({
          data
        }) {
          wx.hideLoading()
          const response = JSON.parse(data),
            paymentVoucheUrl = response.result
          if (paymentVoucheUrl) _this.setData({
            paymentVoucheUrl
          })
          else throw response.message
        },
        fail(err) {
          throw err
        }
      })
    } catch (error) {
      wx.$api.showToast(String(JSON.stringify(error)))
    }
  },
  dialogClose() {
    this.setData({
      showModal: false
    })
  },
  dialogConfirm() {
    const {
      success
    } = this.data
    if (success) wx.$router.back()
    else this.dialogClose()
  },
  async submit() {
    this.setData({
      submitLoading: true
    })
    try {
      const {
        paymentVoucheUrl,
        subOrderCode
      } = this.data, {
        message,
        code
      } = await ActivityApi.uploadPaymentVoucher({
        subOrderCode,
        paymentVoucheUrl
      })
      this.setData({
        submitLoading: false
      })
      if (code == '0') {
        this.setData({
          showModal: true,
          success: true
        })
      } else {
        if (message == '请勿上传同一张付款凭证') this.setData({
          showModal: true,
          success: false
        })
        else wx.$api.showToast(message)
      }
    } catch (error) {
      this.setData({
        submitLoading: false
      })
      wx.$api.showToast(error)
    }
  }
})