Table of Contents
1. Introduction to authorized login of Mini Program
2.Uniapp applet authorization login process
2.1 Obtain basic user information
2.2 Determine whether the user is logged in
2.3 Request the server to obtain the login status
2.4 Jump to the authorization page
2.5 Authorize login and obtain user information
3. Summary
Home Web Front-end uni-app Let's talk about the uniapp applet authorization login process

Let's talk about the uniapp applet authorization login process

Apr 23, 2023 pm 04:41 PM

With the popularization of mobile Internet, small programs have become an indispensable application in people's daily lives. As a developer, when implementing the functions of a mini program, it often involves authorized login in order to obtain user information and perform some necessary operations. This article will introduce you to the authorization login process of the uniapp applet.

1. Introduction to authorized login of Mini Program

Authorized login of Mini Program is one of the most common scenarios in Mini Program development, and it is also one of the important means to achieve interaction and user experience. Authorized login allows the mini program to obtain the user's avatar, nickname and other information, and provide more personalized services. Currently, there are two main ways to log in to mini programs: WeChat authorized login and mobile phone number authorized login. WeChat authorized login is the official login method provided by WeChat. Users can scan the QR code to log in through WeChat, or they can authorize the login directly in the mini program. Mobile phone number authorization login requires the user to enter the mobile phone number and verification code for verification, and then log in.

2.Uniapp applet authorization login process

The authorization login process of uniapp applet mainly involves the following steps:

2.1 Obtain basic user information

Using the built-in API of the uniapp framework, you can easily obtain the basic information of the user. You can obtain user-related information as follows:

uni.getUserInfo({
  success: function (res) {
    console.log(res.userInfo);
  }
});
Copy after login

This code simply implements the method of obtaining user information. Once the user authorizes the login, the applet You can easily obtain the user's nickname, avatar and other information.

2.2 Determine whether the user is logged in

Before performing some necessary operations, we need to ensure that the user has logged in to the mini program. At this time, we need to determine the user's login status. We can use local storage to determine whether the user is logged in.

//判断用户是否登录
function isLoggedIn() {
  const token = uni.getStorageSync('token');
  return token ? true : false;
}
Copy after login

2.3 Request the server to obtain the login status

If the user has not logged in or the login status has expired, then we need to request the server through the login interface to obtain the user's login status. While requesting the login interface, you also need to obtain the temporary login credential code through the login interface provided by uniapp.

//获取登录凭证code
uni.login({
  provider: 'weixin',
  success(loginRes) {
    const code = loginRes.code;
    //调用登录接口
    uni.request({
      url: '接口地址',
      data: {
        code: code,
      },
      success: function (res) {
        console.log('response from server', res);
        //将登录凭证存储到本地
        uni.setStorageSync('token', res.data.access_token);
      },
      fail: function (err) {
        console.log('err', err);
      }
    });
  },
  fail(err) {
    console.log('login fail', err);
  }
});
Copy after login

2.4 Jump to the authorization page

After determining whether the user has logged in, if it is found that the user has not logged in or the login status has expired, then we need to jump to the authorization page and proceed Authorized to log in.

//跳转到授权页面
function toAuthorizationPage(){
    uni.navigateTo({
        url: '/pages/authorize/index',
        success:function(res){
            console.log('navigate success',res);
        },
        fail:function(err){
            console.log('navigate fail',err);
        }
    })
}
Copy after login

2.5 Authorize login and obtain user information

After jumping to the authorization page, we can call the API provided by WeChat to allow the user to log in with WeChat authorization and obtain the user's authorization information. The specific steps to obtain user information are as follows:

  1. Use wx.getUserInfo to obtain the user’s basic information;
  2. After obtaining the user’s basic information, submit it Go to the server to register or update user information.
//获取用户信息
wx.getUserInfo({
    success:function(res){
        var userInfo = res.userInfo;
        //将用户信息提交到服务器进行注册或更新
        uni.request({
            url:'用户信息提交地址',
            method:"post",
            data:userInfo,
            success:function(res){
                console.log('response from server',res);
            },
            fail:function(err){
                console.log('err',err);
            }
        })
    },
    fail:function(err){
        console.log('get user info fail',err);
    }
})
Copy after login

3. Summary

Authorized login is a core function of Uniapp applet development and an important means to achieve user interaction and experience. The authorized login process introduced in this article can provide some reference for your mini program, making it easier for you to implement the authorized login function. Of course, due to the limited length of this article, there are still many details to consider in the authorized login function. It is recommended that developers continue to explore and practice in specific practices.

The above is the detailed content of Let's talk about the uniapp applet authorization login process. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I use uni-app's social sharing APIs? How do I use uni-app's social sharing APIs? Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use preprocessors (Sass, Less) with uni-app? How do I use preprocessors (Sass, Less) with uni-app? Mar 18, 2025 pm 12:20 PM

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

How do I use uni-app's animation API? How do I use uni-app's animation API? Mar 18, 2025 pm 12:21 PM

The article explains how to use uni-app's animation API, detailing steps to create and apply animations, key functions, and methods to combine and control animation timing.Character count: 159

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? Mar 18, 2025 pm 12:22 PM

The article explains how to use uni-app's storage APIs (uni.setStorage, uni.getStorage) for local data management, discusses best practices, troubleshooting, and highlights limitations and considerations for effective use.

What is the file structure of a uni-app project? What is the file structure of a uni-app project? Mar 14, 2025 pm 06:55 PM

The article details the file structure of a uni-app project, explaining key directories like common, components, pages, static, and uniCloud, and crucial files such as App.vue, main.js, manifest.json, pages.json, and uni.scss. It discusses how this o

What debugging tools are available for UniApp development? What debugging tools are available for UniApp development? Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

See all articles