public/index 引入
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" referrerpolicy="origin"></script>
import { getAuthorizationStatus, wechatAgentInfo } from '@/api/index'
import { mapState } from 'vuex'
export default {
data() {
return {
configStatus: this.$store.state.configStatus || false,
authorizationStatus: this.$store.state.authorizationStatus || '-1'
}
},
computed: {
...mapState({ userInfo: (state) => state.user.userInfo })
},
watch: {
userInfo: {
handler(val) {
console.log(val)
if (val.config && val.config.accountId) {
this.getAuthorizationStatus()
}
},
deep: true,
immediate: true
}
},
methods: {
// 获取应用是否授权通讯录
getAuthorizationStatus() {
if (this.authorizationStatus == '2') {
this.getWechatAgentInfo()
} else if (this.authorizationStatus == '-1') {
// 接口返回鉴权信息
getAuthorizationStatus().then((res) => {
this.$store.commit('SET_AUTH_STATUS', res.data)
this.authorizationStatus = res.data
if (res.data == '2') {
this.getWechatAgentInfo()
}
})
}
},
// 获取第三方应用鉴权参数
getWechatAgentInfo() {
const _url = window.location.href.split('#')
wechatAgentInfo({ url: _url[0], accountId: this.userInfo.config.accountId }).then((res) => {
if (res.code === '0') {
this.agentConfig(res.data)
} else if (res.code === '501') {
this.$store.commit('SET_AUTH_STATUS', '3')
this.authorizationStatus = '3'
}
})
},
// 第三方应用鉴权
agentConfig(res) {
const wx = window.wx
const that = this
wx.agentConfig({
corpid: res.cropId, // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: parseInt(res.agentId), // 必填,企业微信的应用id (e.g. 1000247)
timestamp: res.timestamp, // 必填,生成签名的时间戳
nonceStr: res.nonceStr, // 必填,生成签名的随机串
signature: res.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: ['selectExternalContact'], // 必填,传入需要使用的接口名称
success: function (result) {
console.log(result, '请求微信成功') // 回调
that.configStatus = true
that.$store.commit('SET_CONFIG_STATUS', true)
},
fail: function (res) {
console.log('fail')
console.log(res)
if (res.errMsg.indexOf('function not exist') > -1) {
}
}
})
}
}
}
然后在 layout 中 调用
import setAgentConfig from '@/mixins/setAgentConfig'
mixins: [setAgentConfig],