miao-plugin/adapter/mys.js

151 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-07-23 20:32:10 +00:00
import lodash from 'lodash'
import { MysInfo } from './index.js'
2022-07-23 20:32:10 +00:00
class User {
constructor (cfg) {
this.id = cfg.id
this.uid = cfg.uid
this.cookie = ''
}
// 保存用户配置
async setCfg (path, value) {
let userCfg = await redis.get(`miao:user-cfg:${this.id}`) || await redis.get(`genshin:user-cfg:${this.id}`)
2022-07-23 20:32:10 +00:00
userCfg = userCfg ? JSON.parse(userCfg) : {}
lodash.set(userCfg, path, value)
await redis.set(`miao:user-cfg:${this.id}`, JSON.stringify(userCfg))
2022-07-23 20:32:10 +00:00
}
/* 获取用户配置 */
async getCfg (path, defaultValue) {
let userCfg = await redis.get(`miao:user-cfg:${this.id}`) || await redis.get(`genshin:user-cfg:${this.id}`)
2022-07-23 20:32:10 +00:00
userCfg = userCfg ? JSON.parse(userCfg) : {}
return lodash.get(userCfg, path, defaultValue)
}
async getMysUser () {
return {
uid: this.uid
}
}
}
class Mys {
constructor (e, uid, MysApi) {
2022-09-17 13:16:48 +00:00
let ckUid = MysApi.ckInfo?.uid
2022-07-23 20:32:10 +00:00
this.selfUser = new User({ id: e.user_id, uid })
2022-09-17 13:16:48 +00:00
this.targetUser = this.selfUser
2022-07-23 20:32:10 +00:00
this.e = e
this.MysApi = MysApi
2022-09-17 13:16:48 +00:00
2022-07-23 20:32:10 +00:00
e.targetUser = this.targetUser
e.selfUser = this.selfUser
2022-09-19 05:45:42 +00:00
e.isSelfCookie = uid * 1 === ckUid * 1
2022-07-23 20:32:10 +00:00
}
async getData (api, data) {
if (!this.MysApi) {
return false
}
let e = this.e
// 暂时先在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-07-23 20:32:10 +00:00
let ret = await MysInfo.get(this.e, api, data)
e._reqCount--
if (e._reqCount === 0) {
e.reply = e._original_reply
}
2022-07-23 20:32:10 +00:00
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 })
}
get isSelfCookie () {
2022-09-17 13:16:48 +00:00
return this.e.isSelfCookie
2022-07-23 20:32:10 +00:00
}
}
export async function getMysApi (e, cfg) {
let { auth = 'all' } = cfg
2022-09-17 12:46:39 +00:00
let MysApi = await MysInfo.init(e, 'roleIndex')
if (!MysApi) {
return false
}
let uid = MysApi.uid
let ckUid = MysApi.ckInfo?.uid
2022-07-23 20:32:10 +00:00
/* 检查user ck */
if (auth === 'cookie') {
2022-09-17 12:46:39 +00:00
let isCookieUser = await MysInfo.checkUidBing(uid)
if (!isCookieUser || uid !== ckUid) {
e.reply('尚未绑定Cookie...')
return false
}
2022-07-23 20:32:10 +00:00
}
return new Mys(e, uid, MysApi)
}
export async function checkAuth (e, cfg) {
2022-07-25 17:45:44 +00:00
let { auth = 'all' } = cfg
let uid = await MysInfo.getUid(e)
if (!uid) return false
if (auth === 'master' && !e.isMaster) {
return false
}
/* 检查user ck */
2022-09-17 12:46:39 +00:00
if (auth === 'cookie') {
let isCookieUser = await MysInfo.checkUidBing(uid)
if (!isCookieUser) {
e.reply('尚未绑定Cookie...')
return false
}
2022-07-25 17:45:44 +00:00
}
e.selfUser = new User({ id: e.user_id, uid })
return e.selfUser
2022-07-23 20:32:10 +00:00
}