2022-09-20 11:50:27 +00:00
|
|
|
|
import { User } from './index.js'
|
2022-10-21 14:48:19 +00:00
|
|
|
|
import YzMysInfo from './mys-lib/YzMysInfo.js'
|
|
|
|
|
import YzMysApi from './mys-lib/YzMysApi.js'
|
2022-09-20 11:50:27 +00:00
|
|
|
|
|
|
|
|
|
export default class MysApi {
|
2022-10-21 14:48:19 +00:00
|
|
|
|
constructor (e, uid, mysInfo) {
|
2022-09-20 11:50:27 +00:00
|
|
|
|
this.e = e
|
2022-10-21 14:48:19 +00:00
|
|
|
|
this.mysInfo = mysInfo
|
|
|
|
|
this.ckInfo = mysInfo.ckInfo
|
2022-09-20 11:50:27 +00:00
|
|
|
|
this.uid = uid
|
|
|
|
|
e.targetUser = this.targetUser
|
|
|
|
|
e.selfUser = this.selfUser
|
|
|
|
|
e.isSelfCookie = this.isSelfCookie
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async init (e, cfg = 'all') {
|
|
|
|
|
if (typeof (cfg) === 'string') {
|
|
|
|
|
cfg = { auth: cfg }
|
|
|
|
|
}
|
|
|
|
|
let { auth = 'all' } = cfg
|
2022-10-26 20:28:44 +00:00
|
|
|
|
let mys = false
|
|
|
|
|
if (e.runtime) {
|
|
|
|
|
mys = await e.runtime.getMysInfo(auth)
|
|
|
|
|
} else {
|
|
|
|
|
mys = await YzMysInfo.init(e, auth === 'cookie' ? 'detail' : 'roleIndex')
|
|
|
|
|
}
|
2022-09-20 11:50:27 +00:00
|
|
|
|
if (!mys) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
let uid = mys.uid
|
2022-09-20 19:15:22 +00:00
|
|
|
|
e._mys = new MysApi(e, uid, mys)
|
|
|
|
|
return e._mys
|
2022-09-20 11:50:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async initUser (e, cfg = 'all') {
|
|
|
|
|
if (typeof (cfg) === 'string') {
|
|
|
|
|
cfg = { auth: cfg }
|
|
|
|
|
}
|
2022-10-21 14:48:19 +00:00
|
|
|
|
let uid = await YzMysInfo.getUid(e)
|
2022-09-20 11:50:27 +00:00
|
|
|
|
if (uid) {
|
|
|
|
|
return new User({ id: e.user_id, uid: uid })
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isSelfCookie () {
|
2022-10-21 14:48:19 +00:00
|
|
|
|
return this.uid * 1 === this.ckUid * 1 || this?.mysInfo?.isSelf
|
2022-09-20 11:50:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get ckUid () {
|
|
|
|
|
return this.ckInfo.uid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get ck () {
|
|
|
|
|
return this.ckInfo.ck
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get selfUser () {
|
|
|
|
|
return new User({ id: this.e.user_id, uid: this.uid })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get targetUser () {
|
|
|
|
|
return new User({ id: this.e.user_id, uid: this.uid })
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 14:48:19 +00:00
|
|
|
|
getMysApi (e) {
|
|
|
|
|
if (this.mys) {
|
|
|
|
|
return this.mys
|
|
|
|
|
}
|
|
|
|
|
this.mys = new YzMysApi(this.uid, this.ck, { log: false, e })
|
|
|
|
|
return this.mys
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 11:50:27 +00:00
|
|
|
|
async getData (api, data) {
|
2022-10-21 14:48:19 +00:00
|
|
|
|
if (!this.mysInfo) {
|
2022-09-20 11:50:27 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
let e = this.e
|
2022-10-21 14:48:19 +00:00
|
|
|
|
let mys = this.getMysApi(e)
|
2022-09-20 11:50:27 +00:00
|
|
|
|
// 暂时先在plugin侧阻止错误,防止刷屏
|
|
|
|
|
e._original_reply = e._original_reply || e.reply
|
|
|
|
|
e._reqCount = e._reqCount || 0
|
|
|
|
|
e.reply = function (msg) {
|
|
|
|
|
if (!e._isReplyed) {
|
|
|
|
|
e._isReplyed = true
|
|
|
|
|
return e._original_reply(msg)
|
|
|
|
|
} else {
|
|
|
|
|
// console.log('请求错误')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
e._reqCount++
|
2022-10-21 14:48:19 +00:00
|
|
|
|
let ret = await mys.getData(api, data)
|
2022-09-20 11:50:27 +00:00
|
|
|
|
e._reqCount--
|
|
|
|
|
if (e._reqCount === 0) {
|
|
|
|
|
e.reply = e._original_reply
|
|
|
|
|
}
|
|
|
|
|
if (!ret) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return ret.data || ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取角色信息
|
|
|
|
|
async getCharacter () {
|
|
|
|
|
return await this.getData('character')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取角色详情
|
|
|
|
|
async getAvatar (id) {
|
|
|
|
|
return await this.getData('detail', { avatar_id: id })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 首页宝箱信息
|
|
|
|
|
async getIndex () {
|
|
|
|
|
return await this.getData('index')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取深渊信息
|
|
|
|
|
async getSpiralAbyss (type = 1) {
|
|
|
|
|
return await this.getData('spiralAbyss', { schedule_type: type })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDetail (id) {
|
|
|
|
|
return await this.getData('detail', { avatar_id: id })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getCompute (data) {
|
|
|
|
|
return await this.getData('compute', data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAvatarSkill (id) {
|
|
|
|
|
return await this.getData('avatarSkill', { avatar_id: id })
|
|
|
|
|
}
|
2022-09-20 12:16:19 +00:00
|
|
|
|
}
|