微信小程序實戰篇-個人中心

轉載自:https://blog.csdn.net/u012927188/article/details/76684951
哈嘍,大家好,又到週五啦,有木有期待今天的更新呀~今天要教大家的是製作個人中心界面,先上效果圖
在這裏插入圖片描述

個人中心製作

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

總結

界面佈局差不多就講到這裏了,大家也看到了,其實越到後面,要講的界面內容知識就越少,萬變不離其中吧,後續的話,代碼君打算講一下微信小程序的網絡請求框架,敬請期待,最後祝大家週末愉快~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章