企业微信-获取外部联系人实践指南

783 阅读1分钟

企业微信

企业微信概念:用户(登陆人)与外部联系人
用户id:业务主数据部分
外部联系人:关联业务部分
开发-(相当于在一个第三方平台做个交互)认证之后,再根据id关联业务

逻辑概览

后台
access_token记录在服务器,设置和返回签名接口
前端
企业微信开发平台:developer.work.weixin.qq.com/document/pa…
引入js:script.src = 'res.wx.qq.com/open/js/jwe…'
从后台获取signature:getSignature \ 企业签名(企业signature)、应用签名(应用signature)--一般为应用注册,而应用签名前需先调用企业签名
调用微信的获取外部联系人方法window.wx.invoke('getCurExternalContact', {}, function (res) {})-获取联系人信息

问题解决

1.开发者中心的api错误码查询
2.社区中关键字模糊查询
3.签名校验的问题
let url = location.href.split('#')[0] 确保生成签名的url为 http://testtesttest:999/xxtest/test/ 这样的形式,最后的斜杠也是必要的

编码部分

async mounted () {
    // 本地环境
    const isLocal = false
    if(isLocal){
      return
    }
    // 获取用户id
    this.userId = this.$store.getters.userId
    if (!this.userId) {
      this.infoCode = this.$route.query.code
      if (!this.infoCode) {
        // 用户认证
        // 创建微信网页授权接口
        let params = {
          redirect_url: 'customerBind',
          authorize_name: 'setQYWX'
        }
        // 后台接口
        await createWxAuthorize(params).then(res => {
          if (res) {
            let url = res.data.authorize_url
            if (url) {
              window.location.href = url
            }
          }
        })
      } else {
        // 根据code获取user_info
        let params = {
          code: this.infoCode
        }
        await getUserInfoByCode(params).then(res => {
          this.userId = res.data.user_id
          this.userName = res.data.user_code
          this.$store.commit('user/SET_USER_ID', this.userId)
        })
      }
    }

    // 调用wx.agentConfig之前,必须确保先成功调用wx.config. 注意:从企业微信3.0.24及以后版本(可通过[企业微信UA]判断),无须先调用wx.config,可直接wx.agentConfig. 
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js'
    document.body.appendChild(script)
    const agentscript = document.createElement('script')
    agentscript.type = 'text/javascript'
    agentscript.src =
      'https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js'
    document.body.appendChild(agentscript)
    setTimeout(async () => {
      let timestamp = moment().format('x')
      let nonceStr = 'qywxgetSignature'
      this.printInfo.timestamp = timestamp
      this.printInfo.nonceStr = nonceStr
      // 签名接口
      let params = {
        timestamp,
        noncestr: nonceStr,
        url: this.$route.path.slice(1, this.$route.path.length)
      }
      this.printInfo.wx = window.wx

      getSignature(params).then(res => {
        this.printInfo.signature = res.signature
        this.printInfo.wx = window.wx
        this.printInfo.data = res.data
        const that = this
        window.wx.config({
          beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
          appId: res.data.appid, // 必填,企业微信的corpID
          timestamp, // 必填,生成签名的时间戳
          nonceStr, // 必填,生成签名的随机串
          signature: res.data.qySignature, // 必填,签名,见附录1
          jsApiList: ['getCurExternalContact'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
        })
        window.wx.ready(function () {
          // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
          window.wx.agentConfig({
            corpid: res.data.appid, // 必填,企业微信的corpid,必须与当前登录的企业一致
            agentid: res.data.agentid, // 必填,企业微信的应用id
            timestamp, // 必填,生成签名的时间戳
            nonceStr, // 必填,生成签名的随机串
            signature: res.data.signature, // 必填,签名,见附录1
            jsApiList: ['getCurExternalContact'], //必填
            success: function () {
              // 回调
              window.wx.invoke('getCurExternalContact', {}, function (res) {
                if (res.err_msg == 'getCurExternalContact:ok') {
                  that.contactId = res.userId //返回当前外部联系人userId
                  alert('getCurExternalContact:' + res.userId + '==' + that.contactId + '==')
                  queryCustInfoByContact({
                    contact_id: that.contactId
                  }).then(res => {
                  alert('queryCustInfoByContact: >>' + res)
                    if (res) {
                      that.custId = res[0].cust_id
                      that.$router.push({
                        name: 'customerDetails',
                        params: {
                          cust_id: that.custId
                        }
                      })
                    }
                  })
                } else {
                  //错误处理
                }
              })
            },
            fail: function (res) {
              if (res.errMsg.indexOf('function not exist') > -1) {
                alert('版本过低请升级')
              }
              alert('返回错误:', res)
            }
          })
        })
        window.wx.error(function () {})
      })

    }, 1000)
  }