diff --git a/src/menu/modules/demo-playground.js b/src/menu/modules/demo-playground.js index 2694de35..acc3c425 100644 --- a/src/menu/modules/demo-playground.js +++ b/src/menu/modules/demo-playground.js @@ -40,9 +40,7 @@ export default { title: '数据持久化', icon: 'database', children: [ - { path: `${pre}db/all`, title: '全部数据', icon: 'table' }, - { path: `${pre}db/user`, title: '用户数据', icon: 'user' }, - { path: `${pre}db/public`, title: '公用数据', icon: 'users' } + { path: `${pre}db/util`, title: 'db.util', icon: 'cube' } ] }, { diff --git a/src/pages/demo/playground/db/all/index.vue b/src/pages/demo/playground/db/all/index.vue deleted file mode 100644 index 93ecdf33..00000000 --- a/src/pages/demo/playground/db/all/index.vue +++ /dev/null @@ -1,29 +0,0 @@ - - - diff --git a/src/pages/demo/playground/db/public/index.vue b/src/pages/demo/playground/db/public/index.vue deleted file mode 100644 index d5948805..00000000 --- a/src/pages/demo/playground/db/public/index.vue +++ /dev/null @@ -1,121 +0,0 @@ - - - diff --git a/src/pages/demo/playground/db/user/index.vue b/src/pages/demo/playground/db/user/index.vue deleted file mode 100644 index b70eb776..00000000 --- a/src/pages/demo/playground/db/user/index.vue +++ /dev/null @@ -1,121 +0,0 @@ - - - diff --git a/src/pages/demo/playground/db/util/index.vue b/src/pages/demo/playground/db/util/index.vue new file mode 100644 index 00000000..e7338df5 --- /dev/null +++ b/src/pages/demo/playground/db/util/index.vue @@ -0,0 +1,116 @@ + + diff --git a/src/router/routes.js.REMOVED.git-id b/src/router/routes.js.REMOVED.git-id index 66314f5e..87fea966 100644 --- a/src/router/routes.js.REMOVED.git-id +++ b/src/router/routes.js.REMOVED.git-id @@ -1 +1 @@ -ca12a11b6d9d0552250eb2da1e7bac555902f68d \ No newline at end of file +16b7e123957b7e4ef7d5c6270616b8f00d2dd381 \ No newline at end of file diff --git a/src/store/modules/d2admin/index.js.REMOVED.git-id b/src/store/modules/d2admin/index.js.REMOVED.git-id index 3ce6d6d8..d2d9c661 100644 --- a/src/store/modules/d2admin/index.js.REMOVED.git-id +++ b/src/store/modules/d2admin/index.js.REMOVED.git-id @@ -1 +1 @@ -4fee0ebd82dca4d2b9cd0ff053df6807cfd880ac \ No newline at end of file +03c3404e1d1584485b9a7b73671b1dd1d17a0e14 \ No newline at end of file diff --git a/src/store/modules/d2admin/modules/account.js b/src/store/modules/d2admin/modules/account.js index 01dabc63..6f117dd5 100644 --- a/src/store/modules/d2admin/modules/account.js +++ b/src/store/modules/d2admin/modules/account.js @@ -34,17 +34,6 @@ 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 9b3b2657..b7bff041 100644 --- a/src/store/modules/d2admin/modules/db.js +++ b/src/store/modules/d2admin/modules/db.js @@ -5,83 +5,50 @@ import util from '@/libs/util.js' * @description 检查路径是否存在 不存在的话初始化 * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 路径 + * @param {Object} param user {Boolean} 区分用户 * @param {Object} param validator {Function} 数据校验钩子 返回 true 表示验证通过 * @param {Object} param defaultValue {*} 初始化默认值 */ function pathInit ({ dbName = 'db', path = '', + user = true, validator = () => true, defaultValue = '' }) { const sys = db.get(dbName) - const value = sys.get(path).value() + const uuid = util.cookies.get('uuid') || 'ghost-uuid' + const currentPath = `${user ? `user.${uuid}` : 'public'}.${path}` + const value = sys.get(currentPath).value() if (!(value && validator(value))) { sys - .set(path, defaultValue) + .set(currentPath, defaultValue) .write() } - return sys.get(path) -} - -/** - * @description 检查路径下是否有当前用户的档案 - * @param {Object} param dbName {String} 数据库名称 - * @param {Object} param path {String} 路径 - */ -function isRowExistByUser ({ - dbName = 'sys', - path = '' -}) { - const sys = db.get(dbName) - const row = sys - .get(path) - .find({ - uuid: util.cookies.get('uuid') - }) - // 返回可以操作的 row 或者布尔值 false - // 外部判断返回值的时候建议使用 if (!isRowExistByUser({})) - if (row.value()) { - return row - } else { - return false - } + return `${dbName}.${currentPath}` } 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 = '' }) { - db - .get(dbName) - .set(path, value) - .write() - }, - /** - * @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({ + set (state, { + dbName = 'db', + path = '', + value = '' + }) { + db.set(pathInit({ dbName, path, - validator: value => Array.isArray(value), - defaultValue: [] - }) - .push(value) - .write() + user: false + }), value).write() }, /** * @description 将数据存储到指定位置 | 路径不存在会自动初始化 [区分用户] @@ -96,24 +63,35 @@ export default { path = '', value = '' }) { - // 得到路径在数据库中的对象 没有初始化会自动初始化 - // ByUser 类型的默认值设置为数组 - // 以后数组的每一项是一个用户的存档 - const currentPath = pathInit({ dbName, path, defaultValue: [] }) - // 得到当前用户在数据库此路径下的存档 - const row = isRowExistByUser({ dbName, path }) - // 合并 or 追加 - if (!row) { - currentPath.push({ - uuid: util.cookies.get('uuid'), - value - }).write() - } else { - row.assign({ value }).write() - } + db.set(pathInit({ + dbName, + path + }), value).write() } }, actions: { + /** + * @description 获取数据 + * @description 效果类似于 dbName.path || defaultValue + * @param {Object} state vuex state + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param defaultValue {*} 取值失败的默认值 + */ + get (context, { + dbName = 'db', + path = '', + defaultValue = '' + }) { + return new Promise((resolve, reject) => { + resolve(db.get(pathInit({ + dbName, + path, + user: false, + defaultValue + })).value()) + }) + }, /** * @description 获取数据 [区分用户] * @description 效果类似于 dbName.path[user] || defaultValue @@ -128,10 +106,12 @@ export default { defaultValue = '' }) { return new Promise((resolve, reject) => { - // 得到当前用户在数据库此路径下的存档 - const row = isRowExistByUser({ path }) - // 返回存档或者默认值 - resolve(!row ? defaultValue : row.value().value) + resolve(db.get(pathInit({ + dbName, + path, + user: true, + defaultValue + })).value()) }) } } diff --git a/src/store/modules/d2admin/modules/user.js b/src/store/modules/d2admin/modules/user.js index 6a5d139e..36c72be3 100644 --- a/src/store/modules/d2admin/modules/user.js +++ b/src/store/modules/d2admin/modules/user.js @@ -31,7 +31,9 @@ export default { state.info = await this.dispatch('d2admin/db/getByUser', { dbName: 'sys', path: 'user.info', - defaultValue: '请重新登陆' + defaultValue: { + name: 'Ghost' + } }) } }