update plugins/genshin/model/mys/mysInfo.js.

Signed-off-by: hbj白夜 <hei_bai_jun@163.com>
This commit is contained in:
hbj白夜 2023-10-10 02:38:58 +00:00 committed by Gitee
parent 8893df2ee4
commit b3be01ddbe
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 134 additions and 75 deletions

View File

@ -1,9 +1,11 @@
import MysApi from './mysApi.js'
import GsCfg from '../gsCfg.js'
import lodash from 'lodash'
import fetch from 'node-fetch'
import NoteUser from './NoteUser.js'
import MysUser from './MysUser.js'
import DailyCache from './DailyCache.js'
import common from '../../../../lib/common/common.js'
export default class MysInfo {
static tips = '请先#绑定cookie\n发送【体力帮助】查看配置教程'
@ -69,6 +71,17 @@ export default class MysInfo {
return mysInfo
}
static async getNoteUser(e) {
await MysInfo.initCache()
let user = await NoteUser.create(e)
if (user) {
// 强制读取一次ck防止一些问题
user._getCkData()
return user
}
return false
}
/**
* 获取UID
* @param e
@ -134,6 +147,23 @@ export default class MysInfo {
return selfUser.getUid(e)
}
/** 判断绑定ck才能查询 */
checkAuth(api) {
if (api === 'cookie') {
return true
}
if (lodash.isObject(api)) {
for (let i in api) {
if (this.auth.includes(i)) {
return true
}
}
} else if (this.auth.includes(api)) {
return true
}
return false
}
/**
* @param e
* @param e.apiSync 多个请求时是否同步请求
@ -187,10 +217,52 @@ export default class MysInfo {
} else {
res = await mysApi.getData(api, data)
res = await mysInfo.checkCode(res, api, mysApi, data)
if (res === 'repeat') {
res = await mysApi.getData(api, data)
}
}
return res
}
async checkReply() {
if (this.e.noTips === true) return
if (!this.uid) {
this.e.reply('请先#绑定uid')
}
if (!this.ckInfo.ck) {
this.e.reply('暂无可用CK请绑定更多用户或设置公共ck..')
}
this.e.noTips = true
}
/* 获取请求所需ck */
/**
* 获取请求所需CK
* @param game 游戏
* @param onlySelfCk 是否只获取uid自己对应的ck为true则只获取uid对应ck若无则返回为空
* @returns {Promise<string|string|*>} 查询ck获取失败则返回空
*/
async getCookie(game = 'gs', onlySelfCk = false) {
if (this.ckUser?.ck) return this.ckUser?.ck
let mysUser = await MysUser.getByQueryUid(this.uid, game, onlySelfCk)
if (mysUser) {
if (mysUser.ck) {
this.ckInfo = mysUser.getCkInfo()
this.ckUser = mysUser
// 暂时直接记录请求uid后期优化分析MysApi请求结果分状态记录结果
await mysUser.addQueryUid(this.uid, game)
} else {
// 重新分配
await mysUser.disable(game)
return onlySelfCk ? '' : await this.getCookie(game)
}
}
return this.ckUser?.ck
}
/**
* 初始化公共CK
@ -261,81 +333,6 @@ export default class MysInfo {
return true
}
static async getBingCkUid() {
let res = await GsCfg.getBingCk()
return { ...res.ck }
}
// 获取uid绑定的ck信息
static async checkUidBing(uid, game = 'gs') {
let ckUser = await MysUser.getByQueryUid(uid, game, true)
if (ckUser && ckUser.ck) {
return ckUser
}
return false
}
static async delDisable() {
return await MysUser.delDisable()
}
/** 判断绑定ck才能查询 */
checkAuth(api) {
if (api === 'cookie') {
return true
}
if (lodash.isObject(api)) {
for (let i in api) {
if (this.auth.includes(i)) {
return true
}
}
} else if (this.auth.includes(api)) {
return true
}
return false
}
async checkReply() {
if (this.e.noTips === true) return
if (!this.uid) {
this.e.reply('请先#绑定uid')
}
if (!this.ckInfo.ck) {
this.e.reply('暂无可用CK请绑定更多用户或设置公共ck..')
}
this.e.noTips = true
}
/* 获取请求所需ck */
/**
* 获取请求所需CK
* @param game 游戏
* @param onlySelfCk 是否只获取uid自己对应的ck为true则只获取uid对应ck若无则返回为空
* @returns {Promise<string|string|*>} 查询ck获取失败则返回空
*/
async getCookie(game = 'gs', onlySelfCk = false) {
if (this.ckUser?.ck) return this.ckUser?.ck
let mysUser = await MysUser.getByQueryUid(this.uid, game, onlySelfCk)
if (mysUser) {
if (mysUser.ck) {
this.ckInfo = mysUser.getCkInfo()
this.ckUser = mysUser
// 暂时直接记录请求uid后期优化分析MysApi请求结果分状态记录结果
await mysUser.addQueryUid(this.uid, game)
} else {
// 重新分配
await mysUser.disable(game)
return onlySelfCk ? '' : await this.getCookie(game)
}
}
return this.ckUser?.ck
}
async checkCode(res, type, mysApi = {}, data = {}, isTask = false) {
if (!res) {
if (!isTask) this.e.reply('米游社接口请求失败,暂时无法查询')
@ -391,6 +388,9 @@ export default class MysInfo {
if (res.api === 'detail') res.retcode = 0
break
case 1034:
if (await this.bbsVerification()) {
return 'repeat'
}
logger.mark(`[米游社查询失败][uid:${this.uid}][qq:${this.userId}] 遇到验证码`)
if (!isTask) this.e.reply('米游社查询遇到验证码,请稍后再试')
break
@ -406,6 +406,47 @@ export default class MysInfo {
return res
}
/** 刷新米游社验证 */
async bbsVerification () {
let create = await MysInfo.get(this.e, 'createVerification')
if (!create || create.retcode !== 0) return false
let verify = await MysInfo.verify(this.e, { uid: this.uid, ...create.data })
if (!verify) return false
let submit = await MysInfo.get(this.e, 'verifyVerification', verify)
if (!submit || submit.retcode !== 0) return false
return true
}
/** 手动验证 */
static async verify (e, data) {
if (!data.gt || !data.challenge || !e?.reply) return false
let cfg = { ...GsCfg.getdefSet('mys', 'set'), ...GsCfg.getYaml('mys', 'set', 'config') }
if (!cfg.verify || !cfg.verifyAddr) return false
/** 传递gt、challenge参数返回link、result地址 */
let res = await fetch(cfg.verifyAddr, {
method: 'POST',
body: JSON.stringify(data)
})
res = await res.json()
if (!res.data) return false
await e.reply(`请打开地址并完成验证\n${res.data.link}`, true)
for (let i = 0; i < 60; i++) {
let validate = await fetch(res.data.result)
validate = await validate.json()
if (validate.data) {
logger.mark(`[米游社验证成功][uid:${e.uid || data.uid}][qq:${e.user_id}]`)
return validate.data
}
await common.sleep(2000)
}
return false
}
/** 删除失效ck */
async delCk() {
if (!this.ckUser) {
@ -421,4 +462,22 @@ export default class MysInfo {
/** 统计次数设为超限 */
await this.ckUser.disable(game)
}
static async getBingCkUid() {
let res = await GsCfg.getBingCk()
return { ...res.ck }
}
// 获取uid绑定的ck信息
static async checkUidBing(uid, game = 'gs') {
let ckUser = await MysUser.getByQueryUid(uid, game, true)
if (ckUser && ckUser.ck) {
return ckUser
}
return false
}
static async delDisable() {
return await MysUser.delDisable()
}
}