diff --git a/lib/routes/xiaoheihe/discount.ts b/lib/routes/xiaoheihe/discount.ts index ee6afbd46af022..82a269ee463e09 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,59 @@ 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; + const formatPrice = Number.isInteger(discountPrice) ? discountPrice.toFixed(0) : discountPrice.toFixed(2); + return `| 券后价: ${formatPrice} [${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 +87,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 +139,7 @@ async function handler(ctx) { }); return { - title: `小黑盒 ${PLATFORM_MAP[platform]} 游戏折扣`, + title: `小黑盒 ${platformInfo.desc} 游戏折扣`, link: `https://xiaoheihe.cn`, item: items, };