From 2f78494a92b7609b6efbc217f09595162f409bb2 Mon Sep 17 00:00:00 2001 From: Hank CP Date: Tue, 13 Aug 2019 18:04:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0=E9=9C=80=E7=BB=8F=E8=BF=87Vuex?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8,=20?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=9C=A8util.db.js=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 70de9bedcb0a013586e2291dc6d96a21424b3e1a [formerly 70de9bedcb0a013586e2291dc6d96a21424b3e1a [formerly 70de9bedcb0a013586e2291dc6d96a21424b3e1a [formerly 70de9bedcb0a013586e2291dc6d96a21424b3e1a [formerly f5b4193a7f9a692cb9f864e5b5758566922375a7 [formerly d943fc6b2fa98fb01bb982b65dda540c529368f8]]]]] Former-commit-id: d25c75f2b207464847f7562decb61a6b7c948fe4 Former-commit-id: aaba17e2bd19cfbdc419827c2befa2c752f134e4 Former-commit-id: ba304f7d50114d9db7af9a5c8550189472737c94 [formerly 6da3cf28be122da279c0c29a0a7e537c2acd32da] Former-commit-id: 79e25f36a1db5967cfa8bf491072dc3e54ba431e Former-commit-id: c2fecfae7de9248206fe9d455400fc32dd473806 Former-commit-id: 0069e2d536d55d9380a612837cf04e9004cfe199 Former-commit-id: 8361c528cfd02a49b8c0a0f83628cd2a0f284f9e Former-commit-id: 775af25d3562dd11cca87455bfe2b0b8735544e9 --- src/libs/util.db.js | 92 +++++++++++++++++ src/store/modules/d2admin/modules/db.js | 129 +++++++----------------- 2 files changed, 127 insertions(+), 94 deletions(-) diff --git a/src/libs/util.db.js b/src/libs/util.db.js index be8fe965..a54d9979 100644 --- a/src/libs/util.db.js +++ b/src/libs/util.db.js @@ -1,5 +1,7 @@ import low from 'lowdb' import LocalStorage from 'lowdb/adapters/LocalStorage' +import util from '@/libs/util' +import { cloneDeep } from 'lodash' const adapter = new LocalStorage(`d2admin-${process.env.VUE_APP_VERSION}`) const db = low(adapter) @@ -12,3 +14,93 @@ db .write() export default db + +/** + * @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 {*} 初始化默认值 + * @returns {String} 可以直接使用的路径 + */ +export function pathInit({ + dbName = 'database', + path = '', + user = true, + validator = () => true, + defaultValue = '' +}) { + const uuid = util.cookies.get('uuid') || 'ghost-uuid' + const currentPath = `${dbName}.${user ? `user.${uuid}` : 'public'}${path ? `.${path}` : ''}` + const value = db.get(currentPath).value() + if (!(value !== undefined && validator(value))) { + db + .set(currentPath, defaultValue) + .write() + } + return currentPath +} + +/** + * @description 将数据存储到指定位置 | 路径不存在会自动初始化 + * @description 效果类似于取值 dbName.path = value + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param value {*} 需要存储的值 + * @param {Object} param user {Boolean} 是否区分用户 + */ +export function dbSet ({ + dbName = 'database', + path = '', + value = '', + user = false +}) { + db.set(pathInit({ + dbName, + path, + user + }), value).write() +} + +/** + * @description 获取数据 + * @description 效果类似于取值 dbName.path || defaultValue + * @param {Object} param dbName {String} 数据库名称 + * @param {Object} param path {String} 存储路径 + * @param {Object} param defaultValue {*} 取值失败的默认值 + * @param {Object} param user {Boolean} 是否区分用户 + */ +export function dbGet ({ + dbName = 'database', + path = '', + defaultValue = '', + user = false +}) { + return new Promise(resolve => { + resolve(cloneDeep(db.get(pathInit({ + dbName, + path, + user, + defaultValue + })).value())) + }) +} + +/** + * @description 获取存储数据库对象 + * @param {Object} param user {Boolean} 是否区分用户 + */ +export function database ({ + dbName = 'database', + path = '', + user = false, + validator = () => true, + defaultValue = '' +} = {}) { + return new Promise(resolve => { + resolve(db.get(pathInit({ + dbName, path, user, validator, defaultValue + }))) + }) +} diff --git a/src/store/modules/d2admin/modules/db.js b/src/store/modules/d2admin/modules/db.js index 240a5d10..00cc136b 100644 --- a/src/store/modules/d2admin/modules/db.js +++ b/src/store/modules/d2admin/modules/db.js @@ -1,33 +1,6 @@ -import util from '@/libs/util' import router from '@/router' import { cloneDeep } from 'lodash' - -/** - * @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 {*} 初始化默认值 - * @returns {String} 可以直接使用的路径 - */ -function pathInit ({ - dbName = 'database', - path = '', - user = true, - validator = () => true, - defaultValue = '' -}) { - const uuid = util.cookies.get('uuid') || 'ghost-uuid' - const currentPath = `${dbName}.${user ? `user.${uuid}` : 'public'}${path ? `.${path}` : ''}` - const value = util.db.get(currentPath).value() - if (!(value !== undefined && validator(value))) { - util.db - .set(currentPath, defaultValue) - .write() - } - return currentPath -} +import { database as getDatabase, dbGet, dbSet } from '@/libs/util.db' export default { namespaced: true, @@ -35,6 +8,7 @@ export default { /** * @description 将数据存储到指定位置 | 路径不存在会自动初始化 * @description 效果类似于取值 dbName.path = value + * @param {Object} context context * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 存储路径 * @param {Object} param value {*} 需要存储的值 @@ -46,15 +20,12 @@ export default { value = '', user = false }) { - util.db.set(pathInit({ - dbName, - path, - user - }), value).write() + dbSet({ dbName, path, value, user }) }, /** * @description 获取数据 * @description 效果类似于取值 dbName.path || defaultValue + * @param {Object} context context * @param {Object} param dbName {String} 数据库名称 * @param {Object} param path {String} 存储路径 * @param {Object} param defaultValue {*} 取值失败的默认值 @@ -66,14 +37,7 @@ export default { defaultValue = '', user = false }) { - return new Promise(resolve => { - resolve(cloneDeep(util.db.get(pathInit({ - dbName, - path, - user, - defaultValue - })).value())) - }) + return dbGet({ dbName, path, defaultValue, user }) }, /** * @description 获取存储数据库对象 @@ -83,13 +47,9 @@ export default { database (context, { user = false } = {}) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: '', - user, - defaultValue: {} - }))) + return getDatabase({ + user, + defaultValue: {} }) }, /** @@ -100,14 +60,10 @@ export default { databaseClear (context, { user = false } = {}) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: '', - user, - validator: () => false, - defaultValue: {} - }))) + return getDatabase({ + user, + validator: () => false, + defaultValue: {} }) }, /** @@ -120,13 +76,10 @@ export default { basis = 'fullPath', user = false } = {}) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: `$page.${router.app.$route[basis]}`, - user, - defaultValue: {} - }))) + return getDatabase({ + path: `$page.${router.app.$route[basis]}`, + user, + defaultValue: {} }) }, /** @@ -139,14 +92,11 @@ export default { basis = 'fullPath', user = false } = {}) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: `$page.${router.app.$route[basis]}`, - user, - validator: () => false, - defaultValue: {} - }))) + return getDatabase({ + path: `$page.${router.app.$route[basis]}`, + user, + validator: () => false, + defaultValue: {} }) }, /** @@ -161,14 +111,11 @@ export default { basis = 'fullPath', user = false }) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: `$page.${router.app.$route[basis]}.$data`, - user, - validator: () => false, - defaultValue: cloneDeep(instance.$data) - }))) + return getDatabase({ + path: `$page.${router.app.$route[basis]}.$data`, + user, + validator: () => false, + defaultValue: cloneDeep(instance.$data) }) }, /** @@ -183,13 +130,10 @@ export default { basis = 'fullPath', user = false }) { - return new Promise(resolve => { - resolve(cloneDeep(util.db.get(pathInit({ - dbName: 'database', - path: `$page.${router.app.$route[basis]}.$data`, - user, - defaultValue: cloneDeep(instance.$data) - })).value())) + return dbGet({ + path: `$page.${router.app.$route[basis]}.$data`, + user, + defaultValue: cloneDeep(instance.$data) }) }, /** @@ -202,14 +146,11 @@ export default { basis = 'fullPath', user = false }) { - return new Promise(resolve => { - resolve(util.db.get(pathInit({ - dbName: 'database', - path: `$page.${router.app.$route[basis]}.$data`, - user, - validator: () => false, - defaultValue: {} - }))) + return getDatabase({ + path: `$page.${router.app.$route[basis]}.$data`, + user, + validator: () => false, + defaultValue: {} }) } }