diff --git a/.gitignore b/.gitignore index abc5995..8be9fb6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ test node_modules config/config data -data.db \ No newline at end of file +data.db +temp diff --git a/README.md b/README.md index 58389bf..d167314 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ pnpm install --filter=steam-plugin - [x] steam绑定 - [x] steam更换绑定 - [x] steam解除绑定 +- [x] steam状态 - [x] steam库存 - [x] steam最近游玩 - [x] steam愿望单 @@ -84,7 +85,6 @@ pnpm install --filter=steam-plugin - [x] 开启/关闭推送 - [x] steam特惠 - [x] 推送黑/白群名单 -- [ ] steam状态渲染成图片 - [ ] steam绑定渲染成图片 - [x] steam评论 - [ ] steam游戏详情 diff --git a/apps/info.js b/apps/info.js index 749d3f5..69d9ade 100644 --- a/apps/info.js +++ b/apps/info.js @@ -29,9 +29,9 @@ export const rule = { const color = info.gameid ? 1 : info.personastate === 0 ? 3 : 2 const bg = await api.IPlayerService.GetProfileItemsEquipped(steamId) const img = await Render.render('info/index', { - background: utils.getStaticUrl(bg.mini_profile_background.image_large), - frame: utils.getStaticUrl(bg.avatar_frame.image_small), - avatar: Config.other.steamAvatar ? info.avatarfull : await utils.getUserAvatar(e.self_id, uid, e.group_id), + background: await utils.saveImg(utils.getStaticUrl(bg.mini_profile_background.image_large)), + frame: await utils.saveImg(utils.getStaticUrl(bg.avatar_frame.image_small)), + avatar: await utils.saveImg(Config.other.steamAvatar ? info.avatarfull : await utils.getUserAvatar(e.self_id, uid, e.group_id)), name: info.personaname, status: utils.getPersonaState(info.personastate), gameId: info.gameid, @@ -41,7 +41,10 @@ export const rule = { lastTime: (info.lastlogoff && info.personastate === 0) ? moment.unix(info.lastlogoff).format('YYYY-MM-DD HH:mm:ss') : '', country: info.loccountrycode ? getLoccountryCode(info.loccountrycode) : '', color, - scale: 1.4 + scale: 1.4, + pageGotoParams: { + waitUntil: 'load' + } }) if (img) { await e.reply(img) diff --git a/models/utils/index.js b/models/utils/index.js index 3d81ccf..9777f5f 100644 --- a/models/utils/index.js +++ b/models/utils/index.js @@ -1,10 +1,19 @@ -import { Version } from '#components' -import * as request from './request.js' +import fs from 'fs' import moment from 'moment' import { Bot, logger } from '#lib' +import { Version } from '#components' +import * as request from './request.js' +import { join } from 'path' export { request } +const tempDir = join(Version.pluginPath, 'temp') + +if (fs.existsSync(tempDir)) { + fs.rmdirSync(tempDir, { recursive: true }) +} +fs.mkdirSync(tempDir) + const steamIdOffset = 76561197960265728n /** @@ -206,11 +215,13 @@ export function getStaticUrl (path) { /** * 获取图片buffer * @param {string} url + * @param {number} retry 重试次数 默认3 * @returns {Promise} */ -export async function getImgUrlBuffer (url) { +export async function getImgUrlBuffer (url, retry = 3) { if (!url) return null - for (let i = 0; i < 3; i++) { + retry = Number(retry) || 3 + for (let i = 0; i < retry; i++) { try { const buffer = await request.get(url, { responseType: 'arraybuffer', baseUrl: '' }).then(res => res.data) if (Version.BotName === 'Karin') { @@ -225,6 +236,36 @@ export async function getImgUrlBuffer (url) { return null } +/** + * 将图片保存到临时文件夹 + * @param {*} url + * @param {number} retry 重试次数 默认3 + * @returns {Promise} 图片绝对路径 + */ +export async function saveImg (url, retry = 3) { + if (!url) return '' + retry = Number(retry) || 3 + for (let i = 0; i < retry; i++) { + try { + let ext = '' + const buffer = await request.get(url, { responseType: 'arraybuffer', baseUrl: '' }).then(res => { + ext = res.headers['content-type']?.split('/')?.pop() || 'png' + return res.data + }) + const filename = `${Date.now()}.${ext}` + const filepath = join(tempDir, filename) + fs.writeFileSync(filepath, buffer) + setTimeout(() => { + fs.unlinkSync(filepath) + }, 1000 * 60 * 10) // 10分钟后删除 + return filepath.replace(/\\/g, '/') + } catch (error) { + logger.error(`保存图片${url}失败: ${error.message}, 第${i + 1}次重试`) + } + } + return '' +} + /** * 将用户状态码转换为中文 * @param {number} state diff --git a/resources/info/index.html b/resources/info/index.html index 219cbb6..5bafaa4 100644 --- a/resources/info/index.html +++ b/resources/info/index.html @@ -4,7 +4,7 @@ {{/block}}