Home  >  Article  >  WeChat Applet  >  Implement authorized login of WeChat applet

Implement authorized login of WeChat applet

coldplay.xixi
coldplay.xixiforward
2021-04-12 10:10:037396browse

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.net. If there is any infringement, please contact admin@php.cn delete