search
HomeWeChat AppletMini Program DevelopmentImplement authorized login of WeChat applet

Implement authorized login of WeChat applet

Apr 12, 2021 am 10:10 AM
WeChat appletAuthorized login

Implement authorized login of WeChat applet

Foreword: Since WeChat has officially modified the getUserInfo interface, it is now impossible to pop up the authorization window as soon as you enter the WeChat applet. You can only trigger it through the button

.

1. Implementation ideas

Write a WeChat authorization login page to allow users to click on it, which means triggering the getUserInof interface through the button component. When the user enters the WeChat applet, it is determined whether the user is authorized. If not, the first picture of the "Interface Introduction" below is displayed to allow the user to perform the authorized operation. If

is already authorized, skip this page directly and go to the homepage.

Related free learning recommendations:

WeChat Mini Program Development

2. Interface Introduction

3. Source code

login.wxml

<view wx:if="{{canIUse}}">
    <view class=&#39;header&#39;>
        <image src=&#39;/images/wx_login.png&#39;></image>
    </view>
 
    <view class=&#39;content&#39;>
        <view>申请获取以下权限</view>
        <text>获得你的公开信息(昵称,头像等)</text>
    </view>
 
    <button class=&#39;bottom&#39; type=&#39;primary&#39; open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
        授权登录
    </button>
</view>
 
<view wx:else>请升级微信版本</view>

login.wcss

.header {
    margin: 90rpx 0 90rpx 50rpx;
    border-bottom: 1px solid #ccc;
    text-align: center;
    width: 650rpx;
    height: 300rpx;
    line-height: 450rpx;
}
 
.header image {
    width: 200rpx;
    height: 200rpx;
}
 
.content {
    margin-left: 50rpx;
    margin-bottom: 90rpx;
}
 
.content text {
    display: block;
    color: #9d9d9d;
    margin-top: 40rpx;
}
 
.bottom {
    border-radius: 80rpx;
    margin: 70rpx 50rpx;
    font-size: 35rpx;
}

login.json

{

"navigationBarTitleText": "授权登录"

}

login.js

The wx.request of the code is some interaction between my project and the background, which can be deleted directly.

Things that need to be modified:

Remember to fill in the url attribute in the wx.switchTab interface. This is the page path to jump to after successful authorization. Since my homepage is a tarBar page, so Use

wx.switchTab here. If it is not a tarBar page, you can use wx.navigateTo and wx.reirecTo to jump

Page({
    data: {
        //判断小程序的API,回调,参数,组件等是否在当前版本可用。
        canIUse: wx.canIUse(&#39;button.open-type.getUserInfo&#39;)
    },
    onLoad: function () {
        var that = this;
        // 查看是否授权
        wx.getSetting({
            success: function (res) {
                if (res.authSetting[&#39;scope.userInfo&#39;]) {
                    wx.getUserInfo({
                        success: function (res) {
                            //从数据库获取用户信息
                            that.queryUsreInfo();
                            //用户已经授权过
                            wx.switchTab({
                                url: &#39;&#39;
                            })
                        }
                    });
                }
            }
        })
    },
    bindGetUserInfo: function (e) {
        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this;
            //插入登录的用户的相关信息到数据库
            wx.request({
                url: getApp().globalData.urlPath + &#39;hstc_interface/insert_user&#39;,
                data: {
                    openid: getApp().globalData.openid,
                    nickName: e.detail.userInfo.nickName,
                    avatarUrl: e.detail.userInfo.avatarUrl,
                    province:e.detail.userInfo.province,
                    city: e.detail.userInfo.city
                },
                header: {
                    &#39;content-type&#39;: &#39;application/json&#39;
                },
                success: function (res) {
                    //从数据库获取用户信息
                    that.queryUsreInfo();
                    console.log("插入小程序登录用户信息成功!");
                }
            });
            //授权成功后,跳转进入小程序首页
            wx.switchTab({
                url: &#39;&#39;  
            })
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title:&#39;警告&#39;,
                content:&#39;您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!&#39;,
                showCancel:false,
                confirmText:&#39;返回授权&#39;,
                success:function(res){
                    if (res.confirm) {
                        console.log(&#39;用户点击了“返回授权”&#39;)
                    } 
                }
            })
        }
    },
    //获取用户信息接口
    queryUsreInfo: function () {
        wx.request({
            url: getApp().globalData.urlPath + &#39;hstc_interface/queryByOpenid&#39;,
            data: {
                openid: getApp().globalData.openid
            },
            header: {
                &#39;content-type&#39;: &#39;application/json&#39;
            },
            success: function (res) {
                console.log(res.data);
                getApp().globalData.userInfo = res.data;
            }
        });
    },
    
})

Related free learning recommendations:

WeChat Mini Program Development Tutorial

The above is the detailed content of Implement authorized login of WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment