无需经过Vuex访问本地存储, 可以在util.db.js直接访问
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
This commit is contained in:
parent
d911bb3a43
commit
2f78494a92
|
|
@ -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
|
||||
})))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: {}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue