
uni-app 全局变量wx污染,导致企微agentConfig失败,ww-open-data无法显示
uni-app 全局变量wx污染,导致企微agentConfig失败,ww-open-data无法显示
·
uni-app 全局变量wx污染,导致企微agentConfig失败,ww-open-data无法显示
可能导致问题
- WWOpenData is undefined:在生成WWOpenData时,需要先进行wx.agentConfig,被污染后,没有agentConfig
<template>
<ww-open-data :type="type" :openid="openid" />
</template>
<script>
export default {
props: ['type', 'openid'],
mounted() {
WWOpenData.bind(this.$el)
}
}
</script>
- wx.agentConfig is not a function:企微的wx变量被uniapp污染,导致方法找不到
let sign = ww.getSignature(appJsApiTicket)
wx.agentConfig({
corpid: wmsWxconfig.corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: wmsWxconfig.agentId, // 必填,企业微信的应用id (e.g. 1000247)
timestamp: sign.timestamp, // 必填,生成签名的时间戳
nonceStr: sign.nonceStr, // 必填,生成签名的随机串
signature: sign.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: ['selectEnterpriseContact'], //必填,传入需要使用的接口名称
success: function() {
WWOpenData.bind(this.$el)
}
})
解决方法
在App.vue文件onLaunch钩子中对wx变量进行重置,然后重新赋值
import * as ww from '@wecom/jssdk' //官方npm包,可以快捷进行config
onLaunch(opts) {
this.clearWX()
},
methods: {
clearWX(){
window.wx = null
console.log('clearWX')
let that = this
let script1 = document.createElement('script')
script1.type = 'text/javascript'
// script1.setAttribute('referrerpolicy', 'origin')
script1.src = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js'
document.head.appendChild(script1)
script1.onload = function() {
const script2 = document.createElement('script')
script2.type = 'text/javascript'
// script2.setAttribute('referrerpolicy', 'origin')
script2.src = 'https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js'
document.head.appendChild(script2)
script2.onload = function() {
// 进行config、agentConfig
ww.register({
corpId: corpId,
agentId: agentId,
jsApiList: ['selectEnterpriseContact'],
getConfigSignature () {
return ww.getSignature(corpJsApiTicket)
},
getAgentConfigSignature () {
return ww.getSignature(appJsApiTicket)
},
})
}
}
}
}
ww-open-data组件
<template>
<ww-open-data
:type="type"
:openid="openid"/>
</template>
<script>
import * as ww from '@wecom/jssdk'
export default {
props: {
type: {
type: String,
default: 'userName'
},
openid: {
type: String,
default: ''
}
},
mounted() {
this.agentConfig()
},
methods: {
agentConfig() {
let sign = ww.getSignature(appJsApiTicket)
wx.agentConfig({
corpid: corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: agentId, // 必填,企业微信的应用id (e.g. 1000247)
timestamp: sign.timestamp, // 必填,生成签名的时间戳
nonceStr: sign.nonceStr, // 必填,生成签名的随机串
signature: sign.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: ['selectEnterpriseContact'], //必填,传入需要使用的接口名称
success: function() {
WWOpenData.bind(this.$el)
}
})
}
}
}
</script>
更多推荐
所有评论(0)