wwf
12 小时以前 9a6cd220224fd3a9a6c84b5bb37c6410a470969f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import wx from 'weixin-js-sdk'
import axios from './axios'
import $qxueyou from '@/config/qxueyou.js'
import { isWeixin, isMobile } from '@/utils/UA.js'
 
let newFeature = false
let oldShare = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone']
let newShare = ['updateTimelineShareData', 'updateAppMessageShareData']
 
let weixinFlag = isWeixin
let mobileFlag = isMobile
let channel = weixinFlag && mobileFlag ? 'wx_pub' : 'wx_pub_qr'
let isWxpub = 'wx_pub'.includes(channel)
 
/**
 * 微信获取签名
 * @param {*} toRoute 目标路由
 */
function getWxSignature(toRoute) { 
  if (!weixinFlag) { return false }
 
  axios.get('/system/wx/js/signature', {
    params: { url: location.href } 
  }).then(res => {
    if (!res || !res.data) {
      return false
    }
    let result = res.data.data || {}
    wx.config({ // 微信配置
      debug: false,
      appId: result.appId,
      timestamp: result.timestamp,
      nonceStr: result.nonceStr,
      signature: result.signature,
      jsApiList: [
        ...(newFeature ? newShare : oldShare),
        // 'chooseWXPay',
        'chooseImage',
        'getLocalImgData'
      ],
      openTagList: ['wx-open-launch-app']
    })
    // wx.ready(function () {
    //   initShareOption(toRoute)
    // })
    wx.error(function (res) {
      console.log(res)
    })
  })
}
 
function chooseImage(){
  return new Promise((resolve) => {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['camera'],
      success: (res) => {
        if (res && res.localIds) {
          getLocalImgData(res.localIds[0]).then((localData) => {
            resolve(localData)
          })
        } else {
          store.commit('snack/error', `微信上传图片失败:${res}`)
        }
      },
      fail:function(e) {
        store.commit('snack/error', `微信上传图片异常:${JSON.stringify(e)}`)
      }
    })
  })
}
 
function getLocalImgData(localId){
  return new Promise((resolve) => {
    wx.getLocalImgData({
      localId: localId,
      success: function (res) {
        resolve(res.localData)
      },
      fail:function(e) {
        store.commit('snack/error', `微信获取图片异常:${JSON.stringify(e)}`)
      }
    })
  })
}
 
export {
  getWxSignature,
  chooseImage
}