企业微信获取userId

1,218 阅读1分钟

第一步,先引入这两个

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>

第二步,调试工具是企业微信调试,引入这个

<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
var vConsole = new VConsole();

第三步,具体代码

<script>
    var vConsole = new VConsole();
    new Vue({
        el: '#app',
        data: {
            agentid: '',//企业微信的应用id
            corpid: '',//企业微信的corpid
            nonceStr: '',//生成签名的随机串 
            signature: '',//签名
            timestamp: '',//生成签名的时间戳
            userId: '',
            list: [],
            getid: '',
            all:''
        },
        mounted() {
            this.getsigna()
        },
        watch: {
            getid: function (news, old) {
                this.getCompany()
            },
            deep: true
        },
        methods: {
            getsigna() {
                axios
                    .post("api/h5/getSignature", {
                        url: window.location.href,
                    })
                    .then((res) => {
                        if (res.data.code == 200) {
                            let data = res.data.data
                            this.agentid = data.agentid
                            this.corpid = data.corpid
                            this.nonceStr = data.nonceStr
                            this.signature = data.signature
                            this.timestamp = data.timestamp
                            this.config()
                        }
                    })
            },
            config() {
                wx.agentConfig({
                    corpid: this.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
                    agentid: this.agentid, // 必填,企业微信的应用id (e.g. 1000247)
                    timestamp: this.timestamp, // 必填,生成签名的时间戳
                    nonceStr: this.nonceStr, // 必填,生成签名的随机串
                    signature: this.signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
                    jsApiList: ['getCurExternalContact'], //必填,传入需要使用的接口名称
                    success: (res) => {
                        this.invoke()
                    }
                });
            },
            invoke() {
                wx.invoke('getCurExternalContact', {
                }, (res) => {
                    if (res.err_msg == "getCurExternalContact:ok") {
                        this.userId = res.userId
                    }
                });
            },

           
        }
    })
</script>