wxConfig.js 3.15 KB
import util from '@/utils/util'
import { Notify } from 'vant'
const WxConfig = {
  init (jsApiList) {
    return new Promise((resolve, reject) => {
      jsApiList.map(e => {
        if (e == 'chooseImage') jsApiList.push('getLocalImgData')
      })
      let [redirectUrl] = window.location.href.split('#')
      let appId = util.getAppId()
      return window.$HTTP({
        url: '/rop-scrm-equity/router',
        data: {
          url: redirectUrl,
          appId,
          method: 'scrm.api.equity.common.wechat.jsconfig'
        }
      }).then(data => {
        if (data) {
          let obj = {}
          Object.keys(data).map(key => {
            if (data[key]) obj[key] = data[key]
          })
          wx.config({
            ...obj,
            jsApiList,
            openTagList: ['wx-open-launch-weapp']
          })
          resolve()
        } else {
          reject()
          Notify('网络异常')
        }
      })
    })
  },
  ready (callBack) {
    return new Promise((resolve, reject) => {
      wx.ready(() => {
        callBack(resolve, reject)
      })
    })
  },
  /**获取地理位置接口 */
  getLocation (type = 'gcj02') {
    return this.ready((resolve) => {
      wx.getLocation({
        type, // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
        success (res) {
          resolve(res)
        }
      })
    })
  },
  openLocation (params) {
    return this.ready(resolve => {
      let { latitude, longitude, name, address, scale = '18', infoUrl = '' } = params
      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('已取消支付')
        },
        cancel () {
          window.vm.showToast('支付失败,请重试')
        }
      })
    })
  },
  updateAppMessageShareData (params) {
    // title, // 分享标题
    // desc: '', // 分享描述
    // link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
    // imgUrl: '', // 分享图标
    return this.ready(resolve => {
      wx.updateAppMessageShareData({
        ...params,
        success: function () {
          resolve()
          // 设置成功
        }
      })
    })
  }

}

export default WxConfig