preface

The usual development is micro channel small program, it is running in the micro channel App small program, recently happened to develop an enterprise micro channel small program, a little panic ~, deliberately understand, record sauce ~

What is the enterprise micro channel applets

Enterprise wechat applets are small programs running in enterprise wechat App. In essence, enterprise wechat applets are an extended subset of wechat applets. Most of the capabilities of wechat applets can be run on enterprise wechat, while enterprise wechat extends some enterprise-related capabilities to meet more scene requirements.

How to develop enterprise micro channel small program

According to what is said, it is only micro channel small program on the expansion, then the development tool is still micro channel developer tools, but in the debugging run, and micro channel small program slightly different ~

WeChat developer tools download address: developers.weixin.qq.com/miniprogram…

The mobile phone needs to download the wechat App of the enterprise. During debugging, the QR code generated by the preview on the wechat developer tool can be debug-ed on the mobile phone.

The adjustments that need to be made on wechat developer tools are as follows

Step 1: Add the enterprise wechat applet plug-in

Step 2: Switch during development and debugging

Step 3: You can choose the enterprise to belong to or use the test account

Enterprise WeChat small program has a big characteristic, is that the user is likely to have a subordinate enterprises, one or more enterprises, why say so, because we are a small development program requires the user to log in, need to take the user information, then we may need according to user’s enterprise show different features.

In wechat applets, OpenID is the unique identifier to identify users, while in enterprise wechat applets, userID is the unique identifier to identify users.

Back, I just want to say this step, you can choose the enterprise to develop.

Then you can develop

The login process of enterprise wechat mini program

First say the difference between the two, in fact, the difference is not big, just to understand, convenient for us to build the login process of the code ~

The figure on the left is the login process of the enterprise micro channel applet, and the figure on the right is the login process of the micro channel applet. There is little difference between the two processes. The red arrow is the specific difference, but the parameters to be given by the front end are different.

In fact, there is no need to consider too complicated. The front end only needs to know that the enterprise wechat applet obtains the code through wx.qy.login and sends it to the back end. The back end obtains the corPID of the enterprise to which the user belongs according to the current code and then goes to the wechat background to obtain the unique id of the current user, userID, and makes data records on the back end. Then give us the front-end return token, we can save in storage!

Similarly, the wechat applet obtains the code through wx.login and sends it to the back end. The back end obtains the user appID according to the current code and then goes to the background of wechat to obtain the unique identifier of the current user openID. Record the data of the back end, and then returns the token to the front end.

Specific code ideas:

If not, the authorization page is displayed. After confirming the authorization, the user logs in to wx.qy.login, invokes the get backend interface to obtain the sessionKey and userID, and invokes the backend login interface to obtain the token.

GetUserProfile (wx.getUserInfo) is used until April 29, 2021. This API has popover, but after April 29, 2021, it will change to no popover. You are advised to use wx.getUserProfile to obtain user information), then call wx.qy.login to obtain the code, call the backend interface to obtain the sessionKey and userID, and then call the backend login interface to obtain the token.

Code in app.js:

The following code executes immediately upon entering the applet

OnLaunch: function (options) {// Check whether the user is authorized, then login wx.getSetting({success: Res0 => {if (res0.authSetting['scope.userInfo']) {// Wx. getUserInfo ({success: Res1 => {console.log(' get user information ') wx.qy.login({success: Res2 => {// After obtaining the sessionKey and userID from the backend request interface, Then (res=>{// if(res.code==200){// getToken({}).then(res=>{// wx.setStorageSync('token', res.data.token) // }) // } // }) } }) } }) } else { wx.reLaunch({ url: '/pages/login/index', }) } } }) }Copy the code

Following the above code, jump to the LOGIN page, the code on the login page is as follows:

login.wxml

<view> <button type="primary" bindtap="getUserProfile">Copy the code

login.js

Wx.getuserprofile () {wx.getUserProfile() {desc: 'to improve the member's profile ', // declare the user's personal information after the use, will be displayed in the subsequent popup, please be careful to fill in success: (res) => { wx.qy.login({ success: Res1 => {// After obtaining the sessionKey and userID from the backend request interface, Then (res=>{// if(res.code==200){// getToken({}).then(res=>{// wx.setStorageSync('token', res.data.token) // }) // } // }) } }) } }) }Copy the code

The specific login process is probably like this, if you have better login process suggestions, can also share, there is a mistake is also welcome to correct, I hope we make progress together! Follow-up to do small procedures encountered various problems, I will continue to modify and update in the article drip ~