微信小程序的授權(quán)實(shí)現(xiàn)過程解析
自從小程序文檔更新后,自動(dòng)授權(quán)已不存在啦
目前的授權(quán)都是通過button來實(shí)現(xiàn)的,具體知識(shí)點(diǎn)可參考小程序的官方文檔,以下是我做的一個(gè)小demo(進(jìn)入首頁,跳出一個(gè)登錄彈出框,彈出框是自己寫的一個(gè)UI組件),廢話不多說,直接上代碼
UI組件部分(modal)
modal.wxml
<view class='modal-mask' wx:if='{{show}}' bindtap='clickMask'>
<view class='modal-content'>
<scroll-view scroll-y class='main-content'>
<slot></slot>
</scroll-view>
</view>
</view>
modal.wxss
n: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 999;
}
/*遮罩內(nèi)容*/
.modal-content{
display: flex;
flex-direction: column;
width: 65%;
background-color: #fff;
border-radius: 10rpx;
padding: 20rpx;
text-align: center;
}
/*中間內(nèi)容*/
.main-content{
flex: 1;
height: 100%;
overflow-y: hidden;
max-height: 80vh; /* 內(nèi)容高度最高80vh 以免內(nèi)容太多溢出*/
}
.bottom {
border-radius: 80rpx;
margin: 70rpx 50rpx;
font-size: 35rpx;
}
modal.js
Component({
/**
* 組件的屬性列表
*/
properties: {
//是否顯示modal彈窗
show: {
type: Boolean,
value: false
},
//控制底部是一個(gè)按鈕還是兩個(gè)按鈕,默認(rèn)兩個(gè)
single: {
type: Boolean,
value: false
}
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
},
/**
* 組件的方法列表
*/
methods: {
// 點(diǎn)擊modal的回調(diào)函數(shù)
clickMask() {
// 點(diǎn)擊modal背景關(guān)閉遮罩層,如果不需要注釋掉即可
this.setData({ show: false })
},
// 點(diǎn)擊取消按鈕的回調(diào)函數(shù)
cancel() {
this.setData({ show: false })
this.triggerEvent('cancel') //triggerEvent觸發(fā)事件
},
// 點(diǎn)擊確定按鈕的回調(diào)函數(shù)
confirm() {
this.setData({ show: false })
this.triggerEvent('confirm')
}
}
})
modal.json
{
"component": true,
"usingComponents": {}
}
pages頁面
home.wxml(這個(gè)是彈框,home頁面內(nèi)容直接在下面加一個(gè)<view>這里寫home頁面的內(nèi)容</view>)
<!-- modal彈窗-->
<modalView show="{{showModal}}" bindcancel="modalCancel" bindconfirm='modalConfirm' single='{{single}}'>
<view class='modal-content'>
<scroll-view scroll-y class='main-content'>
<view wx:if="{{canIUse}}" >
<view class='header'>
<text>提示</text>
</view>
<view class='content'>
<image src="/images/goods_img2.png"></image>
<text>是否登錄并繼續(xù)使用該程序</text>
</view>
<view class="header_title">
<text class="dian">•</text>
<text>登錄程序需進(jìn)行微信授權(quán)</text>
</view>
<view class="modal_footer">
<view class="bottom">
<button class='bottom_a'>
拒絕
</button>
<button class='bottom_b' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
去登錄
</button>
</view>
</view>
</view>
</scroll-view>
</view>
</modalView>
home.wxss
.header {
text-align: start;
height: 100rpx;
line-height: 100rpx;
}
.header image {
width: 200rpx;
height: 200rpx;
}
.content {
display: flex;
margin-left: 50rpx;
height: 100rpx;
line-height: 100rpx;
}
.content image{
width: 100rpx;
height: 100rpx;
}
.content text {
font-size: 24rpx;
margin-left: 20rpx;
}
.header_title{
margin-left: 50rpx;
text-align: start;
font-size: 24rpx;
color: #ccc;
line-height: 100rpx;
display: flex;
}
.dian{
margin-right: 6rpx;
font-size: 36rpx;
}
.modal_footer{
display: flex;
justify-content: flex-end;
}
.bottom {
display: flex;
color: #ccc;
font-size: 24rpx;
width: 280rpx;
}
button::after {
border: none;
}
.bottom button{
background-color: #fff;
height: 50rpx;
line-height: 50rpx;
}
.bottom_a{
font-size: 24rpx;
}
.bottom_b{
font-size: 28rpx;
color: #0db95a;
}
home.js
//home.js
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo'),
showModal: true,
single: false
},
onLoad:function(){
var that = this;
// 查看是否授權(quán)
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: function (res) {
wx.login({
success: res => {
console.log("用戶的code:" + res.code);
}
});
}
});
} else {
that.setData({
showModal: true
});
}
}
});
},
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
//用戶按了允許授權(quán)按鈕
var that = this;
// 獲取到用戶的信息了,打印到控制臺(tái)上看下
console.log("用戶的信息如下:");
console.log(e.detail.userInfo);
//授權(quán)成功后,通過改變 showModal的值,讓實(shí)現(xiàn)頁面顯示出來,把授權(quán)頁面隱藏起來
that.setData({
showModal: false
});
} else {
var that = this;
//用戶按了拒絕按鈕
wx.showModal({
title: '警告',
content: '您點(diǎn)擊了拒絕授權(quán),將無法獲取你的信息!!!',
showCancel: false,
confirmText: '返回授權(quán)',
success: function (res) {
// 用戶沒有授權(quán)成功,不需要改變 isHide 的值
if (res.confirm) {
that.setData({
showModal: true
});
}
}
});
}
}
})
home.json
{
"usingComponents": {
"modalView": "../../components/modal/modal"
}
}
好啦~這是全部代碼,效果如下(點(diǎn)擊登錄可進(jìn)行授權(quán))

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
多個(gè)js毫秒倒計(jì)時(shí)同時(shí)進(jìn)行效果
這篇文章主要以實(shí)例的方式講解多個(gè)js毫秒倒計(jì)時(shí)同時(shí)進(jìn)行效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
JavaScript使用arcgis實(shí)現(xiàn)截圖截屏功能
這篇文章主要為大家詳細(xì)介紹了JavaScript如何使用arcgis實(shí)現(xiàn)截圖截屏功能,類似于qq截圖,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01
使用JavaScript實(shí)現(xiàn)LRU緩存的代碼詳解
LRU(Least?Recently?Used)算法是一種廣泛應(yīng)用于內(nèi)存管理和緩存系統(tǒng)的策略,本文將介紹LRU算法的基本原理,并通過JavaScript實(shí)現(xiàn)案例,幫助讀者理解其在前端開發(fā)中的應(yīng)用場景,需要的朋友可以參考下2024-05-05
使用JavaScript實(shí)現(xiàn)隨機(jī)曲線之間進(jìn)行平滑切換
今天,我運(yùn)用拉格朗日插值法繪制了一條曲線,然而,我并未止步于靜態(tài)展示,而是引入了一個(gè)定時(shí)器,每隔一段時(shí)間便對(duì)曲線上的點(diǎn)進(jìn)行動(dòng)態(tài)更新,從而賦予曲線生命般的動(dòng)態(tài)變化,本文介紹了使用JavaScript實(shí)現(xiàn)隨機(jī)曲線之間進(jìn)行平滑切換,感興趣的朋友可以參考下2024-11-11
微信小程序?qū)崿F(xiàn)2048小游戲的詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)2048小游戲的相關(guān)資料,文中將實(shí)現(xiàn)的代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
小程序頁面動(dòng)態(tài)配置實(shí)現(xiàn)方法
這篇文章主要介紹了小程序頁面動(dòng)態(tài)配置實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

