2022-08-18 10:13:42 +00:00
|
|
|
import { Data } from '../components/index.js'
|
2022-03-26 08:21:44 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
let cacheMap = {}
|
|
|
|
let reFn = {}
|
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
export default class Base {
|
2022-07-30 20:20:10 +00:00
|
|
|
getData (arrList = '', cfg = {}) {
|
2022-08-18 10:13:42 +00:00
|
|
|
arrList = arrList || this._dataKey || ''
|
2022-07-30 20:20:10 +00:00
|
|
|
return Data.getData(this, arrList, cfg)
|
2022-03-26 08:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 获取指定值数据,支持通过
|
2022-07-30 20:20:10 +00:00
|
|
|
getVal (key, defaultValue) {
|
|
|
|
return Data.getVal(this, key, defaultValue)
|
2022-03-26 08:21:44 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
|
2022-09-08 20:34:32 +00:00
|
|
|
// 获取缓存
|
2022-09-04 21:03:23 +00:00
|
|
|
_getCache (uuid = '', time = 10 * 60) {
|
|
|
|
if (uuid && cacheMap[uuid]) {
|
2022-09-08 20:34:32 +00:00
|
|
|
return cacheMap[uuid]._expire(time)
|
2022-09-04 21:03:23 +00:00
|
|
|
}
|
|
|
|
this._uuid = uuid
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:34:32 +00:00
|
|
|
// 设置缓存
|
|
|
|
_cache (time = 10 * 60) {
|
2022-09-03 21:08:57 +00:00
|
|
|
let id = this._uuid
|
2022-09-08 20:34:32 +00:00
|
|
|
this._expire(time)
|
|
|
|
cacheMap[id] = this._proxy()
|
|
|
|
return cacheMap[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置超时时间
|
|
|
|
_expire (time = 10 * 60) {
|
|
|
|
this._delCache()
|
|
|
|
if (time > 0) {
|
|
|
|
let id = this._uuid
|
|
|
|
if (id) {
|
|
|
|
reFn[id] = setTimeout(() => {
|
|
|
|
reFn[id] && clearTimeout(reFn[id])
|
|
|
|
delete reFn[id]
|
|
|
|
delete cacheMap[id]
|
|
|
|
}, time * 1000)
|
|
|
|
}
|
|
|
|
return cacheMap[id]
|
|
|
|
} else {
|
|
|
|
return this._delCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除缓存
|
|
|
|
_delCache () {
|
|
|
|
let id = this._uuid
|
|
|
|
let ret = reFn[id] || false
|
2022-09-03 21:08:57 +00:00
|
|
|
if (id) {
|
|
|
|
reFn[id] && clearTimeout(reFn[id])
|
|
|
|
delete reFn[id]
|
|
|
|
}
|
2022-09-08 20:34:32 +00:00
|
|
|
return ret
|
2022-09-03 21:08:57 +00:00
|
|
|
}
|
2022-09-04 21:03:23 +00:00
|
|
|
|
2022-09-08 20:34:32 +00:00
|
|
|
// 返回代理对象
|
|
|
|
_proxy () {
|
|
|
|
return new Proxy(this, {
|
|
|
|
get (self, key) {
|
|
|
|
if (key in self) {
|
|
|
|
return self[key]
|
|
|
|
}
|
|
|
|
if (self._get) {
|
|
|
|
if (key in self) {
|
|
|
|
return self._get(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (self._meta || self._data || self.meta || {})[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-07-30 20:20:10 +00:00
|
|
|
}
|