Miao-Yunzai/src/core/plugins/runtime.ts

331 lines
7.5 KiB
TypeScript
Raw Normal View History

import { filter, repeat } from 'lodash-es'
2024-06-17 22:49:05 +08:00
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
2024-06-09 01:31:31 +08:00
import {
gsCfg,
mysApi as MysApi,
mysInfo as MysInfo,
NoteUser,
MysUser
2024-06-09 21:21:20 +08:00
} from '../../mys/index.js'
2024-06-15 13:37:38 +08:00
import puppeteer from '../../utils/puppeteer/puppeteer.js'
2024-06-15 23:40:16 +08:00
import * as common from '../../utils/common.js'
import cfg from '../../config/config.js'
import Handler from './handler.js'
2024-06-17 13:19:18 +08:00
import { Version } from '../../mys/miao.js'
2024-06-15 13:37:38 +08:00
2024-06-09 01:00:07 +08:00
/**
2024-06-15 23:40:16 +08:00
* @deprecated
2024-06-09 01:00:07 +08:00
*/
export default class Runtime {
e = null
_mysInfo = null
handler = null
2024-06-09 11:40:24 +08:00
/**
*
* @param e
*/
2024-06-09 01:00:07 +08:00
constructor(e) {
this.e = e
this._mysInfo = {}
this.handler = {
has: Handler.has,
call: Handler.call,
callAll: Handler.callAll
}
}
2024-06-09 11:40:24 +08:00
/**
*
*/
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 11:40:24 +08:00
/**
*
*/
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 11:40:24 +08:00
/**
*
*/
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 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get cfg() {
2024-06-09 01:00:07 +08:00
return cfg
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get gsCfg() {
2024-06-09 01:00:07 +08:00
return gsCfg
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get common() {
2024-06-09 01:00:07 +08:00
return common
}
2024-06-09 01:31:31 +08:00
/**
* @deprecated
*/
2024-06-09 01:00:25 +08:00
get puppeteer() {
2024-06-15 13:37:38 +08:00
return puppeteer
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get MysInfo() {
2024-06-09 01:00:07 +08:00
return MysInfo
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get NoteUser() {
2024-06-09 01:00:07 +08:00
return NoteUser
}
2024-06-09 11:40:24 +08:00
/**
*
*/
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()
2024-06-15 23:40:16 +08:00
const runtime = new Runtime(e)
2024-06-09 01:00:07 +08:00
e.runtime = runtime
await runtime.initUser()
return runtime
}
/**
2024-06-09 01:31:31 +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) {
2024-06-09 01:31:31 +08:00
// 对象代理
2024-06-09 01:00:07 +08:00
e.user = new Proxy(user, {
2024-06-10 15:28:42 +08:00
get(self, key) {
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:31:31 +08:00
// 不能将类型“symbol”分配给类型“string”。
2024-06-09 01:00:25 +08:00
if (
[
'getUid',
'getUidList',
'getMysUser',
'getCkUidList',
'getUidMapList',
'getGameDs'
2024-06-09 01:31:31 +08:00
].includes(key as string)
2024-06-09 01:00:25 +08:00
) {
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'
2024-06-09 01:31:31 +08:00
].includes(key as string)
2024-06-09 01:00:25 +08:00
) {
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)
}
/**
2024-06-09 01:31:31 +08:00
* @deprecated
2024-06-15 23:40:16 +08:00
* @param plugin_name plugin key
2024-06-15 13:37:38 +08:00
* @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:07 +08:00
*/
2024-06-15 23:40:16 +08:00
async render(
plugin_name: string,
basePath: string,
2024-06-15 23:40:16 +08:00
data: {
[key: string]: any
saveId?: any,
save_id?: any,
_htmlPath?: any,
} = {}, cfg: {
[key: string]: any
retType?: any,
recallMsg?: any
beforeRender?: any
} = {}
) {
// 处理传入的path
basePath = basePath.replace(/.html$/, '')
let paths = filter(basePath.split('/'), (p) => !!p)
basePath = paths.join('/')
2024-06-15 23:40:16 +08:00
// 创建目录
const mkdir = (check) => {
let currDir = `${process.cwd()}/temp`
for (let p of check.split('/')) {
currDir = `${currDir}/${p}`
2024-06-17 22:49:05 +08:00
if (!existsSync(currDir)) {
mkdirSync(currDir)
2024-06-15 23:40:16 +08:00
}
}
return currDir
}
mkdir(`html/${plugin_name}/${basePath}`)
2024-06-15 23:40:16 +08:00
// 自动计算pluResPath
const pluResPath = `../../../${repeat('../', paths.length)}plugins/${plugin_name}/resources/`
const miaoResPath = `../../../${repeat('../', paths.length)}plugins/miao-plugin/resources/`
2024-06-15 23:40:16 +08:00
const layoutPath = process.cwd() + '/plugins/miao-plugin/resources/common/layout/'
// 渲染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_name,
_htmlPath: basePath,
2024-06-15 23:40:16 +08:00
pluResPath,
tplFile: `./plugins/${plugin_name}/resources/${basePath}.html`,
2024-06-15 23:40:16 +08:00
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的命名冲突
2024-06-17 22:49:05 +08:00
const saveDir = mkdir(`ViewData/${plugin_name}`)
const file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json`
writeFileSync(file, JSON.stringify(data))
2024-06-15 23:40:16 +08:00
}
// 截图
const base64 = await puppeteer.screenshot(`${plugin_name}/${basePath}`, data)
2024-06-15 23:40:16 +08:00
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:07 +08:00
}
2024-06-15 23:40:16 +08:00
}