Merge pull request #211 from hank-cp/master
无需经过Vuex访问本地存储, 可以在util.db.js直接访问 Former-commit-id: 28e6b839c29d4aab6937291e1aa521d141a7ee72 [formerly 28e6b839c29d4aab6937291e1aa521d141a7ee72 [formerly 28e6b839c29d4aab6937291e1aa521d141a7ee72 [formerly 28e6b839c29d4aab6937291e1aa521d141a7ee72 [formerly 764a02f918588d1cde8d95b7b9884541c834c5e4 [formerly ed17c099489f5e44554e0f2b393a9d3de02124b2]]]]] Former-commit-id: dbd4487e958839075eb967eeb376ab1892815795 Former-commit-id: 7ebd4f9f2985ed3f4aab76af439dee1cffa1d2f0 Former-commit-id: 846485c19483fb2ba48e82c82140ce13c76d0706 [formerly e2aabb3d2c9f48f3c05b74aded46fc2a9343ad1d] Former-commit-id: 3b51ff7d21f80e241fb9e0bc46df4064c47ae30f Former-commit-id: 8a85c1dd93f24b87fff84a89fb5ffd1081ddf8d9 Former-commit-id: 2d22fc2773bf8c78a719f13cff7afe1edfa734e9 Former-commit-id: da4674cd2bfcf4d5f5f7cc39070c64d0c2abc102 Former-commit-id: d7fe0fe7fc6dfc6e1f73d190e984e1b7683cfb5f
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import low from 'lowdb'
|
import low from 'lowdb'
|
||||||
import LocalStorage from 'lowdb/adapters/LocalStorage'
|
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 adapter = new LocalStorage(`d2admin-${process.env.VUE_APP_VERSION}`)
|
||||||
const db = low(adapter)
|
const db = low(adapter)
|
||||||
@@ -12,3 +14,93 @@ db
|
|||||||
.write()
|
.write()
|
||||||
|
|
||||||
export default db
|
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 router from '@/router'
|
||||||
import { cloneDeep } from 'lodash'
|
import { cloneDeep } from 'lodash'
|
||||||
|
import { database as getDatabase, dbGet, dbSet } from '@/libs/util.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} 可以直接使用的路径
|
|
||||||
*/
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
@@ -35,6 +8,7 @@ export default {
|
|||||||
/**
|
/**
|
||||||
* @description 将数据存储到指定位置 | 路径不存在会自动初始化
|
* @description 将数据存储到指定位置 | 路径不存在会自动初始化
|
||||||
* @description 效果类似于取值 dbName.path = value
|
* @description 效果类似于取值 dbName.path = value
|
||||||
|
* @param {Object} context context
|
||||||
* @param {Object} param dbName {String} 数据库名称
|
* @param {Object} param dbName {String} 数据库名称
|
||||||
* @param {Object} param path {String} 存储路径
|
* @param {Object} param path {String} 存储路径
|
||||||
* @param {Object} param value {*} 需要存储的值
|
* @param {Object} param value {*} 需要存储的值
|
||||||
@@ -46,15 +20,12 @@ export default {
|
|||||||
value = '',
|
value = '',
|
||||||
user = false
|
user = false
|
||||||
}) {
|
}) {
|
||||||
util.db.set(pathInit({
|
dbSet({ dbName, path, value, user })
|
||||||
dbName,
|
|
||||||
path,
|
|
||||||
user
|
|
||||||
}), value).write()
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* @description 获取数据
|
* @description 获取数据
|
||||||
* @description 效果类似于取值 dbName.path || defaultValue
|
* @description 效果类似于取值 dbName.path || defaultValue
|
||||||
|
* @param {Object} context context
|
||||||
* @param {Object} param dbName {String} 数据库名称
|
* @param {Object} param dbName {String} 数据库名称
|
||||||
* @param {Object} param path {String} 存储路径
|
* @param {Object} param path {String} 存储路径
|
||||||
* @param {Object} param defaultValue {*} 取值失败的默认值
|
* @param {Object} param defaultValue {*} 取值失败的默认值
|
||||||
@@ -66,14 +37,7 @@ export default {
|
|||||||
defaultValue = '',
|
defaultValue = '',
|
||||||
user = false
|
user = false
|
||||||
}) {
|
}) {
|
||||||
return new Promise(resolve => {
|
return dbGet({ dbName, path, defaultValue, user })
|
||||||
resolve(cloneDeep(util.db.get(pathInit({
|
|
||||||
dbName,
|
|
||||||
path,
|
|
||||||
user,
|
|
||||||
defaultValue
|
|
||||||
})).value()))
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* @description 获取存储数据库对象
|
* @description 获取存储数据库对象
|
||||||
@@ -83,13 +47,9 @@ export default {
|
|||||||
database (context, {
|
database (context, {
|
||||||
user = false
|
user = false
|
||||||
} = {}) {
|
} = {}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: '',
|
|
||||||
user,
|
user,
|
||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -100,14 +60,10 @@ export default {
|
|||||||
databaseClear (context, {
|
databaseClear (context, {
|
||||||
user = false
|
user = false
|
||||||
} = {}) {
|
} = {}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: '',
|
|
||||||
user,
|
user,
|
||||||
validator: () => false,
|
validator: () => false,
|
||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -120,13 +76,10 @@ export default {
|
|||||||
basis = 'fullPath',
|
basis = 'fullPath',
|
||||||
user = false
|
user = false
|
||||||
} = {}) {
|
} = {}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: `$page.${router.app.$route[basis]}`,
|
path: `$page.${router.app.$route[basis]}`,
|
||||||
user,
|
user,
|
||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -139,14 +92,11 @@ export default {
|
|||||||
basis = 'fullPath',
|
basis = 'fullPath',
|
||||||
user = false
|
user = false
|
||||||
} = {}) {
|
} = {}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: `$page.${router.app.$route[basis]}`,
|
path: `$page.${router.app.$route[basis]}`,
|
||||||
user,
|
user,
|
||||||
validator: () => false,
|
validator: () => false,
|
||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -161,14 +111,11 @@ export default {
|
|||||||
basis = 'fullPath',
|
basis = 'fullPath',
|
||||||
user = false
|
user = false
|
||||||
}) {
|
}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: `$page.${router.app.$route[basis]}.$data`,
|
path: `$page.${router.app.$route[basis]}.$data`,
|
||||||
user,
|
user,
|
||||||
validator: () => false,
|
validator: () => false,
|
||||||
defaultValue: cloneDeep(instance.$data)
|
defaultValue: cloneDeep(instance.$data)
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -183,13 +130,10 @@ export default {
|
|||||||
basis = 'fullPath',
|
basis = 'fullPath',
|
||||||
user = false
|
user = false
|
||||||
}) {
|
}) {
|
||||||
return new Promise(resolve => {
|
return dbGet({
|
||||||
resolve(cloneDeep(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: `$page.${router.app.$route[basis]}.$data`,
|
path: `$page.${router.app.$route[basis]}.$data`,
|
||||||
user,
|
user,
|
||||||
defaultValue: cloneDeep(instance.$data)
|
defaultValue: cloneDeep(instance.$data)
|
||||||
})).value()))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -202,14 +146,11 @@ export default {
|
|||||||
basis = 'fullPath',
|
basis = 'fullPath',
|
||||||
user = false
|
user = false
|
||||||
}) {
|
}) {
|
||||||
return new Promise(resolve => {
|
return getDatabase({
|
||||||
resolve(util.db.get(pathInit({
|
|
||||||
dbName: 'database',
|
|
||||||
path: `$page.${router.app.$route[basis]}.$data`,
|
path: `$page.${router.app.$route[basis]}.$data`,
|
||||||
user,
|
user,
|
||||||
validator: () => false,
|
validator: () => false,
|
||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
})))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user