轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序获取用户头像和昵称,顶部导航栏的实现

发布者: mzlihui | 发布时间: 2018-7-9 22:24| 查看数: 6521| 评论数: 1|帖子模式

作者:Fenchow,来自原文地址

一:顶部导航栏的实现

  1. <view class="swiper-tab">
  2. <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">11</view>
  3. <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">22</view>
  4. <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">33</view>
  5. <view class="swiper-tab-list {{currentTab==3 ? 'on' : ''}}" data-current="3" bindtap="swichNav">44</view>
  6. <view class="swiper-tab-list {{currentTab==4 ? 'on' : ''}}" data-current="4" bindtap="swichNav">55</view>
  7. </view>
  8. <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">
  9. <!-- 我是哈哈 -->
  10. <swiper-item>
  11. <view>我是哈哈</view>
  12. </swiper-item>
  13. <!-- 我是呵呵 -->
  14. <swiper-item>
  15. <view>我是呵呵</view>
  16. </swiper-item>
  17. <!-- 我是嘿嘿 -->
  18. <swiper-item>
  19. <view>我是嘿嘿</view>
  20. </swiper-item>
  21. <swiper-item>
  22. <view>我是嘿嘿</view>
  23. </swiper-item>
  24. <swiper-item>
  25. <view>我是嘿嘿</view>
  26. </swiper-item>
  27. </swiper>
  1. .swiper-tab{
  2. width: 100%;
  3. border-bottom: 2rpx solid #777777;
  4. text-align: center;
  5. line-height: 80rpx;
  6. }
  7. .swiper-tab-list{ font-size: 30rpx;
  8. display: inline-block;
  9. width: 20%;
  10. color: #777777;
  11. }
  12. .on{ color: #da7c0c;
  13. border-bottom: 5rpx solid #da7c0c;}
  14. .swiper-box{ display: block; height: 100%; width: 100%; overflow: hidden; }
  15. .swiper-box view{
  16. text-align: center;
  17. }
  1. var app = getApp()
  2. Page( {
  3. data: {
  4. /**
  5. * 页面配置
  6. */
  7. winWidth: 0,
  8. winHeight: 0,
  9. // tab切换
  10. currentTab: 0,
  11. },
  12. onLoad: function() {
  13. var that = this;
  14. /**
  15. * 获取系统信息
  16. */
  17. wx.getSystemInfo( {
  18. success: function( res ) {
  19. that.setData( {
  20. winWidth: res.windowWidth,
  21. winHeight: res.windowHeight
  22. });
  23. }
  24. });
  25. },
  26. /**
  27. * 滑动切换tab
  28. */
  29. bindChange: function( e ) {
  30. var that = this;
  31. that.setData( { currentTab: e.detail.current });
  32. },
  33. /**
  34. * 点击tab切换
  35. */
  36. swichNav: function( e ) {
  37. var that = this;
  38. if( this.data.currentTab === e.target.dataset.current ) {
  39. return false;
  40. } else {
  41. that.setData( {
  42. currentTab: e.target.dataset.current
  43. })
  44. }
  45. },
  46. /**
  47. * 点击分享
  48. */
  49. onShareAppMessage: function () {
  50. return {
  51. title: '装逼小程序',
  52. path: '/page/user?id=123'
  53. }
  54. }
  55. })

二:获取用户头像和昵称

获取用户头像和昵称: 

  1. <!--index.wxml-->
  2. <view class="container">
  3. <view bindtap="bindViewTap" class="userinfo">
  4. <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
  5. <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  6. </view>
  7. <view class="usermotto">
  8. <text class="user-motto">{{motto}}</text>
  9. </view>
  10. </view>
  1. /**index.wxss**/
  2. .userinfo {
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. }
  7. .userinfo-avatar {
  8. width: 128rpx;
  9. height: 128rpx;
  10. margin: 20rpx;
  11. border-radius: 50%;
  12. }
  13. .userinfo-nickname {
  14. color: #aaa;
  15. }
  16. .usermotto {
  17. margin-top: 200px;
  18. }
  1. //index.js
  2. //获取应用实例
  3. var app = getApp()
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {}
  8. },
  9. //事件处理函数
  10. bindViewTap: function() {
  11. wx.navigateTo({
  12. url: '../logs/logs'
  13. })
  14. },
  15. onLoad: function () {
  16. console.log('onLoad')
  17. var that = this
  18. //调用应用实例的方法获取全局数据
  19. app.getUserInfo(function(userInfo){
  20. //更新数据
  21. that.setData({
  22. userInfo:userInfo
  23. })
  24. })
  25. }
  26. })

调用登陆接口 app.js

  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. //调用API从本地缓存中获取数据
  5. // var logs = wx.getStorageSync('logs') || []
  6. // logs.unshift(Date.now())
  7. // wx.setStorageSync('logs', logs)
  8. },
  9. getUserInfo:function(cb){
  10. var that = this;
  11. if(this.globalData.userInfo){
  12. typeof cb == "function" && cb(this.globalData.userInfo)
  13. }else{
  14. //调用登录接口
  15. wx.login({
  16. success: function () {
  17. wx.getUserInfo({
  18. success: function (res) {
  19. that.globalData.userInfo = res.userInfo;
  20. typeof cb == "function" && cb(that.globalData.userInfo)
  21. }
  22. })
  23. }
  24. });
  25. }
  26. },
  27. globalData:{
  28. userInfo:null
  29. }
  30. })

最新评论

波妞. 发表于 2022-7-5 10:05
学习代码的网站

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表