[TOC]
## 1.用戶登錄 不需要用戶授權
```
<open-data type="userAvatarUrl" class="logo"></open-data>
```
## 2.需授權
```
<v-btn class="image-btn" openType="getUserInfo" bind:onGet="onGetUserInfo" wx:if="{{!authorized}}">
<image slot="image" src="/images/weixin.png" />
</v-btn>
<image src="{{authorized?userInfo.avatarUrl:'/images/weixin.png'}}" class="logo" wx:if="{{authorized}}" />
```
### 2.1 js
```
Page({
/**
* 頁面的初始數據
*/
data: {
userInfo: '',
authorized: false,
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
/* 得到用戶的相關信息 */
this._authorized()
},
//v-btn綁定事件
onGetUserInfo(event) {
let userInfo = event.detail.userInfo;
this.setData({
userInfo,
authorized: true
})
},
/* 判斷用戶是否授權 */
_authorized() {
//獲取用戶的當前設置。返回值中只會出現小程序已經向用戶請求過的權限。3.6
wx.getSetting({
success: (res) => {
console.log(res)
/* scope.userInfo判斷用戶是否授權依據 */
if (res.authSetting['scope.userInfo']) {
/* 獲取用戶信息 // 必須是在用戶已經授權的情況下調用*/
wx.getUserInfo({
success: (res) => {
this.setData({
userInfo: res.userInfo,
authorized: true
})
}
})
}
}
})
}
})
```
### 2.2v-btn (組件image-button)
```
<button plain="{{true}}" open-type="{{openType}}" bindgetuserinfo="onGetUserInfo">
<!-- plain="{{true}}" 設置成透明-->
<slot name="image"></slot>
</button>
```
### 2.3 image-button的js
```
properties: {
openType:String
},
options:{
multipleSlots: true // 在組件定義時的選項中啟用多slot支持
},
/**
* 組件的方法列表
*/
methods: {
//得到用戶信息
onGetUserInfo(event){
const userInfo = event.detail.userInfo;
this.triggerEvent('onGet',{
userInfo
})
}
}
```
- 開發環境及接口
- 0.豆瓣接口
- 1.開發環境配置
- 2.一些相關文檔
- 小程序實例效果
- 第0節、TodoList
- 第一節、豆瓣相關
- 1、tabBar的配置及導航加標題
- 2、數據加載及下拉加載
- 3、加載相關
- 4、輪播
- 5、星星評分
- 第二節、音樂播放相關
- 1.點擊收藏分享
- 2.音樂播放
- 初始版
- 組件版
- 組件加強版
- 3.點贊
- 點贊初級版
- 點贊第二版
- 5.左右按鈕
- 6.緩存
- 第三節、補充
- 地圖
- 點擊拍照換圖
- 掃一掃
- 小程序語法
- 第一節 、HTTP的封裝
- 0.http請求
- 1.function封裝
- 2.class封裝http
- 3.promise封裝
- 4.config地址
- 第二節、組件
- 2.組件單獨設置樣式
- 3.一些有意義的標簽
- 4.behavior
- 5.SLOT
- 6.左右按鈕
- 5.點贊組件
- 6.用戶授權
- 圖片按鈕 如分享
- 第三節、api
- 1.頁面跳轉
- 獲取input里的值
- 1.添加評論
- 2.搜索框
- 3. 獲取input里的值
- 2.設置緩存
- 3.模態框,彈出框
- 4.分享showActionSheet
- 5.定義全局的數據
- 2. 基礎知識
- 1.setData
- 2.文件結構
- 3.wxml語法
- 第一節 數據綁定
- 第二節 列表渲染
- 第三節 條件渲染
- 第四節 模板
- 第五節 事件
- 第六節 引用
- 4.wxs
- 1.文本縮進問題
- 5.小程序中遇到的wxss 問題
- 1.width100%越界問題
- 廢棄的文件
- 一個完整的小程序
- 1.啟動頁面
- 2.yuedu輪播+封裝及數據調用
- yuedu的詳情頁
- 3.電影
- movie-more
- web-view