2
0
mirror of https://github.com/ctrlcvs/xiaoyao-cvs-plugin.git synced 2024-12-23 20:00:51 +08:00
xiaoyao-cvs-plugin/model/user.js

193 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-08-16 23:24:54 +08:00
import YAML from 'yaml'
import chokidar from 'chokidar'
import MihoYoApi from "../model/mys/mihoyo-api.js"
import fs from 'node:fs'
import promiseRetry from 'promise-retry';
import lodash from 'lodash'
2022-08-17 08:06:58 +08:00
import utils from '../model/mys/utils.js';
2022-08-16 23:24:54 +08:00
import gsCfg from './gsCfg.js';
import {
isV3
} from '../components/Changelog.js';
const _path = process.cwd();
const plugin = "xiaoyao-cvs-plugin"
const RETRY_OPTIONS = {
retries: 3,
minTimeout: 5000,
maxTimeout: 10000
};
const nameData = ["原神", "崩坏3", "崩坏2", "未定事件簿"];
const YamlDataUrl = `${_path}/plugins/xiaoyao-cvs-plugin/data/yaml`;
2022-08-16 23:24:54 +08:00
/** 配置文件 */
export default class user {
constructor(e) {
this.e = e;
this.stokenPath = `./plugins/${plugin}/data/yaml/`
this.yunPath = `./plugins/${plugin}/data/yunToken/`;
2022-08-16 23:24:54 +08:00
this.getyunToken(this.e)
}
async getCkData() {
let sumData = {};
2022-08-16 23:24:54 +08:00
await this.cookie(this.e)
this.miHoYoApi = new MihoYoApi(this.e);
if (this.e.yuntoken) {
2022-08-17 23:12:40 +08:00
let yunres = await this.miHoYoApi.logyunGenshen();
2022-08-16 23:44:24 +08:00
let yundata = yunres.data
if (yunres.retcode === 0) {
sumData["云原神"] = {
"今日可获取": yundata?.coin?.coin_num,
"免费时长": yundata?.free_time?.free_time,
"总时长": yundata.total_time
2022-08-16 23:44:24 +08:00
}
2022-08-16 23:24:54 +08:00
}
}
if (this.e.cookies) {
2022-08-17 23:12:40 +08:00
let mysres = await this.miHoYoApi.getTasksList();
if (mysres.retcode === 0) {
sumData["米游社"] = {
"米游币任务": mysres.data.can_get_points != 0 ? "未完成" : "已完成",
"米游币余额": mysres.data.total_points,
"今日剩余可获取": mysres.data.can_get_points
2022-08-16 23:44:24 +08:00
}
}
2022-08-16 23:44:24 +08:00
}
if (this.e.cookie) {
for (let name of nameData) {
2022-08-17 23:12:40 +08:00
let resSign = await this.miHoYoApi.honkai3rdSignTask(name);
if (resSign?.upData) {
2022-08-16 23:44:24 +08:00
// console.log(resSign?.upData)
for (let item of resSign?.upData) {
let num = lodash.random(0, 9999);
item.upName = item.upName == "原神" ? "ys" : item.upName == "崩坏3" ? "bh3" : item.upName ==
"崩坏2" ? "bh2" : item.upName == "未定事件簿" ? "wdy" : ""
sumData[item.upName + "" + num] = {
"uid": item.game_uid,
"游戏昵称": item.nickname,
"等级": item.level,
"今日签到": item.is_sign ? "已签到" : "未签到",
"累计签到": item.total_sign_day + "天",
"今天奖励": item.awards
2022-08-16 23:44:24 +08:00
}
2022-08-16 23:24:54 +08:00
}
}
}
}
return sumData;
}
2022-08-16 23:24:54 +08:00
getyunToken(e) {
let file = `${this.yunPath}${e.user_id}.yaml`
try {
let ck = fs.readFileSync(file, 'utf-8')
ck = YAML.parse(ck)
this.e.devId = ck.devId;
this.e.yuntoken = ck.yuntoken;
return ck
} catch (error) {
return ""
}
}
async cookie(e) {
let {
cookie,
uid,
skuid
2022-08-16 23:24:54 +08:00
} = await this.getCookie(e);
let cookiesDoc = await this.getcookiesDoc();
let miHoYoApi = new MihoYoApi(this.e);
2022-08-16 23:24:54 +08:00
if (!cookie) {
2022-08-17 08:00:28 +08:00
e.reply("请先#绑定cookie\n发送【体力帮助】查看配置教程")
2022-08-16 23:24:54 +08:00
return false;
}
let stokens = this.getStoken(e.user_id)
if (!stokens) {
2022-08-16 23:24:54 +08:00
return true;
}
if (!cookie.includes("login_ticket") && (isV3 && !skuid?.login_ticket)) {
2022-08-17 21:51:22 +08:00
// e.reply("米游社登录cookie不完整请前往米游社通行证处重新获取cookie~\ncookies必须包含login_ticket【教程】 " + cookiesDoc)
2022-08-16 23:24:54 +08:00
return false;
}
let flot = (await miHoYoApi.stoken(cookie, e));
// console.log(flot)
await utils.sleepAsync(1000); //延迟加载防止文件未生成
if (!flot) {
e.reply("登录失效请重新登录获取cookie发送机器人~")
return false;
}
return true;
}
async getcookiesDoc() {
2022-08-16 23:24:54 +08:00
return await gsCfg.getfileYaml(`${_path}/plugins/xiaoyao-cvs-plugin/config/`, "config").cookiesDoc
}
async getCookie(e) {
let skuid, cookie, uid
if (isV3) {
skuid = await gsCfg.getBingCookie(e.user_id);
cookie = skuid.ck;
uid = skuid.item;
} else {
if (NoteCookie[e.user_id]) {
cookie = NoteCookie[e.user_id].cookie;
uid = NoteCookie[e.user_id].uid;
skuid = NoteCookie[e.user_id];
} else if (BotConfig.dailyNote && BotConfig.dailyNote[e.user_id]) {
cookie = BotConfig.dailyNote[e.user_id].cookie;
uid = BotConfig.dailyNote[e.user_id].uid;
skuid = BotConfig.NoteCookie[e.user_id];
}
}
this.e.uid = uid;
this.e.cookie = cookie;
return {
cookie,
uid,
skuid
}
}
getStoken(userId) {
let file = `${YamlDataUrl}/${userId}.yaml`
try {
let ck = fs.readFileSync(file, 'utf-8')
ck = YAML.parse(ck)
if (ck?.uid) {
let datalist = {};
ck.userId = this.e.user_id
datalist[ck.uid] = ck;
ck = datalist
gsCfg.saveBingStoken(this.e.user_id, datalist)
}
return ck[this.e.uid] || {}
} catch (error) {
return {}
}
}
async delSytk(path,e){
await this.getCookie(e);
let file = `${path}/${e.user_id}.yaml`
fs.exists(file, (exists) => {
if (!exists) {
return true;
}
let ck = fs.readFileSync(file, 'utf-8')
ck = YAML.parse(ck)
if(ck?.yuntoken){
fs.unlinkSync(file);
}else if(ck){
if(!ck[e.uid]) {
return true;
}
delete ck[e.uid];
if(Object.keys(ck)==0){
fs.unlinkSync(file);
}else{
ck = YAML.stringify(ck)
fs.writeFileSync(file, ck, 'utf8')
}
}
e.reply(`已删除${e.msg}`)
return true;
})
}
2022-08-16 23:24:54 +08:00
}