参考链接:
(1)微信小程序学习笔记(八)----用户个人中心界面
https://blog.csdn.net/qq_24127447/article/details/84450861
(2)微信小程序实战篇-个人中心
https://blog.csdn.net/u012927188/article/details/76684951
(3)基于java的微信小程序的实现(五)用户个人信息小程序端开发
https://blog.csdn.net/qq_36258498/article/details/84640534

一、例子1:用户个人中心界面

效果图
在这里插入图片描述
第一,准备图片素材
从阿里矢量图上找的图片,下面附上网站:http://www.iconfont.cn/collections/detail?cid=29,找到自己需要的矢量图下载png格式就可以了。

第二,目录结构
在这里插入图片描述
第三,代码实现
wxml文件

<!--pages/user/user.wxml-->
<view class="mine-wrapper">
  <view class="avatar-wrapper">
    <view>
      <view class="avatar">
        <image style="border-radius:50%;" src="{{userinfo.avatarUrl ? userinfo.avatarUrl:'/images/user/user1.png'}}"></image>
      </view>
      <view class="text">
        <text wx:if="{{userinfo.nickName}}">{{userinfo.nickName}}</text>
        <text wx:else bindtap="toLogin">注册 / 登录</text>
      </view>
      <view class="text">
        <text wx:if="{{userSign==2}}">{{"您还没有填写真实信息,请填写"}}</text>
        <text wx:elif="{{userSign==1}}">{{"会员用户"}}</text>
      </view>
    </view>
  </view>
  <view class="list-wrapper">
    <view class="alllists">
      <block wx:for="{{menuitems}}" wx:key="menu_for">
        <navigator url="{{item.url}}" class="lists">
        <view class='content'>
          <view class="listimg">
            <image src="{{item.icon}}"></image>
          </view>
          <view class="listtext">{{item.text}}</view>
          <view class="listicon">{{item.tips}}</view>
          <view class="arrows">
            <image src="{{item.arrows}}"></image>
          </view>
        </view>
        </navigator>
        <!-- 分割线 -->
        <view class="divLine"></view>
      </block>
    </view>
  </view>
</view>

wxss文件

/* pages/user/user.wxss */
.avatar-wrapper {
  background: #1b82d1;
  padding: 25px 0;
}
.avatar-wrapper .avatar {
  margin: 0 auto;
  text-align: center;
}
.avatar-wrapper .avatar image {
  width: 100px;
  height: 100px;
}
.avatar-wrapper .text {
  text-align: center;
  color: #fff;
}
.listimg image {
  margin-right: 5px;
  vertical-align: middle;
  width: 20px;
  height: 20px;
}
.content{
  display: flex;
  margin-top: 10px;
  margin-bottom: 10px;
}
/*分割线样式*/
.divLine{
 background: #E0E3DA;
 width: 100%;
 height: 5rpx;
}
.arrows image {
  vertical-align: middle;
  width: 15px;
  height: 15px;
  margin-left: 500rpx;
}
/* user.wxss */

js文件

// pages/user/user.js
var _app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    menuitems: [
      { text: '个人资料', url: '#', icon: '/images/user/user1.png', tips: '', arrows: '/images/user/arrows.png' },
      { text: '邀请好友', url: '#', icon: '/images/user/user2.png', tips: '', arrows: '/images/user/arrows.png' },
      { text: '积分兑换', url: '#', icon: '/images/user/user3.png', tips: '', arrows: '/images/user/arrows.png' },
      { text: '帮助说明', url: '#', icon: '/images/user/user4.png', tips: '', arrows: '/images/user/arrows.png' }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})

json文件

暂无

二、例子2:个人中心

效果图
在这里插入图片描述

wxml文件

<!--pages/mine/mine.wxml-->
<view class="container">
  <view class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    <image src="../../images/person/account_bg.png" class="account-bg">
    </image>
  </view>
  <view class="separate"></view>
  <view class="order" catchtap="toOrder">
    <text class="myorder-text">我的订单</text>
    <text class="myorderlook-text">查看全部订单</text>
    <image class="next-image" src="../../images/person/next.png"></image>
  </view>
  <view class="line"></view>
  <view class="navs">
    <block wx:for-items="{{orderItems}}" wx:key="name">
      <view class="nav-item" catchtap="toOrder" data-type="{{item.name}}" data-typeid="{{item.typeId}}">
        <image src="{{item.imageurl}}" class="nav-image" />
        <text>{{item.name}}</text>
      </view>
    </block>
  </view>
  <view class="separate"></view>
  <view class="person-list">
    <view class="list-item">
      <image class="item-image" src="../../images/person/personal_favorite.png"></image>
      <text class="item-text">我的收藏</text>
    </view>
    <view class="person-line"></view>
    <view class="list-item">
      <image class="item-image" src="../../images/person/personal_site.png"></image>
      <text class="item-text">收货地址</text>
    </view>
    <view class="person-line"></view>
    <view class="list-item">
      <image class="item-image" src="../../images/person/personal_sale_record.png"></image>
      <text class="item-text">售后记录</text>
    </view>
    <view class="person-line"></view>
    <view class="list-item">
      <image class="item-image" src="../../images/person/personal_evaluated.png"></image>
      <text class="item-text">我的评价</text>
    </view>
    <view class="person-line"></view>
    <view class="list-item">
      <image class="item-image" src="../../images/person/personal_share.png"></image>
      <text class="item-text">推广邀请</text>
    </view>
  </view>
  <view class="separate"></view>
</view>

wxss文件(样式列表重点讲解一下 userinfo-avatar 类中的 border-radius 属性,大家看一下效果就知道他的作用,没错设置圆角图片,正常我们都是方方正正的图片,有了这个属性,可以轻松实现圆角图片了)

/* pages/mine/mine.wxss */
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #f0145a;
}
.account-bg {
  width: 100%;
  height: 150rpx;
}
.userinfo-avatar {
  width: 108rpx;
  height: 108rpx;
  margin: 20rpx;
  border-radius: 50%;
}
.userinfo-nickname {
  color: #fff;
}
/* 订单 */
.order {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 100%;
  height: 90rpx;
}
.myorder-text {
  font-size: 30rpx;
  color: gray;
  margin: 20rpx;
  width: 40%;
}
.myorderlook-text {
  font-size: 30rpx;
  color: gray;
  position: relative;
  right: 20rpx;
  width: 60%;
  text-align: right;
}
.next-image {
  width: 20rpx;
  height: 25rpx;
  position: relative;
  right: 10rpx;
}
.navs {
  display: flex;
}
.nav-item {
  width: 25%;
  display: flex;
  align-items: center;
  flex-direction: column;
  padding: 20rpx;
}
.nav-item .nav-image {
  width: 40rpx;
  height: 40rpx;
  margin: 5rpx;
}
.nav-item text {
  margin-top: 20rpx;
  font-size: 25rpx;
  color: gray;
}
/* 列表 */
.person-list {
  display: flex;
  flex-direction: column;
  align-items: left;
}
.list-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  height: 80rpx;
}
.item-image {
  width: 40rpx;
  height: 40rpx;
  margin: 20rpx;
}
.item-text {
  color: gray;
  font-size: 25rpx;
  margin-left: 20rpx;
}
.person-line {
  width: 100%;
  height: 2rpx;
  background: lightgray;
  margin-left: 90rpx;
}

js文件

// pages/mine/mine.js
var app = getApp()
Page({
  data: {
    userInfo: {},
    motto: 'Hello World',
    // orderItems
    orderItems: [
      {
        typeId: 0,
        name: '待付款',
        url: 'bill',
        imageurl: '../../images/person/personal_pay.png',
      },
      {
        typeId: 1,
        name: '待发货',
        url: 'bill',
        imageurl: '../../images/person/personal_shipped.png',
      },
      {
        typeId: 2,
        name: '待收货',
        url: 'bill',
        imageurl: '../../images/person/personal_receipt.png'
      },
      {
        typeId: 3,
        name: '待评价',
        url: 'bill',
        imageurl: '../../images/person/personal_comment.png'
      }
    ],
  },
  //事件处理函数
  //toOrder :事件监听,跳转到我的订单界面;onLoad:在加载过程中,获取用户的信息
  toOrder: function () {
    wx.navigateTo({
      url: '../order/order'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  }
})

json文件

暂无

order.js(我的订单制作)
我的订单,其实界面的实现原理和首页的导航栏是一样的,只不过换了一下内容以及导航栏标题罢了,所以这里我就不细讲了,不懂的朋友可以看我首页导航栏是怎么制作的,模仿写出来就好。

// pages/order/order.js
var Zan = require('../../template/contract.js');
Page(Object.assign({}, Zan.Tab, {
  data: {
    tab1: {
      list: [{
        id: 0,
        title: '全部'
      }, {
        id: 1,
        title: '待付款'
      }, {
        id: 2,
        title: '待发货'
      }, {
        id: 3,
        title: '待收货'
      }, {
        id: 4,
        title: '待评价'
      }],
      selectedId: 0,
      scroll: false,
    },
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    this.setData({
      `${componentId}.selectedId`: selectedId
    });
  }
}));

order.wxml

<!--pages/order/order.wxml-->
<import src="/template/tab/tab.wxml" />
 <view class="tab">
    <template is="zan-tab" data="{{tab: tab1, componentId: 'tab1'}}"></template>
  </view>
<view class="tab-content1" >
<image class="tab-image" src="../../images/order/non_cart.png"></image>
<text>您还没有相关订单哦!</text>
</view>

order.wxss

/* pages/order/order.wxss */
.tab-content1
{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tab-image
{
  width: 50%;
  margin-top: 130rpx;
  margin-bottom: 50rpx;
}

友情链接:微信小程序实战篇-购物车 https://www.jianshu.com/p/a6cb3940f92f

三、例子3:用户个人信息小程序端

效果图
在这里插入图片描述
1、显示个人信息功能
【需求分析】在该页面首先需要在加载完毕后去调用后端的查找个人信息的接口,并将返回的值回显到个人信息页面上。

wxml文件

  <view class='container'>
    <label class='nickname'>{{nickname}}</label>
    <view class='container-row'>
          <label class='info-items'>{{fansCounts}} 粉丝</label>
          <label class='info-items'>{{followCounts}} 关注</label>
          <label class='info-items'>{{receiveLikeCounts}} 获赞</label>
    	</view>
 	 </view>
	</view>
</view>

wxss文件

暂无

js文件

onLoad:function(params){
    var me=this;
    // var userInfo=app.userInfo;
    //从缓存中获取到用户对象
    var userInfo = app.getGlobalUserInfo("userInfo");
    var serverUrl = app.serverUrl;
    //获取当前登陆者的id
    var userId=userInfo.id;

   //请求后端接口查找个人信息
    wx.request({
      url: serverUrl + '/user/findUserInfo?id=' + userId + '&fansId='+userInfo.id,
      method:'POST',
      header: {
        'content-type': 'application/json',
        //因为需要做认证处理,所以要传入当前对象的id和token
        'userId': userInfo.id,
        'userToken':userInfo.token
      },
      success:function(res){
        var user = res.data.data;
        console.log(res.data);
        if(res.data.status==200){
          if (user.faceImage == null && user.faceImage == '' && user.faceImage == undefined) {
            me.setData({
              //如果用户为第一次登陆,该用户没有头像信息,将系统默认的头像路径赋值给faceUrl
              faceUrl: "../resource/images/noneface.png"
            })
          }
          me.setData({
            //分别获取返回信息并赋值给对应变量
            faceUrl: serverUrl + user.faceImage,
            fansCounts: user.fansCounts,
            followCounts: user.followCounts,
            receiveLikeCounts: user.receiveLikeCounts,
            nickname: user.nickname
          })
        }else if(res.data.status==502){
         //502状态码为我们自定义返回的状态码,当token认证出现问题是就会返回该值,会将页面重定向到登录页
          wx.showToast({
            title: res.data.msg,
            icon:"none",
            duration:3000,
            success:function(){
              wx.redirectTo({
                url: '../userLogin/login',
              })
            }
          })
        }
      }
    })
  }

json文件

暂无

2、用户上传头像功能
【需求分析】用户需要点击头像时触发一个选择图片的事件,然后从相册中选择一张图片,之后会返回一个该图片的临时路径,然后在调用微信的上传文件的api,将该临时路径传入,并调用到后台上传头像的接口(详细请参考微信官方api文档,这里就不一一介绍官方相关的api了,代码中会做详细的注释)

wxml文件

暂无

wxss文件

暂无

js文件

 changeFace:function(){
    var me=this;
    //调用微信官方的图片选择接口
    wx.chooseImage({
      //最多可以选择的图片张数
      count: 1,
      //所选的图片的尺寸
      sizeType: [ 'compressed'],
      //选择图片的来源
      sourceType: ['album', 'camera'],
      //该回调函数会返回一个该文件的临时路径
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片,该返回值为一个数组对象
        const tempFilePaths = res.tempFilePaths
            var serverUrl=app.serverUrl;
            // var userInfo=app.userInfo;
            var userInfo=app.getGlobalUserInfo("userInfo");
            //向用户显示上传状态
            wx.showLoading({
              title: '上传中',
            })
            wx.uploadFile({
              //调用后端的上传文件接口
              url: serverUrl+'/user/upload?id='+userInfo.id, 
              //因为上传的为单文件,所以只需要取得数组中的第一个值即可
              filePath: tempFilePaths[0],
              //该名字需要和后端接口定义的文件的变量名相同
              name: 'file',
              //传入认证消息
              header: {
                'content-type': 'application/json',
                'userId': userInfo.id,
                'userToken': userInfo.token
              },
              success(res) {
                console.log(res.data)
               //因为该方法的回调函数的返回值为String类型的字符串,并不是一个json对象,所以需要进行转换
                const data = JSON.parse(res.data);
                //隐藏提醒消息
                wx.hideLoading();
                wx.showToast({
                  title: '上传成功',
                })
               //将后端返回的头像的相对路径获取并赋值给imageUrl
                var imageUrl=data.data;
                me.setData({
                  faceUrl:serverUrl+imageUrl
                })
              }
            })
      }
    })
  }

json文件

暂无

3、用户注销功能
【需求分析】当用户点击注销按钮时候会将用户的本地缓存删除,并返回到登录页面
wxml文件

暂无

wxss文件

暂无

js文件

logout:function(){
    var serverUrl = app.serverUrl;
    // var userInfo=app.userInfo;
    var userInfo=app.getGlobalUserInfo("userInfo");
    //调用注销接口
    wx.request({
      url:serverUrl+ '/logout?id='+userInfo.id,
      method:'POST',
      header: {
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res.data);
        wx.showToast({
          title: '注销成功',
        })
        //调用该方法清除用户的本地缓存
        wx.removeStorageSync("userInfo");
        //并重定向到登录页面
        wx.redirectTo({
          url: '../userLogin/login'
        })
      }
    })
  }

json文件

暂无
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐