企业微信群消息推送的JS-SDK接入方法分享
最编程
2024-02-12 22:32:05
...
import { getCorp, getAgentConfig } from '@/api/qywx/crop' // 这是后端给的接口,用于获取签名算法等信息
// 调用wx.agentConfig之前,必须确保先成功调用wx.config. 注意:从企业微信3.0.24及以后版本(可通过企业微信UA判断版本号),无须先调用wx.config,可直接wx.agentConfig.
// 仅部分接口才需要调用agentConfig,需注意每个接口的说明
const weChatMixin = {
data() {
return {}
},
methods: {
// 用于调用wx.config
async wxInterfaceCall(url, wxApi, fn) {
const wx = window.wx
const res = await getCorp({ url: url })
if (res.code === 0) {
const datas = res.result
this.$wx.config({
beta: true,
debug: false,
appId: datas.corpId,
timestamp: datas.timestamp,
nonceStr: datas.nonceStr,
signature: datas.signature,
jsApiList: Array.isArray(wxApi) && wxApi.length > 0 ? wxApi : []
})
if (fn && typeof fn === 'function') {
this.$wx.ready(() => {
fn(this.$wx)
})
}
}
},
// 用于需要 调用agentConfig的接口
async wxAgentConfig(url, wxApi, fn) {
let me = this
const wx = window.wx
const res = await getCorp({ url: url })
if (res.code === 0) {
const datas = res.result
this.$wx.config({
beta: true,
debug: false,
appId: datas.corpId,
timestamp: datas.timestamp,
nonceStr: datas.nonceStr,
signature: datas.signature,
jsApiList: wxApi
})
// 通过ready接口处理成功验证
this.$wx.ready(() => {
me.$wx.checkJsApi({
jsApiList: wxApi,
success: async () => {
const res1 = await getAgentConfig({ url: url })
if (res1.code === 0) {
const dataRes = res1.result
me.$wx.agentConfig({
corpid: dataRes.corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: dataRes.agentId, // 必填,企业微信的应用id (e.g. 1000247)
timestamp: dataRes.timestamp, // 必填,生成签名的时间戳
nonceStr: dataRes.nonceStr, // 必填,生成签名的随机串
signature: dataRes.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: Array.isArray(wxApi) && wxApi.length > 0 ? wxApi : [],
success: res => {
if (fn && typeof fn === 'function') {
fn(me.$wx)
}
}
})
}
}
})
})
}
}
}
}
export default weChatMixin