wwf
12 小时以前 9a6cd220224fd3a9a6c84b5bb37c6410a470969f
src/utils/wxjssdk.js
@@ -1,16 +1,14 @@
import wx from 'weixin-js-sdk'
import axios from './axios'
import store from '../store.js'
import $qxueyou from '@/config/qxueyou.js'
import utilsUA from '@/plugins/utilsUA'
import { getUUID, qxyResImg } from '@/plugins/utils'
import { isWeixin, isMobile } from '@/utils/UA.js'
let newFeature = false
let oldShare = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone']
let newShare = ['updateTimelineShareData', 'updateAppMessageShareData']
let weixinFlag = utilsUA.isWeixin
let mobileFlag = utilsUA.isMobile
let weixinFlag = isWeixin
let mobileFlag = isMobile
let channel = weixinFlag && mobileFlag ? 'wx_pub' : 'wx_pub_qr'
let isWxpub = 'wx_pub'.includes(channel)
@@ -21,7 +19,7 @@
function getWxSignature(toRoute) { 
  if (!weixinFlag) { return false }
  axios.get('/wx/js/signature', {
  axios.get('/system/wx/js/signature', {
    params: { url: location.href } 
  }).then(res => {
    if (!res || !res.data) {
@@ -36,193 +34,19 @@
      signature: result.signature,
      jsApiList: [
        ...(newFeature ? newShare : oldShare),
        'chooseWXPay',
        // 'chooseWXPay',
        'chooseImage',
        'getLocalImgData'
      ],
      openTagList: ['wx-open-launch-app']
    })
    wx.ready(function () {
      initShareOption(toRoute)
    })
    // wx.ready(function () {
    //   initShareOption(toRoute)
    // })
    wx.error(function (res) {
      console.log(res)
    })
  })
}
/**
 * 初始化分享数据
 * @param {*} toRoute
 */
function initShareOption(toRoute) {
  if (!weixinFlag) { return false }
  let uuid = getUUID()
  let shareOptions = getShareOptions(uuid,toRoute)
  shareOptions.success = function () {
    shareSuccess(uuid,toRoute)
  }
  shareOptions.cancel = function () {
    console.log('取消分享')
  }
  shareOptions.trigger = function () {
    console.log('用户点击发送给朋友')
  }
  // 向下兼容旧版分享接口
  if (!newFeature) {
    wx.onMenuShareTimeline(shareOptions)
    wx.onMenuShareAppMessage(shareOptions)
  } else {
    wx.updateAppMessageShareData(shareOptions)
    wx.updateTimelineShareData(shareOptions)
  }
}
/**
 * 获取分享数据
 * @param {*} list
 */
function getShareOptions(uuid,toRoute) {
  let result = {}
  let customShare = store.state.share.custom
  if (customShare.title) {
    result.title = customShare.title
    result.desc = customShare.desc
  } else {
    result.title = toRoute.name
    result.desc = toRoute.name
  }
  if (customShare.imgUrl) {
    if (customShare.imgUrl.includes('http')) {
      result.imgUrl = customShare.imgUrl
    } else {
      result.imgUrl = qxyResImg(customShare.imgUrl)
    }
  }
  if (customShare.uuidLink) {
    let pageUrl = customShare.pageUrl || toRoute.fullPath
    let encode = encodeURI(`${uuid},${$qxueyou.htmlRoot + pageUrl}`)
    result.link = customShare.uuidLink + btoa(encode)
    // result.link = customShare.uuidLink + uuid
  } else if (customShare.link) {
    result.link = customShare.link
  } else {
    result.link = location.href
  }
  return result
}
/**
 * 分享成功回调
 */
function shareSuccess(uuid,toRoute) {
  let customShare = store.state.share.custom
  if (customShare.targetId) {
    let pageUrl = customShare.pageUrl || toRoute.fullPath
    axios.post('/wx/share/callback', {
      urlId: uuid,
      pageUrl: $qxueyou.htmlRoot + pageUrl,
      targetId: customShare.targetId,
      planIds: customShare.planIds
    }).then(() => {
      initShareOption(toRoute)
    })
  }
  // 当分享有方案Id,触发是否关注公众号
  if (customShare.planIds) {
    store.commit('wxh5/subscribe', '及时获取奖励提醒')
  }
  let mask = store.state.share.mask
  if (mask.show) {
    let text = mask.type === 'plan' ? '分享成功,继续助力' : '邀请成功,继续邀请'
    let newMask = { show: true, type: mask.type, text: text, codeText: mask.codeText }
    store.commit("share/maskText", newMask)
  }
}
/**
 * 统一支付接口处理
 * @param {*} orderId
 * @param {*} successCallback // 支付成功回调
 * @param {*} showCodeCallback // 显示二维码回调
 */
function unipayPay(orderId, successCallback, showCodeCallback) {
  axios.post('/wx/pay/createOrder', {
    orderId: orderId,
    channel: channel,
    redirectUrl: weixinFlag ? store.state.order.paySuccessUrl : undefined,
  }).then(res => {
    if (!res.data.data) { return false }
    let params = res.data.data.param
    if (isWxpub) { // 公众号支付
      chooseWXPay(params, successCallback)
    } else { // 扫码支付
      store.commit('timer/paying', true)
      showCodeCallback && showCodeCallback(params.codeUrl)
      checkIsPay(orderId, successCallback)
    }
  })
}
function chooseWXPay(result, successCallback) {
  //调用微信支付接口
  wx.chooseWXPay({
    timestamp: result.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
    nonceStr: result.nonceStr, // 支付签名随机串,不长于 32 位
    package: result.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
    signType: result.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
    paySign: result.paySign, // 支付签名
    success: () => {
      successCallback && successCallback()
    },
    fail: (e) => {
      store.commit('snack/error', '请联系技术客服解决' + e.errMsg)
    }
  })
}
/**
 * 扫码支付成功的回调
 * @param {} orderId
 * @returns {}
 */
function checkIsPay(orderId, successCallback) {
  if (!store.state.timer.paying) {
    return false
  }
  setTimeout(function () {
    axios.get('/transact/order/payResult', {
      params: { orderId: orderId }
    }).then(res => {
      if (res.data.success) {
        successCallback && successCallback()
        store.commit('timer/paying', false)
      } else {
        checkIsPay(orderId, successCallback)
      }
    })
  }, 2000)
}
/**
 * 获取定位
 * @param {*} locationCallback
 */
function getPosition(locationCallback){
  wx.getLocation({
    success: function (res) {
      locationCallback(res)
    }
  })
}
function chooseImage(){
@@ -246,6 +70,7 @@
    })
  })
}
function getLocalImgData(localId){
  return new Promise((resolve) => {
    wx.getLocalImgData({
@@ -260,75 +85,7 @@
  })
}
/**
 * 预览图片
 * @param {*} url
 * @param {*} urlList
 */
function previewImage(current, urls){
  wx.previewImage({
    current: current, // 当前显示图片的http链接
    urls: urls ? urls : [current] // 需要预览的图片http链接列表
  })
}
/**
 * 返回小程序页面
 * @param {*} mpRouter // 小程序的路由
 * @param {*} otherCallback // 其他回调操作
 */
function redirectToMp(mpRouter) {
  return new Promise(resolve => {
    // 非微信 或 人社局活动入口
    if (!weixinFlag || store.state.course.isRsjActivity) {
      resolve(true)
      return false
    }
    wx.miniProgram.getEnv(function(res) {
      if (res.miniprogram) { // 小程序
        wx.miniProgram.redirectTo({
          url: mpRouter,
          success: function(){
            console.log('跳转成功')
            setTimeout(() => { // 处理其他机构的小程序跳转场景
              resolve(true);
            }, 3000)
          },
          fail: function(){
            resolve(true)
          }
        })
      } else { // 非小程序
        resolve(true)
      }
    })
  })
}
/**
 * 向小程序发送消息
 */
function postMessage(data) {
  // 非微信
  if (!weixinFlag) { return false }
  wx.miniProgram.getEnv(function(res) {
    if (res.miniprogram) { // 小程序
      wx.miniProgram.postMessage({
        data: data
      })
    }
  })
}
export {
  getWxSignature,
  initShareOption,
  unipayPay,
  getPosition,
  previewImage,
  chooseImage,
  redirectToMp,
  postMessage
  chooseImage
}