From c24fbe82fe8b52ebd053be4118c5523366a55afa Mon Sep 17 00:00:00 2001 From: Ctrlcvs <1509167646@qq.com> Date: Thu, 1 Sep 2022 22:52:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4render=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 +- apps/Note.js | 4 - apps/render.js | 226 -------------------------------------- apps/user.js | 15 +-- defSet/config/config.yaml | 5 +- model/gsCfg.js | 8 +- 6 files changed, 19 insertions(+), 245 deletions(-) delete mode 100644 apps/render.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d0a1327..d9561a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # 1.1.9 * 优化部分代码以及配置 * 新增指令`#更新抽卡记录` - *必须绑定stoken + * 必须绑定stoken + * 定义限制5分钟可请求一次避免刷屏`默认5分钟具体修改规则请自行修改config配置` * 默认配置文件位于****xiaoyao-cvs-plugin/defSet/config/config.yaml**** *后续叠加配置数据会在此文件中增加 + # 1.1.2~1.1.8 * 新增指令`#ck查询` * 可查询当前cookie绑定状态的签到信息以及米游币信息数据 @@ -24,7 +26,7 @@ * 具体有`原神、崩坏3、崩坏2、未定事件簿` * `大别野、崩坏星穹铁道、绝区零` 并未提供签到入口待定,后续概率补充 * 需要引入包文件`cnpm i promise-retry `跟`cnpm i superagent` -* V3版本引入文件`pnpm i promise-retry `跟`pnpm i superagent` +* V3版本引入文件`pnpm add promise-retry `跟`pnpm add superagent` * 如果出现了安装其中一个包导致另外的包出问题的请群里询问 * 需要完整的cookie内容要包含`login_ticket`不然是无法实现米游社签到的 * 获取到的cookies发送bot后请触发一次`#mys原神签到`指令以保存`stoken`实现自动签到**(注:触发指令后无需多次获取cookies)** diff --git a/apps/Note.js b/apps/Note.js index 69654d4..861c3be 100644 --- a/apps/Note.js +++ b/apps/Note.js @@ -11,10 +11,6 @@ import { // import { MysUser } from "../../../lib/components/Models.js"; // import common from "../../../lib/common.js"; import lodash from "lodash"; -import { - getPluginRender -} from "./render.js"; - import gsCfg from '../model/gsCfg.js' import { Cfg, diff --git a/apps/render.js b/apps/render.js deleted file mode 100644 index 0b7afa1..0000000 --- a/apps/render.js +++ /dev/null @@ -1,226 +0,0 @@ -import template from "art-template"; -import fs from "fs"; -import puppeteer from "puppeteer"; -import lodash from "lodash"; - -import { Data } from "../components/index.js"; - -const _path = process.cwd(); -//html模板 -const html = {}; -//浏览器 -let browser = ""; -//截图数达到时重启浏览器 避免生成速度越来越慢 -let restartNum = 20; -//截图次数 -let renderNum = 0; -//锁住 -let lock = false; -//截图中 -let shoting = []; - -/** - * 渲染生成图片,调试命令 npm run debug,window会直接打开无头浏览器 - * - * 原始html文件路径:/resources/app/type/type.html,文件夹名要和html名一致 - * - * 生成html文件路径:/data/html/app/type/save_id.html - * - * 模板生成art-template文档 http://aui.github.io/art-template/zh-cn/docs/ - * - * @param app 应用名称 - * @param type 方法名 - * @param data 前端参数,必传 data.save_id 用来区分模板 - * @param imgType 图片类型 jpeg,png(清晰一点,大小更大) - */ -const plugin="xiaoyao-cvs-plugin" -async function render1(app = "", type = "", data = {}, imgType = "jpeg") { - if (lodash.isUndefined(data._res_path)) { - data._res_path = `../../../../../plugins/xiaoyao-cvs-plugin/resources/`; - } - if (lodash.isUndefined(data._sys_res_path)) { - data._sys_res_path = `../../../../../plugins/xiaoyao-cvs-plugin/resources/`; - } - let tplKey = `${app}.${type}`; - let saveId = data.save_id || type; - let tplFile = `${_path}/plugins/xiaoyao-cvs-plugin/resources/${app}/${type}.html`; - Data.createDir(_path + `/data/`, `html/plugin_xiaoyao-cvs-plugin/${app}/${type}`); - let savePath = _path + `/data/html/plugin_xiaoyao-cvs-plugin/${app}/${type}/${saveId}.html`; - - return await doRender(app, type, data, imgType, { - tplKey, - tplFile, - savePath, - saveId, - }); -} - -async function doRender(app, type, data, imgType, renderCfg) { - - let { tplKey, tplFile, savePath, saveId } = renderCfg; - - if (global.debugView === "web-debug") { - // debug下保存当前页面的渲染数据,方便模板编写与调试 - // 由于只用于调试,开发者只关注自己当时开发的文件即可,暂不考虑app及plugin的命名冲突 - let saveDir = _path + "/data/ViewData/"; - if (!fs.existsSync(saveDir)) { - fs.mkdirSync(saveDir); - } - let file = saveDir + type + ".json"; - data._app = app; - fs.writeFileSync(file, JSON.stringify(data)); - - Bot.logger.mark(`${type}-tplFile:${tplFile}`); - Bot.logger.mark(`${type}-savePath:${savePath}`); - } - - if (!html[tplKey] || global.debugView) { - html[tplKey] = fs.readFileSync(tplFile, "utf8"); - } - - //替换模板 - let tmpHtml = template.render(html[tplKey], data); - //保存模板 - fs.writeFileSync(savePath, tmpHtml); - - - if (!(await browserInit())) { - return false; - } - - let base64 = ""; - let start = Date.now(); - try { - shoting.push(saveId); - //图片渲染 - const page = await browser.newPage(); - await page.goto("file://" + savePath); - let body = await page.$("#container"); - let randData = { - type: imgType, - encoding: "base64", - } - if(imgType == "jpeg"){ - randData.quality = 100; - } - if(imgType == "png"){ - randData.omitBackground=true; - } - base64 = await body.screenshot(randData); - if (!global.debugView) { - page.close().catch((err) => Bot.logger.error(err)); - } - shoting.pop(); - } catch (error) { - Bot.logger.error(`图片生成失败:${type}:${error}`); - //重启浏览器 - if (browser) { - await browser.close().catch((err) => Bot.logger.error(err)); - } - browser = ""; - base64 = ""; - return false; - } - - if (!base64) { - Bot.logger.error(`图片生成为空:${type}`); - return false; - } - - renderNum++; - Bot.logger.mark(`图片生成 ${type}:${Date.now() - start}ms 次数:${renderNum}`); - - if (typeof test != "undefined") { - return `图片base64:${type}`; - } - - //截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 - if (renderNum % restartNum == 0) { - if (shoting.length <= 0) { - setTimeout(async function () { - browser.removeAllListeners("disconnected"); - await browser.close().catch((err) => Bot.logger.error(err)); - browser = ""; - Bot.logger.mark("puppeteer 关闭重启"); - }, 100); - } - } - - return base64; -} - -async function browserInit() { - if (browser) { - return browser; - } - if (lock) { - return false; - } - lock = true; - Bot.logger.mark("puppeteer 启动中。。"); - //初始化puppeteer - browser = await puppeteer - .launch({ - // executablePath:'',//chromium其他路径 - headless: global.debugView === "debug" ? false : true, - args: [ - "--disable-gpu", - "--disable-dev-shm-usage", - "--disable-setuid-sandbox", - "--no-first-run", - "--no-sandbox", - "--no-zygote", - "--single-process", - ], - }) - .catch((err) => { - Bot.logger.error(err); - if(String(err).includes("correct Chromium")){ - Bot.logger.error("没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js"); - } - }); - - lock = false; - - if (browser) { - Bot.logger.mark("puppeteer 启动成功"); - - //监听Chromium实例是否断开 - browser.on("disconnected", function (e) { - Bot.logger.error("Chromium实例关闭或崩溃!"); - browser = ""; - }); - - return browser; - } else { - Bot.logger.error("puppeteer 启动失败"); - return false; - } -} - -function getPluginRender(plugin) { - return async function (app = "", type = "", data = {}, imgType = "jpeg") { - // 在data中保存plugin信息 - data._plugin = plugin; - - if (lodash.isUndefined(data._res_path)) { - data._res_path = `../../../../../plugins/${plugin}/resources/`; - } - if (lodash.isUndefined(data._sys_res_path)) { - data._sys_res_path = `../../../../../resources/`; - } - let tplKey = `${plugin}.${app}.${type}`; - let saveId = data.save_id; - let tplFile = _path + `/plugins/${plugin}/resources/${app}/${type}.html`; - Data.createDir(_path + `/data/`, `html/plugin_${plugin}/${app}/${type}`); - let savePath = _path + `/data/html/plugin_${plugin}/${app}/${type}/${saveId}.html`; - return await doRender(app, type, data, imgType, { - tplKey, - tplFile, - savePath, - saveId, - }); - } -} - -export { render1, browserInit, renderNum,getPluginRender }; diff --git a/apps/user.js b/apps/user.js index 4575bea..c970a45 100644 --- a/apps/user.js +++ b/apps/user.js @@ -58,14 +58,10 @@ export async function userInfo(e, { }) return true; } - +let configData=gsCfg.getfileYaml(`${_path}/plugins/xiaoyao-cvs-plugin/config/`, "config"); export async function gclog(e) { let user = new User(e); await user.cookie(e) - if(!e.cookies||e.cookies.includes("undefined")){ - e.reply("请先绑定stoken") - return true; - } let redis_Data = await redis.get(`xiaoyao:gclog:${e.user_id}`); if(redis_Data){ let time=redis_Data*1-Math.floor(Date.now()/1000); @@ -73,6 +69,10 @@ export async function gclog(e) { return true; } let miHoYoApi = new MihoYoApi(e); + if(!e.cookies||e.cookies.includes("undefined")){ + e.reply("请先绑定stoken") + return true; + } let kkbody = await miHoYoApi.getbody("原神"); const objData = await miHoYoApi.getUserInfo(kkbody) let data = objData.data @@ -116,8 +116,9 @@ export async function gclog(e) { e.isPrivate = true; await bing(e) } - redis.set(`xiaoyao:gclog:${e.user_id}`, Math.floor(Date.now()/1000)+(60*5), { //把色图链接写入缓存防止一直色色 - EX: 60*5 + let time=(configData.gclogEx||5)*60 + redis.set(`xiaoyao:gclog:${e.user_id}`, Math.floor(Date.now()/1000)+time, { //把色图链接写入缓存防止一直色色 + EX: time }); return true; } diff --git a/defSet/config/config.yaml b/defSet/config/config.yaml index c9dc633..4782c92 100644 --- a/defSet/config/config.yaml +++ b/defSet/config/config.yaml @@ -30,6 +30,5 @@ YunSignTime: 0 2 4 * * ? isYunSignMsg: false # 云原神帮助 yunDoc: docs.qq.com/doc/DTnJIREZwTmxEVHBl - - - +#更新抽卡记录缓存时间默认5分钟 +gclogEx: 5 diff --git a/model/gsCfg.js b/model/gsCfg.js index 370eb07..4949594 100644 --- a/model/gsCfg.js +++ b/model/gsCfg.js @@ -152,11 +152,13 @@ class GsCfg { let ck = fs.readFileSync(file, 'utf-8') let yaml = YAML.stringify(data) ck = YAML.parse(ck) - if (!ck) { + if (ck?.uid) { fs.writeFileSync(file, yaml, 'utf8') } else { - ck = YAML.stringify(ck) - fs.writeFileSync(file, yaml + ck, 'utf8') + if(!ck[Object.keys(data)[0]]){ + ck = YAML.stringify(ck) + fs.writeFileSync(file, yaml + ck, 'utf8') + } } }) }