接上文: 微信小程式開發06-一個業務頁面的完成
github地址:https://github.com/yexiaochai/wxdemo
我們首頁功能基本完成,我對比了下實際工作中的需求,完成度有70%以上,如果再花一兩天時間,便能跟之前工作做的差不多了,今天我們繼續實現一個頁面列表,便結束這次的學習,後面幾天要出差,所以總結性的文章本週未必能出來,靜待下週吧。
這裡考慮demo複雜度,列表頁功能完成度也僅僅完成主功能模組,與真實工作完成度對比60%左右吧,於是我們開始愉快的程式碼,首先是將我們的頁面樣式實現:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<view class="page-wrapper "> <view class="bus-list js_bus_list "> <view data-supplierid="100020" data-key="" class="bus-list-item "> <view class="bus-time"> 08:25</view> <view class="tobooking"> 預訂 </view> <view class="detail"> <view class="detail1"> <view class="start"> <text class="icon-circle s-icon1"></text>東莞市南城汽車客運站</view> <view class="end"> <text class="icon-circle s-icon2"></text>連州</view> </view> <view class="tags"> <view> <text class="price">¥135</text> </view> <view> <text class="countleft">10張</text> </view> <view> <text class="b-tags js_tags"></text> </view> </view> </view> </view> </view> <view class="bus-list js_bus_list "> <view data-supplierid="100020" data-key="" class="bus-list-item "> <view class="bus-time"> 08:25</view> <view class="tobooking"> 預訂 </view> <view class="detail"> <view class="detail1"> <view class="start"> <text class="icon-circle s-icon1"></text>東莞市南城汽車客運站</view> <view class="end"> <text class="icon-circle s-icon2"></text>連州</view> </view> <view class="tags"> <view> <text class="price">¥135</text> </view> <view> <text class="countleft">10張</text> </view> <view> <text class="b-tags js_tags"></text> </view> </view> </view> </view> </view> <view class="bus-list js_bus_list "> <view data-supplierid="100020" data-key="" class="bus-list-item "> <view class="bus-time"> 08:25</view> <view class="tobooking"> 預訂 </view> <view class="detail"> <view class="detail1"> <view class="start"> <text class="icon-circle s-icon1"></text>東莞市南城汽車客運站</view> <view class="end"> <text class="icon-circle s-icon2"></text>連州</view> </view> <view class="tags"> <view> <text class="price">¥135</text> </view> <view> <text class="countleft">10張</text> </view> <view> <text class="b-tags js_tags"></text> </view> </view> </view> </view> </view> <include src="../mod/calendar.wxml" /> <include src="../../utils/abstract-page.wxml" /> </view> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
.page-wrapper { margin: 0; font-size: 28rpx; line-height: 1.5; color: #333; background-color: #efefef; overflow-x: hidden; -webkit-overflow-scrolling: touch; -webkit-tap-highlight-color: transparent; min-height:2000rpx; } .bus-list .bus-list-item { position: relative; height: 160rpx; background-color: #fff; margin: 16rpx 0; border: 2rpx solid #e5e5e5; border-width: 2rpx 0; } .bus-list .bus-list-item::before,.bus-list .bus-list-item::after { position: absolute; left: 122rpx; content: ''; width: 24rpx; height: 12rpx; background-color: #efefef; border: 2rpx solid #e5e5e5; } .bus-list .bus-list-item::before { border-radius: 0 0 60rpx 60rpx; border-top: none; top: -2rpx; } .bus-list .bus-list-item::after { border-radius: 60rpx 60rpx 0 0; border-bottom: none; bottom: -2rpx; } .bus-list .bus-list-item .bus-time { position: absolute; left: 0; width: 134rpx; height: 100rpx; line-height: 100rpx; margin: 30rpx 0; color: #00b358; text-align: center; font-size: 40rpx; font-family: Arial; border-right: 2rpx dashed #e5e5e5; } .bus-list .bus-list-item .tobooking { background-color: #00B358; position: absolute; right: 0; width: 120rpx; height: 160rpx; line-height: 160rpx; text-align: center; color: #fff; font-size: 30rpx; } .bus-list .bus-list-item.disabled .tobooking { background-color: #c5c5c5; } .bus-list .bus-list-item .detail { height: 80rpx; padding: 36rpx 0; margin: 0 140rpx 0 144rpx; } .bus-list .bus-list-item .detail .sub-list{ height: 52rpx; } .bus-list .bus-list-item .start, .bus-list .bus-list-item .end { color: #333333; font-size: 26rpx; } .bus-list .bus-list-item .price { font-family: Arial; color: #fd8f01; font-size: 32rpx; font-weight: 600; } .bus-list .bus-list-item.disabled .ticket { display: none; } .bus-list .bus-list-item .ticket { color: #fd8f01; font-size: 24rpx; border: 2rpx solid #fd8f01; padding: 2rpx 8rpx; border-radius: 10rpx; font-family: Arial; } .bus-list .bus-list-item.disabled .ticket { color: #c5c5c5; } .bus-list .bus-list-item .s-icon1 { margin: 0 10rpx; border-color: #00B358; } .bus-list .bus-list-item .s-icon2 { margin: 0 10rpx; border-color: #f06463; } .bus-list .bus-list-item .tags { width: 160rpx; text-align: right; position: absolute; right: 0; margin-right: 138rpx; margin-top: 34rpx; top: 0; } |
輕輕鬆鬆完成了頁面主體佈局:
然後這裡需求請求資料,所以我們去設定一個請求實體:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class ListModel extends DemoModel { constructor() { super(); this.url = '/schedule/list'; } } module.exports = { cityModel: new CityModel, city2Model: new City2Model, listModel: new ListModel } |
開始請求引數:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
onLoad: function (data) { let scope = this; if(!data || !data.sid || !data.aid || !data.date) { this.showToast('引數錯誤'); return } this.index = 0; let listModel = models.listModel; listModel.setParam({ startcityid: data.sid, arrivalcityid: data.aid, startdatetime: data.date / 1000, page: this.index + 1 }); this.showLoading(); listModel.execute(function(data) { scope.hideLoading(); scope._appendList(data.schedules); }); } |
接下來的工作便是渲染頁面即可,如果不考慮細節,只是做demo,真的很輕易呢:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
//獲取公共ui操作類例項 const _page = require('../../utils/abstract-page.js'); let modCalendar = require('../mod/calendar.js'); const models = require('../../data/demo-model.js') const util = require('../../utils/util.js') //獲取應用例項 const app = getApp() Page(_page.initPage({ data: { listData: [] }, // methods: uiUtil.getPageMethods(), methods: { }, goIndex: function () { wx.navigateTo({ url: '../index/index' }) }, onShow: function () { global.sss = this; let scope = this; }, _appendList: function (data) { for(let i = 0, len = data.length; i < len; i++) { data[i].dateStr = util.dateUtil.format(new Date(data[i].datetime * 1000), 'H:F' ) } this.setData({ listData: this.data.listData.concat(data) }); }, onLoad: function (data) { let scope = this; if(!data || !data.sid || !data.aid || !data.date) { this.showToast('引數錯誤'); return } this.index = 0; let listModel = models.listModel; listModel.setParam({ startcityid: data.sid, arrivalcityid: data.aid, startdatetime: data.date / 1000, page: this.index + 1 }); this.showLoading(); listModel.execute(function(data) { scope.hideLoading(); scope._appendList(data.schedules); }); } }, { modCalendar: modCalendar })) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<view class="page-wrapper "> <view class="calendar-bar-wrapper js_calendar_wrapper"> <view class="bus-tabs calendar-bar"> <view class="tabs-item js_pre_day">前一天</view> <view class="tabs-item js_show_calendar" style="-webkit-flex: 2; flex: 2;">2018-8-6 週一(明天)</view> <view class="tabs-item js_next_day">後一天</view> </view> </view> <view class="bus-list js_bus_list "> <block wx:for="{{listData}}" wx:key="k"> <view class="bus-list-item "> <view class="bus-time">{{item.dateStr}}</view> <view class="tobooking"> 預訂 </view> <view class="detail"> <view class="detail1"> <view class="start"> <text class="icon-circle s-icon1"></text>{{item.startstationname}}</view> <view class="end"> <text class="icon-circle s-icon2"></text>{{item.arrivalstationname}}</view> </view> <view class="tags"> <view> <text class="price">¥{{item.price / 100}}</text> </view> <view> <text class="countleft">{{item.cansellcountamount}}張</text> </view> <view> <text class="b-tags js_tags"></text> </view> </view> </view> </view> </block> </view> <include src="../mod/calendar.wxml" /> <include src="../../utils/abstract-page.wxml" /> <view class="bus-list js_bus_list " ontap="goIndex"> 去首頁 </view> </view> |
最後,我們完善一下這裡日期相關操作,便暫時結束這次學習:
列表頁的一些總結
我們做小程式相關學習有快兩週了,完成了一個簡單的demo,專案還是有一定複雜度,感覺上還是比較適合做小程式瞭解的,但是也有一些問題,比如寫到後面事實上更多是業務的東西了,業務會涉及很多細節體驗,需要耗時費力,比如今天的列表業務,顯然就偷懶了,程式碼質量也沒怎麼關注,事實上大家可以思考一些問題,這裡還差很多功能:
① 滾動載入
② 列表各種狀態
③ ……
事實上,列表頁是常用的一種業務頁面,雖然各種列表頁的篩選條件不一樣,但是主體功能無非都是:
① 列表渲染
② 滾動載入
③ 條件篩選、重新渲染
所以說我們其實可以將其做成一個頁面基類,跟abstract-page一個意思,這裡留待我們下次來處理吧,藉此我們微信小程式的學習教程就此結束,我後面幾天總結下前面所學整理一個部落格出來,幫助各位更好的瞭解。