Miao-Yunzai/src/core/runtime.ts

307 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-06-09 01:00:07 +08:00
import lodash from 'lodash'
import fs from 'node:fs'
/**
* tudo
*/
import common from '../../lib/common/common.js'
import cfg from '../../lib/config/config.js'
import Handler from '../../lib/plugins/handler.js'
import puppeteer from '../../lib/puppeteer/puppeteer.js'
/**
* tudo
*
*/
import { Version } from './local.js'
/**
* tudo
*
*/
import gsCfg from '../../plugins/genshin/model/gsCfg.js'
//
import MysApi from '../../plugins/genshin/model/mys/mysApi.js'
import MysInfo from '../../plugins/genshin/model/mys/mysInfo.js'
import NoteUser from '../../plugins/genshin/model/mys/NoteUser.js'
import MysUser from '../../plugins/genshin/model/mys/MysUser.js'
/**
* *********************
* e进行重构的危险代码
*/
export default class Runtime {
e = null
_mysInfo = null
handler = null
constructor(e) {
this.e = e
this._mysInfo = {}
this.handler = {
has: Handler.has,
call: Handler.call,
callAll: Handler.callAll
}
}
2024-06-09 01:00:25 +08:00
get uid() {
2024-06-09 01:00:07 +08:00
return this.user?.uid
}
2024-06-09 01:00:25 +08:00
get hasCk() {
2024-06-09 01:00:07 +08:00
return this.user?.hasCk
}
2024-06-09 01:00:25 +08:00
get user() {
2024-06-09 01:00:07 +08:00
return this.e.user
}
2024-06-09 01:00:25 +08:00
get cfg() {
2024-06-09 01:00:07 +08:00
return cfg
}
2024-06-09 01:00:25 +08:00
get gsCfg() {
2024-06-09 01:00:07 +08:00
return gsCfg
}
2024-06-09 01:00:25 +08:00
get common() {
2024-06-09 01:00:07 +08:00
return common
}
2024-06-09 01:00:25 +08:00
get puppeteer() {
2024-06-09 01:00:07 +08:00
return puppeteer
}
2024-06-09 01:00:25 +08:00
get MysInfo() {
2024-06-09 01:00:07 +08:00
return MysInfo
}
2024-06-09 01:00:25 +08:00
get NoteUser() {
2024-06-09 01:00:07 +08:00
return NoteUser
}
2024-06-09 01:00:25 +08:00
get MysUser() {
2024-06-09 01:00:07 +08:00
return MysUser
}
/**
2024-06-09 01:00:25 +08:00
*
* @param e
* @returns
2024-06-09 01:00:07 +08:00
*/
2024-06-09 01:00:25 +08:00
static async init(e) {
2024-06-09 01:00:07 +08:00
await MysInfo.initCache()
let runtime = new Runtime(e)
e.runtime = runtime
await runtime.initUser()
return runtime
}
/**
2024-06-09 01:00:25 +08:00
*
2024-06-09 01:00:07 +08:00
*/
2024-06-09 01:00:25 +08:00
async initUser() {
2024-06-09 01:00:07 +08:00
let e = this.e
let user = await NoteUser.create(e)
if (user) {
e.user = new Proxy(user, {
2024-06-09 01:00:25 +08:00
get(self, key, receiver) {
2024-06-09 01:00:07 +08:00
let game = e.game
let fnMap = {
uid: 'getUid',
uidList: 'getUidList',
mysUser: 'getMysUser',
ckUidList: 'getCkUidList'
}
if (fnMap[key]) {
return self[fnMap[key]](game)
}
if (key === 'uidData') {
return self.getUidData('', game)
}
2024-06-09 01:00:25 +08:00
if (
[
'getUid',
'getUidList',
'getMysUser',
'getCkUidList',
'getUidMapList',
'getGameDs'
].includes(key)
) {
2024-06-09 01:00:07 +08:00
return (_game, arg2) => {
return self[key](_game || game, arg2)
}
}
// 不能将类型“symbol”分配给类型“string”。
2024-06-09 01:00:25 +08:00
if (
[
'getUidData',
'hasUid',
'addRegUid',
'delRegUid',
'setMainUid'
].includes(key)
) {
2024-06-09 01:00:07 +08:00
return (uid, _game = '') => {
return self[key](uid, _game || game)
}
}
return self[key]
}
})
}
}
/**
* MysInfo实例
*
* @param targetType all: 所有用户均可 cookieCookie
* @returns {Promise<boolean|MysInfo>}
*/
2024-06-09 01:00:25 +08:00
async getMysInfo(targetType = 'all') {
2024-06-09 01:00:07 +08:00
if (!this._mysInfo[targetType]) {
2024-06-09 01:00:25 +08:00
this._mysInfo[targetType] = await MysInfo.init(
this.e,
targetType === 'cookie' ? 'detail' : 'roleIndex'
)
2024-06-09 01:00:07 +08:00
}
return this._mysInfo[targetType]
}
/**
2024-06-09 01:00:25 +08:00
*
* @returns
2024-06-09 01:00:07 +08:00
*/
2024-06-09 01:00:25 +08:00
async getUid() {
2024-06-09 01:00:07 +08:00
return await MysInfo.getUid(this.e)
}
/**
* MysApi实例
*
* @param targetType all: 所有用户均可 cookieCookie
* @param option MysApi option
* @param isSr
* @returns {Promise<boolean|MysApi>}
*/
2024-06-09 01:00:25 +08:00
async getMysApi(targetType = 'all', option = {}, isSr = false) {
2024-06-09 01:00:07 +08:00
let mys = await this.getMysInfo(targetType)
if (mys.uid && mys?.ckInfo?.ck) {
return new MysApi(mys.uid, mys.ckInfo.ck, option, isSr)
}
return false
}
/**
* MysApi实例
* @param uid
* @param ck
* @param option
* @param isSr
* @returns {Promise<MysApi>}
*/
2024-06-09 01:00:25 +08:00
async createMysApi(uid, ck, option, isSr = false) {
2024-06-09 01:00:07 +08:00
return new MysApi(uid, ck, option, isSr)
}
/**
*
* @param plugin plugin key
* @param path html文件路径plugin resources目录
* @param data
* @param cfg
* @param cfg.retType
* * default/true
* * msgIdmsg id
* * base64: 不自动发送图像base64数据
* @param cfg.beforeRender({data}) data数据
* @returns {Promise<boolean>}
*/
2024-06-09 01:00:25 +08:00
async render(plugin, path, data = {}, cfg = {}) {
2024-06-09 01:00:07 +08:00
// 处理传入的path
path = path.replace(/.html$/, '')
2024-06-09 01:00:25 +08:00
let paths = lodash.filter(path.split('/'), p => !!p)
2024-06-09 01:00:07 +08:00
path = paths.join('/')
// 创建目录
2024-06-09 01:00:25 +08:00
const mkdir = check => {
2024-06-09 01:00:07 +08:00
let currDir = `${process.cwd()}/temp`
for (let p of check.split('/')) {
currDir = `${currDir}/${p}`
if (!fs.existsSync(currDir)) {
fs.mkdirSync(currDir)
}
}
return currDir
}
mkdir(`html/${plugin}/${path}`)
// 自动计算pluResPath
let pluResPath = `../../../${lodash.repeat('../', paths.length)}plugins/${plugin}/resources/`
let miaoResPath = `../../../${lodash.repeat('../', paths.length)}plugins/miao-plugin/resources/`
2024-06-09 01:00:25 +08:00
const layoutPath =
process.cwd() + '/plugins/miao-plugin/resources/common/layout/'
2024-06-09 01:00:07 +08:00
/**
* tudo
* data重写data
*/
// 渲染data
data = {
sys: {
scale: 1
},
/** miao 相关参数 **/
copyright: `Created By Miao-Yunzai<span class="version">${Version.yunzai}</span> `,
_res_path: pluResPath,
_miao_path: miaoResPath,
_tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/',
defaultLayout: layoutPath + 'default.html',
elemLayout: layoutPath + 'elem.html',
...data,
/** 默认参数 **/
_plugin: plugin,
_htmlPath: path,
pluResPath,
tplFile: `./plugins/${plugin}/resources/${path}.html`,
saveId: data.saveId || data.save_id || paths[paths.length - 1],
pageGotoParams: {
waitUntil: 'networkidle2'
}
}
// 处理beforeRender
if (cfg.beforeRender) {
data = cfg.beforeRender({ data }) || data
}
// 保存模板数据
if (process.argv.includes('dev')) {
// debug下保存当前页面的渲染数据方便模板编写与调试
// 由于只用于调试开发者只关注自己当时开发的文件即可暂不考虑app及plugin的命名冲突
let saveDir = mkdir(`ViewData/${plugin}`)
let file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json`
fs.writeFileSync(file, JSON.stringify(data))
}
// 截图
let base64 = await puppeteer.screenshot(`${plugin}/${path}`, data)
if (cfg.retType === 'base64') {
return base64
}
let ret = true
if (base64) {
if (cfg.recallMsg) {
ret = await this.e.reply(base64, false, {})
} else {
ret = await this.e.reply(base64)
}
}
return cfg.retType === 'msgId' ? ret : true
}
2024-06-09 01:00:25 +08:00
}