nebula/store/topic.js

183 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-07-31 17:40:17 +08:00
/**
* @file 分类数据状态 / ES module
* @module store/category
2020-08-01 00:39:29 +08:00
* @author Ronger <https://github.com/ronger-x>
2020-07-31 17:40:17 +08:00
*/
export const TOPIC_API_PATH = '/api/topic'
2020-08-11 22:39:59 +08:00
export const ADMIN_API_PATH = '/api/admin'
const getDefaultListData = () => {
return {
topics: [],
pagination: {}
}
}
2020-09-08 07:59:55 +08:00
const getDefaultTagsData = () => {
return {
tags: [],
pagination: {}
}
}
2020-07-31 17:40:17 +08:00
const getDefaultData = () => {
return {
topicTitle: '',
topicIconPath: '',
topicDescription: '',
topicNva: '0',
topicStatus: '0',
topicTagCount: 0,
topicSort: 10
}
}
2020-07-31 17:40:17 +08:00
export const state = () => {
return {
fetching: false,
2020-08-11 22:39:59 +08:00
data: [],
list: {
fetching: false,
data: getDefaultListData()
2020-09-08 07:59:55 +08:00
},
detail: {
fetching: false,
data: {}
},
tags: {
fetching: false,
data: getDefaultTagsData()
2020-08-11 22:39:59 +08:00
}
2020-07-31 17:40:17 +08:00
}
}
export const mutations = {
2020-08-11 22:39:59 +08:00
updateListFetching(state, action) {
state.list.fetching = action
},
updateListData(state, action) {
state.list.data = action
},
2020-07-31 17:40:17 +08:00
updateFetching(state, action) {
state.fetching = action
},
2020-08-11 22:39:59 +08:00
updateNavData(state, action) {
2020-07-31 17:40:17 +08:00
state.data = action
2020-09-08 07:59:55 +08:00
},
updateDetailFetching(state, action) {
state.detail.fetching = action
},
updateDetailData(state, action) {
state.detail.data = action
},
updateTopicTagsFetching(state, action) {
state.tags.fetching = action
},
updateTopicTagsData(state, action) {
state.tags.data = action
2020-07-31 17:40:17 +08:00
}
}
export const actions = {
2020-08-11 22:39:59 +08:00
fetchList({commit}, params = {}) {
2022-06-21 08:33:33 +08:00
if (params && params.reset === '0') {
return true;
}
2020-08-11 22:39:59 +08:00
// 清空已有数据
commit('updateListData', getDefaultListData())
commit('updateListFetching', true)
let data = {
page: params.page || 1
}
return this.$axios
.$get(`${ADMIN_API_PATH}/topics`, {
params: data
})
.then(response => {
commit('updateListFetching', false);
commit('updateListData', response);
})
.catch(error => {
console.log(error);
commit('updateListFetching', false);
});
},
fetchNavList({ commit }, params) {
2020-08-01 00:39:29 +08:00
commit('updateFetching', true);
2020-07-31 17:40:17 +08:00
return this.$axios
.$get(`${TOPIC_API_PATH}/topic-nav`)
.then(response => {
2020-08-11 22:39:59 +08:00
commit('updateNavData', response)
2020-07-31 17:40:17 +08:00
commit('updateFetching', false)
})
.catch(error => {
commit('updateFetching', false)
})
2020-09-08 07:59:55 +08:00
},
fetchDetail({ commit }, params) {
commit('updateDetailFetching', true);
return this.$axios
.$get(`${ADMIN_API_PATH}/topic/${params.topic_uri}`)
.then(response => {
commit('updateDetailData', response)
commit('updateDetailFetching', false)
})
.catch(error => {
commit('updateDetailFetching', false)
})
},
// 获取详情
fetchPostDetail({ commit }, params = {}) {
if (typeof params.topic_id === 'undefined') {
commit('updateDetailData', getDefaultData())
return;
}
commit('updateDetailFetching', true)
// commit('updateDetailData', {})
return this.$axios
.$get(`${ADMIN_API_PATH}/topic/detail/${params.topic_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)
})
},
2020-09-08 07:59:55 +08:00
fetchTopicTags({ commit }, params) {
commit('updateTopicTagsFetching', true);
return this.$axios
.$get(`${ADMIN_API_PATH}/topic/${params.topic_uri}/tags?page=${params.page}`)
.then(response => {
commit('updateTopicTagsData', response)
commit('updateTopicTagsFetching', false)
})
.catch(error => {
commit('updateTopicTagsFetching', false)
})
},
fetchUnBindTags({ commit }, params) {
commit('updateTopicTagsFetching', true);
return this.$axios
.$get(`${ADMIN_API_PATH}/topic/unbind-topic-tags`, {
params: params
})
.then(response => {
commit('updateTopicTagsData', response)
commit('updateTopicTagsFetching', false)
})
.catch(error => {
commit('updateTopicTagsFetching', false)
})
2020-07-31 17:40:17 +08:00
}
}