- 价格监控页超长文本改 break-words 自动换行(规则/关键词/最近一次列 + 历史弹窗), 消除单行撑宽导致的横向滚动条 - 邮件通知渠道 SMTP 四项改为可留空,发送阶段自动回退「系统设置 → 邮件服务」, 只需填接收邮箱;去掉强制 587 默认端口 - 「未配置 price_watch 事件的通知渠道」报错改为可操作指引(通知与日志页勾选价格监控) - email 渠道必填校验收窄为仅接收邮箱 验证:端到端 _run_rule notified=True 邮件真实发出;生产日志 00:25 通知=成功; CSS break-words 与新提示文案均进构建产物;测试脏数据已清理。
1010 lines
45 KiB
TypeScript
1010 lines
45 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
||
import {
|
||
AlertTriangle, Bell, CheckCircle2, Clock3, Edit3, History, Play, Plus,
|
||
RefreshCw, Search, Trash2, TrendingDown, X,
|
||
} from 'lucide-react';
|
||
import { EmptyState, NoticeBanner, PageHeader, PageLoading } from './ui';
|
||
import {
|
||
createPrRule, deletePrRule, getPrCookies, getPrRecords, getPrRules,
|
||
previewPrSearch, runPrRule, updatePrRule,
|
||
} from '../services/api';
|
||
import type {
|
||
PriceWatchListResponse, PriceWatchPreviewResponse, PriceWatchRecord,
|
||
PriceWatchRule, PriceWatchRuleInput,
|
||
} from '../types';
|
||
|
||
const POLL_MS = 2500; // 规则列表轮询:有任务在跑时靠它看进度
|
||
|
||
const money = (v: number | null | undefined): string =>
|
||
v === null || v === undefined
|
||
? '—'
|
||
: `¥${Number(v).toLocaleString('zh-CN', { maximumFractionDigits: 2 })}`;
|
||
|
||
/** 当前秒级时间戳:表格统一用它算相对时间,避免每行各拿各的 now。 */
|
||
const nowSec = (): number => Math.floor(Date.now() / 1000);
|
||
|
||
const ago = (sec: number | null | undefined): string => {
|
||
if (!sec || sec <= 0) return '—';
|
||
const s = Math.floor(sec);
|
||
if (s < 60) return `${s} 秒前`;
|
||
if (s < 3600) return `${Math.floor(s / 60)} 分钟前`;
|
||
if (s < 86400) return `${Math.floor(s / 3600)} 小时前`;
|
||
return `${Math.floor(s / 86400)} 天前`;
|
||
};
|
||
|
||
const countdown = (sec: number | null | undefined): string => {
|
||
if (sec === null || sec === undefined || sec < 0) return '已停用';
|
||
if (sec < 60) return `${Math.max(0, Math.round(sec))} 秒后`;
|
||
if (sec < 3600) return `${Math.floor(sec / 60)} 分钟后`;
|
||
return `${Math.floor(sec / 3600)} 小时后`;
|
||
};
|
||
|
||
/** 空串 / undefined 统一成 null(后端把 null 当「不限」)。 */
|
||
const numOrNull = (raw: string): number | null => {
|
||
const trimmed = raw.trim();
|
||
if (trimmed === '') return null;
|
||
const n = Number(trimmed);
|
||
return Number.isFinite(n) && n >= 0 ? n : null;
|
||
};
|
||
|
||
interface RuleDraft {
|
||
name: string;
|
||
keyword: string;
|
||
min_price: string;
|
||
max_price: string;
|
||
target_price: string;
|
||
interval_minutes: string;
|
||
pages: string;
|
||
enabled: boolean;
|
||
cookie_id: string;
|
||
/** 卖家信用度下限:''=不限 */
|
||
min_seller_credit: string;
|
||
/** 卖家成交量下限(N条评价),''=不限 */
|
||
min_seller_deals: string;
|
||
/** AI 精准监控 */
|
||
ai_precise: boolean;
|
||
}
|
||
|
||
const EMPTY_DRAFT: RuleDraft = {
|
||
name: '', keyword: '', min_price: '', max_price: '', target_price: '',
|
||
interval_minutes: '30', pages: '2', enabled: true, cookie_id: '',
|
||
min_seller_credit: '', min_seller_deals: '', ai_precise: false,
|
||
};
|
||
|
||
/** 规则回填进表单(编辑态)。 */
|
||
const draftOf = (rule: PriceWatchRule): RuleDraft => ({
|
||
name: rule.name,
|
||
keyword: rule.keyword,
|
||
min_price: rule.min_price === null ? '' : String(rule.min_price),
|
||
max_price: rule.max_price === null ? '' : String(rule.max_price),
|
||
target_price: rule.target_price === null ? '' : String(rule.target_price),
|
||
interval_minutes: String(rule.interval_minutes),
|
||
pages: String(rule.pages || 2),
|
||
enabled: rule.enabled !== false,
|
||
cookie_id: rule.cookie_id || '',
|
||
min_seller_credit: rule.min_seller_credit || '',
|
||
min_seller_deals: rule.min_seller_deals != null ? String(rule.min_seller_deals) : '',
|
||
ai_precise: !!rule.ai_precise,
|
||
});
|
||
|
||
/** 表单 → 后端入参;区间大小与关键词缺失直接挡在前端。 */
|
||
const toInput = (
|
||
draft: RuleDraft,
|
||
): { input: PriceWatchRuleInput | null; error?: string } => {
|
||
const keyword = draft.keyword.trim();
|
||
if (!keyword) return { input: null, error: '请填写搜索关键词' };
|
||
const name = draft.name.trim() || keyword;
|
||
const min = numOrNull(draft.min_price);
|
||
const max = numOrNull(draft.max_price);
|
||
if (min !== null && max !== null && min > max) {
|
||
return { input: null, error: '最低价不能大于最高价' };
|
||
}
|
||
const target = numOrNull(draft.target_price);
|
||
if (target !== null && max !== null && target > max) {
|
||
return { input: null, error: '目标价不能高于最高价' };
|
||
}
|
||
const interval = Number(draft.interval_minutes);
|
||
if (!Number.isFinite(interval) || interval < 5) {
|
||
return { input: null, error: '检查间隔至少 5 分钟' };
|
||
}
|
||
const pages = Number(draft.pages);
|
||
if (!Number.isFinite(pages) || pages < 1 || pages > 5) {
|
||
return { input: null, error: '每轮页数取 1~5' };
|
||
}
|
||
return {
|
||
input: {
|
||
name, keyword, min_price: min, max_price: max, target_price: target,
|
||
interval_minutes: Math.round(interval), pages: Math.round(pages),
|
||
enabled: draft.enabled, cookie_id: draft.cookie_id || null,
|
||
min_seller_credit: draft.min_seller_credit || null,
|
||
min_seller_deals: numOrNull(draft.min_seller_deals),
|
||
ai_precise: draft.ai_precise,
|
||
},
|
||
};
|
||
};
|
||
|
||
type Banner = { type: 'success' | 'error' | 'warning' | 'info'; message: string };
|
||
|
||
export default function PriceWatch() {
|
||
const [rules, setRules] = useState<PriceWatchRule[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [serverError, setServerError] = useState('');
|
||
const [banner, setBanner] = useState<Banner | null>(null);
|
||
const [cookies, setCookies] = useState<{ id: string }[]>([]);
|
||
|
||
// 新建 / 编辑
|
||
const [modalOpen, setModalOpen] = useState(false);
|
||
const [editingId, setEditingId] = useState<number | null>(null);
|
||
const [draft, setDraft] = useState<RuleDraft>(EMPTY_DRAFT);
|
||
const [saving, setSaving] = useState(false);
|
||
const [formError, setFormError] = useState('');
|
||
|
||
// 实时试搜
|
||
const [previewing, setPreviewing] = useState(false);
|
||
const [preview, setPreview] = useState<PriceWatchPreviewResponse | null>(null);
|
||
const [previewError, setPreviewError] = useState('');
|
||
|
||
// 检查历史
|
||
const [historyFor, setHistoryFor] = useState<PriceWatchRule | null>(null);
|
||
const [records, setRecords] = useState<PriceWatchRecord[]>([]);
|
||
const [historyLoading, setHistoryLoading] = useState(false);
|
||
|
||
/** 上一轮在跑的规则 id:跑完后补一次刷新,把最终结果带回来。 */
|
||
const runningIds = useRef<Set<number>>(new Set());
|
||
|
||
const load = useCallback(async () => {
|
||
try {
|
||
const res: PriceWatchListResponse = await getPrRules();
|
||
const next = res.rules ?? [];
|
||
setRules(next);
|
||
setServerError('');
|
||
|
||
const stillRunning = new Set<number>();
|
||
for (const r of next) if (r.running) stillRunning.add(r.id);
|
||
const finished = [...runningIds.current].filter((id) => !stillRunning.has(id));
|
||
runningIds.current = stillRunning;
|
||
if (finished.length) void load();
|
||
} catch (e) {
|
||
setServerError(e instanceof Error ? e.message : '加载失败');
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
void getPrCookies()
|
||
.then((r) => setCookies(r.cookies ?? []))
|
||
.catch(() => setCookies([]));
|
||
const t = setInterval(() => void load(), POLL_MS);
|
||
return () => clearInterval(t);
|
||
}, [load]);
|
||
|
||
const openCreate = () => {
|
||
setEditingId(null);
|
||
setDraft(EMPTY_DRAFT);
|
||
setPreview(null);
|
||
setPreviewError('');
|
||
setFormError('');
|
||
setModalOpen(true);
|
||
};
|
||
|
||
const openEdit = (rule: PriceWatchRule) => {
|
||
setEditingId(rule.id);
|
||
setDraft(draftOf(rule));
|
||
setPreview(null);
|
||
setPreviewError('');
|
||
setFormError('');
|
||
setModalOpen(true);
|
||
};
|
||
|
||
/** 试搜:不落库、不通知,只为保存前看到实时最低价。 */
|
||
const doPreview = async () => {
|
||
const { input, error } = toInput(draft);
|
||
if (!input) { setPreviewError(error ?? '参数有误'); return; }
|
||
setPreviewing(true);
|
||
setPreviewError('');
|
||
setPreview(null);
|
||
try {
|
||
const res = await previewPrSearch({
|
||
keyword: input.keyword,
|
||
min_price: input.min_price,
|
||
max_price: input.max_price,
|
||
pages: input.pages,
|
||
cookie_id: input.cookie_id,
|
||
min_seller_credit: input.min_seller_credit,
|
||
min_seller_deals: input.min_seller_deals,
|
||
ai_precise: input.ai_precise,
|
||
});
|
||
setPreview(res);
|
||
if (res.error) setPreviewError(res.error);
|
||
else if (!res.lowest) {
|
||
setPreviewError('没搜到符合条件的商品,换个关键词或放宽价格区间试试');
|
||
}
|
||
} catch (e) {
|
||
setPreviewError(e instanceof Error ? e.message : '试搜失败');
|
||
} finally {
|
||
setPreviewing(false);
|
||
}
|
||
};
|
||
|
||
const submit = async () => {
|
||
const { input, error } = toInput(draft);
|
||
if (!input) { setFormError(error ?? '参数有误'); return; }
|
||
setSaving(true);
|
||
setFormError('');
|
||
try {
|
||
if (editingId === null) await createPrRule(input);
|
||
else await updatePrRule(editingId, input);
|
||
setModalOpen(false);
|
||
setBanner({
|
||
type: 'info',
|
||
message: editingId === null ? '规则已创建,可以立即检查一次' : '规则已保存',
|
||
});
|
||
await load();
|
||
} catch (e) {
|
||
setFormError(e instanceof Error ? e.message : '保存失败');
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
};
|
||
|
||
/** 立即检查:同步等结果,跑完把本次最低价与是否通知亮出来。 */
|
||
const runRule = async (rule: PriceWatchRule) => {
|
||
runningIds.current.add(rule.id);
|
||
setBanner(null);
|
||
try {
|
||
const res = await runPrRule(rule.id);
|
||
if (!res.started) {
|
||
setBanner({ type: 'info', message: res.message ?? '任务未启动' });
|
||
} else if (res.record) {
|
||
const price = res.record.lowest_price;
|
||
if (price === null || price === undefined) {
|
||
setBanner({
|
||
type: 'warning',
|
||
message: `「${rule.name}」没搜到可比价的商品(已记录一次空结果)`,
|
||
});
|
||
} else if (res.notified) {
|
||
setBanner({
|
||
type: 'success',
|
||
message: `已找到最低价 ${money(price)},通知已发送(${res.notify_reason || '命中'})`,
|
||
});
|
||
} else {
|
||
setBanner({
|
||
type: 'info',
|
||
message: `本次最低价 ${money(price)}(${res.notify_reason || '未触发通知'})`,
|
||
});
|
||
}
|
||
if (res.notify_error) {
|
||
setBanner({ type: 'error', message: `通知发送失败:${res.notify_error}` });
|
||
}
|
||
}
|
||
} catch (e) {
|
||
setBanner({ type: 'error', message: e instanceof Error ? e.message : '检查失败' });
|
||
} finally {
|
||
runningIds.current.delete(rule.id);
|
||
await load();
|
||
}
|
||
};
|
||
|
||
const removeRule = async (rule: PriceWatchRule) => {
|
||
if (!window.confirm(`删除监控规则「${rule.name}」?检查历史会一并删除。`)) return;
|
||
try {
|
||
await deletePrRule(rule.id);
|
||
setBanner({ type: 'info', message: '规则已删除' });
|
||
await load();
|
||
} catch (e) {
|
||
setBanner({ type: 'error', message: e instanceof Error ? e.message : '删除失败' });
|
||
}
|
||
};
|
||
|
||
const openHistory = async (rule: PriceWatchRule) => {
|
||
setHistoryFor(rule);
|
||
setHistoryLoading(true);
|
||
setRecords([]);
|
||
try {
|
||
const res = await getPrRecords(rule.id, 50);
|
||
setRecords(res.records ?? []);
|
||
} catch {
|
||
setRecords([]);
|
||
} finally {
|
||
setHistoryLoading(false);
|
||
}
|
||
};
|
||
|
||
if (loading && !rules.length) return <PageLoading />;
|
||
|
||
return (
|
||
<div className="mx-auto max-w-[1600px] space-y-6 px-4 py-5 sm:px-6">
|
||
<PageHeader
|
||
title="价格监控"
|
||
description="填关键词和价格条件,实时搜出最低价并记录;命中目标价自动发通知。"
|
||
icon={TrendingDown}
|
||
actions={
|
||
<div className="flex items-center gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => void load()}
|
||
className="ios-btn-secondary rounded-md px-4 py-2"
|
||
aria-label="刷新"
|
||
>
|
||
<RefreshCw className="h-4 w-4" />
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={openCreate}
|
||
className="ios-btn-primary rounded-md px-4 py-2"
|
||
>
|
||
<Plus className="mr-1.5 inline h-4 w-4" />
|
||
新建监控规则
|
||
</button>
|
||
</div>
|
||
}
|
||
/>
|
||
|
||
<NoticeBanner
|
||
type="info"
|
||
message="「最低价」按搜索结果本地排序得出(闲鱼搜索接口不返回销量)。为避免触发风控,所有规则共用串行队列、两次搜索固定间隔 1 秒,请把检查间隔设得宽松些,建议不低于 15 分钟。"
|
||
/>
|
||
|
||
{banner && (
|
||
<NoticeBanner
|
||
type={banner.type}
|
||
message={banner.message}
|
||
onClose={() => setBanner(null)}
|
||
/>
|
||
)}
|
||
{serverError && <NoticeBanner type="error" message={`加载失败:${serverError}`} />}
|
||
|
||
{!rules.length && !serverError ? (
|
||
<EmptyState
|
||
title="还没有监控规则"
|
||
description="点「新建监控规则」,填关键词与价格条件后即可实时搜到最低价。"
|
||
icon={Bell}
|
||
/>
|
||
) : (
|
||
<div className="overflow-hidden rounded-lg border border-[var(--border-light)] bg-[var(--surface)] shadow-sm">
|
||
<div className="overflow-x-auto">
|
||
<table className="data-table responsive-data-table min-w-[1100px]">
|
||
<thead>
|
||
<tr>
|
||
<th className="w-[20%]">规则</th>
|
||
<th className="w-[17%]">条件</th>
|
||
<th className="w-[22%]">最近一次</th>
|
||
<th className="w-[13%]">下次检查</th>
|
||
<th className="w-[11%]">进度</th>
|
||
<th className="w-[17%] text-right">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{rules.map((rule) => {
|
||
const isRunning = rule.running;
|
||
const rec = rule.last_record;
|
||
const hasResult =
|
||
rec !== null && rec !== undefined &&
|
||
rec.lowest_price !== null && rec.lowest_price !== undefined;
|
||
return (
|
||
<tr key={rule.id}>
|
||
<td data-label="规则">
|
||
<div className="min-w-0">
|
||
<div className="break-words font-medium text-[var(--text-primary)]" title={rule.name}>
|
||
{rule.name}
|
||
</div>
|
||
<div className="break-words text-xs text-[var(--text-muted)]" title={rule.keyword}>
|
||
关键词:{rule.keyword}
|
||
</div>
|
||
<div className="mt-1 flex flex-wrap items-center gap-2 text-xs">
|
||
<span
|
||
className={`inline-flex items-center rounded px-1.5 py-0.5 ${
|
||
rule.enabled
|
||
? 'bg-emerald-50 text-emerald-700'
|
||
: 'bg-slate-100 text-slate-500'
|
||
}`}
|
||
>
|
||
{rule.enabled ? '运行中' : '已停用'}
|
||
</span>
|
||
<span className="text-[var(--text-muted)]">
|
||
每 {rule.interval_minutes} 分钟
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</td>
|
||
|
||
<td data-label="条件">
|
||
<div className="text-sm text-[var(--text-secondary)]">
|
||
<div>
|
||
{rule.min_price === null ? '不限' : money(rule.min_price)}
|
||
{' ~ '}
|
||
{rule.max_price === null ? '不限' : money(rule.max_price)}
|
||
</div>
|
||
<div
|
||
className={
|
||
rule.target_price !== null
|
||
? 'text-[var(--brand-primary)]'
|
||
: 'text-[var(--text-muted)]'
|
||
}
|
||
>
|
||
{rule.target_price !== null
|
||
? `目标价 ${money(rule.target_price)}`
|
||
: '新低价即通知'}
|
||
</div>
|
||
{(rule.min_seller_credit || rule.min_seller_deals != null || rule.ai_precise) && (
|
||
<div className="mt-1 flex flex-wrap gap-1">
|
||
{rule.min_seller_credit && (
|
||
<span className="rounded bg-slate-100 px-1.5 py-0.5 text-[11px] text-slate-600">
|
||
信用≥{rule.min_seller_credit}
|
||
</span>
|
||
)}
|
||
{rule.min_seller_deals != null && (
|
||
<span className="rounded bg-slate-100 px-1.5 py-0.5 text-[11px] text-slate-600">
|
||
销量≥{rule.min_seller_deals}
|
||
</span>
|
||
)}
|
||
{rule.ai_precise && (
|
||
<span className="rounded bg-indigo-100 px-1.5 py-0.5 text-[11px] text-indigo-700">
|
||
AI 精准
|
||
</span>
|
||
)}
|
||
</div>
|
||
)}
|
||
<div className="text-xs text-[var(--text-muted)]">
|
||
每轮 {rule.pages} 页 · 历史 {rule.history_count} 条
|
||
</div>
|
||
</div>
|
||
</td>
|
||
|
||
<td data-label="最近一次">
|
||
{!rec ? (
|
||
<span className="text-sm text-[var(--text-muted)]">还没检查过</span>
|
||
) : rec.error ? (
|
||
<span className="text-sm text-amber-600" title={rec.error}>
|
||
上次出错:{rec.error.slice(0, 24)}
|
||
</span>
|
||
) : !hasResult ? (
|
||
<span className="text-sm text-[var(--text-muted)]">
|
||
没搜到可比价商品 · {ago(nowSec() - rec.checked_at)}
|
||
</span>
|
||
) : (
|
||
<div className="min-w-0">
|
||
<div className="flex items-center gap-1.5 font-semibold text-emerald-600">
|
||
<TrendingDown className="h-3.5 w-3.5 shrink-0" />
|
||
{money(rec.lowest_price)}
|
||
{rec.notified ? (
|
||
<Bell className="h-3 w-3 text-[var(--brand-primary)]" aria-label="已通知" />
|
||
) : null}
|
||
</div>
|
||
<div className="break-words text-xs text-[var(--text-muted)]" title={rec.lowest_title ?? ''}>
|
||
{rec.lowest_title}
|
||
</div>
|
||
<div className="text-xs text-[var(--text-muted)]">
|
||
{ago(nowSec() - rec.checked_at)} · 比价 {rec.item_count} 件
|
||
{rec.source === 'auto' ? ' · 自动' : ''}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</td>
|
||
|
||
<td data-label="下次检查">
|
||
<div className="text-sm text-[var(--text-secondary)]">
|
||
{isRunning ? '检查中…' : countdown(rule.next_run_in)}
|
||
<div className="text-xs text-[var(--text-muted)]">
|
||
上次{' '}
|
||
{rule.last_run_at ? ago(nowSec() - rule.last_run_at) : '—'}
|
||
</div>
|
||
</div>
|
||
</td>
|
||
|
||
<td data-label="进度">
|
||
{!isRunning ? (
|
||
<span className="text-xs text-[var(--text-muted)]">
|
||
{rule.manual_cooldown_remaining > 0
|
||
? `手动冷却 ${Math.ceil(rule.manual_cooldown_remaining)}s`
|
||
: '空闲'}
|
||
</span>
|
||
) : (
|
||
<div className="text-xs text-[var(--text-secondary)]">
|
||
<span className="inline-flex items-center gap-1">
|
||
<RefreshCw className="h-3 w-3 animate-spin" />
|
||
{rule.progress ? `${rule.progress.done}/${rule.progress.total}` : '启动中'}
|
||
</span>
|
||
{rule.progress?.keyword && (
|
||
<div className="truncate text-[var(--text-muted)]" title={rule.progress.keyword}>
|
||
{rule.progress.keyword}
|
||
</div>
|
||
)}
|
||
{rule.progress?.error && (
|
||
<div className="text-amber-600">{rule.progress.error.slice(0, 20)}</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</td>
|
||
|
||
<td data-label="操作">
|
||
<div className="flex flex-wrap justify-end gap-1.5">
|
||
<button
|
||
type="button"
|
||
onClick={() => void runRule(rule)}
|
||
disabled={isRunning || rule.manual_cooldown_remaining > 0}
|
||
className="ios-btn-secondary inline-flex items-center gap-1 rounded-md px-2.5 py-1.5 text-xs disabled:opacity-50"
|
||
title={
|
||
rule.manual_cooldown_remaining > 0
|
||
? '手动检查冷却中'
|
||
: '立即搜索一次'
|
||
}
|
||
>
|
||
<Play className="h-3.5 w-3.5" />
|
||
立即检查
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void openHistory(rule)}
|
||
className="ios-btn-secondary inline-flex items-center gap-1 rounded-md px-2.5 py-1.5 text-xs"
|
||
>
|
||
<History className="h-3.5 w-3.5" />
|
||
历史
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => openEdit(rule)}
|
||
className="ios-btn-secondary inline-flex items-center gap-1 rounded-md px-2.5 py-1.5 text-xs"
|
||
>
|
||
<Edit3 className="h-3.5 w-3.5" />
|
||
编辑
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void removeRule(rule)}
|
||
className="ios-btn-secondary inline-flex items-center gap-1 rounded-md px-2.5 py-1.5 text-xs text-rose-600 hover:bg-rose-50"
|
||
>
|
||
<Trash2 className="h-3.5 w-3.5" />
|
||
删除
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
);
|
||
})}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{rules.some((r) => r.running) && (
|
||
<div className="flex items-center gap-2 text-sm text-[var(--text-muted)]">
|
||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||
有规则正在检查,结果会自动刷新。
|
||
</div>
|
||
)}
|
||
|
||
{/* ---------------------------------------------------- 新建 / 编辑 */}
|
||
{modalOpen && (
|
||
<div className="modal-overlay" role="dialog" aria-modal="true" aria-label="监控规则">
|
||
<div className="modal-container max-h-[92vh] w-full max-w-[680px] overflow-y-auto">
|
||
<div className="flex items-center justify-between border-b border-[var(--border-light)] px-6 py-4">
|
||
<h3 className="text-lg font-semibold text-[var(--text-primary)]">
|
||
{editingId === null ? '新建监控规则' : '编辑监控规则'}
|
||
</h3>
|
||
<button
|
||
type="button"
|
||
onClick={() => setModalOpen(false)}
|
||
className="rounded-full p-1.5 text-[var(--text-muted)] hover:bg-slate-100"
|
||
aria-label="关闭"
|
||
>
|
||
<X className="h-5 w-5" />
|
||
</button>
|
||
</div>
|
||
|
||
<div className="space-y-4 px-6 py-5">
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">规则名称</span>
|
||
<input
|
||
type="text"
|
||
value={draft.name}
|
||
onChange={(e) => setDraft({ ...draft, name: e.target.value })}
|
||
placeholder="留空则用关键词"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">搜索关键词 *</span>
|
||
<input
|
||
type="text"
|
||
value={draft.keyword}
|
||
onChange={(e) => setDraft({ ...draft, keyword: e.target.value })}
|
||
placeholder="如 iPhone 15"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">最低价(元)</span>
|
||
<input
|
||
type="number" min="0" step="1" inputMode="decimal"
|
||
value={draft.min_price}
|
||
onChange={(e) => setDraft({ ...draft, min_price: e.target.value })}
|
||
placeholder="不限"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">最高价(元)</span>
|
||
<input
|
||
type="number" min="0" step="1" inputMode="decimal"
|
||
value={draft.max_price}
|
||
onChange={(e) => setDraft({ ...draft, max_price: e.target.value })}
|
||
placeholder="不限"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">目标价(元)</span>
|
||
<input
|
||
type="number" min="0" step="1" inputMode="decimal"
|
||
value={draft.target_price}
|
||
onChange={(e) => setDraft({ ...draft, target_price: e.target.value })}
|
||
placeholder="留空=新低价即通知"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
</div>
|
||
<p className="-mt-2 text-xs text-[var(--text-muted)]">
|
||
目标价留空时,只要比上次低就通知;填了则要低于等于目标价才通知。
|
||
</p>
|
||
|
||
{/* ---------------------------------------- 卖家信用度 / 卖家销量 */}
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">卖家信用度</span>
|
||
<select
|
||
value={draft.min_seller_credit}
|
||
onChange={(e) => setDraft({ ...draft, min_seller_credit: e.target.value })}
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
>
|
||
<option value="">不限(含未标注信用的卖家)</option>
|
||
<option value="极好">「极好」及以上</option>
|
||
<option value="优秀">「优秀」及以上</option>
|
||
<option value="良好">「良好」及以上</option>
|
||
</select>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">卖家销量下限(条评价)</span>
|
||
<input
|
||
type="number" min="0" step="1"
|
||
value={draft.min_seller_deals}
|
||
onChange={(e) => setDraft({ ...draft, min_seller_deals: e.target.value })}
|
||
placeholder="不限"
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
</div>
|
||
<p className="-mt-1 text-xs text-[var(--text-muted)]">
|
||
<b>销量</b>:闲鱼是单件二手成交、没有商品级销量,这里按卖家的「N 条评价」
|
||
累计量筛(如「126 条评价」≈ 该卖家已成交 126 单,越大越靠谱)——
|
||
实测每条商品<b>都能取到</b>,放心用。
|
||
<br />
|
||
<b>信用度</b>:取自搜索结果的「卖家信用极好 / 优秀」标签,
|
||
<b>但闲鱼只给部分卖家显示该标签</b>(实测同类商品可能 0~3 条有标注),
|
||
<b>选了具体等级后,未标注的卖家会被排除</b>,结果可能明显变少甚至为空——
|
||
这是数据本身的限制,不是故障;建议先点下方「实时试搜」确认数量再保存,
|
||
拿不准就选「不限」。
|
||
</p>
|
||
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">检查间隔(分钟)</span>
|
||
<input
|
||
type="number" min="5" step="1"
|
||
value={draft.interval_minutes}
|
||
onChange={(e) => setDraft({ ...draft, interval_minutes: e.target.value })}
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">每轮页数</span>
|
||
<input
|
||
type="number" min="1" max="5" step="1"
|
||
value={draft.pages}
|
||
onChange={(e) => setDraft({ ...draft, pages: e.target.value })}
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
/>
|
||
</label>
|
||
<label className="block text-sm">
|
||
<span className="mb-1.5 block font-medium text-[var(--text-secondary)]">搜索账号</span>
|
||
<select
|
||
value={draft.cookie_id}
|
||
onChange={(e) => setDraft({ ...draft, cookie_id: e.target.value })}
|
||
className="ios-input w-full rounded-md px-3 py-2.5"
|
||
>
|
||
<option value="">自动选择</option>
|
||
{cookies.map((c) => (
|
||
<option key={c.id} value={c.id}>账号 {c.id}</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
</div>
|
||
|
||
<label className="flex cursor-pointer items-center gap-2 text-sm text-[var(--text-secondary)]">
|
||
<input
|
||
type="checkbox"
|
||
checked={draft.enabled}
|
||
onChange={(e) => setDraft({ ...draft, enabled: e.target.checked })}
|
||
className="h-4 w-4 rounded border-slate-300"
|
||
/>
|
||
启用自动检查(按间隔后台运行)
|
||
</label>
|
||
|
||
|
||
{/* -------------------------------------------- AI 精准监控 */}
|
||
<label className="flex cursor-pointer items-center gap-2 text-sm text-[var(--text-secondary)]">
|
||
<input
|
||
type="checkbox"
|
||
checked={draft.ai_precise}
|
||
onChange={(e) => setDraft({ ...draft, ai_precise: e.target.checked })}
|
||
className="h-4 w-4 rounded border-slate-300"
|
||
/>
|
||
<span>
|
||
启用 <b className="text-indigo-600">AI 精准监控</b>
|
||
<span className="ml-1 text-xs text-[var(--text-muted)]">
|
||
(大模型逐条判断商品是否真的匹配关键词)
|
||
</span>
|
||
</span>
|
||
</label>
|
||
|
||
{draft.ai_precise && (
|
||
<div className="rounded-lg border border-indigo-200 bg-indigo-50/70 p-3.5 text-xs leading-relaxed text-indigo-950">
|
||
<div className="mb-2 font-semibold text-indigo-700">启用 AI 有什么好处?</div>
|
||
<ul className="list-disc space-y-1.5 pl-4">
|
||
<li>
|
||
<b>过滤标题党与无关商品</b>:关键词命中标题但实际不卖这个的直接剔除,
|
||
通知里不再混进一堆看似相关、点进去却对不上的结果。
|
||
</li>
|
||
<li>
|
||
<b>识别真实规格与成色</b>:分清 128G / 256G、有锁 / 国行、全新 / 骗修,
|
||
避免「假低价」——便宜那台其实是另一个规格,比价才准。
|
||
</li>
|
||
<li>
|
||
<b>排除风险商品</b>:描述前后矛盾、疑似引流或诈骗话术的商品不进入比价。
|
||
</li>
|
||
<li>
|
||
<b>通知更精准、少打扰</b>:只有通过判断的商品参与「新低价」比较,
|
||
你收到的每条降价提醒都是真值得点的。
|
||
</li>
|
||
</ul>
|
||
<div className="mt-2.5 border-t border-indigo-200 pt-2 text-[11px] text-indigo-800">
|
||
<b>成本与兜底</b>:每轮对前 20 条商品<b>批量调用一次</b>大模型
|
||
(走「AI 优化建议」里已配置的模型 KEY,约增加几秒延迟);
|
||
<b>未配置 KEY 或调用失败会自动跳过并保留全部结果</b>——AI 挂了监控照常跑,
|
||
宁可多报也不漏报。
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* ------------------------------------------------ 实时试搜 */}
|
||
<div className="rounded-lg border border-[var(--border-light)] bg-slate-50 p-4">
|
||
<div className="mb-3 flex items-center justify-between gap-3">
|
||
<div className="text-sm font-medium text-[var(--text-secondary)]">
|
||
<Search className="mr-1.5 inline h-4 w-4" />
|
||
实时试搜(不保存、不通知)
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={() => void doPreview()}
|
||
disabled={previewing}
|
||
className="ios-btn-secondary inline-flex items-center gap-1.5 rounded-md px-3 py-2 text-sm disabled:opacity-50"
|
||
>
|
||
<Search className={`h-4 w-4 ${previewing ? 'animate-pulse' : ''}`} />
|
||
{previewing ? '搜索中…' : '搜索最低价'}
|
||
</button>
|
||
</div>
|
||
|
||
{previewError && (
|
||
<div className="mb-3 flex items-start gap-2 rounded-md bg-amber-50 px-3 py-2 text-sm text-amber-700">
|
||
<AlertTriangle className="mt-0.5 h-4 w-4 shrink-0" />
|
||
<span>{previewError}</span>
|
||
</div>
|
||
)}
|
||
|
||
{preview?.lowest && (
|
||
<div className="mb-3 rounded-md border border-emerald-200 bg-emerald-50 p-3">
|
||
<div className="flex items-center justify-between gap-3">
|
||
<div className="flex items-center gap-2 text-sm font-medium text-emerald-700">
|
||
<CheckCircle2 className="h-4 w-4" />
|
||
当前最低价 {money(preview.lowest.price_value)}
|
||
</div>
|
||
<a
|
||
href={preview.lowest.url}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="shrink-0 text-xs text-[var(--brand-primary)] hover:underline"
|
||
>
|
||
查看商品
|
||
</a>
|
||
</div>
|
||
<div className="mt-1 line-clamp-2 text-sm text-emerald-900/80">
|
||
{preview.lowest.title}
|
||
</div>
|
||
<div className="mt-1 text-xs text-emerald-700/70">
|
||
参与比价 {preview.matched} / {preview.count} 件
|
||
{preview.ai_note ? ` · ${preview.ai_note}` : ''}
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{preview && preview.items.length > 0 && (
|
||
<div className="max-h-[220px] space-y-1.5 overflow-y-auto">
|
||
{preview.items.slice(0, 8).map((it, idx) => (
|
||
<a
|
||
key={it.item_id || idx}
|
||
href={it.url}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="flex items-center justify-between gap-3 rounded-md bg-white px-3 py-2 text-sm hover:bg-slate-100"
|
||
>
|
||
<span className="min-w-0 flex-1 truncate text-[var(--text-secondary)]">
|
||
{it.title}
|
||
</span>
|
||
<span className="shrink-0 font-medium text-emerald-600">
|
||
{money(it.price_value)}
|
||
</span>
|
||
</a>
|
||
))}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{formError && <div className="text-sm text-rose-600">{formError}</div>}
|
||
</div>
|
||
|
||
<div className="modal-footer flex justify-end gap-3 border-t border-[var(--border-light)] px-6 py-4">
|
||
<button
|
||
type="button"
|
||
onClick={() => setModalOpen(false)}
|
||
className="ios-btn-secondary rounded-md px-4 py-2.5"
|
||
>
|
||
取消
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void submit()}
|
||
disabled={saving}
|
||
className="ios-btn-primary rounded-md px-4 py-2.5 disabled:opacity-50"
|
||
>
|
||
{saving ? '保存中…' : editingId === null ? '创建规则' : '保存修改'}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* ---------------------------------------------------- 检查历史 */}
|
||
{historyFor && (
|
||
<div className="modal-overlay" role="dialog" aria-modal="true" aria-label="检查历史">
|
||
<div className="modal-container max-h-[90vh] w-full max-w-[980px] overflow-y-auto">
|
||
<div className="flex items-center justify-between border-b border-[var(--border-light)] px-6 py-4">
|
||
<div>
|
||
<h3 className="text-lg font-semibold text-[var(--text-primary)]">
|
||
检查历史 · {historyFor.name}
|
||
</h3>
|
||
<p className="mt-0.5 text-xs text-[var(--text-muted)]">
|
||
关键词「{historyFor.keyword}」,保留最近 100 次
|
||
</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={() => setHistoryFor(null)}
|
||
className="rounded-full p-1.5 text-[var(--text-muted)] hover:bg-slate-100"
|
||
aria-label="关闭"
|
||
>
|
||
<X className="h-5 w-5" />
|
||
</button>
|
||
</div>
|
||
|
||
<div className="px-6 py-5">
|
||
{historyLoading ? (
|
||
<PageLoading />
|
||
) : !records.length ? (
|
||
<EmptyState
|
||
title="还没有检查记录"
|
||
description="点规则右侧的「立即检查」跑一次。"
|
||
icon={Clock3}
|
||
/>
|
||
) : (
|
||
<div className="overflow-x-auto">
|
||
<table className="data-table responsive-data-table min-w-[760px]">
|
||
<thead>
|
||
<tr>
|
||
<th className="w-[18%]">时间</th>
|
||
<th className="w-[12%]">最低价</th>
|
||
<th>商品</th>
|
||
<th className="w-[10%]">比价数</th>
|
||
<th className="w-[16%]">结果</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{records.map((rec) => (
|
||
<tr key={rec.id}>
|
||
<td data-label="时间">
|
||
<div className="text-sm text-[var(--text-secondary)]">
|
||
{new Date(rec.checked_at * 1000).toLocaleString('zh-CN', {
|
||
hour12: false,
|
||
})}
|
||
</div>
|
||
<div className="text-xs text-[var(--text-muted)]">
|
||
{rec.source === 'auto' ? '自动' : '手动'}
|
||
</div>
|
||
</td>
|
||
<td data-label="最低价">
|
||
{rec.lowest_price === null || rec.lowest_price === undefined ? (
|
||
<span className="text-sm text-[var(--text-muted)]">—</span>
|
||
) : (
|
||
<span className="font-semibold text-emerald-600">
|
||
{money(rec.lowest_price)}
|
||
</span>
|
||
)}
|
||
</td>
|
||
<td data-label="商品">
|
||
{rec.lowest_url ? (
|
||
<a
|
||
href={rec.lowest_url}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="block max-w-[380px] break-words text-sm text-[var(--brand-primary)] hover:underline"
|
||
title={rec.lowest_title ?? ''}
|
||
>
|
||
{rec.lowest_title || '(无标题)'}
|
||
</a>
|
||
) : (
|
||
<span className="block max-w-[380px] break-words text-sm text-[var(--text-muted)]">
|
||
{rec.lowest_title || '—'}
|
||
</span>
|
||
)}
|
||
</td>
|
||
<td data-label="比价数">
|
||
<span className="text-sm text-[var(--text-secondary)]">
|
||
{rec.item_count} 件
|
||
</span>
|
||
</td>
|
||
<td data-label="结果">
|
||
{rec.error ? (
|
||
<span className="text-xs text-amber-600" title={rec.error}>
|
||
出错:{rec.error.slice(0, 18)}
|
||
</span>
|
||
) : rec.lowest_price === null || rec.lowest_price === undefined ? (
|
||
<span className="text-xs text-[var(--text-muted)]">无可比价商品</span>
|
||
) : (
|
||
<span
|
||
className={`inline-flex items-center gap-1 rounded px-1.5 py-0.5 text-xs ${
|
||
rec.notified
|
||
? 'bg-emerald-50 text-emerald-700'
|
||
: 'bg-slate-100 text-slate-500'
|
||
}`}
|
||
>
|
||
{rec.notified ? (
|
||
<>
|
||
<Bell className="h-3 w-3" />
|
||
已通知
|
||
</>
|
||
) : (
|
||
'已记录'
|
||
)}
|
||
</span>
|
||
)}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|