From a98566e4cd67a02345a1879cdc72293171ac3ff8 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Thu, 25 Apr 2019 13:01:57 +0800 Subject: [PATCH] Reactor util.js. --- src/api/journal.js | 23 ++++++++++++++ src/utils/util.js | 40 ++++++++++++++---------- src/views/sheet/internal/JournalList.vue | 38 ++++++++++++++++++---- 3 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 src/api/journal.js diff --git a/src/api/journal.js b/src/api/journal.js new file mode 100644 index 00000000..c2ff6b03 --- /dev/null +++ b/src/api/journal.js @@ -0,0 +1,23 @@ +import service from '@/utils/service' + +const baseUrl = '/api/admin/journals' + +const journalApi = {} + +journalApi.query = params => { + return service({ + url: baseUrl, + params: params, + method: 'get' + }) +} + +journalApi.create = (journal) => { + return service({ + url: baseUrl, + data: journal, + method: 'post' + }) +} + +export default journalApi diff --git a/src/utils/util.js b/src/utils/util.js index 81194d26..2c1195db 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -1,3 +1,5 @@ +import moment from 'moment' +import 'moment/locale/zh-cn' /** * 触发 window.resize */ @@ -22,24 +24,28 @@ export function removeLoadingAnimate(id = '', timeout = 1500) { }, timeout) } -function pluralize(time, label) { - if (time === 1) { - return time + label - } - return time + label -} - -/** - * time ago - * @param {*} time - */ export function timeAgo(time) { - const between = (Date.now() - Number(time)) / 1000 - if (between < 3600) { - return pluralize(~~(between / 60), ' 分钟前') - } else if (between < 86400) { - return pluralize(~~(between / 3600), ' 小时前') + var currentTime = new Date().getTime() + var between = currentTime - time + var days = Math.floor(between / (24 * 3600 * 1000)) + if (days === 0) { + var leave1 = between % (24 * 3600 * 1000) + var hours = Math.floor(leave1 / (3600 * 1000)) + if (hours === 0) { + var leave2 = leave1 % (3600 * 1000) + var minutes = Math.floor(leave2 / (60 * 1000)) + if (minutes === 0) { + var leave3 = leave2 % (60 * 1000) + var seconds = Math.round(leave3 / 1000) + return seconds + ' 秒前' + } + return minutes + ' 分钟前' + } + return hours + ' 小时前' + } + if (days < 5) { + return days + ' 天前' } else { - return pluralize(~~(between / 86400), ' 天前') + return moment(time).format('YYYY-MM-DD HH:mm:ss') } } diff --git a/src/views/sheet/internal/JournalList.vue b/src/views/sheet/internal/JournalList.vue index 5de42411..6e7503ef 100644 --- a/src/views/sheet/internal/JournalList.vue +++ b/src/views/sheet/internal/JournalList.vue @@ -67,7 +67,8 @@