6月26日日志-消費記錄和支出報告界面實現(xiàn)
1.界面需求介紹
展示個人消費歷史記錄,包括消費時間,消費類別,消費錢數(shù)等個人信息。實現(xiàn)用戶個人的收支記賬功能,除校園卡記錄的消費信息之外,用戶個人可以在消費記錄界面增添收入和支出記錄,使得我們的消費記錄更加細致,靈活,更加精準,并具有良好的用戶體驗性。實現(xiàn)個人收支分析報告,能對用戶一學期的收入支出總情況進行展示分析,重點關(guān)注學生在食堂中的消費情況,處于全年級什么水平。有助于我們提出針對性的建議。
2.界面設(shè)計
共包含三個前端界面設(shè)計,一個是消費記錄展示界面,采用滑動列表實現(xiàn)。為列表中的每一次記錄項設(shè)置固定的樣式。
2.1 消費列表界面
列表界面的部分css樣式代碼。
.navi
{width: 100%
;
}.add-button
{float: right
;
}.list
{display: flex
;flex-direction: column
;padding: 40rpx
;
}.item
{display: flex
;flex-direction: row
;text-align: left
;width: 100%
;
}.text
{display: flex
;flex-direction: column
;width: 100%
;border-bottom: 1px solid line-height: 30px
;
}.stamp
{font-size: 30rpx
;color: margin: 20rpx
;display: flex
;flex-direction: row
;justify-content: space-between
;
}.title
{margin: 20rpx
;margin-bottom: 0rpx
;font-size: 40rpx
;
}
實現(xiàn)的列表界面如下,記錄了消費的類別,金額,和時間。同時提供添加收支按鈕,和顯示報告按鈕。
每一條數(shù)據(jù)如上圖所示格式,包括消費類別,支出收入類別,金額,時間
通過后端開發(fā)隊友留下的數(shù)據(jù)庫接口,導入學生的消費數(shù)據(jù),傳入學生的消費列表。
wx.getStorage
({key:
'db',success:
function (res
) {console.log
(that.data.items
),that.data.items
= (res.data
).concat
(that.data.items
); that.setData
({'items': that.data.items
});console.log
(items
)}})}
2.2 個人收支記賬界面
為方便學生在收支記錄上能自由地添加賬目,于是進行了相應增加收支界面地設(shè)計。通過github開源項目查找類似地界面設(shè)計并進行模仿實現(xiàn)。
能自由設(shè)置消費標題,類型,金額,以及消費時間等信息。
主要界面實現(xiàn)借助radio-group 和flex布局實現(xiàn)。
<view class
="page"><view class
="section"><view class
="section-title">標題
</view
><input bindinput
="bindTitleInput" placeholder
="內(nèi)容" value
="{{title}}" /
></view
><view class
="section"><view class
="section-title">類型
</view
><radio-group class
="radio-group" bindchange
="radioChange"><label class
="radio"><radio class
="radio" value
="+" checked
="true" /
>收入
</label
><label class
="radio"><radio class
="radio" value
="-" /
>支出
</label
></radio-group
></view
><view class
="section"><view class
="section-title">金額
</view
><input bindinput
="bindAccountInput" type
="number" value
="{{account}}" placeholder
="請輸入數(shù)字,不加正負號" /
></view
><view class
="section"><picker mode
="date" value
="{{date}}" start
="2016-1-1" end
="{{date}}" bindchange
="bindDateChange"><view class
="section-title">日期:
{{date
}}</view
></picker
></view
><button class
="button" type
="primary" bindtap
="bindSaveInput">添加
</button
>
</view
>
<modal class
="modal" hidden
="{{modalHidden}}" no-cancel bindconfirm
="hideModal"><view
>添加成功
</view
>
</modal
>
<modal class
="modal" hidden
="{{alertHidden}}" no-cancel bindconfirm
="hideAlertView"><view
>{{alertTitle
}}</view
>
</modal
>
重點在于這一步的邏輯實現(xiàn),在于將增添的信息與數(shù)據(jù)庫中的信息進行整合,并將整合后的數(shù)據(jù)輸出到列表中顯示。
在添加消費的界面邏輯如下
bindTitleInput:
function (e
) {this.setData
({title: e.detail.value
})},radioChange:
function (e
) {this.setData
({cate: e.detail.value
})},bindAccountInput:
function (e
) {this.setData
({account: e.detail.value
})},bindSaveInput:
function () {let that
= this
;if (!this.data.title
) {that.setData
({alertHidden: false,alertTitle:
'標題不能為空'});return;}let reg
= /^
[0-9
]+.?
[0-9
]*$/
;if (!reg.test
(this.data.account
)) {that.setData
({alertHidden: false,alertTitle:
'金額只能是數(shù)字'});return;}let record
= {title: this.data.title,cate: this.data.cate,account: this.data.account,date: this.data.date
};let data
= [];wx.getStorage
({key:
'db',success:
function (res
) {data
= res.data
;data.push
(record
);wx.setStorage
({key:
'db',data: data
});that.setData
({modalHidden:
false});},
});},bindDateChange:
function (e
) {this.setData
({date: e.detail.value
});},onLoad:
function () {wx.getStorage
({key:
'db',fail:
function () {wx.setStorage
({key:
'db',data:
[]});}});let date = new Date
();let strDate
= date.getFullYear
() +
"-" +
(date.getMonth
() + 1
) +
"-" + date.getDate
();console.debug
(strDate
);this.setData
({date: strDate
})},hideModal:
function () {this.setData
({'modalHidden': true})wx.navigateBack
()},hideAlertView:
function () {this.setData
({'alertHidden': true})}});
關(guān)鍵代碼如下,將原來列表中的數(shù)據(jù)與學生用戶增添的數(shù)據(jù)進行整合。使得一起顯示。
wx.getStorage
({key:
'db',success:
function (res
) {console.log
(that.data.items
),that.data.items
= (res.data
).concat
(that.data.items
); that.setData
({'items': that.data.items
});console.log
(items
)}})
效果如下圖所示。
2.3利用canvas畫布生成支出報告和海報
效果如圖所示,將以海報的風格展示學生醫(yī)學系的總支出,以及食堂消費。方便學生用戶轉(zhuǎn)發(fā)朋友圈。
這個界面設(shè)計較為復雜,使用canvas進行畫面構(gòu)造,其中遇到許多坑,比如圖片生成,界面局都出現(xiàn)了許多問題,不夠最后還是畫出來了。
主要畫布代碼
extend:function
(data,dataExtend
){var res
={};for (var key
in data
) {res
[key
] = data
[key
];} for (var key
in dataExtend
) {res
[key
] = dataExtend
[key
]; } return res
;},onLoad: function
() {this.getAvaterInfo
();//對接數(shù)據(jù)var uid
= wx.getStorageSync
('uid');var that
= this//這里默認學期2wx.cloud.callFunction
({name:
"score",data:
{uid
: uid,學期:
'2'},complete: res
=> {console.log
('callFunction test result: ', res
)// var data
= this.extend
(res.result,
{token: this.data.cardInfo
});var data
= this.extend
(res.result,this.data.cardInfo
);console.log
('data',data
)that.setData
({cardInfo:data
})console.log
('cardInfor',this.data.cardInfo
)}})},/*** 先下載頭像圖片*/getAvaterInfo: function
() {wx.showLoading
({title:
'生成中...',mask: true,
});console.log
(this.data.cardInfo.avater
);var that
= this
;wx.downloadFile
({url: that.data.cardInfo.avater, //頭像圖片路徑success: function
(res
) {wx.hideLoading
();if (res.statusCode
=== 200
) {console.log
(res.tempFilePath
);var avaterSrc
= res.tempFilePath
; //下載成功返回結(jié)果that.getQrCode
(avaterSrc
); //繼續(xù)下載二維碼圖片
} else {wx.showToast
({title:
'頭像下載失敗!',icon:
'none',duration: 2000,success: function
() {var avaterSrc
= "";that.getQrCode
(avaterSrc
);}})}},fail:err
=>{console.log
(err
)}})},/*** 下載二維碼圖片*/getQrCode: function
(avaterSrc
) {wx.showLoading
({title:
'生成中...',mask: true,
});var that
= this
;wx.downloadFile
({url: that.data.cardInfo.qrCode, //二維碼路徑success: function
(res
) {wx.hideLoading
();if (res.statusCode
=== 200
) {var codeSrc
= res.tempFilePath
;that.sharePosteCanvas
(avaterSrc, codeSrc
);} else {wx.showToast
({title:
'二維碼下載失敗!',icon:
'none',duration: 2000,success: function
() {var codeSrc
= "";that.sharePosteCanvas
(avaterSrc, codeSrc
);}})}}})},/*** 開始用canvas繪制分享海報* @param avaterSrc 下載的頭像圖片路徑* @param codeSrc 下載的二維碼圖片路徑*/sharePosteCanvas: function
(avaterSrc, codeSrc
) {wx.showLoading
({title:
'生成中...',mask: true,
})var that
= this
;var cardInfo
= that.data.cardInfo
; //需要繪制的數(shù)據(jù)集合const ctx
= wx.createCanvasContext
('myCanvas'); //創(chuàng)建畫布var width
= "";wx.createSelectorQuery
().select
('#canvas-container').boundingClientRect
(function
(rect
) {var height
= rect.height
;var right
= rect.right
;width
= rect.width * 0.8
;var left
= rect.left + 5
;ctx.setFillStyle
('#fff');ctx.fillRect
(0, 0, rect.width, height
);//頭像為正方形
if (avaterSrc
) {ctx.drawImage
(avaterSrc, left, 20, width, width
);ctx.setFontSize
(14
);ctx.setFillStyle
('#fff');ctx.setTextAlign
('left');}//標簽
if (cardInfo.TagText
) {ctx.fillText
(cardInfo.TagText, left + 10, width - 4
);const metrics
= ctx.measureText
(cardInfo.TagText
); //測量文本信息ctx.stroke
();ctx.rect
(left + 10, width - 40, metrics.width + 20, 25
);ctx.setFillStyle
('rgba(255,255,255,0.4)');ctx.fill
();}//總消費前言
if (cardInfo.TotalcostTxt
) {ctx.setFontSize
(14
);ctx.setFillStyle
('#000');ctx.setTextAlign
('left');ctx.fillText
(cardInfo.TotalcostTxt, left, width + 60
);}//紅色總消費顯示
if (cardInfo.Totalcost
) {ctx.setFillStyle
('#ff0000');ctx.setFontSize
(15
);ctx.setTextAlign
('left');ctx.fillText
(cardInfo.Totalcost, left, width + 85
);}//食堂消費前言
if (cardInfo.cafecostTxt
) {const CONTENT_ROW_LENGTH
= 24
; // 正文 單行顯示字符長度
let [contentLeng, contentArray, contentRows
] = that.textByteLength
(cardInfo.cafecostTxt, CONTENT_ROW_LENGTH
);ctx.setFontSize
(12
);ctx.setFillStyle
('#666');ctx.setTextAlign
('left');ctx.fillText
(cardInfo.cafecostTxt, left, width + 105
);}// 紅色食堂消費分析報告
if (cardInfo.pricetxt1
) {const CONTENT_ROW_LENGTH
= 24
; // 正文 單行顯示字符長度cardInfo.pricetxt1
= cardInfo.cafeprice+cardInfo.pricetxt1+cardInfo.cafeovflow+cardInfo.cafepercente+cardInfo.pricetxt2
;console.log
(cardInfo.pricetxt1
);let [contentLeng, contentArray, contentRows
] = that.textByteLength
(cardInfo.pricetxt1, CONTENT_ROW_LENGTH
);ctx.setTextAlign
('left');ctx.setFillStyle
('#ff0000');ctx.setFontSize
(20
);let contentHh
= 22 * 1
;for (let m
= 0
; m
< contentArray.length
; m++
) {ctx.fillText
(contentArray
[m
], left, width + 150 + contentHh * m
);}}
將數(shù)據(jù)接口給隊友之后,有隊友上傳數(shù)據(jù)庫中的記錄信息。
3.工作總結(jié)
比較完善地實現(xiàn)了消費支出和收入地記賬功能,并利用canvas畫布實現(xiàn)了比較不錯地學期支出報告。
總結(jié)
以上是生活随笔為你收集整理的6月26日日志-消费记录界面实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。