微信小程序:“我的”页面布局(一):微信用户信息获取及UI

新项目挖新坑,公司又需要做新的小程序,没有其他人支持,这次还得前后台一起做...不懂前端的我又来挖坑了...

先做小程序“我的”页面,需要注意的有两个点:

  1. 考虑到前几个版本getUserInfo不再弹出授权窗口,所以增加了不可见的button来唤起授权窗口;
  2. "我的"页面功能列表虽然现在不多,但是指不定什么时候就要增加或者修改,所以将该列表做成可配置的,使用了wx:for和navigator。

先上图,有图才有真相:

微信小程序--“我的“页面.jpg

一、 用户信息获取

这一块我增加了一个隐藏的button, 未获取到用户信息时,显示为默认头像和“微信授权”的文字提示,登陆后,头像变为微信头像,文字变为微信昵称。

<button class="login-button head-height" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>

具体ui为判断没有授权则显示默认头像及提示,并在其上层覆盖一个透明的button,用户触发授权事件。

wxml:
  <view class='head head-height'>
    <block wx:if="{{!hasUserInfo && canIUse}}">
      <view class="userinfo">
        <image class="userinfo-avatar" src="../../images/icon-mine.png" mode="cover"></image>
        <text class="userinfo-nickname">微信授权</text>
      </view>
      <button class="login-button head-height" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
    </block>
    <block wx:else>
      <view class="userinfo">
        <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
        <text class="userinfo-nickname">{{userInfo.nickName}}</text>
      </view>
    </block>
  </view>
wxss:
.head-height {
  height: 240rpx;
}

.head {
  display:flex;
  width: 100%;
  background-color: #15c261;
  align-items: center;
}

.login-button {
  opacity: 0; 
  width: 100%;
  position: fixed;
}

.userinfo {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 100%;
  position: fixed;
}

.userinfo-avatar {
  width: 150rpx;
  height: 150rpx;
  margin: 30rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #333333;
  font-size: 42rpx;
}
js:

在js中需要注意的是,open-type="getUserInfo"需要做老版本兼容,老版本通过wx.getUserInfo即可唤起授权页面,不需要添加button去触发

//获取应用实例
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.userInfo) {
      that.setUserInfo(app.globalData.userInfo);
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setUserInfo(res.userInfo);
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          that.setUserInfo(res.userInfo);
        }
      })
    }
  },

  getUserInfo: function (e) {
    this.setUserInfo(e.detail.userInfo);
  },

  setUserInfo: function (userInfo) {
    if (userInfo != null) {
      app.globalData.userInfo = userInfo
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
    }
  }
})

二、 功能菜单列表

详情见下一篇:微信小程序:“我的”页面布局(二):可配置功能菜单列表

个人博客: IT老五
微信公众号:【IT老五(it-lao5)】,一起源创,一起学习!

相关推荐:

微信小程序:“我的”页面布局(一):微信用户信息获取及UI
微信小程序:“我的”页面布局(二):可配置功能菜单列表

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.getUserInfo接口。我们发现几乎所有的小程...
    未央大佬阅读 15,593评论 0 23
  • 背景 ✦✦01✦✦ 小程序一个比较重要的能力就是获取用户信息,也就是使用wx.getUserInfo接口。我们发现...
    连胜老师阅读 2,019评论 0 2
  • 什么是微信小程序 腾讯公司于2016年9月21日开始微信小程序内测,一时间小程序的讨论热度不断。网络上流传一张张小...
    centuryscr阅读 5,702评论 0 3
  • 小程序面试题 小程序授权登录流程 0、如何获得用户信息...
    X秀秀阅读 1,855评论 0 8
  • 最近诸事不顺,包括工作,生活等等。又狠狠的被生活虐了一把。不只是我,我女朋友也这样,工作压力巨大,影响了睡眠,生活...
    南山依旧阅读 235评论 0 0