Home  >  Article  >  WeChat Applet  >  Detailed explanation of enterprise WeChat related mini program to obtain employee information

Detailed explanation of enterprise WeChat related mini program to obtain employee information

coldplay.xixi
coldplay.xixiforward
2021-04-16 10:59:453462browse

Detailed explanation of enterprise WeChat related mini program to obtain employee information

Enterprise WeChat associated applet obtains employee information currently logged in to the applet

Recently, at the request of the leader, employee information is automatically obtained. I encountered some problems and will record them here.

  • # If the applet wants to obtain the user information of employees, it needs to obtain the following parameters in sequence, which are linked together.

    • code. The code here is used to pass values ​​​​to the server and produce and sell them by yourself.
      Detailed explanation of enterprise WeChat related mini program to obtain employee information

    • #corpid The corporate id of the enterprise. Find path -& gt; log in to the company's WeChat web page version
      Detailed explanation of enterprise WeChat related mini program to obtain employee information

    • # Corpsecret. Are you surprised or surprised? The requirement is like I want to run but I don’t have legs yet. Solution: Use the corpsecret of the mini program that has been associated with Enterprise WeChat. Search path for this parameter:

    Detailed explanation of enterprise WeChat related mini program to obtain employee information

    • Use the above two parameters to obtain access_token. Code:

      wx.request({
              url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
              data: {
                corpid:'你的企业corpid',
                corpsecret:'你的小程序corpsecret'
              },})

      Official return parameter list

      //成功的返回结果{
         "errcode":0,
         "errmsg":"",
         "access_token": "accesstoken000001",
         "expires_in": 7200}//失败的返回结果{
         "errcode":40091,
         "errmsg":"secret is invalid"}
    • Get the userid through access_token and code. Explanation of userid: from official

      用户在企业内的UserID,对应管理端的帐号,企业内唯一。注意:如果该企业没有关联该小程序,则此处返回加密的userid

      Get the userid code

      wx.request({
                  url: 'https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session',
                  data:{
                    access_token:res.data.access_token,
                    js_code:login.code,
                    grant_type:'authorization_code',
                  },})
    • Then get the access_token and userid and let’s get the current employee’s information

       wx.request({
                      url: 'https://qyapi.weixin.qq.com/cgi-bin/user/get',
                      data:{
                        access_token:res.data.access_token,
                        userid:getInfo.data.userid                },
       })

      The returned result json object: from official

      {
          "errcode": 0,
          "errmsg": "ok",
          "userid": "zhangsan",
          "name": "张三",
          "department": [1, 2],
          "order": [1, 2],
          "position": "后台工程师",
          "mobile": "13800000000",
          "gender": "1",
          "email": "zhangsan@gzdev.com",
          "is_leader_in_dept": [1, 0],
          "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0",
          "thumb_avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/100",
          "telephone": "020-123456",
          "alias": "jackzhang",
          "address": "广州市海珠区新港中路",
          "open_userid": "xxxxxx",
          "main_department": 1,
          "extattr": {
              "attrs": [
                  {
                      "type": 0,
                      "name": "文本名称",
                      "text": {
                          "value": "文本"
                      }
                  },
                  {
                      "type": 1,
                      "name": "网页名称",
                      "web": {
                          "url": "http://www.test.com",
                          "title": "标题"
                      }
                  }
              ]
          },
          "status": 1,
          "qr_code": "https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=xxx",
          "external_position": "产品经理",
          "external_profile": {
              "external_corp_name": "企业简称",
              "external_attr": [{
                      "type": 0,
                      "name": "文本名称",
                      "text": {
                          "value": "文本"
                      }
                  },
                  {
                      "type": 1,
                      "name": "网页名称",
                      "web": {
                          "url": "http://www.test.com",
                          "title": "标题"
                      }
                  },
                  {
                      "type": 2,
                      "name": "测试app",
                      "miniprogram": {
                          "appid": "wx8bd80126147dFAKE",
                          "pagepath": "/index",
                          "title": "my miniprogram"
                      }
                  }
              ]
          }}

      WeChat Tip: When your mini program is not associated with Enterprise WeChat, even if you use the corpsecret of other mini programs, invalid code will still be displayed in the WeChat developer tools . This is because your current WeChat applet development project is not associated with Enterprise WeChat, so there are two solutions:

      1. You can try to use the official test parameters, but the official said that the parameters used for testing cannot be Obtain employee information for testing purposes only. So happy (emm…).
      2. Test under the associated applet code. The premise is that you can get all the information about the associated applet (mentioned above).

Related free learning recommendations: WeChat Mini Program Development

The above is the detailed content of Detailed explanation of enterprise WeChat related mini program to obtain employee information. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete