From c9483180c2b7fee8c141932ca61129cf27e57e3c Mon Sep 17 00:00:00 2001 From: xingwenliang Date: Sun, 6 Nov 2022 04:29:17 +0800 Subject: [PATCH] feat: add download page --- pages/result/result.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- pages/result/result.wxml | 30 +++++++++++++++++------------- pages/result/result.wxss | 47 +++++++++++++++++++++-------------------------- utils/util.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- 4 files changed, 169 insertions(+), 84 deletions(-) diff --git a/pages/result/result.js b/pages/result/result.js index bc6d2ed..3983cc0 100644 --- a/pages/result/result.js +++ b/pages/result/result.js @@ -1,35 +1,58 @@ + +import { convertDate } from '../../utils/util.js'; +//获取应用实例 +const app = getApp(); +const audioList = app.globalData.getAudioList(); +const type = app.globalData.getTypeList(); + +let audio = null; +let formateDate = function (audioList) { + return audioList.map(audio => { + return { + ...audio, + date: convertDate(audio.time) + } + }); +}; + Page({ data: { - total: '', - benefit: '', - diff: '' + audioList: formateDate(audioList) + }, + onLoad (options) { + }, - onLoad: function(options){ - var total = new Number((options.price*2*options.days).toFixed(2)); - var benefit = isNaN(total) ? 0 : this.calc(total).toFixed(2); - this.setData({ - total: isNaN(total) ? 0 : total.toFixed(2), - benefit: benefit, - diff: (total - benefit).toFixed(2) + play(e){ + const { index } = e.target.dataset; + const target = app.globalData.getAudioList()[index]; + audio && audio.destroy(); + audio = wx.createInnerAudioContext({useWebAudioImplement: true}); + audio.autoplay = true; + audio.src = target.path; + }, + download(e){ + const { index } = e.target.dataset; + const target = app.globalData.getAudioList()[index]; + const ext = target.path.split('.').pop(); + wx.shareFileMessage({ + filePath: target.path, + fileName: `${target.text.slice(0, 8)}.${ext}` }); - wx.showShareMenu(); }, - calc: function(fee){ - //662.5 = 100 + 50/0.8 + 250/0.5 - if(fee > 662.5){ - return fee - 262.5; - } - //162.5 = 100 + 50/0.8 - else if(fee > 162.5){ - //(fee - 162.5)/2 + 150; - return fee/2 + 68.75; - } - else if(fee > 100){ - //(fee - 100)*0.8 + 100; - return fee*0.8 + 20; - } - else{ - return fee; - } + delete(e){ + const { index } = e.target.dataset; + wx.showModal({ + title: '提示', + content: '确认删除吗?', + success: res => { + if(!res.confirm){ + return; + } + const newList = app.globalData.setAudioList('delete', index); + this.setData({ + audioList: formateDate(newList) + }); + } + }); } }); \ No newline at end of file diff --git a/pages/result/result.wxml b/pages/result/result.wxml index bc28e68..e336c22 100644 --- a/pages/result/result.wxml +++ b/pages/result/result.wxml @@ -1,13 +1,17 @@ - - 每月总消费: - {{total}} 元 - - - 优惠后: - {{benefit}} 元 - - - 共节省了: - {{diff}} 元 - - \ No newline at end of file + + + + {{item.text}} + + + {{item.date}} + + + + + \ No newline at end of file diff --git a/pages/result/result.wxss b/pages/result/result.wxss index ecc7acf..c718e98 100644 --- a/pages/result/result.wxss +++ b/pages/result/result.wxss @@ -1,35 +1,30 @@ +.ad{ + margin-bottom: 10px; +} .text-view{ - display: flex; - flex-direction: row; - width: 100%; - padding: 15rpx; + margin-bottom: 10px; + padding: 10px; background: #efefef; } -.text-view .label{ - flex: 1; - height: 72rpx; - padding-right: 20rpx; - vertical-align: top; - line-height: 72rpx; - font-size: 36rpx; - color: #666; +.btn-view{ + padding-top: 10px; text-align: right; + border-top: 0.5px solid #d0d0d0; } -.text-view .val{ - flex: 2; - height: 72rpx; - line-height: 72rpx; - font-size: 36rpx; -} -.border{ - border-bottom: 1rpx solid #dfdfdf; +.button{ + margin-left: 10px; } -.red{ - color: red; +.left{ + float: left; + margin-left: 0; } -.green{ - color: green; +.text{ + padding-top: 10px; + padding-bottom: 20px; } -.blue{ - color: blue; +.text-time{ + float: left; + font-size: 13px; + line-height: 2.3; + color: #bababa; } \ No newline at end of file diff --git a/utils/util.js b/utils/util.js index 784a534..59258d8 100644 --- a/utils/util.js +++ b/utils/util.js @@ -1,21 +1,84 @@ -function formatTime(date) { - var year = date.getFullYear() - var month = date.getMonth() + 1 - var day = date.getDate() +/** + * 语义化时间转换 + * @param {number} [timestamp] - 当前的时间,可传格式化的字符串或时间戳 + */ +function convertDate(timestamp=Date.now()){ + // pass in `yyyy-mm-dd hh:ii:ss` + // convert to `yyyy-mm-ddThh:ii:ss.000+08:00` + const targetTimestamp = Number(timestamp) ? timestamp : Date.parse(`${timestamp.split(' ').join('T')}.000+08:00`); + const diff = new Date().getTime() - targetTimestamp; + let time = ''; + //小于5分钟->刚刚 + if(diff < 5*60*1000){ + time = '刚刚'; + } + //小于1小时->xx分钟前 + else if(diff < 1*60*60*1000){ + time = parseInt(diff/1000/60) + '分钟前'; + } + //小于24小时->xx小时前 + else if(diff < 24*60*60*1000){ + time = parseInt(diff/1000/60/60) + '小时前'; + } + //小于30天->xx天前 + else if(diff < 30*24*60*60*1000){ + time = parseInt(diff/1000/60/60/24) < 2 ? '昨天' : (parseInt(diff/1000/60/60/24) + '天前'); + } + //小于1年->xx个月前 + else if(diff < 365*24*60*60*1000){ + time = parseInt(diff/1000/60/60/24/30) + '个月前'; + } + //x年x月前 + else{ + let days = parseInt(diff/1000/60/60/24); + time = parseInt(days/365) + '年' + (parseInt(days%365/30) ? (parseInt(days%365/30) + '个月前') : '前'); + } + return time; +}; - var hour = date.getHours() - var minute = date.getMinutes() - var second = date.getSeconds() +/** + * 时间戳转化为日期 + * @param {string} [formateString] 要转化的格式,默认 'y-m-d h:i:s' + * @param {number} [timestamp] 要转化的时间戳,默认 Date.now() + * @returns {string} 格式化后的日期 + */ +function timestampToDate(formateString='y-m-d h:i:s', timestamp=Date.now()){ + if(isNaN(+timestamp)){ + console.error('util.timeStampToDate: timestamp is invilid.'); + return null; + } + if(String(timestamp).length === 10){ + timestamp *= 1000; + } + const reg = /y|m|d|h|i|s/g; + const date = new Date(+timestamp); + const saithToDual = time => { + if(time.toString().length < 2){ + time = '0' + time; + } + return time; + }; - - return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') -} - -function formatNumber(n) { - n = n.toString() - return n[1] ? n : '0' + n -} + return formateString.replace(reg, function($1){ + switch($1){ + case 'y': + return date.getFullYear(); + case 'm': + return saithToDual(date.getMonth() + 1); + case 'd': + return saithToDual(date.getDate()); + case 'h': + return saithToDual(date.getHours()); + case 'i': + return saithToDual(date.getMinutes()); + case 's': + return saithToDual(date.getSeconds()); + } + return $1; + }); +}; module.exports = { - formatTime: formatTime -} + convertDate, + timestampToDate +}; -- libgit2 0.22.2