小程序受權登陸

1、前言
因爲微信官方修改了 getUserInfo 接口,因此如今沒法實現一進入微信小程序就彈出受權窗口,只能經過 button 去觸發。官方鏈接:https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01css

2、實現思路
本身寫一個微信受權登陸頁面讓用戶實現點擊的功能,也就是實現了經過 button 組件去觸發 getUserInof 接口。在用戶進入微信小程序的時候,判斷用戶是否受權了,若是沒有受權的話就顯示受權頁面,讓用戶去執行受權的操做。若是已經受權了,則直接跳過這個頁面,進入首頁。git

3、界面簡介

1.不帶 tabBar

2.帶 tabBar

4、源碼

1.index.wxml

<view wx:if="{{isHide}}">
    <view wx:if="{{canIUse}}" >
        <view class='header'>
            <image src='/images/wx_login.png'></image>
        </view>
 
        <view class='content'>
            <view>申請獲取如下權限</view>
            <text>得到你的公開信息(暱稱,頭像等)</text>
        </view>
 
        <button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
            受權登陸
        </button>
    </view>
    <view wx:else>請升級微信版本</view>
</view>
 
<view wx:else>
    <view>個人首頁內容</view>
</view>

2.index.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;
}

3.index.js

Page({
    data: {
        //判斷小程序的API,回調,參數,組件等是否在當前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isHide: false
    },
 
    onLoad: function() {
        var that = this;
        // 查看是否受權
        wx.getSetting({
            success: function(res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function(res) {
                            // 用戶已經受權過,不須要顯示受權頁面,因此不須要改變 isHide 的值
                            // 根據本身的需求有其餘操做再補充
                            // 我這裏實現的是在用戶受權成功後,調用微信的 wx.login 接口,從而獲取code
                            wx.login({
                                success: res => {
                                    // 獲取到用戶的 code 以後:res.code
                                    cosole.log("用戶的code:" + res.code);
                                    // 能夠傳給後臺,再通過解析獲取用戶的 openid
                                    // 或者能夠直接使用微信的提供的接口直接獲取 openid ,方法以下:
                                    // wx.request({
                                    //     // 自行補上本身的 APPID 和 SECRET
                                    //     url: 'https://api.weixin.qq.com/sns/jscode2session?appid=本身的APPID&secret=本身的SECRET&js_code=' + res.code + '&grant_type=authorization_code',
                                    //     success: res => {
                                    //         // 獲取到用戶的 openid
                                    //         console.log("用戶的openid:" + res.data.openid);
                                    //     }
                                    // });
                                }
                            });
                        }
                    });
                } else {
                    // 用戶沒有受權
                    // 改變 isHide 的值,顯示受權頁面
                    that.setData({
                        isHide: true
                    });
                }
            }
        });
    },
 
    bindGetUserInfo: function(e) {
        if (e.detail.userInfo) {
            //用戶按了容許受權按鈕
            var that = this;
            // 獲取到用戶的信息了,打印到控制檯上看下
            console.log("用戶的信息以下:");
            console.log(e.detail.userInfo);
            //受權成功後,經過改變 isHide 的值,讓實現頁面顯示出來,把受權頁面隱藏起來
            that.setData({
                isHide: false
            });
        } else {
            //用戶按了拒絕按鈕
            wx.showModal({
                title: '警告',
                content: '您點擊了拒絕受權,將沒法進入小程序,請受權以後再進入!!!',
                showCancel: false,
                confirmText: '返回受權',
                success: function(res) {
                    // 用戶沒有受權成功,不須要改變 isHide 的值
                    if (res.confirm) {
                        console.log('用戶點擊了「返回受權」');
                    }
                }
            });
        }
    }
})

關於 TabBar 的處理,只須要把上面寫好的頁面設置到 app.json 裏面便可。github

4.github 下載

https://github.com/yyzheng1729/loginDemojson

相關文章
相關標籤/搜索