2 lines
9.4 KiB
JavaScript
Vendored
2 lines
9.4 KiB
JavaScript
Vendored
import{r as n,bO as H,bP as O,j as e,bQ as S,L as y,e as A,E as B}from"./index-CVcbgRej.js";import{P as F,a as U,N as q,b as z,E as Q}from"./ui-CDlGJJ1B.js";import{T as R}from"./trending-up-dkeCkSTk.js";import{P as V}from"./package-CcBNOlll.js";const W=2500,L=r=>r?r>=1e4?`${(r/1e4).toFixed(1).replace(/\.0$/,"")}万`:String(r):"0",Y=r=>{if(!r)return"—";const i=Date.now()-r;if(i<6e4)return"刚刚";if(i<36e5)return`${Math.floor(i/6e4)}分钟前`;if(i<864e5)return`${Math.floor(i/36e5)}小时前`;if(i<7*864e5)return`${Math.floor(i/864e5)}天前`;const o=new Date(r);return`${o.getMonth()+1}月${o.getDate()}日`},G=r=>{if(!r)return"—";const i=new Date(r),o=v=>String(v).padStart(2,"0");return`${i.getFullYear()}-${o(i.getMonth()+1)}-${o(i.getDate())}`},J=({rank:r})=>{const i=r===1?"bg-amber-400 text-white":r===2?"bg-gray-300 text-gray-700":r===3?"bg-orange-300 text-orange-900":"bg-[var(--surface-subtle)] text-[var(--text-soft)]";return e.jsx("span",{className:`inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-sm font-bold ${i}`,children:r})},se=()=>{const[r,i]=n.useState(!0),[o,v]=n.useState({}),[$,P]=n.useState([]),[d,M]=n.useState("digital"),[j,f]=n.useState(null),[h,_]=n.useState(0),[u,k]=n.useState(!1),g=n.useRef(null),I=n.useRef(d);I.current=d;const w=n.useCallback(()=>{g.current&&(clearInterval(g.current),g.current=null)},[]),b=n.useCallback(async s=>{try{const a=await H();v(a.boards||{}),P(a.categories||[]),s!=null&&s.silent||i(!1),Object.values(a.boards||{}).some(x=>x.refreshing)?E():w()}catch(a){s!=null&&s.silent||(i(!1),f({type:"error",message:a.message||"榜单加载失败"}))}},[]),E=n.useCallback(()=>{g.current||(g.current=setInterval(()=>{b({silent:!0})},W))},[b]);n.useEffect(()=>(b(),w),[b,w]),n.useEffect(()=>{if(h<=0)return;const s=setInterval(()=>_(a=>a<=1?0:a-1),1e3);return()=>clearInterval(s)},[h]);const t=o[d];n.useEffect(()=>{const s=o[d];!s||r||s.refreshing||s.items.length>0||s.cooldown_remaining>0||N(d)},[d,r,o]);const N=async s=>{if(!u){k(!0);try{const a=await O(s);a.started?(f({type:"info",message:a.categories&&a.categories.length>1?"已开始刷新全部类目,完成后自动更新。":"已开始抓取,十几秒后榜单自动刷新。"}),E()):a.reason==="cooldown"?f({type:"error",message:a.message||"刷新过于频繁,请稍后再试"}):f({type:"info",message:a.message||"已有刷新任务进行中"})}catch(a){f({type:"error",message:a.message||"刷新触发失败"})}finally{k(!1)}}};n.useEffect(()=>{t&&t.cooldown_remaining>0&&_(t.cooldown_remaining)},[t]);const T=n.useMemo(()=>$.map(s=>{var a;return{id:s.id,label:s.name,count:(a=o[s.id])==null?void 0:a.items.length}}),[$,o]),C=n.useMemo(()=>{if(!t)return[];const s={};return(t.items||[]).forEach(l=>{(l.keywords||[]).forEach(x=>{const p=s[x]||(s[x]={hits:0,want:0});p.hits+=1,p.want+=l.want_count||0})}),(t.keywords&&t.keywords.length?t.keywords:Object.keys(s)).map(l=>{var x,p;return{kw:l,hits:((x=s[l])==null?void 0:x.hits)||0,want:((p=s[l])==null?void 0:p.want)||0}}).sort((l,x)=>x.want-l.want)},[t]),m=!!(t!=null&&t.refreshing),c=t==null?void 0:t.progress,D=t!=null&&t.updated_at?Y(t.updated_at*1e3):"尚未生成";return r?e.jsx(F,{label:"正在加载热卖榜"}):e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsx(U,{title:"实时热卖榜",description:"按「想要人数」排序的全网热卖商品,数据来自闲鱼实时搜索聚合。",icon:S,badge:e.jsx("span",{className:"rounded-full bg-[var(--surface-subtle)] px-2 py-0.5 text-xs text-[var(--text-soft)]",children:t?`更新于 ${D}`:"待生成"}),actions:e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsxs("button",{type:"button",className:"ios-btn-secondary flex items-center gap-1.5 rounded-md px-3 py-2 text-sm",onClick:()=>void N("all"),disabled:u||m||h>0,children:[u||m?e.jsx(y,{className:"h-4 w-4 animate-spin"}):e.jsx(A,{className:"h-4 w-4"}),"全部刷新"]}),e.jsxs("button",{type:"button",className:"ios-btn-primary flex items-center gap-1.5 rounded-md px-3 py-2 text-sm",onClick:()=>void N(d),disabled:u||m||h>0,children:[m?e.jsx(y,{className:"h-4 w-4 animate-spin"}):e.jsx(R,{className:"h-4 w-4"}),h>0?`${h}s 后可刷新`:"刷新本类目"]})]})}),j&&e.jsx(q,{type:j.type==="info"?"success":j.type,message:j.message,onClose:()=>f(null)}),e.jsx("div",{className:"toolbar",children:e.jsx(z,{value:d,onChange:M,items:T,ariaLabel:"热卖榜类目"})}),m&&e.jsx("div",{className:"section-panel",children:e.jsxs("div",{className:"flex items-center gap-3 text-sm text-[var(--text-soft)]",children:[e.jsx(y,{className:"h-4 w-4 animate-spin text-[#ff4400]"}),e.jsxs("span",{children:["正在抓取「",(c==null?void 0:c.keyword)||"…","」",c&&c.total>0?`(${Math.min(c.done+1,c.total)}/${c.total})`:""]}),c&&c.total>0&&e.jsx("span",{className:"h-1.5 flex-1 overflow-hidden rounded-full bg-[var(--surface-subtle)]",children:e.jsx("span",{className:"block h-full rounded-full bg-[#ff4400] transition-all",style:{width:`${Math.round(c.done/c.total*100)}%`}})})]})}),!t||t.items.length===0?e.jsx(Q,{icon:S,title:m?"正在生成榜单":"榜单还没生成",description:m?"首次抓取需要十几个关键词、约 20 秒,完成后自动展示。":"点右上角「刷新本类目」开始抓取,按想要人数排出前 30 名。",action:m?void 0:e.jsx("button",{type:"button",className:"ios-btn-primary rounded-md px-4 py-2 text-sm",onClick:()=>void N(d),disabled:u||h>0,children:"生成榜单"})}):e.jsxs("section",{className:"section-panel",children:[e.jsx("div",{className:"section-panel__header",children:e.jsxs("div",{className:"flex min-w-0 items-start gap-3",children:[e.jsx("span",{className:"section-header__icon","aria-hidden":"true",children:e.jsx(R,{className:"h-4 w-4"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsxs("h2",{className:"section-title",children:[t.name,"热卖榜 · Top ",t.items.length]}),e.jsxs("p",{className:"section-description",children:["共 ",t.source_count," 个在售商品参与排序,按「想要人数」取前 ",t.items.length," 名"]}),C.length>0&&e.jsxs("div",{className:"mt-1.5 flex flex-wrap items-center gap-1.5",children:[e.jsx("span",{className:"text-xs text-[var(--text-soft)]",children:"热门搜索词:"}),C.map(({kw:s,hits:a,want:l})=>e.jsxs("a",{href:`https://www.goofish.com/search?q=${encodeURIComponent(s)}`,target:"_blank",rel:"noreferrer noopener",title:a>0?`点击搜索「${s}」· 命中 ${a} 个商品 · 合计 ${l} 人想要`:`点击搜索「${s}」(本轮未命中商品)`,className:"group inline-flex items-center gap-1 rounded-full bg-[var(--surface-subtle)] px-2 py-0.5 text-xs text-[var(--text)] transition-colors hover:bg-[#ff4400] hover:text-white",children:[e.jsx("span",{children:s}),a>0&&e.jsx("span",{className:"font-medium text-[#ff4400] group-hover:text-white",children:L(l)})]},s))]})]})]})}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"data-table responsive-data-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{className:"w-[6%]",children:"排名"}),e.jsx("th",{className:"w-[44%]",children:"商品"}),e.jsx("th",{className:"w-[11%]",children:"价格"}),e.jsx("th",{className:"w-[12%]",children:"想要人数"}),e.jsx("th",{className:"w-[12%]",children:"发布时间"}),e.jsx("th",{className:"w-[15%] text-right",children:"操作"})]})}),e.jsx("tbody",{children:t.items.map(s=>{var a;return e.jsxs("tr",{children:[e.jsx("td",{"data-label":"排名",children:e.jsx(J,{rank:s.rank})}),e.jsx("td",{"data-label":"商品",children:e.jsxs("div",{className:"flex min-w-0 items-center gap-3",children:[e.jsx("div",{className:"h-12 w-12 shrink-0 overflow-hidden rounded-lg bg-[var(--surface-subtle)]",children:s.image?e.jsx("img",{src:s.image,alt:"",loading:"lazy",className:"h-full w-full object-cover",onError:l=>{l.currentTarget.style.display="none"}}):e.jsx("div",{className:"flex h-full items-center justify-center text-gray-400",children:e.jsx(V,{className:"h-5 w-5"})})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("a",{href:s.url,target:"_blank",rel:"noreferrer noopener",className:"block text-sm text-[var(--text)] break-words hover:text-[#ff4400] hover:underline",title:s.title,children:s.title||"(无标题)"}),e.jsxs("p",{className:"mt-0.5 flex flex-wrap items-center gap-x-2 text-xs text-[var(--text-soft)]",children:[s.area&&e.jsx("span",{children:s.area}),(a=s.keywords)==null?void 0:a.slice(0,3).map(l=>e.jsx("span",{className:"rounded bg-[var(--surface-subtle)] px-1.5 py-0.5",children:l},l))]})]})]})}),e.jsx("td",{"data-label":"价格",children:e.jsx("span",{className:"font-bold text-[#ff4400]",children:s.price||"面议"})}),e.jsxs("td",{"data-label":"想要人数",children:[e.jsx("span",{className:"font-semibold text-[var(--text)]",children:L(s.want_count)}),e.jsx("span",{className:"ml-1 text-xs text-[var(--text-soft)]",children:"人想要"})]}),e.jsx("td",{"data-label":"发布时间",className:"whitespace-nowrap text-sm text-[var(--text-soft)]",children:G(s.publish_time)}),e.jsx("td",{"data-label":"操作",className:"text-right",children:e.jsxs("a",{href:s.url,target:"_blank",rel:"noreferrer noopener",className:"ios-btn-secondary inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm",children:[e.jsx(B,{className:"h-3.5 w-3.5"}),"查看商品"]})})]},s.item_id)})})]})}),e.jsx("p",{className:"px-4 py-3 text-xs text-[var(--text-soft)]",children:"口径说明:闲鱼不公开销量数据,本榜按商品「想要人数」降序排列,同分按发布时间取新; 数据为实时搜索快照,每 10 分钟可刷新一次。"})]})]})};export{se as default};
|