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

150 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-07-24 11:46:05 +08:00
import YAML from 'yaml'
import chokidar from 'chokidar'
import fs from 'node:fs'
2022-07-29 01:42:35 +08:00
import {
promisify
} from 'node:util'
2022-07-24 11:46:05 +08:00
import lodash from 'lodash'
import {
Data
} from "../components/index.js";
import {
isV3
} from '../components/Changelog.js';
import utils from './mys/utils.js';
2022-07-29 01:42:35 +08:00
const plugin = "xiaoyao-cvs-plugin"
const pathPlugin=`./plugins/${plugin}/data/`
2022-09-09 10:24:10 +08:00
/**
* 配置文件
* 主要用于处理 stoken以及云原神账号数据
*/
2022-07-24 11:46:05 +08:00
class GsCfg {
2022-07-29 01:42:35 +08:00
constructor() {
2022-09-09 10:24:10 +08:00
2022-08-21 22:40:37 +08:00
}
2022-07-31 12:32:03 +08:00
/** 通用yaml读取*/
getfileYaml(path, name) {
2022-07-29 22:43:28 +08:00
return YAML.parse(
fs.readFileSync(path + name + ".yaml", 'utf8')
2022-07-29 22:43:28 +08:00
)
}
2022-07-29 01:42:35 +08:00
/** 读取用户绑定的ck */
async getBingCk() {
let ck = {}
let ckQQ = {}
let dir = './data/MysCookie/'
let files = fs.readdirSync(dir).filter(file => file.endsWith('.yaml'))
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
const readFile = promisify(fs.readFile)
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
let promises = []
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
files.forEach((v) => promises.push(readFile(`${dir}${v}`, 'utf8')))
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
const res = await Promise.all(promises)
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
res.forEach((v) => {
let tmp = YAML.parse(v)
lodash.forEach(tmp, (v, i) => {
ck[String(i)] = v
if (v.isMain && !ckQQ[String(v.qq)]) {
ckQQ[String(v.qq)] = v
}
})
})
2022-09-09 10:24:10 +08:00
2022-07-29 01:42:35 +08:00
return {
ck,
ckQQ
}
}
2022-09-09 10:24:10 +08:00
async getUserStoken(userId){
2022-09-09 10:17:33 +08:00
try {
2022-09-09 10:24:10 +08:00
let ck=YAML.parse(
2022-09-09 10:17:33 +08:00
fs.readFileSync(`plugins/${plugin}/data/yaml/${userId}.yaml`, 'utf8')
)
2022-09-09 10:24:10 +08:00
return ck||{}
2022-09-09 10:17:33 +08:00
}catch (ex) {
return {}
}
}
/** 读取所有用户绑定的stoken */
2022-08-01 23:13:34 +08:00
async getBingStoken() {
let ck = []
let ckQQ = {}
let dir = `plugins/${plugin}/data/yaml/`
let files = fs.readdirSync(dir).filter(file => file.endsWith('.yaml'))
2022-09-09 10:24:10 +08:00
2022-08-01 23:13:34 +08:00
const readFile = promisify(fs.readFile)
2022-09-09 10:24:10 +08:00
2022-08-01 23:13:34 +08:00
let promises = []
2022-09-09 10:24:10 +08:00
2022-08-01 23:13:34 +08:00
files.forEach((v) => promises.push(readFile(`${dir}${v}`, 'utf8')))
const res = await Promise.all(promises)
res.forEach((v, index) => {
2022-08-01 23:13:34 +08:00
let tmp = YAML.parse(v)
ck.push(tmp)
})
return ck
}
2022-07-29 01:42:35 +08:00
getBingCkSingle(userId) {
let file = `./data/MysCookie/${userId}.yaml`
try {
let ck = fs.readFileSync(file, 'utf-8')
ck = YAML.parse(ck)
return ck
} catch (error) {
return {}
}
}
getBingCookie(userId) {
let file = `./data/MysCookie/${userId}.yaml`
try {
let ck = fs.readFileSync(file, 'utf-8')
ck = YAML.parse(ck)
for (let item in ck) {
let login_ticket;
if (!ck[item].isMain) {
continue;
}
login_ticket = ck[item]?.login_ticket
ck = ck[item].ck
return {
ck,
item,
login_ticket
};
2022-07-29 01:42:35 +08:00
}
} catch (error) {
return {}
}
}
saveBingStoken(userId, data) {
let file = `./plugins/${plugin}/data/yaml/${userId}.yaml`
if (lodash.isEmpty(data)) {
fs.existsSync(file) && fs.unlinkSync(file)
} else {
2022-09-09 10:24:10 +08:00
fs.exists(file, (exists) => {
if (!exists) {
fs.writeFileSync(file, "", 'utf8')
}
let ck = fs.readFileSync(file, 'utf-8')
let yaml = YAML.stringify(data)
ck = YAML.parse(ck)
if (ck?.uid||!ck) {
fs.writeFileSync(file, yaml, 'utf8')
} else {
2022-09-01 22:52:29 +08:00
if(!ck[Object.keys(data)[0]]){
ck = YAML.stringify(ck)
fs.writeFileSync(file, yaml + ck, 'utf8')
}
}
})
}
}
2022-07-24 11:46:05 +08:00
}
2022-07-24 11:46:05 +08:00
export default new GsCfg()