Small micro-channel program acquiring user unionId

unionId

The same subject at a WeChat open platform App, the public number, unionid applet is the same, so that you can lock a user is not the same

Micro-channel for different user has only one openId in different applications, however, to determine whether the user is not the same user, it is necessary to distinguish by unionid

The same subject at the same WeChat open App platform, the public number, when the applet, if the user is already public concern number, or have logged App or the public number, the user opens a small program, developers can get directly through wx.login to the user unionID, again without user authorization
(read: if the user is not logged app, there is no public number logged, and no public concern over the case number, the applet is available through wx.login of less than unionid)

UnionId mechanism Documentation: https://developers.weixin.qq.com/miniprogram/dev/api/unionID.html

UnionID acquisition path

  • Bound applet developer account, you can obtain the following three ways UnionID.

  • Call Interface wx.getUserInfo, get UnionID from the decrypted data. Note that this interface requires a user license, please developer to properly handle the situation after the user denying authorization.

  • If there are public body under the same numbers developer account, and the user has the attention of the public number. Developers can get directly to the user by wx.login UnionID, again without user authorization.

  • If there is the same number or a public body under the mobile application developer account, and the user has logged in the public authorization number or mobile applications. Developers can also get directly to the user by wx.login UnionID, again without user authorization

Used API

  • wx.login(obj)
  • wx.getUserInfo(obj)

Note: getUserInfo adjust this interface, the interface will authorize the use of pop does not occur, the use of <button open-type = "getUserInfo"> </ button>

pit:

We are generally the first to get unionid micro letter, and then unionid to log into your site, you can associate a user user_id on your site through, but in a small program to log in, sometimes you can get to unionid, sometimes get less than in the case of acquisition is less than unionid, the user can not log on the site properly.

The reason: with the same subject at a WeChat open platform App, when the public number, applet, if the user has been concerned about the public number, or have logged App or the public number, the user opens a small program, developers can directly wx. login to get the user unionID, again without user authorization
(read: if the user is not logged app, there is no public number logged, and no public concern over the case number, the applet is acquired by wx.login not unionid of)

All have two situations:

  1. Generally, users log in through the associated number of other public

    Use wx.login get code, spread to the back-end, code change openid, unionId

   // 1.login 
   wx.login ({ 
     Success: function (Data) { 

       wx.request ({ 
         URL: openIdUrl, 
         Data: { 
           code: data.code 
         }, 
         Success: function (RES) { 
           self.globalData.openid = RES .data.openid 
         }, 
         fail: function (RES) { 
           the console.log ( 'pulling openid user fails, will not function properly open interface services' , RES) 
         } 
       }) 

     }, 
     fail: function (ERR) { 
       Console. log ('wx.login interface calls fail, it will not work properly open interfaces and other services' , ERR) 
       callback (ERR) 
     } 
   })

 

  1. No. public and other users have not used associated

    This time wx.login would get less than the unionId. You need to use wx.getUserInfo

    Solutions: wx.getUserInfo obtained by the user with a login state to the initial vector iv encryptedData encrypted data and encryption algorithms, and then EncryptData, iv pass code and a rear end, a rear end received encryptedData go through, to iv , code and before session_key decrypt the user's openid, unionid etc.

  wx.getUserInfo ({ 
    withCredentials: to false , 
    Success: (obj) => { 
     
        wx.request ({ 
            URL: openIdUrl, 
            Data: { 
                code: data.code, 
                encryptedData: obj.encryptedData, 
                IV: obj.iv, 
            }, 
            Success : function (RES) { 
                self.globalData.openid = res.data.openid 
            }, 
            fail: function (RES) { 
                the console.log ( 'pulling openid user fails, will not function properly open interface services' , RES) 
            } 
        } )


    }
  })

 

The actual project, the need to integrate the use of the two cases

Two options:

The first determines whether the front end :( unionid) wx.login and upload code to the rear after the rear end of the return data, the front end determines whether a return value or unionid unionid case whether null, null down the call with user login state the wx.getUserInfo (), then the micro-channel return and iv encryptedData the back-end, the rear end of the corresponding decrypted information then returned to the front end;

Determining whether a second rear :( unionid) distal call wx.login (), wx.getUserInfo (), the code, and iv encryptedData the back-end, the front end of the rear end received after the request code to the micro-channel interface take unionid, if the returned unionid is empty, then the encryptedData, iv and decrypt unionid session_key before, the rear end of the corresponding decryption information and then return to the distal end



Link: https: //www.jianshu.com/p/46efa68d9033

Guess you like

Origin www.cnblogs.com/Ph-one/p/12156192.html