From 0453ab7f36df29c7af03d96286c5826822df902e Mon Sep 17 00:00:00 2001
From: liyang <1711467488@qq.com>
Date: Fri, 10 Aug 2018 10:58:08 +0800
Subject: [PATCH] db update
Former-commit-id: 227391acd445196d063c7e5424cbf5243ed34fff [formerly 227391acd445196d063c7e5424cbf5243ed34fff [formerly 227391acd445196d063c7e5424cbf5243ed34fff [formerly 227391acd445196d063c7e5424cbf5243ed34fff [formerly 09be4dfe2d27d6b5cae7f5229fd89e107df524d2 [formerly af907a0206a24d385946e44d22e9dc02e65c3d5d]]]]]
Former-commit-id: 8c415cd0ae09c25683be79488e84cf39aa6143a7
Former-commit-id: 7e7392d78cdbd636dc856df3276ee66a2a14b9cb
Former-commit-id: 4417f57b963cce4a8b24e6729c2f914505cfaf16 [formerly 0addaeab1e075b68324da0c7a29fcac878fb938a]
Former-commit-id: 596cd1e5b343e096aa7974e42ff238138b45b65a
Former-commit-id: 87e1da947c638b286f5a7e4d63ec99b063acdbbc
Former-commit-id: f6b6ba6cb2a25ae7fabb657f0903e856058c1647
Former-commit-id: ce0fbdb1493e80b5afd920c95a78f1e919f221e4
Former-commit-id: df86d4b69730e071f71e34511d8fb6cc39664726
---
src/menu/modules/demo-playground.js | 4 +-
src/pages/demo/playground/db/all/index.vue | 29 -----
src/pages/demo/playground/db/public/index.vue | 121 ------------------
src/pages/demo/playground/db/user/index.vue | 121 ------------------
src/pages/demo/playground/db/util/index.vue | 116 +++++++++++++++++
src/router/routes.js.REMOVED.git-id | 2 +-
.../modules/d2admin/index.js.REMOVED.git-id | 2 +-
src/store/modules/d2admin/modules/account.js | 11 --
src/store/modules/d2admin/modules/db.js | 116 +++++++----------
src/store/modules/d2admin/modules/user.js | 4 +-
10 files changed, 170 insertions(+), 356 deletions(-)
delete mode 100644 src/pages/demo/playground/db/all/index.vue
delete mode 100644 src/pages/demo/playground/db/public/index.vue
delete mode 100644 src/pages/demo/playground/db/user/index.vue
create mode 100644 src/pages/demo/playground/db/util/index.vue
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 @@
+
+
+ header
+
+
+
+
+
+ dbName
+
+
+
+ path
+
+
+
+
+
+ key
+
+
+
+ value
+
+
+
+ 存储
+ 保存 {{key}} = "{{value}}"
+
+ 保存 {{key}} = "{{value}}" 当前用户
+ 取值
+ 取值 {{key}}
+
+ 取值 {{key}} 当前用户
+
+
+
+ db.get('{{dbName}}').value()
+
+
+
+
+
+
+
+
+
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'
+ }
})
}
}