From ec1a661177c459f6e5eb608e72e96d3fa1a34d69 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 3 Sep 2019 16:58:30 +0800 Subject: [PATCH] Cache options by vuex. --- src/components/Tools/UserMenu.vue | 15 ++------ src/core/bootstrap.js | 5 +-- src/store/getters.js | 3 +- src/store/index.js | 4 ++- src/store/modules/option.js | 36 +++++++++++++++++++ src/store/modules/user.js | 28 +++++++++++---- src/store/mutation-types.js | 1 + .../comment/components/CommentDetail.vue | 11 +++--- src/views/comment/components/CommentTab.vue | 13 ++----- src/views/dashboard/Dashboard.vue | 21 +++-------- .../dashboard/components/RecentCommentTab.vue | 15 +++----- .../interface/components/ThemeSetting.vue | 13 +++---- src/views/post/PostList.vue | 20 +++++------ src/views/post/components/PostSetting.vue | 13 ++----- src/views/sheet/SheetList.vue | 15 +++----- src/views/sheet/components/SheetSetting.vue | 13 ++----- src/views/system/OptionForm.vue | 9 ++--- src/views/user/Login.vue | 3 +- src/views/user/Profile.vue | 21 +++++------ 19 files changed, 122 insertions(+), 137 deletions(-) create mode 100644 src/store/modules/option.js diff --git a/src/components/Tools/UserMenu.vue b/src/components/Tools/UserMenu.vue index d197efdf..4282eee2 100644 --- a/src/components/Tools/UserMenu.vue +++ b/src/components/Tools/UserMenu.vue @@ -63,7 +63,6 @@ import HeaderComment from './HeaderComment' import SettingDrawer from '@/components/SettingDrawer/SettingDrawer' import { mapActions, mapGetters } from 'vuex' -import optionApi from '@/api/option' export default { name: 'UserMenu', @@ -73,19 +72,14 @@ export default { }, data() { return { - optionVisible: true, - options: [], - keys: ['blog_url'] + optionVisible: true } }, mounted() { this.optionVisible = this.$refs.drawer.visible }, - created() { - this.loadOptions() - }, computed: { - ...mapGetters(['user']) + ...mapGetters(['user', 'options']) }, methods: { ...mapActions(['logout']), @@ -114,11 +108,6 @@ export default { showOptionModal() { this.optionVisible = this.$refs.drawer.visible this.$refs.drawer.toggle() - }, - loadOptions() { - optionApi.listAll(this.keys).then(response => { - this.options = response.data.data - }) } } } diff --git a/src/core/bootstrap.js b/src/core/bootstrap.js index 8922e305..ced01194 100644 --- a/src/core/bootstrap.js +++ b/src/core/bootstrap.js @@ -11,7 +11,8 @@ import { DEFAULT_FIXED_SIDEMENU, DEFAULT_CONTENT_WIDTH_TYPE, USER, - API_URL + API_URL, + OPTIONS } from '@/store/mutation-types' import config from '@/config/defaultSettings' @@ -27,6 +28,6 @@ export default function Initializer() { store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN)) store.commit('SET_USER', Vue.ls.get(USER)) store.commit('SET_API_URL', Vue.ls.get(API_URL)) - + store.commit('SET_OPTIONS', Vue.ls.get(OPTIONS)) // last step } diff --git a/src/store/getters.js b/src/store/getters.js index 089b8517..d9b627dd 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -12,7 +12,8 @@ const getters = { return state.app.apiUrl } return `${window.location.protocol}//${window.location.host}` - } + }, + options: state => state.option.options } export default getters diff --git a/src/store/index.js b/src/store/index.js index 4e8529a2..5771af7e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import Vuex from 'vuex' import app from './modules/app' import user from './modules/user' import permission from './modules/permission' +import option from './modules/option' import getters from './getters' Vue.use(Vuex) @@ -12,7 +13,8 @@ export default new Vuex.Store({ modules: { app, user, - permission + permission, + option }, state: { diff --git a/src/store/modules/option.js b/src/store/modules/option.js new file mode 100644 index 00000000..d27dba80 --- /dev/null +++ b/src/store/modules/option.js @@ -0,0 +1,36 @@ +import Vue from 'vue' +import { + OPTIONS +} from '@/store/mutation-types' +import optionApi from '@/api/option' +const keys = ['blog_url'] +const option = { + state: { + options: [] + }, + mutations: { + SET_OPTIONS: (state, options) => { + Vue.ls.set(OPTIONS, options) + state.options = options + } + }, + actions: { + loadOptions({ + commit + }) { + return new Promise((resolve, reject) => { + optionApi + .listAll(keys) + .then(response => { + commit('SET_OPTIONS', response.data.data) + resolve(response) + }) + .catch(error => { + reject(error) + }) + }) + } + } +} + +export default option diff --git a/src/store/modules/user.js b/src/store/modules/user.js index acdd27ff..3d5bee89 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,8 @@ import Vue from 'vue' -import { ACCESS_TOKEN, USER } from '@/store/mutation-types' +import { + ACCESS_TOKEN, + USER +} from '@/store/mutation-types' import adminApi from '@/api/admin' import userApi from '@/api/user' @@ -16,7 +19,9 @@ const user = { Vue.ls.set(ACCESS_TOKEN, token) state.token = token }, - SET_NAME: (state, { name }) => { + SET_NAME: (state, { + name + }) => { state.name = name }, SET_AVATAR: (state, avatar) => { @@ -35,7 +40,9 @@ const user = { } }, actions: { - loadUser({ commit }) { + loadUser({ + commit + }) { return new Promise((resolve, reject) => { userApi .getProfile() @@ -48,7 +55,12 @@ const user = { }) }) }, - login({ commit }, { username, password }) { + login({ + commit + }, { + username, + password + }) { return new Promise((resolve, reject) => { adminApi .login(username, password) @@ -64,7 +76,9 @@ const user = { }) }) }, - logout({ commit }) { + logout({ + commit + }) { return new Promise(resolve => { commit('CLEAR_TOKEN') adminApi @@ -77,7 +91,9 @@ const user = { }) }) }, - refreshToken({ commit }, refreshToken) { + refreshToken({ + commit + }, refreshToken) { return new Promise((resolve, reject) => { adminApi .refreshToken(refreshToken) diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js index 75f5cc7c..d227fc6f 100644 --- a/src/store/mutation-types.js +++ b/src/store/mutation-types.js @@ -9,6 +9,7 @@ export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN' export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE' export const USER = 'USER' export const API_URL = 'API_URL' +export const OPTIONS = 'OPTIONS' export const CONTENT_WIDTH_TYPE = { Fluid: 'Fluid', diff --git a/src/views/comment/components/CommentDetail.vue b/src/views/comment/components/CommentDetail.vue index 93c8601b..aee9abd8 100644 --- a/src/views/comment/components/CommentDetail.vue +++ b/src/views/comment/components/CommentDetail.vue @@ -126,8 +126,8 @@