Commit c9483180c2b7fee8c141932ca61129cf27e57e3c

Authored by xingwenliang
1 parent 339f8f40

feat: add download page

pages/result/result.js
  1 +
  2 +import { convertDate } from '../../utils/util.js';
  3 +//获取应用实例
  4 +const app = getApp();
  5 +const audioList = app.globalData.getAudioList();
  6 +const type = app.globalData.getTypeList();
  7 +
  8 +let audio = null;
  9 +let formateDate = function (audioList) {
  10 + return audioList.map(audio => {
  11 + return {
  12 + ...audio,
  13 + date: convertDate(audio.time)
  14 + }
  15 + });
  16 +};
  17 +
1 Page({ 18 Page({
2 data: { 19 data: {
3 - total: '',  
4 - benefit: '',  
5 - diff: '' 20 + audioList: formateDate(audioList)
  21 + },
  22 + onLoad (options) {
  23 +
6 }, 24 },
7 - onLoad: function(options){  
8 - var total = new Number((options.price*2*options.days).toFixed(2));  
9 - var benefit = isNaN(total) ? 0 : this.calc(total).toFixed(2);  
10 - this.setData({  
11 - total: isNaN(total) ? 0 : total.toFixed(2),  
12 - benefit: benefit,  
13 - diff: (total - benefit).toFixed(2) 25 + play(e){
  26 + const { index } = e.target.dataset;
  27 + const target = app.globalData.getAudioList()[index];
  28 + audio && audio.destroy();
  29 + audio = wx.createInnerAudioContext({useWebAudioImplement: true});
  30 + audio.autoplay = true;
  31 + audio.src = target.path;
  32 + },
  33 + download(e){
  34 + const { index } = e.target.dataset;
  35 + const target = app.globalData.getAudioList()[index];
  36 + const ext = target.path.split('.').pop();
  37 + wx.shareFileMessage({
  38 + filePath: target.path,
  39 + fileName: `${target.text.slice(0, 8)}.${ext}`
14 }); 40 });
15 - wx.showShareMenu();  
16 }, 41 },
17 - calc: function(fee){  
18 - //662.5 = 100 + 50/0.8 + 250/0.5  
19 - if(fee > 662.5){  
20 - return fee - 262.5;  
21 - }  
22 - //162.5 = 100 + 50/0.8  
23 - else if(fee > 162.5){  
24 - //(fee - 162.5)/2 + 150;  
25 - return fee/2 + 68.75;  
26 - }  
27 - else if(fee > 100){  
28 - //(fee - 100)*0.8 + 100;  
29 - return fee*0.8 + 20;  
30 - }  
31 - else{  
32 - return fee;  
33 - } 42 + delete(e){
  43 + const { index } = e.target.dataset;
  44 + wx.showModal({
  45 + title: '提示',
  46 + content: '确认删除吗?',
  47 + success: res => {
  48 + if(!res.confirm){
  49 + return;
  50 + }
  51 + const newList = app.globalData.setAudioList('delete', index);
  52 + this.setData({
  53 + audioList: formateDate(newList)
  54 + });
  55 + }
  56 + });
34 } 57 }
35 }); 58 });
36 \ No newline at end of file 59 \ No newline at end of file
pages/result/result.wxml
1 -<view class="text-view border">  
2 - <text class="label">每月总消费: </text>  
3 - <text class="val red">{{total}} 元</text>  
4 -</view>  
5 -<view class="text-view border">  
6 - <text class="label">优惠后: </text>  
7 - <text class="val green">{{benefit}} 元</text>  
8 -</view>  
9 -<view class="text-view">  
10 - <text class="label">共节省了: </text>  
11 - <text class="val blue">{{diff}} 元</text>  
12 -</view>  
13 -<ad unit-id="adunit-0a991618c53a4445"></ad>  
14 \ No newline at end of file 1 \ No newline at end of file
  2 +<ad unit-id="adunit-0a991618c53a4445" class="ad"></ad>
  3 +<view
  4 + class="text-view"
  5 + wx:for="{{audioList}}"
  6 + wx:key="path"
  7 + >
  8 + <view
  9 + class="text">
  10 + {{item.text}}
  11 + </view>
  12 + <view class="btn-view">
  13 + <view class="text-time">{{item.date}}</view>
  14 + <button class="button" type="warn" size="mini" data-index="{{index}}" bindtap="delete">删除</button>
  15 + <button class="button" type="primary" size="mini" data-index="{{index}}" bindtap="play">播放</button>
  16 + <button class="button" type="primary" size="mini" data-index="{{index}}" bindtap="download">下载</button>
  17 + </view>
  18 +</view>
15 \ No newline at end of file 19 \ No newline at end of file
pages/result/result.wxss
  1 +.ad{
  2 + margin-bottom: 10px;
  3 +}
1 .text-view{ 4 .text-view{
2 - display: flex;  
3 - flex-direction: row;  
4 - width: 100%;  
5 - padding: 15rpx; 5 + margin-bottom: 10px;
  6 + padding: 10px;
6 background: #efefef; 7 background: #efefef;
7 } 8 }
8 -.text-view .label{  
9 - flex: 1;  
10 - height: 72rpx;  
11 - padding-right: 20rpx;  
12 - vertical-align: top;  
13 - line-height: 72rpx;  
14 - font-size: 36rpx;  
15 - color: #666; 9 +.btn-view{
  10 + padding-top: 10px;
16 text-align: right; 11 text-align: right;
  12 + border-top: 0.5px solid #d0d0d0;
17 } 13 }
18 -.text-view .val{  
19 - flex: 2;  
20 - height: 72rpx;  
21 - line-height: 72rpx;  
22 - font-size: 36rpx;  
23 -}  
24 -.border{  
25 - border-bottom: 1rpx solid #dfdfdf; 14 +.button{
  15 + margin-left: 10px;
26 } 16 }
27 -.red{  
28 - color: red; 17 +.left{
  18 + float: left;
  19 + margin-left: 0;
29 } 20 }
30 -.green{  
31 - color: green; 21 +.text{
  22 + padding-top: 10px;
  23 + padding-bottom: 20px;
32 } 24 }
33 -.blue{  
34 - color: blue; 25 +.text-time{
  26 + float: left;
  27 + font-size: 13px;
  28 + line-height: 2.3;
  29 + color: #bababa;
35 } 30 }
36 \ No newline at end of file 31 \ No newline at end of file
utils/util.js
1 -function formatTime(date) {  
2 - var year = date.getFullYear()  
3 - var month = date.getMonth() + 1  
4 - var day = date.getDate() 1 +/**
  2 + * 语义化时间转换
  3 + * @param {number} [timestamp] - 当前的时间,可传格式化的字符串或时间戳
  4 + */
  5 +function convertDate(timestamp=Date.now()){
  6 + // pass in `yyyy-mm-dd hh:ii:ss`
  7 + // convert to `yyyy-mm-ddThh:ii:ss.000+08:00`
  8 + const targetTimestamp = Number(timestamp) ? timestamp : Date.parse(`${timestamp.split(' ').join('T')}.000+08:00`);
  9 + const diff = new Date().getTime() - targetTimestamp;
  10 + let time = '';
  11 + //小于5分钟->刚刚
  12 + if(diff < 5*60*1000){
  13 + time = '刚刚';
  14 + }
  15 + //小于1小时->xx分钟前
  16 + else if(diff < 1*60*60*1000){
  17 + time = parseInt(diff/1000/60) + '分钟前';
  18 + }
  19 + //小于24小时->xx小时前
  20 + else if(diff < 24*60*60*1000){
  21 + time = parseInt(diff/1000/60/60) + '小时前';
  22 + }
  23 + //小于30天->xx天前
  24 + else if(diff < 30*24*60*60*1000){
  25 + time = parseInt(diff/1000/60/60/24) < 2 ? '昨天' : (parseInt(diff/1000/60/60/24) + '天前');
  26 + }
  27 + //小于1年->xx个月前
  28 + else if(diff < 365*24*60*60*1000){
  29 + time = parseInt(diff/1000/60/60/24/30) + '个月前';
  30 + }
  31 + //x年x月前
  32 + else{
  33 + let days = parseInt(diff/1000/60/60/24);
  34 + time = parseInt(days/365) + '年' + (parseInt(days%365/30) ? (parseInt(days%365/30) + '个月前') : '前');
  35 + }
  36 + return time;
  37 +};
5 38
6 - var hour = date.getHours()  
7 - var minute = date.getMinutes()  
8 - var second = date.getSeconds() 39 +/**
  40 + * 时间戳转化为日期
  41 + * @param {string} [formateString] 要转化的格式,默认 'y-m-d h:i:s'
  42 + * @param {number} [timestamp] 要转化的时间戳,默认 Date.now()
  43 + * @returns {string} 格式化后的日期
  44 + */
  45 +function timestampToDate(formateString='y-m-d h:i:s', timestamp=Date.now()){
  46 + if(isNaN(+timestamp)){
  47 + console.error('util.timeStampToDate: timestamp is invilid.');
  48 + return null;
  49 + }
  50 + if(String(timestamp).length === 10){
  51 + timestamp *= 1000;
  52 + }
  53 + const reg = /y|m|d|h|i|s/g;
  54 + const date = new Date(+timestamp);
  55 + const saithToDual = time => {
  56 + if(time.toString().length < 2){
  57 + time = '0' + time;
  58 + }
  59 + return time;
  60 + };
9 61
10 -  
11 - return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')  
12 -}  
13 -  
14 -function formatNumber(n) {  
15 - n = n.toString()  
16 - return n[1] ? n : '0' + n  
17 -} 62 + return formateString.replace(reg, function($1){
  63 + switch($1){
  64 + case 'y':
  65 + return date.getFullYear();
  66 + case 'm':
  67 + return saithToDual(date.getMonth() + 1);
  68 + case 'd':
  69 + return saithToDual(date.getDate());
  70 + case 'h':
  71 + return saithToDual(date.getHours());
  72 + case 'i':
  73 + return saithToDual(date.getMinutes());
  74 + case 's':
  75 + return saithToDual(date.getSeconds());
  76 + }
  77 + return $1;
  78 + });
  79 +};
18 80
19 module.exports = { 81 module.exports = {
20 - formatTime: formatTime  
21 -} 82 + convertDate,
  83 + timestampToDate
  84 +};