微信小程序实战篇-个人中心

哈喽,大家好,又到周五啦,有木有期待今天的更新呀~今天要教大家的是制作个人中心界面,先上效果图

个人中心.gif

##个人中心制作
####1. mine.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: function () {
    wx.navigateTo({
      url: '../order/order'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  }
})

toOrder :事件监听,跳转到我的订单界面
onLoad:在加载过程中,获取用户的信息

####2. mine.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>

布局分为三个模块,用户信息模块、我的订单模块、功能列表模块,布局不是很难,相信看源码就可以理解

####3. mine.wxss

/* 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;
}

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

##我的订单制作
我的订单,其实界面的实现原理和首页的导航栏是一样的,只不过换了一下内容以及导航栏标题罢了,所以这里我就不细讲了,不懂的朋友可以看我首页导航栏是怎么制作的,模仿写出来就好
####1. 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
    });
  }
}));

####2. 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>

####3. 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;
}

##总结

界面布局差不多就讲到这里了,大家也看到了,其实越到后面,要讲的界面内容知识就越少,万变不离其中吧,后续的话,代码君打算讲一下微信小程序的网络请求框架,敬请期待,最后祝大家周末愉快~

###上一篇:微信小程序实战篇-购物车

#####建了一个小程序技术交流群,想入群的读者请加微信
小程序入群.png

  • 24
    点赞
  • 204
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
好的,让我们开始微信小程序实战篇-个人中心、订单制作的教程。 ## 1. 创建个人中心页面 首先,我们需要在小程序中创建一个个人中心页面。可以在 app.json 中添加一个 tab 标签,在 pages 数组中添加个人中心页面的路径: ```json { "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/personal/personal", "text": "个人中心" } ] }, "pages": [ "pages/index/index", "pages/personal/personal" ] } ``` 接下来,在 pages 文件夹中创建 personal 文件夹,并创建 personal.wxml、personal.wxss、personal.js、personal.json 四个文件。在 personal.wxml 文件中添加以下代码: ```html <view class="personal"> <view class="avatar"> <image class="avatar-img" src="{{avatarUrl}}"></image> <text class="nickname">{{nickname}}</text> </view> <view class="order"> <view class="order-item" bindtap="goToOrder"> <image class="order-icon" src="/images/personal/order.png"></image> <text class="order-text">我的订单</text> <image class="order-arrow" src="/images/personal/arrow.png"></image> </view> </view> </view> ``` 这里我们创建了一个包含头像、昵称、我的订单等模块的页面。其中,头像和昵称是从后端接口获取的数据。 ```javascript // personal.js Page({ data: { avatarUrl: '', nickname: '' }, onLoad: function () { // 从后端接口获取用户信息 wx.request({ url: 'https://example.com/userinfo', success: (res) => { this.setData({ avatarUrl: res.data.avatarUrl, nickname: res.data.nickname }) } }) }, goToOrder: function () { // 跳转到订单页面 wx.navigateTo({ url: '/pages/order/order' }) } }) ``` 在 personal.js 文件中,我们编写了 onLoad 和 goToOrder 两个函数。onLoad 函数用于从后端接口获取用户信息并在页面上显示,goToOrder 函数用于跳转到订单页面。 ## 2. 创建订单页面 接下来,我们需要创建一个订单页面。在 pages 文件夹中创建 order 文件夹,并创建 order.wxml、order.wxss、order.js、order.json 四个文件。在 order.wxml 文件中添加以下代码: ```html <view class="order"> <view class="order-header"> <text class="order-title">我的订单</text> <text class="order-more">查看更多</text> </view> <view class="order-list"> <view class="order-item" wx:for="{{orders}}" wx:key="index"> <image class="order-img" src="{{item.imgUrl}}"></image> <view class="order-info"> <text class="order-name">{{item.name}}</text> <text class="order-price">¥{{item.price}}</text> </view> </view> </view> </view> ``` 这里我们创建了一个包含订单标题、订单列表等模块的页面。其中,订单列表是从后端接口获取的数据。 ```javascript // order.js Page({ data: { orders: [] }, onLoad: function () { // 从后端接口获取订单列表 wx.request({ url: 'https://example.com/orders', success: (res) => { this.setData({ orders: res.data }) } }) } }) ``` 在 order.js 文件中,我们编写了 onLoad 函数用于从后端接口获取订单列表并在页面上显示。 ## 3. 页面样式 最后,我们需要编写页面的样式。在 app.wxss 中添加以下代码: ```css .tab-bar { position: fixed; bottom: 0; left: 0; width: 100%; height: 50rpx; background-color: #fff; border-top: 1rpx solid #e5e5e5; box-shadow: 0 -2rpx 6rpx rgba(0, 0, 0, 0.05); } .tab-bar-item { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; font-size: 20rpx; color: #8a8a8a; } .tab-bar-item.active { color: #007aff; } .personal { display: flex; flex-direction: column; align-items: center; justify-content: center; margin-top: 20rpx; } .avatar { display: flex; flex-direction: column; align-items: center; justify-content: center; } .avatar-img { width: 80rpx; height: 80rpx; border-radius: 50%; } .nickname { margin-top: 10rpx; font-size: 24rpx; font-weight: bold; } .order { margin-top: 20rpx; } .order-item { display: flex; flex-direction: row; align-items: center; justify-content: space-between; width: 90%; height: 80rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05); padding: 20rpx; margin-bottom: 20rpx; } .order-icon { width: 40rpx; height: 40rpx; } .order-text { font-size: 28rpx; font-weight: bold; } .order-arrow { width: 20rpx; height: 20rpx; } ``` 在 order.wxss 中添加以下代码: ```css .order { margin-top: 20rpx; } .order-header { display: flex; flex-direction: row; align-items: center; justify-content: space-between; padding: 20rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05); margin-bottom: 20rpx; } .order-title { font-size: 32rpx; font-weight: bold; } .order-more { font-size: 24rpx; color: #007aff; } .order-list { display: flex; flex-direction: column; align-items: center; justify-content: center; } .order-item { display: flex; flex-direction: row; align-items: center; justify-content: space-between; width: 90%; height: 120rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05); padding: 20rpx; margin-bottom: 20rpx; } .order-img { width: 80rpx; height: 80rpx; border-radius: 10rpx; } .order-info { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; margin-left: 20rpx; } .order-name { font-size: 28rpx; font-weight: bold; } .order-price { margin-top: 10rpx; font-size: 24rpx; color: #007aff; } ``` 至此,我们完成了微信小程序实战篇-个人中心、订单制作的教程。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农掘金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值