Skip to content

Commit 720c6db

Browse files
author
reco_luan
committed
refactor: get post data
use mixin(post)
1 parent 74534b1 commit 720c6db

File tree

11 files changed

+80
-79
lines changed

11 files changed

+80
-79
lines changed

components/Common.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ export default {
169169
}
170170
},
171171
172-
created () {
173-
this._getPostData()
174-
},
175-
176172
mounted () {
177173
this.$router.afterEach(() => {
178174
this.isSidebarOpen = false

components/HomeBlog.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
<div class="blog-list">
1212
<!-- 博客列表 -->
1313
<note-abstract
14-
:data="$themeConfig.posts"
14+
:data="$recoPosts"
1515
:hideAccessNumber="true"
1616
:currentPage="currentPage"></note-abstract>
1717
<!-- 分页 -->
1818
<pagation
1919
class="pagation"
20-
:total="$themeConfig.posts.length"
20+
:total="$recoPosts.length"
2121
:currentPage="currentPage"
2222
@getCurrentPage="getCurrentPage" />
2323
</div>
@@ -26,7 +26,7 @@
2626
<h3 class="name" v-if="$themeConfig.author || $site.title">{{ $themeConfig.author || $site.title }}</h3>
2727
<div class="num">
2828
<div>
29-
<h3>{{$themeConfig.posts.length}}</h3>
29+
<h3>{{$recoPosts.length}}</h3>
3030
<h6>文章</h6>
3131
</div>
3232
<div>

enhanceApp.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import postMixin from '@theme/mixins/posts'
2+
3+
export default ({
4+
Vue
5+
}) => {
6+
Vue.mixin(postMixin)
7+
}

helpers/postData.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { compareTime } from '@theme/util'
2+
3+
// 过滤博客数据
4+
export function filterPostData (posts, isTimeline) {
5+
posts = posts.filter(item => {
6+
const { title, frontmatter: { home, date, publish }} = item
7+
return isTimeline === true
8+
? !(home == true || title == undefined || date === undefined || publish === false)
9+
: !(home == true || title == undefined || publish === false)
10+
})
11+
return posts
12+
}
13+
14+
// 排序博客数据
15+
export function sortPostData (posts) {
16+
posts.sort((a, b) => {
17+
const aSticky = a.frontmatter.sticky
18+
const bSticky = b.frontmatter.sticky
19+
if (aSticky && bSticky) {
20+
return aSticky == bSticky ? compareTime(a, b) : (aSticky - bSticky)
21+
} else if (aSticky && !bSticky) {
22+
return -1
23+
} else if (!aSticky && bSticky) {
24+
return 1
25+
}
26+
return compareTime(a, b)
27+
})
28+
}

layouts/Category.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import Common from '@theme/components/Common.vue'
3838
import NoteAbstract from '@theme/components/NoteAbstract.vue'
3939
import mixin from '@theme/mixins/index.js'
40+
import { sortPostData, filterPostData } from '@theme/helpers/postData'
4041
4142
export default {
4243
mixins: [mixin],
@@ -54,8 +55,8 @@ export default {
5455
// 时间降序后的博客列表
5556
posts () {
5657
let posts = this.$currentCategories.pages
57-
posts = this._filterPostData(posts)
58-
this._sortPostData(posts)
58+
posts = filterPostData(posts)
59+
sortPostData(posts)
5960
return posts
6061
},
6162
// 标题只显示分类名称
@@ -85,10 +86,6 @@ export default {
8586
this.currentPage = page
8687
this.$page.currentPage = page
8788
this._setStoragePage(page)
88-
},
89-
// 获取时间的数字类型
90-
_getTimeNum (date) {
91-
return parseInt(new Date(date.frontmatter.date).getTime())
9289
}
9390
},
9491

layouts/Tag.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Common from '@theme/components/Common.vue'
2727
import NoteAbstract from '@theme/components/NoteAbstract.vue'
2828
import TagList from '@theme/components/TagList.vue'
2929
import mixin from '@theme/mixins/index.js'
30+
import { sortPostData, filterPostData } from '@theme/helpers/postData'
3031
3132
export default {
3233
mixins: [mixin],
@@ -45,8 +46,8 @@ export default {
4546
// 时间降序后的博客列表
4647
posts () {
4748
let posts = this.$currentTags.pages
48-
posts = this._filterPostData(posts)
49-
this._sortPostData(posts)
49+
posts = filterPostData(posts)
50+
sortPostData(posts)
5051
return posts
5152
}
5253
},

layouts/Tags.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
<TagList :currentTag="currentTag" @getCurrentTag="tagClick"></TagList>
55
<note-abstract
66
class="list"
7-
:data="posts"
7+
:data="$recoPosts"
88
:currentPage="currentPage"
99
:currentTag="currentTag"
1010
@currentTag="getCurrentTag"></note-abstract>
1111

1212
<pagation
1313
class="pagation"
14-
:total="posts.length"
14+
:total="$recoPosts.length"
1515
:currentPage="currentPage"
1616
@getCurrentPage="getCurrentPage"></pagation>
1717
</Common>
@@ -36,12 +36,6 @@ export default {
3636
allTagName: '全部'
3737
}
3838
},
39-
computed: {
40-
// 时间降序后的博客列表
41-
posts () {
42-
return this.$themeConfig.posts || this.$site.pages
43-
}
44-
},
4539
4640
created () {
4741
if (this.$tags.list.length > 0) {

layouts/TimeLines.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<script>
2222
import Common from '@theme/components/Common.vue'
2323
import mixin from '@theme/mixins/index.js'
24+
import { filterPostData, sortPostData } from '@theme/helpers/postData'
2425
2526
export default {
2627
mixins: [mixin],
@@ -55,7 +56,7 @@ export default {
5556
getPages (tag) {
5657
let pages = this.$site.pages
5758
// 时间轴不进行制定处理
58-
pages = this._filterPostData(pages, true)
59+
pages = filterPostData(pages, true)
5960
// reverse()是为了按时间最近排序排序
6061
this.pages = pages.length == 0 ? [] : pages
6162
for (let i = 0, length = pages.length; i < length; i++) {
@@ -69,7 +70,7 @@ export default {
6970
7071
for (const key in this.formatPages) {
7172
const data = this.formatPages[key]
72-
this._sortPostData(data)
73+
sortPostData(data)
7374
this.formatPagesArr.unshift({
7475
year: key,
7576
data
@@ -93,10 +94,6 @@ export default {
9394
// 跳转
9495
go (url) {
9596
this.$router.push({ path: url })
96-
},
97-
// 获取时间的数字类型
98-
_getTimeNum (date) {
99-
return parseInt(new Date(date.frontmatter.date).getTime())
10097
}
10198
}
10299
}

mixins/index.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,10 @@ export default {
55
const index = Math.floor(Math.random() * tagColorArr.length)
66
return tagColorArr[index]
77
},
8-
_filterPostData (posts, isTimeline) {
9-
posts = posts.filter(item => {
10-
const { title, frontmatter: { home, date, publish }} = item
11-
return isTimeline === true
12-
? !(home == true || title == undefined || date === undefined || publish === false)
13-
: !(home == true || title == undefined || publish === false)
14-
})
15-
return posts
16-
},
17-
_sortPostData (posts) {
18-
posts.sort((a, b) => {
19-
const aSticky = a.frontmatter.sticky
20-
const bSticky = b.frontmatter.sticky
21-
if (aSticky && bSticky) {
22-
return aSticky == bSticky ? this._compareTime(a, b) : (aSticky - bSticky)
23-
} else if (aSticky && !bSticky) {
24-
return -1
25-
} else if (!aSticky && bSticky) {
26-
return 1
27-
}
28-
return this._compareTime(a, b)
29-
})
30-
},
318
// 获取时间的数字类型
329
_getTimeNum (date) {
3310
return parseInt(new Date(date.frontmatter.date).getTime())
3411
},
35-
// 比对时间
36-
_compareTime (a, b) {
37-
return this._getTimeNum(b) - this._getTimeNum(a)
38-
},
39-
// 获取博客数据
40-
_getPostData () {
41-
return new Promise(resolve => {
42-
if (!this.$themeConfig.posts) {
43-
const {
44-
$categories: { list: articles },
45-
_filterPostData,
46-
_sortPostData
47-
} = this
48-
49-
let posts = articles.reduce((allData, currnetData) => {
50-
return [...allData, ...currnetData.pages]
51-
}, [])
52-
53-
posts = _filterPostData(posts)
54-
_sortPostData(posts)
55-
56-
this.$themeConfig.posts = posts
57-
resolve(posts)
58-
}
59-
})
60-
},
6112
// 获取当前页码
6213
_getStoragePage () {
6314
const path = window.location.pathname
@@ -76,4 +27,4 @@ export default {
7627
sessionStorage.setItem('currentPage', JSON.stringify({ page, path }))
7728
}
7829
}
79-
}
30+
}

mixins/posts.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { filterPostData, sortPostData } from '../helpers/postData'
2+
3+
export default {
4+
computed: {
5+
$recoPosts () {
6+
const {
7+
$categories: { list: articles }
8+
} = this
9+
10+
let posts = articles.reduce((allData, currnetData) => {
11+
return [...allData, ...currnetData.pages]
12+
}, [])
13+
14+
posts = filterPostData(posts)
15+
sortPostData(posts)
16+
17+
return posts
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)