瀏覽代碼

增加授权

master
wuwenjie 5 年之前
父節點
當前提交
9183393a6e
共有 2 個文件被更改,包括 44 次插入3 次删除
  1. +40
    -1
      pages/record/record.js
  2. +4
    -2
      pages/record/record.wxml

+ 40
- 1
pages/record/record.js 查看文件

@@ -1,10 +1,13 @@
// pages/record/record.js
const app = getApp()
Page({

/**
* 页面的初始数据
*/
data: {
userInfo: {},
canIUse: wx.canIUse('button.open-type.getUserInfo'),
userName: '老门框内部操作',
userNo: '000001', //员工编号
showIndex: 1,
@@ -78,7 +81,43 @@ Page({
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {

if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
console.log(app.globalData.userInfo)
},
/**
* 获取头像昵称
*/
getUserInfo: function (e) {
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},

/**

+ 4
- 2
pages/record/record.wxml 查看文件

@@ -3,9 +3,11 @@
<image class="bg" src="../../static/record/bg.jpg"></image>
<view class="content">
<view class="headBox">
<image src="../../static/record/head.png" mode="aspectFill"></image>
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo" style="width:100%;height:100%;font-size:36rpx;line-height:200rpx;"> 获取头像 </button>
<image wx:else src="{{userInfo.avatarUrl}}" mode="aspectFill"></image>
</view>
<view class="userName">{{userName}}</view>
<view class="userName" wx:if="{{userInfo}}">{{userInfo.nickName}}</view>
<view class="userName" wx:else>用户昵称</view>
<view class="userNo">{{userNo}}</view>
<scroll-view id="collapseBox" scroll-y="true" class="collapseBox">
<view class="item">

Loading…
取消
儲存