调用过程:
- 通过OAuth2授权链接进入页面并完成登录
- 调用后台获取企业签名接口 并进行 wx.config() 注册
- 在 wx.ready() 中继续 企业微信应用身份认证, 调用后台企业微信应用签名接口获取签名,在 wx.agentConfgi() 中注册
- 在 wx.invoke() 中调用需要使用的api方法
下面是完整代码,复制直接可用
export default {
init() {
postWork('/api/login/getSignature', {
url: location.href,
type: 1,
agentId: process.uniEnv.agentId
}).then(res => {
if (res.code == 0) {
wxx.config({
debug: false,
beta: true,
appId: process.uniEnv.corpid,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: ['agentConfig', 'getCurExternalContact'],
success: function(res) {
console.log('config', res)
},
fail: function(res) {
if(res.errMsg.indexOf('function not exist') > -1){
alert('版本过低请升级')
}
},
})
wxx.ready (() => {
this.agentInit()
})
}
})
},
agentInit() {
postWork('/api/login/getSignature', {
url: location.href,
type: 2,
agentId: process.uniEnv.agentId
}).then((res) => {
wxx.agentConfig({
corpid: process.uniEnv.corpid,
agentid: process.uniEnv.agentId,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: ['getContext','getCurExternalContact', 'hideMenuItems'],
success: (res) => {
console.log('应用身份',res)
this.getContext()
},
fail: function(res) {
if(res.errMsg.indexOf('function not exist') > -1){
alert('版本过低请升级')
}
}
});
})
},
getContext() {
wxx.invoke('getContext', {}, (res) => {
console.log('getContext', res)
if(res.err_msg == "getContext:ok"){
if (res.entry == 'normal') {
uni.showModal({
title: '请在下列场景打开应用!',
content: '外部联系人聊天窗口或详情',
confirmColor: '#fff',
showCancel: false,
success() {
}
})
} else {
this.getUserid()
}
}else {
}
});
},
getUserid() {
wxx.invoke('getCurExternalContact', {}, (res) => {
console.log('getCurExternalContact11111', res)
if(res.err_msg == "getCurExternalContact:ok"){
var userId = res.userId ;
uni.$emit('getExternalId', res)
} else {
uni.showModal({
title: '无权限!',
content: '请联系管理员!',
confirmColor: '#fff',
showCancel: false,
success() {
}
})
}
});
}
}