nebula/store/admin.js

229 lines
4.9 KiB
JavaScript
Raw Normal View History

export const ADMIN_API_PATH = '/api/admin'
const getDefaultUsersData = () => {
return {
users: [],
pagination: {}
}
}
const getDefaultRolesData = () => {
return {
role: [],
pagination: {}
}
}
2021-06-28 08:08:24 +08:00
const getDefaultArticlesData = () => {
return {
articles: [],
pagination: {}
}
}
const getDefaultCommentsData = () => {
return {
comments: [],
pagination: {}
}
}
2022-07-25 06:45:33 +08:00
const getDefaultProductsData = () => {
return {
products: [],
pagination: {}
}
}
export const state = () => {
return {
fetching: false,
2022-07-25 06:45:33 +08:00
users: getDefaultUsersData(),
roles: getDefaultRolesData(),
articles: getDefaultArticlesData(),
comments: getDefaultCommentsData(),
products: getDefaultProductsData(),
2022-03-03 11:26:42 +08:00
tabs: [
{
title: 'Dashboard',
name: 'admin-dashboard',
path: '/admin/dashboard',
icon: 'el-icon-s-data',
closable: false
}
],
activeTab: 'admin-dashboard'
}
}
export const mutations = {
updateFetching(state, action) {
state.fetching = action
},
updateUsersData(state, action) {
2022-07-25 06:45:33 +08:00
state.users = action
},
updateRolesData(state, action) {
2022-07-25 06:45:33 +08:00
state.roles = action
2021-06-28 08:08:24 +08:00
},
updateArticlesData(state, action) {
2022-07-25 06:45:33 +08:00
state.articles = action
2021-06-28 08:08:24 +08:00
},
updateCommentsData(state, action) {
2022-07-25 06:45:33 +08:00
state.comments = action
},
updateProductsData(state, action) {
state.products = action
2021-06-28 08:08:24 +08:00
},
updateArticlePreference(state, action) {
2022-07-25 06:45:33 +08:00
let articles = state.articles.list[action.index]
if (articles.idArticle === action.idArticle) {
articles.articlePerfect = action.articlePerfect
2021-06-28 08:08:24 +08:00
}
2022-03-03 11:26:42 +08:00
},
updateTags(state, action) {
state.tabs = action
},
pushTags(state, action) {
let tabs = state.tabs
tabs.push(action)
},
updateActiveTab(state, action) {
state.activeTab = action
}
}
export const actions = {
fetchUsers({commit}, params = {}) {
2022-06-21 08:33:33 +08:00
if (params && params.reset === '0') {
return true;
}
// 清空已有数据
commit('updateUsersData', getDefaultUsersData())
commit('updateFetching', true)
let data = {
page: params.page || 1,
rows: params.rows || 10
}
return this.$axios
.$get(`${ADMIN_API_PATH}/users`, {
params: data
})
.then(response => {
commit('updateFetching', false);
commit('updateUsersData', response);
})
.catch(error => {
console.log(error);
commit('updateFetching', false);
});
},
fetchRoles({commit}, params = {}) {
2022-06-21 08:33:33 +08:00
if (params && params.reset === '0') {
return true;
}
// 清空已有数据
commit('updateRolesData', getDefaultRolesData())
commit('updateFetching', true)
let data = {
page: params.page || 1,
rows: params.rows || 10
}
return this.$axios
.$get(`${ADMIN_API_PATH}/roles`, {
params: data
})
.then(response => {
commit('updateFetching', false);
commit('updateRolesData', response);
})
.catch(error => {
console.log(error);
commit('updateFetching', false);
});
2021-06-28 08:08:24 +08:00
},
fetchArticles({commit}, params = {}) {
2022-06-21 08:33:33 +08:00
if (params && params.reset === '0') {
return true;
}
2021-06-28 08:08:24 +08:00
// 清空已有数据
commit('updateArticlesData', getDefaultArticlesData())
commit('updateFetching', true)
let data = {
page: params.page || 1,
rows: params.rows || 10,
topicUri: 'news'
}
return this.$axios
.$get(`${ADMIN_API_PATH}/articles`, {
params: data
})
.then(response => {
commit('updateFetching', false);
commit('updateArticlesData', response);
})
.catch(error => {
console.log(error);
commit('updateFetching', false);
});
},
fetchComments({commit}, params = {}) {
2022-06-21 08:33:33 +08:00
if (params && params.reset === '0') {
return true;
}
2021-06-28 08:08:24 +08:00
// 清空已有数据
2022-07-25 06:45:33 +08:00
commit('updateCommentsData', getDefaultCommentsData())
2021-06-28 08:08:24 +08:00
commit('updateFetching', true)
let data = {
page: params.page || 1,
rows: params.rows || 10
}
return this.$axios
.$get(`${ADMIN_API_PATH}/comments`, {
params: data
})
.then(response => {
commit('updateFetching', false);
commit('updateCommentsData', response);
})
.catch(error => {
console.log(error);
commit('updateFetching', false);
});
2022-07-25 06:45:33 +08:00
},
fetchProducts({commit}, params = {}) {
if (params && params.reset === '0') {
return true;
}
// 清空已有数据
commit('updateProductsData', getDefaultProductsData())
commit('updateFetching', true)
let data = {
page: params.page || 1,
rows: params.rows || 10
}
return this.$axios
.$get(`${ADMIN_API_PATH}/products`, {
params: data
})
.then(response => {
commit('updateFetching', false);
commit('updateProductsData', response);
})
.catch(error => {
console.log(error);
commit('updateFetching', false);
});
}
}