Today record micro channel small program login is how to achieve

The first step is to use the small program wx. CheckSession to verify the login state

Success: The interface is successfully invoked and session_key has not expired.

Fail: the interface fails to be invoked and the session_key expires.

The applet wx.login obtains the code and wx.request submits the code to its own server

The server submits Appid + appSecret + code to the wechat server to obtain session_key & OpenID

The server generates 3rd_session according to session_key & OpenID (for security reasons, it is suggested that developers do not transfer key information such as OpenID) and returns 3rd_session to the applet

Wx. setStorage on the applet side stores 3rd_session. This parameter is attached when credentials are required for subsequent user operations

Wx. getUserInfo obtains user information + wx.getStorage obtains 3rD_session data, and then submits wx.request to its own server

Update server SQL user data

Function userLogin() {wx.checkSession({success: function () {// there is a login state}, fail: Function () {onLogin()}})} function onLogin() {wx.login({success: Function (res) {if (res.code) {wx.request({url: 'Our Server ApiUrl', data: {code: res.code}, success: Function (res) {const self = this if (logic success) {3rd_session var json = json.parse (res.data.data) wx.setStorage({ key: "third_Session", data: json.third_Session }) getUserInfo() } else { } }, fail: function (res) { } }) } }, fail: function (res) { } }) } function getUserInfo() { wx.getUserInfo({ success: function (res) { var userInfo = res.userInfo userInfoSetInSQL(userInfo) }, fail: function () { userAccess() } }) } function userInfoSetInSQL(userInfo) { wx.getStorage({ key: 'third_Session', success: function (res) { wx.request({ url: 'Our Server ApiUrl', data: { third_Session: res.data, nickName: userInfo.nickName, avatarUrl: userInfo.avatarUrl, gender: userInfo.gender, province: userInfo.province, city: userInfo.city, country: userInfo.country }, success: Function (res) {if (logic succeeded) {//SQL failed to update user data} else {//SQL failed to update user data}})}})}Copy the code