niucloud/uni-app/utils/wechat.ts
全栈小学生 8a600afd4f 更新uni-app
2023-05-20 18:12:41 +08:00

50 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import wx from 'weixin-js-sdk'
import { getWechatSkdConfig } from '@/api/system'
import { isWeixinBrowser } from '@/utils/common'
class Wechat {
constructor() {
// #ifdef H5
// isWeixinBrowser() && this.init()
// #endif
}
public init() {
getWechatSkdConfig({
url: uni.getSystemInfoSync().platform == 'ios' ? uni.getStorageSync('initUrl') : location.href
}).then((res : responseResult) => {
const { data } = res
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名
jsApiList: ['chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData'] // 必填需要使用的JS接口列表
});
})
}
/**
* 发起支付
*/
public pay(options : WeixinJsSdk.ChooseWXPayOptions) {
wx.ready(() => {
wx.chooseWXPay(options)
})
}
/**
* 分享设置
*/
public share(options : WeixinJsSdk.OnMenuShareAppMessageOptions) {
wx.ready(() => {
// 分享给朋友
wx.updateAppMessageShareData(options)
// 分享到朋友圈
wx.updateTimelineShareData(options)
})
}
}
export default new Wechat()