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

Swiper.js plugin makes it super easy to implement carousel images

Swiper is a sliding special effects plug-in built...

Detailed explanation of angular two-way binding

Table of contents Bidirectional binding principle...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...

Detailed explanation of the calculation method of flex-grow and flex-shrink in flex layout

Flex(彈性布局) in CSS can flexibly control the layout...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

CSS and HTML and front-end technology layer diagram

Front-end technology layer (The picture is a bit e...

XHTML Tutorial: XHTML Basics for Beginners

<br />This site’s original content, please i...

Linux unlink function and how to delete files

1. unlink function For hard links, unlink is used...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

jQuery implements Table paging effect

This article shares the specific code of jQuery t...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...