当前位置:网站首页>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 .
边栏推荐
- Pyqt5 installation and use
- HTTP cache
- Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
- Scanning technology (getting started with web security 06)
- Related concepts of TTF, TOF, woff and woff2
- What is ISBN code and how to make it
- Analysis of resolv Conf common parameters
- [target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
- Understand one article: build an activity analysis system
- Some people are crazy, others are running away, and open source software is both hot and cold
猜你喜欢

C language series - Section 4 - arrays

8. greed

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

Vulnhub DC-5

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

5. concept of ruler method

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

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

Spark broadcast variables and accumulators (cases attached)
随机推荐
Reading redis source code (II) underlying data structure
Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
SAP mm initial transaction code MEK1 maintenance pb00 price
Xiamen's hidden gaopuge smart park has finally been uncovered
Optimization method of live weak network
The priority supplier field in the purchase information record of SAP mm primary level
How to customize a finished label template
Markdown - mark above / below symbol (typora, latex)
Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
Goframe framework (RK boot): fast implementation of CSRF verification
Analysis of resolv Conf common parameters
Web components series (I) - Overview
Integrated solution for intelligent supply chain platform management in rubber industry
Hypervisor Necromancy; Recover kernel protector (1)
Section 6: basic configuration I of spingboot
Quic implementation in rust --- Quinn
Add other view components to the audio and video components of the applet
What is ISBN code and how to make it
PNAs: power spectrum shows obvious bold resting state time process in white matter
Use ES6 new Target to simulate abstract classes