detail.js
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const FETCH_DETAIL_INFO = 'FETCH_DETAIL_INFO';
const UPDATE_ARTICLE_PRAISE_INFO = 'UPDATE_ARTICLE_PRAISE_INFO';
const FETCH_RECCOMMEND_ARTICLES = 'FETCH_RECCOMMEND_ARTICLES';
const UPDATE_RECCOMMEND_ARTICLES_PRAISE = 'UPDATE_RECCOMMEND_ARTICLES_PRAISE';
import { UPDATE_ARTICLE_PRAISE } from './list/types';
export default function() {
return {
namespaced: true,
state: {
// sort: 1 笔记 4 视频
detailInfo: {},
recommendArticleList: []
},
actions: {
async fetchDetailInfo({commit}, {articleId}) {
const res = await this.$api.get('/api/grass/columnArticleDetail', {
articleId,
singleDetail: 'Y',
fromXianyu: 'Y'
});
if (res.code === 200) {
// 历史原因返回为List
commit(FETCH_DETAIL_INFO, res.data.detailList[0] || {});
}
},
async updatePraiseStatus({commit}, {articleId, status}) {
const res = await this.$api.get('/api/grass/updateArticlePraise', {
articleId,
status
});
if (res.code === 200) {
commit(`article/articleList/${UPDATE_ARTICLE_PRAISE}`, {articleId, status}, {root: true});
commit(UPDATE_ARTICLE_PRAISE_INFO);
commit(UPDATE_RECCOMMEND_ARTICLES_PRAISE, {articleId, status});
}
},
async fetchRecommendArticle({commit}, {articleId}) {
const res = await this.$api.get('/api/grass/xianyuOtherArticles', {
articleId
});
if (res.code === 200) {
commit(FETCH_RECCOMMEND_ARTICLES, res.data);
}
}
},
mutations: {
// 获取详情
[FETCH_DETAIL_INFO](state, detailInfo) {
state.detailInfo = detailInfo;
},
[UPDATE_ARTICLE_PRAISE_INFO](state) {
let {hasPraise, praiseCount} = state.detailInfo;
state.detailInfo = {
...state.detailInfo,
hasPraise: hasPraise === 'Y' ? 'N' : 'Y',
praiseCount: hasPraise === 'Y' ? praiseCount - 1 : praiseCount + 1
};
},
[UPDATE_RECCOMMEND_ARTICLES_PRAISE](state, {articleId, status}) {
let article = state.recommendArticleList.find(item => +item.articleId === +articleId);
if (article && article.articleId === parseInt(articleId, 10)) {
if (article.praiseCount === 0 && status === 0) {
article.praiseCount += 1;
article.hasPraised = 'Y';
} else if (article.praiseCount > 0) {
if (status === 0) {
article.praiseCount += 1;
article.hasPraised = 'Y';
} else {
article.praiseCount -= 1;
article.hasPraised = 'N';
}
}
}
},
[FETCH_RECCOMMEND_ARTICLES](state, list = []) {
state.recommendArticleList = list.map((item)=> {
let imageHeight = item.imageHeight;
let imageWidth = item.imageWidth;
if (imageWidth > 350) {
imageWidth = 350;
imageHeight = parseInt(350 * item.imageHeight / item.imageWidth, 10);
}
if (item.dataType === 1) {
item.authorHeadIco = item.authorHeadIco.replace(/{mode}/, 2).replace(/{width}/, 40).replace(/{height}/, 40);
item.coverImage = item.coverImage.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
} else if (item.dataType === 2) {
item.resourceSrc = item.resourceSrc.replace(/{mode}/, 2).replace(/{width}/, imageWidth).replace(/{height}/, imageHeight).replace(/\/format\/webp/, '');
}
item.imageNewWidth = imageWidth;
item.imageNewHeight = imageHeight;
return item;
});
}
},
getters: {
authorInfo({detailInfo}) {
const {authorHeadIco, authorName, authorType, authorUid} = detailInfo;
return {authorHeadIco, authorName, authorType, authorUid};
}
}
};
}