调整runtime.user,暴露用户相关方法与数据

This commit is contained in:
Kokomi 2023-06-01 04:27:21 +08:00
parent d29e0137c3
commit e8725f5d3d
3 changed files with 54 additions and 23 deletions

View File

@ -67,9 +67,47 @@ export default class Runtime {
} }
static async init (e) { static async init (e) {
e.runtime = new Runtime(e) await MysInfo.initCache()
e.user = await MysInfo.getNoteUser(e) let runtime = new Runtime(e)
return e.runtime e.runtime = runtime
e.game = e.isSr ? 'sr' : 'gs'
await runtime.initUser()
return runtime
}
async initUser () {
let e = this.e
let user = await NoteUser.create(e)
if (user) {
e.user = new Proxy(user, {
get (self, key, receiver) {
let game = e.isSr ? 'sr' : 'gs'
let fnMap = {
uid: 'getUid',
uidList: 'getUidList',
mysUser: 'getMysUser',
ckUidList: 'getCkUidList'
}
if (fnMap[key]) {
return self[fnMap[key]](game)
}
if (key === 'uidData') {
return self.getUidData('', game)
}
if (['getUid', 'getUidList', 'getMysUser', 'getCkUidList', 'getUidMapList', 'getGameDs'].includes(key)) {
return (_game, arg2) => {
return self[key](_game || game, arg2)
}
}
if (['getUidData', 'hasUid', 'addRegUid', 'delRegUid', 'setMainUid'].includes(key)) {
return (uid, _game = '') => {
return self[key](uid, _game || game)
}
}
return self[key]
}
})
}
} }
/** /**

View File

@ -286,16 +286,6 @@ export default class NoteUser extends BaseModel {
this.save() this.save()
} }
/**
* 获取当前用户的绑定UID
* 主要供内部调用建议使用 user.uid 获取用户uid
* @returns {Promise<*>}
*/
async getRegUid (game = 'gs') {
let gameDs = this.getGameDs(game)
return gameDs.uid || ''
}
getGameDs (game = 'gs') { getGameDs (game = 'gs') {
return this.games[this.gameKey(game)] return this.games[this.gameKey(game)]
} }
@ -359,6 +349,19 @@ export default class NoteUser extends BaseModel {
await this.save() await this.save()
} }
async eachMysUser (fn) {
await Data.forEach(this.mysUsers, async (mys, ltuid) => {
if (!mys) {
return true
}
return fn(mys, ltuid)
})
}
async eachAllMysUser (fn) {
return MysUser.forEach(fn)
}
/** /**
* 检查当前用户绑定的CK状态 * 检查当前用户绑定的CK状态
*/ */

View File

@ -66,19 +66,9 @@ export default class MysInfo {
/** 判断回复 */ /** 判断回复 */
await mysInfo.checkReply() await mysInfo.checkReply()
return mysInfo return mysInfo
} }
static async getNoteUser (e) {
await MysInfo.initCache()
let user = await NoteUser.create(e)
if (user) {
return user
}
return false
}
/** /**
* 获取UID * 获取UID
* @param e * @param e