Briefly review the process of wechat applet :
- The user enters the authentication page of the applet by scanning the code , Update status to ACCESSED Scanned code
- The user clicks confirm Authorization , Wechat through wx.login() Interface to obtain the necessary information of the third party login :Code Login credentials .
Wechat applet mainly provides interactive functions for user authorization , The user scans the code , Provide an interaction UI, as follows :
Project structures,
use first vue-cli Create a web project , Name it mp-weixin
stay Pages Create login/index.vue page , As the login authorization page
The directory structure is as follows :
pages.json:
login New under the directory ajaxRequire.ts, establish request object , This object will utilize uni-axios-ts Library send ajax request
index.vue Created in loginExternalForms As a parameter transfer object
onLoad Function ,option Stored in the scanning applet code scene Parameters , take scene The parameter is assigned to token Variable
start In, we call Access Interface , Change status to ACCESSED( Scanned code ) , If the return is successful , Then prompt the user to click confirm authorization , If the returned result is abnormal "WechatMiniProgramLoginInvalidToken" when , Indicates that the applet code has expired at this time , You need to update the applet code on the webpage .
Prefix Is your service address prefix
stay Html in , We create authorized login and Cancel buttons , Only when the isInvalid by true You can click authorize
establish handleExternalLogin It is used to process the logic after the user clicks the authorization to log in , call Authenticate Interface , Update status to AUTHORIZED( Authorized ) You need to call before that uni.login Get applet login credentials code.
of uni.login function , Please refer to the official documentation
uniapp Support a variety of applets , In order to retain certain expansion capability ,handleExternalLogin We keep parameters in the function authProvider, Wechat applet login has been implemented handleWxLogin Pass parameters when a function is called "WeChat",
Create the unregister function
Execute the success notification function
Next, simply write an interface ,
The interface will clearly reflect isInvalid And loading Corresponding to UI Interaction :
normal
Applet code is out of date
Overall test
Simulator test
When I open the page , Save image as
In wechat applet debugging tool ,“ Through two-dimensional code compilation ”
Wait for the mobile interface to display the authorization page and click “ Authorized login ”:
GetCurrentUser The interface returns the correct data , And displayed on web On the page
So far, the development of the applet side has been completed
Project address
Conclusion
Applet login has certain scalability , Although wechat applet login is introduced throughout , But login authentication is an abstract function of applet ,uniapp Integrated with various platforms ( WeChat 、 Alipay 、 Baidu 、 Bytedance applet ) Login interface , adopt uni.login You can get the code










![[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node](/img/06/b90fa310158669696f94f79ef4cc5a.png)






