WeChat applet implements login interface

WeChat applet implements login interface

The login interface of WeChat applet is implemented for your reference. The specific content is as follows

<view class="container">
  <view class="wrapper">
    <view class="left-top-sign">LOGIN</view>
    <view class="welcome">
      Welcome back!
    </view>
    <view class="input-content">
      <view class="input-item">
        <text class="tit">Mobile phone number</text>
        <input type="text" placeholder="Please enter your phone number" id='phone' data-type='phone' bindinput='handerInput' />
      </view>
      <view class="input-item">
        <text class="tit">Password</text>
        <input type="password" placeholder="Please enter your password" id='password' data-type='password' bindinput='handerInput' />
      </view>
    </view>
    <button class="confirm-btn">Login</button>
    <view class="forget-section">
      forget the password?
    </view>
  </view>
  <view class="register-section">
    No account yet?
    <text>Register now</text>
  </view>
</view>

The most basic form submission.

data: {
    phone: '', //phone number password: '' //password},

  /**
   * Life cycle function--listen for page loading*/
  onLoad: function (options) {

  },
  handerInput(event) {
    //let type = event.currentTarget.dataset.type;
    let type = event.currentTarget.id;
    console.log(event);
    this.setData({
      [type]: event.detail.value
   })
  },
  /**

To implement two-way binding, use the bindinput event and use id or dataset to uniquely identify data.

One data can be passed into id, and multiple data can be passed into dataset.

WeChat applet interaction: message display box. (Official Link)

Bind a click callback function to the login button.

//html
<button class="confirm-btn" bindtap="login">Login</button>

//js
login() {
    let { phone, password } = this.data;
    console.log(password);
    /**
     * Phone number verification* Phone number is empty* Phone number format is wrong* Phone number is correct*/
    if (!phone) {
      wx.showToast({
        title: 'Mobile number cannot be empty',
        icon: 'none'
      })
      return;
    }
    //Define the regular expression for the phone number let phoneReg = /^1(3|4|5|6|7|8|9)\d{9}$/
    if (!phoneReg.test(phone)) {
      wx.showToast({
        title: 'The format of the mobile phone number is wrong',
        icon: 'none'
      })
      return;
    }

    if (!password) {
      wx.showToast({
        title: 'The password cannot be empty',
        icon: 'none'
      })
      return;
    }

    wx.showToast({
      Title: 'Front-end verification passed'

    })

Backend verification, calling the interface, and returning the login information to the user through the response status code.

let result = await request('/login/cellphone', { phone, password });
    if (result.code === 200) {
      wx.showToast({
        title: 'Login successful',
      })
    }
    else if (result.code === 400) {
      wx.showToast({
        title: 'Wrong phone number',
        icon: 'none'
      })
    }
    else if (result.code === 502) {
      wx.showToast({
        title: 'Wrong password',
        icon: 'none'
      })
    }
    else {
      wx.showToast({
        title: 'Login failed, please log in again',
        icon: 'none'
      })
    }
},

Click the avatar in the personal center to jump to the login interface. After successful login, cache the user's personal information data (using setStorageSync and getStorageSync methods), then use switchTab to jump to the personal center page under the tabbar, and then store the obtained cached data in js data. Pay attention to the conversion of json format, and finally

Special judgment on ternary operation in html.

<view class="user-info-box" bindtap='toLogin'>
      <view class="portrait-box">
        <image class="portrait"
          src='{{userInfo.avatarUrl?userInfo.avatarUrl:"/static/images/personal/missing-face.png"}}'></image>
      </view>
      <view class="info-box">
        <text class="username">{{userInfo.nickname?userInfo.nickname: 'Visitor'}}</text>
      </view>
</view>
//login.js
if (result.code === 200) {
      wx.showToast({
        title: 'Login successful',
      })

      wx.setStorageSync('userInfo', JSON.stringify(result.profile));

      wx.switchTab({
        url: '/pages/personal/personal'
      })
    }
// personal.js
onLoad: function (options) {

    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        userInfo: JSON.parse(userInfo)
      })
    }

  }, 

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet implements login page
  • JSF implements a simple login page for WeChat applet (with source code)
  • Implementation of a simple login page for WeChat applet (with source code)
  • WeChat Mini Program Practice: Login Page Production (5)
  • WeChat applet local storage and login page processing example detailed explanation
  • WeChat applet login interface example

<<:  Detailed explanation of the usage of DECIMAL in MySQL data type

>>:  VMware Workstation installation Linux (Ubuntu) system

Recommend

Vendor Prefix: Why do we need a browser engine prefix?

What is the Vendor Prefix? Vendor prefix—Browser ...

Summary of Linux date command knowledge points

Usage: date [options]... [+format] or: date [-u|-...

Vue implements page caching function

This article example shares the specific code of ...

Solution to define the minimum height of span has no effect

The span tag is often used when making HTML web pa...

Solution to Nginx session loss problem

In the path of using nginx as a reverse proxy tom...

VUE+Canvas realizes the whole process of a simple Gobang game

Preface In terms of layout, Gobang is much simple...

Solution to the same IP after cloning Ubuntu 18 virtual machine

Preface I recently used a virtual machine to inst...

Advanced crawler - Use of Scrapy_splash component for JS automatic rendering

Table of contents 1. What is scrapy_splash? 2. Th...

Explaining immutable values ​​in React

Table of contents What are immutable values? Why ...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...

CSS3 mouse hover transition zoom effect

The following is a picture mouse hover zoom effec...

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

js to achieve cool fireworks effect

This article shares the specific code for using j...