当前位置:网站首页>Micro build low code to realize user login and registration
Micro build low code to realize user login and registration
2022-06-23 02:58:00 【Han Kai】
In small program development , We can get the information of wechat users openid,openid It can be used as the unique identification of the user , When querying data, you can filter the data to query the submitted data . Is there also... In the micro building openid The concept , You can get the data submitted by users ?
The answer is yes , How do you do that ? The idea is to create a data source method first , Returns... In the data source openid, Then the low code can call the method of the data source and assign it to the global variable , In this way, the global variables can be obtained in the subsequent business openid 了 , It is convenient to control data permissions .
To achieve this goal , Let's start by creating a data source .
create data source
Let's first create a user managed data source , It contains the basic information of users , And add a openid Field of
Add a custom method , Return the user's openid
const cloud = require('wx-server-sdk');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
module.exports = async function (params, context) {
const wxContext = cloud.getWXContext()
return {
openid: wxContext.OPENID,
appid: wxContext.APPID,
}
}Create an
We create a new blank application , Type selection applet
Create a blank page , Add the following components to the page
The logic of the component is to display an image and unlisted text if you are not logged in , Click the login button , After successful authorization, the user's Avatar and nickname will appear
To implement the above logic, we first define a user variable
The initial values are set as follows
{
"city": "",
"gender": "",
"openid": "",
"country": "",
"language": "zh_CN",
"nickName": "",
"province": "",
"avatarUrl": ""
}Then bind the first picture and the second text component as the avatar and nickname in the variable
We have on components if Judge whether the component is displayed by expression
Components displayed when not logged in If The judgment is just the opposite of the above expression
In this way, you can switch the page effect when you are not logged in and after you log in
Then you need to implement the login logic , Define a low code method for the page , When the applet is loaded, we get openid
export default {
async onAppLaunch(launchOpts) {
//console.log('---------> LifeCycle onAppLaunch', launchOpts)
const objData = await app.cloud.dataSources.userinfo_xpw5sxe.getopenid();
app.dataset.state.useropenid=objData.openid; // Assign to global variables
},
onAppShow(appShowOpts) {
//console.log('---------> LifeCycle onAppShow', appShowOpts)
},
onAppHide() {
//console.log('---------> LifeCycle onAppHide')
},
onAppError(options) {
//console.log('---------> LifeCycle onAppError', options)
},
onAppPageNotFound(options) {
//console.log('---------> LifeCycle onAppPageNotFound', options)
},
onAppUnhandledRejection(options) {
//console.log('---------> LifeCycle onAppUnhandledRejection', options)
}
}Define low code methods on the current page , Implement login logic
/*
* It can be done by $page.handler.xxx Access the methods defined here
* Be careful : This method is only valid on the page to which it belongs
* if necessary async-await, Please amend it to export default async function() {}
*/
export default async function({event, data}) {
try {
wx.getUserProfile({
desc: ' Used to improve user information ',
success:async (res) => {
//const data = await app.cloud.dataSources.userinfo_xpw5sxe.wedaCreate(res);
console.log("res.userinfo",res.userInfo)
$page.dataset.state.userinfo = res.userInfo
$page.dataset.state.userinfo.openid = app.dataset.state.useropenid
let ret =await app.dataSources.userinfo_xpw5sxe.wedaCreate($page.dataset.state.userinfo);
console.log(ret)
}
})
//console.log("userinfo",$page.dataset.state.userinfo)
//const ret = await app.dataSources.userinfo_xpw5sxe.wedaCreate($page.dataset.state.userinfo);
//console.log(ret)
} catch (e) {
console.log(' Error code ', e.code, ' error message ', e.message);
}
}Then define the click event for the button , Call the method
Preview release
After the function is developed , Click publish , Publish as Preview
After authorized login, a record will be written to the data source , Record user information
In this way, the function of user login and registration is completed , Subsequent submission pages need to be accompanied by openid, It is convenient for us to query the page according to openid To filter the data .
边栏推荐
- Phpexcel export with picture Excel
- Biological psychiatry: defining individualized functional neuroanatomy for precision psychiatry
- How to use pictures in Excel in PPT template
- Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
- Daily shift series: memory problem of primary online service
- Online signature with canvas
- Aiot application innovation competition -- I am the master of my project, and use gn+ninja to complete the system construction (vscode Development)
- 2022-01-30: minimum good base. For a given integer n, if K (k) of n
- Easygbs adds websocket message push, which can quickly locate video playback faults
- Reinforcement learning series (III) -gym introduction and examples
猜你喜欢

C language series - Section 4 - arrays

6. template for integer and real number dichotomy
What is sitelock? What is the function?

8. greed

Vulnhub DC-5

Spark broadcast variables and accumulators (cases attached)

How to store, manage and view family photos in an orderly manner?

5. concept of ruler method

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
随机推荐
Integrated solution for intelligent supply chain platform management in rubber industry
Pyqt5 installation and use
Vulnhub DC-5
Pytest common summary
Solution to the problem of easycvr switching MySQL database traffic statistics cannot be displayed
Optimization method of live weak network
Online signature with canvas
Related concepts of TTF, TOF, woff and woff2
Easygbs adds websocket message push, which can quickly locate video playback faults
Line to line interviewer | one side of ByteDance
Web components series (I) - Overview
Wechat applet camera compressed image is Base64
Why can only a small number of condition type prices be maintained in me12 of SAP mm?
Golang string comparison
C language series - Section 4 - arrays
Detailed explanation of online reputation management
How to make traditional Chinese medicine labels with pictures
How to make a borrowing card
Applet control version update best practices
Supervisor multi process management exception automatic restart visual management