Reactor util.js.

pull/9/head
ruibaby 6 years ago
parent ae5ffd9d3a
commit a98566e4cd

@ -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

@ -1,3 +1,5 @@
import moment from 'moment'
import 'moment/locale/zh-cn'
/** /**
* 触发 window.resize * 触发 window.resize
*/ */
@ -22,24 +24,28 @@ export function removeLoadingAnimate(id = '', timeout = 1500) {
}, timeout) }, timeout)
} }
function pluralize(time, label) {
if (time === 1) {
return time + label
}
return time + label
}
/**
* time ago
* @param {*} time
*/
export function timeAgo(time) { export function timeAgo(time) {
const between = (Date.now() - Number(time)) / 1000 var currentTime = new Date().getTime()
if (between < 3600) { var between = currentTime - time
return pluralize(~~(between / 60), ' 分钟前') var days = Math.floor(between / (24 * 3600 * 1000))
} else if (between < 86400) { if (days === 0) {
return pluralize(~~(between / 3600), ' 小时前') 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 { } else {
return pluralize(~~(between / 86400), ' 天前') return moment(time).format('YYYY-MM-DD HH:mm:ss')
} }
} }

@ -67,7 +67,8 @@
<a-list <a-list
itemLayout="vertical" itemLayout="vertical"
:pagination="pagination" :pagination="pagination"
:dataSource="listData" :dataSource="journals"
:loading="listLoading"
> >
<a-list-item <a-list-item
slot="renderItem" slot="renderItem"
@ -119,6 +120,8 @@
</template> </template>
<script> <script>
import journalApi from '@/api/journal'
const listData = [] const listData = []
for (let i = 0; i < 50; i++) { for (let i = 0; i < 50; i++) {
listData.push({ listData.push({
@ -135,18 +138,41 @@ export default {
data() { data() {
return { return {
listData, listData,
listLoading: false,
visible: false, visible: false,
pagination: { pagination: {
onChange: page => { page: 1,
console.log(page) size: 10,
sort: null
}, },
pageSize: 10 queryParam: {
page: 0,
size: 10,
sort: null,
keyword: null
}, },
actions: [{ type: 'star-o', text: '156' }, { type: 'like-o', text: '156' }, { type: 'message', text: '2' }], actions: [{ type: 'star-o', text: '156' }, { type: 'like-o', text: '156' }, { type: 'message', text: '2' }],
journal: {} journals: {}
} }
}, },
created() {
this.loadJournals()
},
methods: { methods: {
loadJournals(isSearch) {
this.queryParam.page = this.pagination.page - 1
this.queryParam.size = this.pagination.size
this.queryParam.sort = this.pagination.sort
if (isSearch) {
this.queryParam.page = 0
}
this.listLoading = true
journalApi.query(this.queryParam).then(response => {
this.journals = response.data.data.content
this.pagination.total = response.data.data.total
this.listLoading = false
})
},
handleNew() { handleNew() {
this.visible = true this.visible = true
}, },

Loading…
Cancel
Save