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

Analysis of Vue element background authentication process

Preface: Recently, I encountered a management sys...

Detailed explanation of JavaScript data types

Table of contents 1. Literals 1.1 Numeric literal...

Analysis of the principle of centering elements with CSS

It is a very common requirement to set the horizo...

HTML table tag tutorial (25): vertical alignment attribute VALIGN

In the vertical direction, you can set the row al...

Several ways to encapsulate axios in Vue

Table of contents Basic Edition Step 1: Configure...

Docker primary network port mapping configuration

Port Mapping Before the Docker container is start...

Detailed explanation of Linux text editor Vim

Vim is a powerful full-screen text editor and the...

Solve the problem that ElementUI custom CSS style does not take effect

For example, there is an input box <el-input r...

A brief discussion on the lock range of MySQL next-key lock

Preface One day, I was suddenly asked about MySQL...

Detailed explanation of MySQL redo log (redo log) and rollback log (undo logo)

Preface: The previous article described several c...

Vue shuttle box realizes up and down movement

This article example shares the specific code for...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

Native JavaScript to achieve slide effects

When we create a page, especially a homepage, we ...