2022-09-25 13:56:10 +00:00
|
|
|
|
/*
|
|
|
|
|
* Base Model
|
|
|
|
|
*
|
|
|
|
|
* 使用Proxy实现meta数据的getter
|
|
|
|
|
* 对Character等可复用设置实例缓存,提高性能
|
|
|
|
|
* */
|
2023-03-07 17:52:11 +00:00
|
|
|
|
import { Data } from '#miao'
|
2022-03-26 08:21:44 +00:00
|
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
|
let cacheMap = {}
|
|
|
|
|
let reFn = {}
|
2022-09-11 19:26:07 +00:00
|
|
|
|
let metaMap = {}
|
2022-09-03 21:08:57 +00:00
|
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
|
export default class Base {
|
2022-09-10 19:59:45 +00:00
|
|
|
|
constructor () {
|
2023-05-14 20:19:33 +00:00
|
|
|
|
this.game = 'gs'
|
2022-09-11 13:10:38 +00:00
|
|
|
|
let proxy = new Proxy(this, {
|
|
|
|
|
get (self, key, receiver) {
|
2022-09-11 19:26:07 +00:00
|
|
|
|
if (self._uuid && key === 'meta') {
|
|
|
|
|
return metaMap[self._uuid]
|
|
|
|
|
}
|
2022-09-10 19:59:45 +00:00
|
|
|
|
if (key in self) {
|
2022-09-11 13:10:38 +00:00
|
|
|
|
return Reflect.get(self, key, receiver)
|
2022-09-10 19:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (self._get) {
|
2022-09-11 13:10:38 +00:00
|
|
|
|
return self._get.call(receiver, key)
|
2022-09-10 19:59:45 +00:00
|
|
|
|
}
|
2022-09-11 19:26:07 +00:00
|
|
|
|
if (self._uuid) {
|
|
|
|
|
return (metaMap[self._uuid] || {})[key]
|
|
|
|
|
} else {
|
|
|
|
|
return (self.meta || {})[key]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
set (target, key, newValue) {
|
|
|
|
|
if (target._uuid && key === 'meta') {
|
|
|
|
|
metaMap[target._uuid] = newValue
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return Reflect.set(target, key, newValue)
|
|
|
|
|
}
|
2022-09-10 19:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2022-09-11 13:10:38 +00:00
|
|
|
|
return proxy
|
2022-09-10 19:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-10 19:59:45 +00:00
|
|
|
|
if (id) {
|
|
|
|
|
this._expire(time)
|
|
|
|
|
cacheMap[id] = this
|
|
|
|
|
return cacheMap[id]
|
|
|
|
|
}
|
|
|
|
|
return this
|
2022-09-08 20:34:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置超时时间
|
|
|
|
|
_expire (time = 10 * 60) {
|
2022-09-11 19:26:07 +00:00
|
|
|
|
let id = this._uuid
|
2023-02-17 20:42:05 +00:00
|
|
|
|
let self = this
|
2022-09-11 19:26:07 +00:00
|
|
|
|
reFn[id] && clearTimeout(reFn[id])
|
2022-09-08 20:34:32 +00:00
|
|
|
|
if (time > 0) {
|
|
|
|
|
if (id) {
|
|
|
|
|
reFn[id] = setTimeout(() => {
|
2023-02-17 20:42:05 +00:00
|
|
|
|
self._delCache()
|
2022-09-08 20:34:32 +00:00
|
|
|
|
}, time * 1000)
|
|
|
|
|
}
|
|
|
|
|
return cacheMap[id]
|
2022-09-03 21:08:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-17 20:42:05 +00:00
|
|
|
|
|
|
|
|
|
_delCache () {
|
|
|
|
|
let id = this._uuid
|
|
|
|
|
reFn[id] && clearTimeout(reFn[id])
|
|
|
|
|
delete reFn[id]
|
|
|
|
|
delete cacheMap[id]
|
|
|
|
|
delete metaMap[id]
|
|
|
|
|
}
|
2023-05-14 20:19:33 +00:00
|
|
|
|
|
|
|
|
|
isSr () {
|
|
|
|
|
return this.game === 'sr'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isGs () {
|
|
|
|
|
return !this.isSr
|
|
|
|
|
}
|
2022-07-30 20:20:10 +00:00
|
|
|
|
}
|