UNPKG

rsshub

Version:
138 lines (137 loc) 6.22 kB
import { t as rofetch } from "./ofetch-C3ts-Hud.mjs"; import { t as cache_default } from "./cache-BkqOokyU.mjs"; import dayjs from "dayjs"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { renderToString } from "hono/jsx/dom/server"; import localizedFormat from "dayjs/plugin/localizedFormat.js"; import "dayjs/locale/zh-cn.js"; import timezone from "dayjs/plugin/timezone.js"; import utc from "dayjs/plugin/utc.js"; //#region lib/routes/mi/utils.tsx dayjs.extend(localizedFormat); dayjs.extend(timezone); dayjs.extend(utc); /** * Fetch the list of crowdfunding projects. * * @returns {Promise<CrowdfundingList[]>} The crowdfunding project list. */ const getCrowdfundingList = async () => { return (await rofetch("https://m.mi.com/v1/crowd/crowd_home", { method: "POST" })).data.list; }; /** * Fetch and cache crowdfunding project details. * * @param {CrowdfundingItem} item - Crowdfunding item. * @returns {Promise<CrowdfundingDetailInfo>} Crowdfunding item details. */ const getCrowdfundingItem = (item) => cache_default.tryGet(`mi:crowdfunding:${item.project_id}`, async () => { const response = await rofetch("https://m.mi.com/v1/crowd/crowd_detail", { headers: { referer: "https://m.mi.com/crowdfunding/home" }, method: "POST", query: { project_id: item.project_id } }); if (response.data.crowd_funding_info.product_market_price === void 0) response.data.crowd_funding_info.product_market_price = item.product_market_price; if (response.data.crowd_funding_info.start_time_desc === void 0) response.data.crowd_funding_info.start_time_desc = formatDate(response.data.crowd_funding_info.start_time); if (response.data.crowd_funding_info.end_time_desc === void 0) response.data.crowd_funding_info.end_time_desc = formatDate(response.data.crowd_funding_info.end_time); return response.data.crowd_funding_info; }); /** * Fetch the list of new products, merging `history_date_list` (primary) with `new_list` (supplement). * * @returns {Promise<NewProductItem[]>} The merged new product list, sorted by start time in descending order. */ const getNewProductList = async () => { const response = await rofetch("https://api.m.mi.com/v1/home/product_channel_get_list", { method: "POST" }); const map = /* @__PURE__ */ new Map(); for (const group of response.data.history_date_list) for (const item of group.product_list) map.set(item.product_id, item); for (const item of response.data.new_list) if (!map.has(item.product_id)) map.set(item.product_id, item); return map.values().toArray().toSorted((a, b) => b.start_time - a.start_time); }; /** * Fetch and cache new product details. * * @param {NewProductItem} item - New product list item. * @returns {Promise<NewProductDetailData>} New product details. */ const getNewProductItem = (item) => cache_default.tryGet(`mi:product:${item.product_id}`, async () => { return (await rofetch("https://m.mi.com/mtop/xiaomishop/product/info", { body: [{}, { productId: item.product_id }], method: "POST" })).data; }); const CrowdfundingDescription = ({ item }) => /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("img", { src: item.big_image }), /* @__PURE__ */ jsx("br", {}), item.project_name, /* @__PURE__ */ jsx("br", {}), item.project_desc, /* @__PURE__ */ jsx("br", {}), "众筹价:", item.price, " 元,建议零售价:", item.product_market_price, " 元", /* @__PURE__ */ jsx("br", {}), "众筹开始:", item.start_time_desc, ",众筹结束:", item.end_time_desc, /* @__PURE__ */ jsx("br", {}), "物流:", item.send_info, /* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("table", { children: /* @__PURE__ */ jsxs("tbody", { children: [/* @__PURE__ */ jsxs("tr", { children: [ /* @__PURE__ */ jsx("th", { children: "档位" }), /* @__PURE__ */ jsx("th", { children: "价格" }), /* @__PURE__ */ jsx("th", { children: "描述" }) ] }), item.support_list.map((support, index) => /* @__PURE__ */ jsxs("tr", { children: [ /* @__PURE__ */ jsx("td", { children: support.name }), /* @__PURE__ */ jsxs("td", { children: [support.price, " 元"] }), /* @__PURE__ */ jsx("td", { children: support.support_desc }) ] }, `${support.name}-${index}`))] }) }) ] }); /** * Render the crowdfunding item description. * * @param {CrowdfundingDetailInfo} item - Crowdfunding item details. * @returns {string} Rendered description HTML. */ const renderCrowdfunding = (item) => renderToString(/* @__PURE__ */ jsx(CrowdfundingDescription, { item })); const NewProductDescription = ({ listItem, detail }) => /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("img", { src: listItem.img }), /* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("ol", { children: detail.product.sellPointList.map((point) => /* @__PURE__ */ jsx("li", { children: point })) }), /* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsxs("table", { children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [ /* @__PURE__ */ jsx("th", { children: "规格" }), /* @__PURE__ */ jsx("th", { children: "原价" }), /* @__PURE__ */ jsx("th", { children: "现价" }) ] }) }), /* @__PURE__ */ jsx("tbody", { children: [...detail.goodsInfo.goodsList ?? [], ...detail.relationBatchedInfo?.relationBatchedList.flatMap((relation) => relation.goodsInfo) ?? []].map((goods) => /* @__PURE__ */ jsxs("tr", { children: [ /* @__PURE__ */ jsx("td", { children: goods.name }), /* @__PURE__ */ jsxs("td", { children: [goods.marketPrice, " 元"] }), /* @__PURE__ */ jsxs("td", { children: [goods.price, " 元"] }) ] })) })] }) ] }); /** * Render the new product item description. * * @param {NewProductItem} listItem - New product list item. * @param {NewProductDetailData} detail - New product details. * @returns {string} Rendered description HTML. */ const renderNewProduct = (listItem, detail) => renderToString(/* @__PURE__ */ jsx(NewProductDescription, { listItem, detail })); const formatDate = (timestamp) => dayjs.unix(timestamp).tz("Asia/Shanghai").locale("zh-cn").format("lll"); var utils_default = { getCrowdfundingList, getCrowdfundingItem, renderCrowdfunding, getNewProductList, getNewProductItem, renderNewProduct }; //#endregion export { utils_default as t };