WxConfig.js 3.29 KB
import { common } from '@r/api'
class WxConfig {
  constructor(jsApiList) {
    jsApiList.map(e => {
      if (e == 'chooseImage') jsApiList.push('getLocalImgData')
    })
    let [redirectUrl] = window.location.href.split('?')
    const appId = sessionStorage.getItem('GL_WX_APPID')
    common.getWxConfig({
      url: redirectUrl.split('#')[0],
      appId
    }).then(res => {
      if (res) {
        let { data } = res
        this.setConfig({
          ...data,
          jsApiList
        })
      } else {
        window.vm.showToast('系统异常')
      }
    })
  }
  setConfig(params) {
    let obj = {}
    Object.keys(params).map(key => {
      if (params[key]) obj[key] = params[key]
    })
    wx.config(obj)
  }
  ready(callBack) {
    return new Promise(resolve => {
      wx.ready(() => {
        callBack(resolve)
      })
    })
  }
  scanQRCode(needResult = 0) {
    return this.ready(resolve => {
      wx.scanQRCode({
        needResult,
        scanType: ['qrCode', 'barCode'],
        success(res) {
          resolve(res)
        }
      })
    })
  }
  chooseImage(count = 1) {
    return this.ready(resolve => {
      wx.chooseImage({
        count, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success(res) {
          let { localIds } = res
          let imgs = []
          localIds.map((e, index) => {
            wx.getLocalImgData({
              localId: e,
              success(img) {
                imgs.push(img)
                if (index == localIds.length - 1) resolve(imgs)
              }
            })
          })
        }
      })
    })
  }
  getLocation(type = 'wgs84') {
    return this.ready(resolve => {
      wx.getLocation({
        type, // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
        success(res) {
          resolve(res)
        }
      })
    })
  }
  openLocation(latitude,longitude,name,address,scale = '18',infoUrl) {
    return this.ready(resolve => {
      wx.openLocation({
        latitude,
        longitude,
        name,
        address,
        scale,
        infoUrl,
        success(res) {
          console.log(res)
          resolve(res)
        }
      })
    })
  }
  chooseWXPay(timestamp,nonceStr,packages,signType,paySign) {
    return this.ready(resolve => {
      wx.chooseWXPay({
        timestamp, // 支付签名时间戳,注意微信js sdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
        nonceStr, // 支付签名随机串,不长于 32 位
        package:packages, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
        signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
        paySign, // 支付签名
        success(res) {
          window.vm.showToast('支付成功')
          resolve(res)
        },
        fail() {
          window.vm.showToast('已取消支付')
          resolve()
        },
        cancel() {
          window.vm.showToast('支付失败,请重试')
          resolve()
        }
      })
    })
  }
}

export default WxConfig