nebula/store/article.js

209 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-06-21 21:56:34 +08:00
import Vue from 'vue';
2020-08-02 00:25:44 +08:00
export const ARTICLE_API_PATH = '/api/article'
export const BASE_API_PATH = '/api/console'
2020-06-21 21:56:34 +08:00
const getDefaultListData = () => {
return {
2020-06-22 17:13:42 +08:00
articles: [],
pagination: {}
2020-06-21 21:56:34 +08:00
}
}
2020-06-22 17:13:42 +08:00
export const state = () => {
return {
list: {
fetching: false,
data: getDefaultListData()
},
2021-05-07 08:26:51 +08:00
announcements: {
fetching: false,
data: getDefaultListData()
},
2020-06-22 17:13:42 +08:00
detail: {
fetching: false,
data: {}
},
topicUri: ''
2020-06-21 21:56:34 +08:00
}
2020-06-22 17:13:42 +08:00
}
2020-06-21 21:56:34 +08:00
2020-06-22 17:13:42 +08:00
export const mutations = {
2020-06-21 21:56:34 +08:00
// 文章列表
updateListFetching(state, action) {
state.list.fetching = action
},
updateListData(state, action) {
state.list.data = action
},
2021-05-07 08:26:51 +08:00
updateAnnouncementListFetching(state, action) {
state.announcements.fetching = action
},
updateAnnouncementListData(state, action) {
state.announcements.data = action
},
2020-06-22 17:13:42 +08:00
updateExistingListData(state, action) {
state.list.data.data.push(...action.data)
state.list.data.pagination = action.pagination
},
2020-06-21 21:56:34 +08:00
// 文章详情
updateDetailFetching(state, action) {
state.detail.fetching = action
},
updateDetailData(state, action) {
2020-06-23 17:40:43 +08:00
state.detail.data = action.article
2020-06-22 17:13:42 +08:00
},
2020-08-02 00:25:44 +08:00
clearDetailData(state, action) {
state.detail.data = {}
},
2020-06-22 17:13:42 +08:00
// 更新文章阅读全文状态
updateDetailRenderedState(state, action) {
Vue.set(
state.detail.data,
'isRenderedFullContent',
action == null ? true : action
)
},
updateArticleThumbsUpCount(state, action) {
state.detail.data.articleThumbsUpCount += action.thumbsUpNumber
},
updateArticleSponsorCount(state, action) {
state.detail.data.articleSponsorCount += action.sponsorNumber
},
updateTopicUri(state, action) {
state.topicUri = action
2020-06-21 21:56:34 +08:00
}
2020-06-22 17:13:42 +08:00
}
2020-06-21 21:56:34 +08:00
2020-06-22 17:13:42 +08:00
export const actions = {
2021-05-07 08:26:51 +08:00
// 获取公告列表
fetchAnnouncementList({commit, state}, params = {}) {
commit('updateAnnouncementListFetching', true)
// 清空已有数据
commit('updateListData', getDefaultListData())
let data = {
page: params.page || 1
}
return this.$axios
.$get(`${BASE_API_PATH}/announcements`, {
params: data
})
.then(response => {
commit('updateAnnouncementListFetching', false);
commit('updateAnnouncementListData', response);
})
.catch(error => {
console.log(error);
commit('updateAnnouncementListFetching', false);
});
},
2020-06-21 21:56:34 +08:00
// 获取文章列表
fetchList({commit, state}, params = {}) {
commit('updateListFetching', true)
// 当前页判断
let currentData = JSON.parse(JSON.stringify(state)).list.data
let topicUri = JSON.parse(JSON.stringify(state)).topicUri
if (Number(params.page) === currentData.pagination.currentPage) {
if (topicUri && topicUri === params.topic_uri) {
commit('updateListFetching', false)
return
}
}
2020-06-21 21:56:34 +08:00
// 清空已有数据
2020-07-02 23:45:27 +08:00
commit('updateListData', getDefaultListData())
2020-08-01 00:41:54 +08:00
let data = {
2020-08-02 00:25:44 +08:00
page: params.page || 1,
topicUri: params.topic_uri || ''
2020-08-01 00:41:54 +08:00
}
2020-06-21 21:56:34 +08:00
return this.$axios
2020-08-02 00:25:44 +08:00
.$get(`${BASE_API_PATH}/articles`, {
2020-08-01 00:41:54 +08:00
params: data
})
2020-06-21 21:56:34 +08:00
.then(response => {
2020-08-02 00:25:44 +08:00
commit('updateListFetching', false);
commit('updateListData', response);
commit('updateTopicUri', params.topic_uri || '');
2020-06-21 21:56:34 +08:00
})
2020-08-02 00:25:44 +08:00
.catch(error => {
console.log(error);
commit('updateListFetching', false);
});
2020-06-22 17:13:42 +08:00
},
// 获取文章详情
fetchDetail({commit, state}, params = {}) {
2020-06-23 17:40:43 +08:00
// const delay = fetchDelay(
// isBrowser
// )
2020-06-30 17:50:32 +08:00
// if (isBrowser) {
// Vue.nextTick(() => {
// window.scrollTo(0, 300);
// })
// }
2020-06-22 17:13:42 +08:00
commit('updateDetailFetching', true)
// 当前文章判断
let currentData = JSON.parse(JSON.stringify(state)).detail.data
if (Number(params.article_id) === currentData?.idArticle) {
commit('updateDetailFetching', false)
return
}
2020-06-30 17:50:32 +08:00
// commit('updateDetailData', {})
2020-06-22 17:13:42 +08:00
return this.$axios
2020-08-02 00:25:44 +08:00
.$get(`${BASE_API_PATH}/article/${params.article_id}`)
.then(response => {
return new Promise(resolve => {
commit('updateDetailData', response)
commit('updateDetailFetching', false)
resolve(response)
// delay(() => {
// resolve(response)
// })
})
})
.catch(error => {
commit('updateDetailFetching', false)
return Promise.reject(error)
})
},
// 获取文章详情
fetchPostDetail({ commit }, params = {}) {
// const delay = fetchDelay(
// isBrowser
// )
// if (isBrowser) {
// Vue.nextTick(() => {
// window.scrollTo(0, 300);
// })
// }
if (typeof params.article_id === 'undefined') {
commit('updateDetailData', getDefaultListData())
return;
}
commit('updateDetailFetching', true)
// commit('updateDetailData', {})
return this.$axios
.$get(`${ARTICLE_API_PATH}/detail/${params.article_id}`)
2020-06-22 17:13:42 +08:00
.then(response => {
2020-06-30 17:50:32 +08:00
return new Promise(resolve => {
2020-07-03 14:43:14 +08:00
commit('updateDetailData', response)
2020-06-30 17:50:32 +08:00
commit('updateDetailFetching', false)
resolve(response)
// delay(() => {
// resolve(response)
// })
})
2020-06-22 17:13:42 +08:00
})
.catch(error => {
commit('updateDetailFetching', false)
return Promise.reject(error)
})
},
updateThumbsUpCount({ commit }, params = {}) {
commit('updateArticleThumbsUpCount', params)
},
updateSponsorCount({ commit }, params = {}) {
commit('updateArticleSponsorCount', params)
2020-06-21 21:56:34 +08:00
}
2020-06-22 17:13:42 +08:00
}