diff --git a/src/layout/header-aside/components/header-user/index.vue b/src/layout/header-aside/components/header-user/index.vue index e5d25609..735d6c90 100644 --- a/src/layout/header-aside/components/header-user/index.vue +++ b/src/layout/header-aside/components/header-user/index.vue @@ -19,7 +19,7 @@ export default { ]) }, methods: { - ...mapActions('d2admin', [ + ...mapActions('d2admin/account', [ 'logout' ]), /** diff --git a/src/main.js b/src/main.js index eab0fcb7..8383fe04 100644 --- a/src/main.js +++ b/src/main.js @@ -62,7 +62,7 @@ new Vue({ // 展示系统信息 util.showInfo() // 用户登陆后从数据库加载一系列的设置 - this.$store.commit('d2admin/loginSuccessLoad') + this.$store.commit('d2admin/account/load') // 初始化全屏监听 this.fullscreenListenerInit() }, diff --git a/src/pages/login/page.vue b/src/pages/login/page.vue index 6945f616..83d979e7 100644 --- a/src/pages/login/page.vue +++ b/src/pages/login/page.vue @@ -105,7 +105,7 @@ export default { particlesJS('login', config) }, methods: { - ...mapActions('d2admin', [ + ...mapActions('d2admin/account', [ 'login' ]), /** diff --git a/src/store/modules/d2admin/index.js.REMOVED.git-id b/src/store/modules/d2admin/index.js.REMOVED.git-id index fb0aad66..801fe7b9 100644 --- a/src/store/modules/d2admin/index.js.REMOVED.git-id +++ b/src/store/modules/d2admin/index.js.REMOVED.git-id @@ -1 +1 @@ -2eb4adc620a4ffb94292cc6c574b7290fccf8356 \ No newline at end of file +4ece1c4e7a74f137bfa094cbb973d62c91bde205 \ No newline at end of file diff --git a/src/store/modules/d2admin/modules/account.js b/src/store/modules/d2admin/modules/account.js new file mode 100644 index 00000000..5c153805 --- /dev/null +++ b/src/store/modules/d2admin/modules/account.js @@ -0,0 +1,115 @@ +import util from '@/libs/util.js' + +export default { + namespaced: true, + actions: { + /** + * 登陆 + * @param {Object} param context + * @param {Object} param vm {Object} vue 实例 + * @param {Object} param username {String} 用户账号 + * @param {Object} param password {String} 密码 + */ + login ({ commit }, { vm, username, password }) { + // 开始请求登录接口 + vm.$axios({ + method: 'post', + url: '/login', + data: { + username, + password + } + }) + .then(res => { + // 设置 cookie 一定要存 uuid 和 token 两个 cookie + // 整个系统依赖这两个数据进行校验和存储 + // uuid 是用户身份唯一标识 用户注册的时候确定 并且不可改变 不可重复 + // token 代表用户当前登录状态 建议在网络请求中携带 token + // 如有必要 token 需要定时更新,默认保存一天 + util.cookies.set('uuid', res.data.uuid) + util.cookies.set('token', res.data.token) + // 设置 vuex 用户信息 + commit('d2admin/user/set', { + name: res.data.name + }, { + root: true + }) + // 用户登陆后从数据库加载一系列的设置 + commit('d2admin/account/load', null, { + root: true + }) + // 跳转路由 + vm.$router.push({ + name: 'index' + }) + }) + .catch(err => { + console.group('登陆结果') + console.log('err: ', err) + console.groupEnd() + }) + }, + /** + * 注销用户并返回登陆页面 + * @param {Object} param context + * @param {Object} param vm {Object} vue 实例 + * @param {Object} param confirm {Boolean} 是否需要确认 + */ + logout ({ commit }, { vm, confirm }) { + /** + * @description 注销 + */ + function logout () { + // 删除cookie + util.cookies.remove('token') + util.cookies.remove('uuid') + // 跳转路由 + vm.$router.push({ + name: 'login' + }) + } + // 判断是否需要确认 + if (confirm) { + commit('d2admin/grayModeSet', true, { + root: true + }) + vm.$confirm('注销当前账户吗? 打开的标签页和用户设置将会被保存。', '确认操作', { + confirmButtonText: '确定注销', + cancelButtonText: '放弃', + type: 'warning' + }) + .then(() => { + commit('d2admin/grayModeSet', false, { + root: true + }) + logout() + }) + .catch(() => { + commit('d2admin/grayModeSet', false, { + root: true + }) + vm.$message('放弃注销用户') + }) + } else { + logout() + } + } + }, + mutations: { + /** + * @class ... + * @description 用户登陆后从数据库加载一系列的设置 + * @param {Object} state vuex state + */ + load (state) { + // DB -> store 加载用户名 + this.commit('d2admin/user/load') + // DB -> store 加载主题 + this.commit('d2admin/theme/load') + // DB -> store 数据库加载上次退出时的多页列表 + this.commit('d2admin/pageOpenedListLoad') + // DB -> store 数据库加载这个用户之前设置的侧边栏折叠状态 + this.commit('d2admin/menu/asideCollapseLoad') + } + } +} diff --git a/src/store/modules/d2admin/modules/log.js b/src/store/modules/d2admin/modules/log.js index 8ec421ab..7528eb10 100644 --- a/src/store/modules/d2admin/modules/log.js +++ b/src/store/modules/d2admin/modules/log.js @@ -39,10 +39,10 @@ export default { /** * @description 添加一个日志 * @param {Object} state vuex state - * @param {Object} param type {String}: 类型 - * @param {Object} param err {Error}: 错误对象 - * @param {Object} param vm {Object}: vue 实例 - * @param {Object} param info {String}: 信息 + * @param {Object} param type {String} 类型 + * @param {Object} param err {Error} 错误对象 + * @param {Object} param vm {Object} vue 实例 + * @param {Object} param info {String} 信息 */ add ({ state, rootState }, { type, err, vm, info }) { // store 赋值 diff --git a/src/store/modules/d2admin/modules/util.js b/src/store/modules/d2admin/modules/util.js index 13ce4799..1d949ddc 100644 --- a/src/store/modules/d2admin/modules/util.js +++ b/src/store/modules/d2admin/modules/util.js @@ -3,9 +3,9 @@ import util from '@/libs/util.js' /** * @description 检查路径是否存在 不存在的话初始化 - * @param {Object} param dbName {String}: 数据库名称 - * @param {Object} param path {String}: 路径 - * @param {Object} param defaultValue {*}: 初始化默认值 + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 路径 + * @param {Object} param defaultValue {*} 初始化默认值 */ function pathInit ({ dbName = 'sys', @@ -23,8 +23,8 @@ function pathInit ({ /** * @description 检查路径下是否有当前用户的档案 - * @param {Object} param dbName {String}: 数据库名称 - * @param {Object} param path {String}: 路径 + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 路径 */ function isRowExistByUser ({ dbName = 'sys', @@ -51,9 +51,9 @@ export default { /** * @description 将数据存储到指定位置 [用户存储区域] * @param {Object} state vuex state - * @param {Object} param dbName {String}: 数据库名称 - * @param {Object} param path {String}: 存储路径 - * @param {Object} param value {*}: 需要存储的值 + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param value {*} 需要存储的值 */ dbValueSetByUser (state, { dbName = 'sys', @@ -81,9 +81,9 @@ export default { /** * @description 从系统存储中获取数据 [用户存储区域] * @param {Object} state vuex state - * @param {Object} param dbName {String}: 数据库名称 - * @param {Object} param path {String}: 存储路径 - * @param {Object} param defaultValue {*}: 取值失败的默认值 + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param defaultValue {*} 取值失败的默认值 */ dbValueGetByUser (context, { dbName = 'sys',