rsshub
Version:
Make RSS Great Again!
161 lines (160 loc) • 6.5 kB
JavaScript
import { t as rofetch } from "./ofetch-eUBdfMpp.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 utc from "dayjs/plugin/utc.js";
import timezonePlugin from "dayjs/plugin/timezone.js";
//#region lib/routes/mi/utils.tsx
dayjs.extend(localizedFormat);
dayjs.extend(timezonePlugin);
dayjs.extend(utc);
/**
* Fetch the list of crowdfunding projects, merging the current projects (primary) with the history projects (supplement).
*
* @returns {Promise<CrowdfundingItem[]>} The merged crowdfunding project list.
*/
const getCrowdfundingList = async () => {
const fetch = (query) => rofetch("https://m.mi.com/v1/crowd/crowd_home", {
method: "POST",
query
});
const [response, historyResponse] = await Promise.all([fetch(), fetch({ status: 1 })]);
const map = /* @__PURE__ */ new Map();
const setIfNeeded = (items) => {
for (const item of items) if (!map.has(item.project_id)) map.set(item.project_id, item);
};
for (const group of response.data.list) setIfNeeded(group.items);
for (const group of historyResponse.data.list) setIfNeeded(group.items);
return map.values().toArray();
};
/**
* Fetch crowdfunding project details.
*
* @param {CrowdfundingItem} item - Crowdfunding item.
* @returns {Promise<CrowdfundingDetailInfo>} Crowdfunding item details.
*/
const getCrowdfundingItem = async (item) => {
return (await rofetch("https://m.mi.com/v1/crowd/crowd_detail", {
method: "POST",
query: { project_id: item.project_id }
})).data.crowd_funding_info;
};
/**
* Fetch the list of new products, merging `date_list` (primary) with `history_date_list` (supplement) and `new_list` (supplement).
*
* @returns {Promise<NewProductItem[]>} The merged new product list.
*/
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();
const setIfNeeded = (items) => {
for (const item of items) if (!map.has(item.product_id)) map.set(item.product_id, item);
};
for (const group of response.data.date_list) setIfNeeded(group.product_list);
for (const group of response.data.history_date_list) setIfNeeded(group.product_list);
setIfNeeded(response.data.new_list);
return map.values().toArray();
};
/**
* Fetch new product details.
*
* @param {NewProductItem} item - New product list item.
* @returns {Promise<NewProductDetailData>} New product details.
*/
const getNewProductItem = async (item) => {
return (await rofetch("https://m.mi.com/mtop/xiaomishop/product/info", {
body: [{}, { productId: item.product_id }],
method: "POST"
})).data;
};
const CrowdfundingDescription = ({ listItem, detail }) => /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx("img", { src: detail.big_image }),
/* @__PURE__ */ jsx("br", {}),
detail.project_desc,
/* @__PURE__ */ jsx("br", {}),
"众筹价:",
detail.price,
" 元,建议零售价:",
listItem.product_market_price,
" 元",
/* @__PURE__ */ jsx("br", {}),
"众筹开始:",
formatDate(detail.start_time),
",众筹结束:",
formatDate(detail.end_time),
/* @__PURE__ */ jsx("br", {}),
"物流:",
detail.send_info,
/* @__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("th", { children: "描述" })
] }) }), /* @__PURE__ */ jsx("tbody", { children: detail.support_list.map((support) => /* @__PURE__ */ jsxs("tr", { children: [
/* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx("img", {
src: support.goods_list[0]?.goods_image,
width: 48,
height: "auto"
}) }),
/* @__PURE__ */ jsx("td", { children: support.name }),
/* @__PURE__ */ jsxs("td", { children: [support.price, " 元"] }),
/* @__PURE__ */ jsx("td", { children: support.support_desc })
] })) })] })
] });
/**
* Render the crowdfunding item description.
*
* @param {CrowdfundingItem} listItem - Crowdfunding item list item.
* @param {CrowdfundingDetailInfo} detail - Crowdfunding item details.
* @returns {string} Rendered description HTML.
*/
const renderCrowdfunding = (listItem, detail) => renderToString(/* @__PURE__ */ jsx(CrowdfundingDescription, {
listItem,
detail
}));
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("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: /* @__PURE__ */ jsx("img", {
src: goods.imgUrl,
width: 48,
height: "auto"
}) }),
/* @__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 };