2022-08-18 10:13:42 +00:00
|
|
|
|
import Base from './Base.js'
|
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
|
|
|
|
|
|
export default class ProfileReq extends Base {
|
2023-02-09 16:57:25 +00:00
|
|
|
|
constructor (e) {
|
2022-08-18 10:13:42 +00:00
|
|
|
|
super()
|
|
|
|
|
this.e = e
|
2023-02-09 16:57:25 +00:00
|
|
|
|
this.uid = e.uid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create (e) {
|
|
|
|
|
if (!e || !e.uid) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return new ProfileReq(e)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setCd (seconds = 60) {
|
|
|
|
|
let ext = new Date() * 1 + seconds * 1000
|
2022-08-18 21:04:31 +00:00
|
|
|
|
await redis.set(`miao:profile-cd:${this.uid}`, ext + '', { EX: seconds })
|
2022-08-18 10:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async inCd () {
|
2022-08-18 21:04:31 +00:00
|
|
|
|
let ext = await redis.get(`miao:profile-cd:${this.uid}`)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
if (!ext || isNaN(ext)) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-01-17 18:40:17 +00:00
|
|
|
|
let cd = (new Date() * 1) - ext
|
|
|
|
|
if (cd < 0 && Math.abs(cd) < 100 * 60 * 1000) {
|
2022-08-18 10:13:42 +00:00
|
|
|
|
return Math.ceil(0 - cd / 1000)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err (msg = '', cd = 0) {
|
2023-02-12 15:39:46 +00:00
|
|
|
|
let serv = this.serv
|
|
|
|
|
let extra = serv.name ? `当前面板服务${serv.name},` : ''
|
2022-08-18 10:13:42 +00:00
|
|
|
|
const msgs = {
|
2023-02-12 21:18:06 +00:00
|
|
|
|
error: `UID${this.uid}更新面板失败,${extra}\n可能是面板服务维护中,请稍后重试...`,
|
2022-08-18 10:13:42 +00:00
|
|
|
|
empty: '请将角色放置在【游戏内】角色展柜,并打开【显示详情】,等待5分钟重新获取面板'
|
|
|
|
|
}
|
|
|
|
|
msg = msgs[msg] || msg
|
2023-02-17 20:42:05 +00:00
|
|
|
|
this.msg(msg)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
// 设置CD
|
|
|
|
|
if (cd) {
|
|
|
|
|
this.setCd(cd)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg (msg) {
|
2023-02-17 20:42:05 +00:00
|
|
|
|
let e = this.e
|
|
|
|
|
if (msg && !e._isReplyed) {
|
|
|
|
|
e.reply(msg)
|
|
|
|
|
e._isReplyed = true
|
|
|
|
|
}
|
2022-08-18 10:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
async requestProfile (player, serv) {
|
2023-02-12 15:39:46 +00:00
|
|
|
|
this.serv = serv
|
2023-02-09 16:57:25 +00:00
|
|
|
|
let reqParam = await serv.getReqParam(this.uid)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
let cdTime = await this.inCd()
|
2023-02-12 15:39:46 +00:00
|
|
|
|
if (cdTime && !process.argv.includes('web-debug')) {
|
2023-02-09 17:17:18 +00:00
|
|
|
|
return this.err(`请求过快,请${cdTime}秒后重试..`)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
await this.setCd(20)
|
|
|
|
|
// 发起请求
|
2023-02-12 15:39:46 +00:00
|
|
|
|
logger.mark(`面板请求UID:${this.uid},面板服务:${serv.name}...`)
|
2022-08-22 20:53:31 +00:00
|
|
|
|
let data = {}
|
|
|
|
|
try {
|
2022-08-30 19:36:40 +00:00
|
|
|
|
let params = reqParam.params || {}
|
2022-09-14 04:25:14 +00:00
|
|
|
|
params.timeout = params.timeout || 1000 * 20
|
2022-08-30 19:36:40 +00:00
|
|
|
|
let req = await fetch(reqParam.url, params)
|
2022-09-14 04:25:14 +00:00
|
|
|
|
data = await req.text()
|
2022-09-14 19:31:10 +00:00
|
|
|
|
if (data[0] === '<') {
|
2022-09-14 04:25:14 +00:00
|
|
|
|
let titleRet = /<title>(.+)<\/title>/.exec(data)
|
2022-09-14 19:31:10 +00:00
|
|
|
|
if (titleRet && titleRet[1]) {
|
2022-09-14 04:25:14 +00:00
|
|
|
|
data = { error: titleRet[1] }
|
|
|
|
|
} else {
|
|
|
|
|
return this.err('error', 60)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data = JSON.parse(data)
|
|
|
|
|
}
|
2022-08-22 20:53:31 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('面板请求错误', e)
|
|
|
|
|
data = {}
|
|
|
|
|
}
|
2023-02-09 16:57:25 +00:00
|
|
|
|
data = await serv.response(data, this)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
// 设置CD
|
2023-02-09 16:57:25 +00:00
|
|
|
|
cdTime = serv.getCdTime(data)
|
2022-08-18 10:13:42 +00:00
|
|
|
|
if (cdTime) {
|
|
|
|
|
await this.setCd(cdTime)
|
|
|
|
|
}
|
2022-08-20 23:40:32 +00:00
|
|
|
|
if (data === false) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-02-09 16:57:25 +00:00
|
|
|
|
serv.updatePlayer(player, data)
|
|
|
|
|
cdTime = serv.getCdTime(data)
|
2022-08-20 23:40:32 +00:00
|
|
|
|
if (cdTime) {
|
|
|
|
|
await this.setCd(cdTime)
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
return player
|
2022-08-18 10:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|