From 7c97a2ed0d49b9707e13fbaf9147f43d80f90959 Mon Sep 17 00:00:00 2001 From: liyang <1711467488@qq.com> Date: Thu, 9 Aug 2018 23:39:51 +0800 Subject: [PATCH] db Former-commit-id: 9f32760c2d5d596dfc7d9ecb1dd3f99bc61215d0 [formerly 9f32760c2d5d596dfc7d9ecb1dd3f99bc61215d0 [formerly 9f32760c2d5d596dfc7d9ecb1dd3f99bc61215d0 [formerly 9f32760c2d5d596dfc7d9ecb1dd3f99bc61215d0 [formerly 44e078233f2b9068249242a4f0d22da3b9f9d245 [formerly 81fcc38ee29c1ccadcbf36e89808a51bc740b109]]]]] Former-commit-id: 88c21c763a38b1c96cd1559dcd216974afc02982 Former-commit-id: d8ee29045146a021c9cd4142d5eebc68c30247a7 Former-commit-id: e17ce19dcf1661329479f6bcb5da1b99ea9be644 [formerly 3d003c11b1146d68e5afae500b842015d6e74fae] Former-commit-id: 6ac226c6a31c71caa1f956a942ef279edc835e81 Former-commit-id: 97da1683261d845bea6b51970921bc451635d181 Former-commit-id: 7abd67bfa7ea8d91a3d99c382dadefdc26c80bcd Former-commit-id: 1c2f4d95e8553b1545d7343be38b52148228a7a6 Former-commit-id: a701b5faf7299f58f46fd2dbe5484274872497f7 --- src/store/modules/d2admin/modules/account.js | 11 ++++++ src/store/modules/d2admin/modules/db.js | 36 +++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/store/modules/d2admin/modules/account.js b/src/store/modules/d2admin/modules/account.js index 6f117dd5..01dabc63 100644 --- a/src/store/modules/d2admin/modules/account.js +++ b/src/store/modules/d2admin/modules/account.js @@ -34,6 +34,17 @@ export default { }, { root: true }) // 用户登陆后从数据库加载一系列的设置 commit('d2admin/account/load', null, { root: true }) + // 测试 + // commit('d2admin/db/set', { + // dbName: 'db', + // path: 'log.login', + // value: res.data.name + // }, { root: true }) + commit('d2admin/db/push', { + dbName: 'db', + path: 'log.login', + value: res.data.name + }, { root: true }) // 跳转路由 vm.$router.push({ name: 'index' diff --git a/src/store/modules/d2admin/modules/db.js b/src/store/modules/d2admin/modules/db.js index 15700566..9b3b2657 100644 --- a/src/store/modules/d2admin/modules/db.js +++ b/src/store/modules/d2admin/modules/db.js @@ -5,15 +5,18 @@ import util from '@/libs/util.js' * @description 检查路径是否存在 不存在的话初始化 * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 路径 + * @param {Object} param validator {Function} 数据校验钩子 返回 true 表示验证通过 * @param {Object} param defaultValue {*} 初始化默认值 */ function pathInit ({ dbName = 'db', path = '', + validator = () => true, defaultValue = '' }) { const sys = db.get(dbName) - if (!sys.get(path).value()) { + const value = sys.get(path).value() + if (!(value && validator(value))) { sys .set(path, defaultValue) .write() @@ -49,24 +52,40 @@ export default { namespaced: true, mutations: { /** - * @description 将数据存储到指定位置 [不区分用户] + * @description 将数据存储到指定位置 | 路径不存在会自动初始化 [不区分用户] + * @description 效果类似于 dbName.path = value * @param {Object} state vuex state * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 存储路径 * @param {Object} param value {*} 需要存储的值 */ - set (state, { - dbName = 'db', - path = '', - value = '' - }) { + set (state, { dbName = 'db', path = '', value = '' }) { db .get(dbName) .set(path, value) .write() }, /** - * @description 将数据存储到指定位置 [区分用户] + * @description 将数据 push 到指定位置 | 路径不存在会自动初始化 [不区分用户] + * @description 效果类似于 dbName.path.push(value) + * @param {Object} state vuex state + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param value {*} 需要存储的值 + */ + push (state, { dbName = 'db', path = '', value = '' }) { + pathInit({ + dbName, + path, + validator: value => Array.isArray(value), + defaultValue: [] + }) + .push(value) + .write() + }, + /** + * @description 将数据存储到指定位置 | 路径不存在会自动初始化 [区分用户] + * @description 效果类似于 dbName.path[user] = value * @param {Object} state vuex state * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 存储路径 @@ -97,6 +116,7 @@ export default { actions: { /** * @description 获取数据 [区分用户] + * @description 效果类似于 dbName.path[user] || defaultValue * @param {Object} state vuex state * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 存储路径