diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..8ae6c93 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,20 +1,41 @@ import Vue from 'vue' import Vuex from 'vuex' -import app from './modules/app' -import user from './modules/user' -import permission from './modules/permission' -import tagsView from './modules/tagsView' import getters from './getters' +// import app from './modules/app' +// import user from './modules/user' +// import permission from './modules/permission' +// import tagsView from './modules/tagsView' +// import tagsView from './modules/settings' + Vue.use(Vuex) +// 1.自定义添加modules方式,需要逐个import +// const modules = { +// app, +// user, +// permission, +// tagsView, +// settings +// } + +/** 2.自动添加全部modules文件夹下所有文件 + * 使用required.context实现从文件夹下自动化导入模块, + * required.context(读取文件路径,是否遍历文件夹子目录,匹配文件的正则),返回一个函数,此函数有3个属性 + * modulesFiles.keys()返回匹配成功模块的名字(如./app.js)的数组 + **/ +const modulesFiles = require.context('./modules', true, /\.js$/) +// arr.reduce(function(prev,cur,index,arr){...},init), +// 其中arr表示原数组,prev表示上次调用回调的返回值,cur当前正在处理的数组元素 +const modules = modulesFiles.keys().reduce((modules, modulePath) => { + // set './app.js' => 'app' + const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') + const value = modulesFiles(modulePath) + modules[moduleName] = value.default + return modules +}, {}) const store = new Vuex.Store({ - modules: { - app, - user, - permission, - tagsView - }, + modules, getters }) diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..8ae6c93 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,20 +1,41 @@ import Vue from 'vue' import Vuex from 'vuex' -import app from './modules/app' -import user from './modules/user' -import permission from './modules/permission' -import tagsView from './modules/tagsView' import getters from './getters' +// import app from './modules/app' +// import user from './modules/user' +// import permission from './modules/permission' +// import tagsView from './modules/tagsView' +// import tagsView from './modules/settings' + Vue.use(Vuex) +// 1.自定义添加modules方式,需要逐个import +// const modules = { +// app, +// user, +// permission, +// tagsView, +// settings +// } + +/** 2.自动添加全部modules文件夹下所有文件 + * 使用required.context实现从文件夹下自动化导入模块, + * required.context(读取文件路径,是否遍历文件夹子目录,匹配文件的正则),返回一个函数,此函数有3个属性 + * modulesFiles.keys()返回匹配成功模块的名字(如./app.js)的数组 + **/ +const modulesFiles = require.context('./modules', true, /\.js$/) +// arr.reduce(function(prev,cur,index,arr){...},init), +// 其中arr表示原数组,prev表示上次调用回调的返回值,cur当前正在处理的数组元素 +const modules = modulesFiles.keys().reduce((modules, modulePath) => { + // set './app.js' => 'app' + const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') + const value = modulesFiles(modulePath) + modules[moduleName] = value.default + return modules +}, {}) const store = new Vuex.Store({ - modules: { - app, - user, - permission, - tagsView - }, + modules, getters }) diff --git a/src/store/modules/app.js b/src/store/modules/app.js index c592ba0..f65d58f 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -2,65 +2,69 @@ import { getConfig } from '@/api/login' import { getPublicKey, setPublicKey } from '../../utils/auth' -const app = { - state: { - sidebar: { - opened: !+Cookies.get('sidebarStatus'), - withoutAnimation: false - }, - device: 'desktop', - kaptcha: '', - publicKey: getPublicKey(), - sid: '' +const state = { + sidebar: { + opened: !+Cookies.get('sidebarStatus'), + withoutAnimation: false }, - mutations: { - TOGGLE_SIDEBAR: state => { - if (state.sidebar.opened) { - Cookies.set('sidebarStatus', 1) - } else { - Cookies.set('sidebarStatus', 0) - } - state.sidebar.opened = !state.sidebar.opened - state.sidebar.withoutAnimation = false - }, - CLOSE_SIDEBAR: (state, withoutAnimation) => { + device: 'desktop', + kaptcha: '', // 验证码 + publicKey: getPublicKey(), // 公钥 + sid: '' // sid +} +const mutations = { + TOGGLE_SIDEBAR: state => { + if (state.sidebar.opened) { Cookies.set('sidebarStatus', 1) - state.sidebar.opened = false - state.sidebar.withoutAnimation = withoutAnimation - }, - TOGGLE_DEVICE: (state, device) => { - state.device = device - }, - SYS_CONFIG: (state, config) => { - state.kaptcha = config.kaptcha - state.publicKey = config.publicKey - state.sid = config.sid - setPublicKey(config.publicKey) + } else { + Cookies.set('sidebarStatus', 0) } + state.sidebar.opened = !state.sidebar.opened + state.sidebar.withoutAnimation = false }, - actions: { - ToggleSideBar: ({ commit }) => { - commit('TOGGLE_SIDEBAR') - }, - CloseSideBar({ commit }, { withoutAnimation }) { - commit('CLOSE_SIDEBAR', withoutAnimation) - }, - ToggleDevice({ commit }, device) { - commit('TOGGLE_DEVICE', device) - }, - // 获取系统配置 - GetConfig({ commit }) { - return new Promise((resolve, reject) => { - getConfig().then(response => { - const data = response.data - commit('SYS_CONFIG', data) - resolve() - }).catch(error => { - reject(error) - }) - }) - } + CLOSE_SIDEBAR: (state, withoutAnimation) => { + Cookies.set('sidebarStatus', 1) + state.sidebar.opened = false + state.sidebar.withoutAnimation = withoutAnimation + }, + TOGGLE_DEVICE: (state, device) => { + state.device = device + }, + SYS_CONFIG: (state, config) => { + state.kaptcha = config.kaptcha + state.publicKey = config.publicKey + state.sid = config.sid + setPublicKey(config.publicKey) } } -export default app +const actions = { + ToggleSideBar: ({ commit }) => { + commit('TOGGLE_SIDEBAR') + }, + CloseSideBar({ commit }, { withoutAnimation }) { + commit('CLOSE_SIDEBAR', withoutAnimation) + }, + ToggleDevice({ commit }, device) { + commit('TOGGLE_DEVICE', device) + }, + // 获取系统配置 + GetConfig({ commit }) { + return new Promise((resolve, reject) => { + getConfig().then(response => { + const data = response.data + commit('SYS_CONFIG', data) + resolve() + }).catch(error => { + reject(error) + }) + }) + } +} + +export default { + namespace: true, + state, + mutations, + actions +}