From 8a1a6fa0950e35a70ac266ecd86d7a76f671869c Mon Sep 17 00:00:00 2001 From: ueiu Date: Sun, 10 Nov 2024 11:09:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(route/xiaoheihe):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=B0=8F=E9=BB=91=E7=9B=92=20-=20=E6=B8=B8=E6=88=8F=E6=8A=98?= =?UTF-8?q?=E6=89=A3=E6=95=B0=E6=8D=AE=E6=8A=93=E5=8F=96=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/xiaoheihe/discount.ts | 99 +++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/lib/routes/xiaoheihe/discount.ts b/lib/routes/xiaoheihe/discount.ts index ee6afbd46af022..6a92277d5093a1 100644 --- a/lib/routes/xiaoheihe/discount.ts +++ b/lib/routes/xiaoheihe/discount.ts @@ -1,5 +1,6 @@ import { Route } from '@/types'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; +import { calculate } from './util'; export const route: Route = { path: '/discount/:platform', @@ -23,20 +24,58 @@ export const route: Route = { }; const PLATFORM_MAP = { - pc: 'PC', - switch: 'Switch', - psn: 'PSN', - xbox: 'Xbox', + pc: { + key: 'pc', + desc: 'PC', + }, + switch: { + key: 'switch', + desc: 'Switch', + }, + psn: { + key: 'ps4', + desc: 'PSN', + }, + xbox: { + key: 'xbox', + desc: 'Xbox', + }, }; +function getDiscountDesc(discount) { + return `${(100 - discount) / 10}折`; +} + +function getLowestDesc(priceInfo, isSuperLowest = false) { + if (!('is_lowest' in priceInfo) || priceInfo.is_lowest === 0) { + return ''; + } else if (isSuperLowest) { + return '[超史低]'; + } else if (priceInfo.is_lowest && priceInfo.is_lowest === 1 && priceInfo.new_lowest && priceInfo.new_lowest === 1) { + return '[新史低]'; + } else if (priceInfo.is_lowest && priceInfo.is_lowest === 1) { + return '[史低]'; + } +} + +function getHeyboxPriceDesc(heyboxPriceInfo) { + if (heyboxPriceInfo.coupon_info) { + let discountPrice = heyboxPriceInfo.cost_coin / 1000; + discountPrice = discountPrice - heyboxPriceInfo.coupon_info.max_reduce; + return `| 券后价: ${discountPrice.toFixed(2)} [${heyboxPriceInfo.coupon_info.coupon_desc}]`; + } else { + return ''; + } +} + async function handler(ctx) { - const platform = ctx.req.param('platform'); + const platformInfo = PLATFORM_MAP[ctx.req.param('platform')]; - const response = await got({ - method: 'get', - url: `https://api.xiaoheihe.cn/game/get_game_list_v3?sort_type=discount&filter_head=${platform}&offset=0&limit=30&os_type=web`, - }); - const data = response.data.result.games; + const dataUrl = calculate( + `https://api.xiaoheihe.cn/game/get_game_list_v3/?filter_head=${platformInfo.key}&offset=0&limit=30&os_type=web&app=heybox&client_type=mobile&version=999.0.3&x_client_type=web&x_os_type=Mac&x_app=heybox&heybox_id=-1&include_filter=-1` + ); + const response = await ofetch(dataUrl); + const data = response.result.games; const items = data.map((item) => { const title = `${item.name}${item.name_en ? '/' + item.name_en : ''}`; @@ -47,44 +86,48 @@ async function handler(ctx) { for (const platform of item.platform_infos) { if (platform.price) { if (platform.key) { - description += `平台: ${platform.key}
>`; + description += `平台: ${platform.key.toUpperCase()}
`; } if (platform.price.current) { - description += `当前价格: ${platform.price.current}${platform.price.discount === platform.price.lowest_discount ? '[史低]' : ''}
`; + description += `当前价格: ${platform.price.current} ${getLowestDesc(platform.price)}
`; } if (platform.price.initial) { description += `原价: ${platform.price.initial}
`; } - if (platform.price.discount_desc) { - description += `折扣力度: ${platform.price.discount_desc}
`; + if (platform.price.discount && platform.price.discount > 0) { + description += `折扣力度: ${getDiscountDesc(platform.price.discount)}
`; } if (platform.price.deadline_date) { description += `截止时间: ${platform.price.deadline_date}
`; } - description += '
'; } } } else { if (item.price) { - description += `平台: ${platform.toUpperCase()}
`; - if (item.price.discount) { - description += `折扣力度: ${(100 - item.price.discount) / 10}折
`; - } - if (item.price.initial && item.price.discount) { - const current = Math.round((item.price.initial * (100 - item.price.discount)) / 100); - description += `当前价格: ${current}${item.price.discount === item.price.lowest_discount ? '[史低]' : ''}  `; + description += `平台: ${platformInfo.desc}
`; + if (item.heybox_price) { + description += `当前价格: ${item.price.current} ${getHeyboxPriceDesc(item.heybox_price)} ${getLowestDesc(item.price, item.heybox_price.super_lowest)}
`; + } else if (item.price.current) { + description += `当前价格: ${item.price.current} ${getLowestDesc(item.price)}
`; } if (item.price.initial) { description += `原价: ${item.price.initial}
`; } - if (item.score) { - description += `评分: ${item.score}
`; + if (item.price.discount && item.price.discount > 0) { + description += `折扣力度: ${getDiscountDesc(item.price.discount)}
`; + } + if (item.price.deadline_date) { + description += `截止时间: ${item.price.deadline_date}
`; } - description += '
'; } } + if (item.score) { + description += `评分: ${item.score}
`; + } + description += '
'; + let link = `https://api.xiaoheihe.cn/game/share_game_detail?appid=${item.steam_appid}`; - if (platform === 'pc') { + if (platformInfo.key === 'pc') { link = `https://store.steampowered.com/app/${item.steam_appid}`; } return { @@ -95,7 +138,7 @@ async function handler(ctx) { }); return { - title: `小黑盒 ${PLATFORM_MAP[platform]} 游戏折扣`, + title: `小黑盒 ${platformInfo.desc} 游戏折扣`, link: `https://xiaoheihe.cn`, item: items, }; From c93f28df22090efb5db1a566cdaf0d39c8baac9b Mon Sep 17 00:00:00 2001 From: ueiu Date: Sun, 10 Nov 2024 11:42:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(route/xiaoheihe):=20=E5=B0=8F?= =?UTF-8?q?=E9=BB=91=E7=9B=92=E6=B8=B8=E6=88=8F=E6=8A=98=E6=89=A3=E5=88=B8?= =?UTF-8?q?=E5=90=8E=E4=BB=B7=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/xiaoheihe/discount.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/routes/xiaoheihe/discount.ts b/lib/routes/xiaoheihe/discount.ts index 6a92277d5093a1..82a269ee463e09 100644 --- a/lib/routes/xiaoheihe/discount.ts +++ b/lib/routes/xiaoheihe/discount.ts @@ -62,7 +62,8 @@ function getHeyboxPriceDesc(heyboxPriceInfo) { if (heyboxPriceInfo.coupon_info) { let discountPrice = heyboxPriceInfo.cost_coin / 1000; discountPrice = discountPrice - heyboxPriceInfo.coupon_info.max_reduce; - return `| 券后价: ${discountPrice.toFixed(2)} [${heyboxPriceInfo.coupon_info.coupon_desc}]`; + const formatPrice = Number.isInteger(discountPrice) ? discountPrice.toFixed(0) : discountPrice.toFixed(2); + return `| 券后价: ${formatPrice} [${heyboxPriceInfo.coupon_info.coupon_desc}]`; } else { return ''; }