base.js 1.66 KB
Newer Older
FE committed
1
import { http, browser } from "@/utils"
2 3


xuzhenghua committed
4 5
// const url = `https://res.wx.qq.com/open/js/jweixin-1.4.0.js`
const url = `https://res.wx.qq.com/open/js/jweixin-1.6.0.js`
6
const appId = 'wx23dac6775ac82877'
xuzhenghua committed
7
const jsApiList = ['updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage', 'onMenuShareTimeline']
8 9

export const getSignature = async (config = {}) => {
xuzhenghua committed
10 11 12 13 14 15 16 17 18 19 20 21
  let res = await http.post(`${API['base-api']}/m/sale/signature`, {
    url: browser.isWeixin && browser.isIOS ? sessionStorage.getItem('enter_url') : window.location.href.split('#')[0],
  })
  return wx.config({
    debug: false, // 开启调试模式,
    appId, // 必填,公众号的唯一标识
    timestamp: res.data.timestamp, // 必填,生成签名的时间戳
    nonceStr: res.data.nonce_str, // 必填,生成签名的随机串
    signature: res.data.signature,// 必填,签名,见附录1
    jsApiList,
    ...config
  })
22 23 24
}

export const getWXObject = () => {
xuzhenghua committed
25 26 27 28 29 30
  return new Promise(resolve => {
    if (!window.wx) {
      return loadScript(url).then(() => resolve())
    }
    resolve()
  })
31 32 33 34
}


function loadScript(url) {
xuzhenghua committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  return new Promise(resolve => {
    var head = document.head || document.getElementsByTagName('head')[0]
    var script = document.createElement('script')

    script.type = 'text/javascript'
    script.src = url

    if (!('onload' in script)) {
      script.onreadystatechange = function () {
        if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
        this.onreadystatechange = null
        resolve()
      }
    }

    script.onload = function () {
      this.onload = null
      resolve()
    }

    head.appendChild(script)
  })
}