import wx from 'weixin-js-sdk'
|
import { Toast } from 'vant'
|
import { getTimestamp, randomString, getSignature } from './utils.ts'
|
|
class AuthWechat {
|
|
/**
|
* 获取浏览器地址
|
* */
|
signLink() {
|
if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
|
window.entryUrl = document.location.href
|
}
|
let url = /(Android)/i.test(navigator.userAgent) ? document.location.href : window.entryUrl;
|
return url.split('#')[0]
|
}
|
|
/**
|
* 当前是否是微信环境
|
* */
|
isWeixin() {
|
return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
|
}
|
|
/**
|
* 初始化
|
* */
|
wechat(ticket) {
|
if (!this.isWeixin()) {
|
return Toast.fail('当前不是微信环境');
|
}
|
|
return new Promise((resolve, reject) => {
|
let url = this.signLink()
|
let timestamp = getTimestamp()
|
let nonceStr = randomString()
|
let signature = getSignature(ticket, nonceStr, timestamp, url)
|
|
wx.config({
|
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
appId: process.env.VUE_APP_APPID, // 必填,公众号的唯一标识
|
timestamp, // 必填,生成签名的时间戳
|
nonceStr, // 必填,生成签名的随机串
|
signature, // 必填,签名
|
jsApiList: [
|
'updateAppMessageShareData', // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
|
'updateTimelineShareData', // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
|
'closeWindow', //关闭当前网页窗口接口
|
'chooseWXPay'
|
]
|
})
|
wx.ready(res => {
|
// 微信SDK准备就绪后执行的回调。
|
// console.log('wx.ready',res);
|
resolve(wx, res)
|
})
|
wx.error(err => {
|
// console.log('wx.error',err)
|
reject(wx, err)
|
})
|
})
|
}
|
|
/**
|
* 微信分享
|
* @param {object} shareObj 分享内容对象
|
* */
|
wxShare(shareObj) {
|
if (!this.isWeixin()) {
|
return Toast.fail('当前不是微信环境');
|
}
|
console.log('分享', shareObj)
|
let norShare = new Promise((resolve, reject) => {
|
wx.ready(() => {
|
wx.updateAppMessageShareData({
|
title: shareObj.title, // 分享标题
|
desc: shareObj.desc, // 分享描述
|
link: shareObj.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
|
imgUrl: shareObj.imgUrl, // 分享图标
|
success: function () {
|
resolve('成功了')
|
},
|
fail: function (res) {
|
console.log('好友分享 失败', res);
|
alert('fail:' + JSON.stringify(res));
|
reject(JSON.stringify(res))
|
}
|
})
|
})
|
})
|
let friendShare = new Promise((resolve, reject) => {
|
wx.ready(() => {
|
wx.updateTimelineShareData({
|
title: shareObj.title, // 分享标题
|
desc: shareObj.desc, // 分享描述
|
link: shareObj.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
|
imgUrl: shareObj.imgUrl, // 分享图标
|
success: function () {
|
resolve('成功了')
|
},
|
fail: function (res) {
|
console.log('朋友圈分享 失败', res);
|
alert('fail:' + JSON.stringify(res));
|
reject(JSON.stringify(res))
|
}
|
})
|
})
|
})
|
return Promise.all([norShare, friendShare])
|
}
|
|
wxPay(response) {
|
if (!this.isWeixin()) {
|
return Toast.fail('当前不是微信环境');
|
}
|
console.log('支付', response);
|
return new Promise((resolve, reject) => {
|
wx.chooseWXPay({
|
appId: response.appId,
|
timestamp: response.timeStamp, // 支付签名时间戳,注意微信 jssdk 中的所有使用 timestamp 字段均为小写。但最新版的支付后台生成签名使用的 timeStamp 字段名需大写其中的 S 字符
|
nonceStr: response.nonceStr, // 支付签名随机串,不长于 32 位
|
package: response.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
|
signType: response.signType, // 微信支付V3的传入 RSA ,微信支付V2的传入格式与V2统一下单的签名格式保持一致
|
paySign: response.paySign, // 支付签名
|
complete: (res) => {
|
console.log(res);
|
reject(res)
|
},
|
success: function (res) {
|
// 支付成功后的回调函数
|
console.log(res);
|
resolve(res)
|
},
|
// cancel: function (err) {
|
// reject(err)
|
// }
|
})
|
})
|
}
|
}
|
|
|
|
export default new AuthWechat();
|