Small micro-channel program needs to get the user's openid as a user's unique identity, used to store and update the corresponding customer information. In an example Node background, the request foreground, background forward access channel corresponding to the interface to acquire the micro openid.
1. distal applet request:
getUserInfo: function(e) { app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) wx.login({ success: res => { app.globalData.usercode = res.code //1. 获取usercode作为后面获取openid的参数 } }) var id=null id=wx.getStorageSync('openid') if (!id) { wx.request({ url: getOpenId, //set in config .js // 2 back the API. Method: "the POST" , Data: { js_code: app.globalData.usercode, }, Success (RES) { var OpenID = res.data.openid var a sessionKey = res.data. session_key of the console.log (RES) wx.setStorageSync ( "OpenID" , OpenID) app.globalData.openId = OpenID IF (!! OpenID) { // 3. after processing the acquired openid, can follow their own logic write app. = globalData.openId OpenID wxlogin (OpenID) } } }) } } })
2. Node JS backstage get openid:
router.post ( '/ wxgetOpenId', function (REQ, RES, Next) { var Data = req.body var to APP_URL = 'HTTPS: //api.weixin.qq.com/sns/jscode2session' var APP_ID = 'wxXXXXXXXXX' // App the above mentioned id applet, in the background you can see the public developer var APP_SECRET = '8ad6f0XXXXXXXXXXXX' // App Secrect program can be seen in the background of public developer var ResData = null var thisRes = RES IF (!! the Data .js_code) { Request ( `$ {} to APP_URL ? AppID APP_ID} = $ {$ & Secret = {} & js_code APP_SECRET data.js_code = {} $ = & grant_type authorization_code`, (error, Response, body) => { Console.log('statusCode:', response && response.statusCode) console.log(body) res.end(body)}) } })