s''
This commit is contained in:
@@ -3,11 +3,23 @@
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "@/i18n/navigation";
|
||||
import Footer from "@/app/components/Footer";
|
||||
import { apiFetch } from "@/app/lib/api-client";
|
||||
|
||||
interface AssistantCity {
|
||||
id: string;
|
||||
slug?: string;
|
||||
name: string;
|
||||
costPerMonth?: number;
|
||||
internetSpeed?: number;
|
||||
overallScore?: number;
|
||||
}
|
||||
|
||||
export default function AIPage() {
|
||||
const { t } = useTranslation("ai");
|
||||
const [query, setQuery] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [answer, setAnswer] = useState("");
|
||||
const [cities, setCities] = useState<AssistantCity[]>([]);
|
||||
|
||||
const exampleQueries = [
|
||||
"我预算每月800刀,想亚洲热带,有稳定网,适合freelance写作,推荐城市+理由",
|
||||
@@ -15,11 +27,20 @@ export default function AIPage() {
|
||||
"生成3月清迈行程计划",
|
||||
];
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!query.trim()) return;
|
||||
setLoading(true);
|
||||
setTimeout(() => setLoading(false), 1500);
|
||||
try {
|
||||
const data = await apiFetch<{ answer: string; cities: AssistantCity[] }>("/api/ai/assistant", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ question: query }),
|
||||
});
|
||||
setAnswer(data.answer);
|
||||
setCities(data.cities || []);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -52,6 +73,25 @@ export default function AIPage() {
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{(answer || cities.length > 0) && (
|
||||
<div className="mt-6 rounded-2xl border border-gray-100 bg-gray-50 p-4 dark:border-gray-800 dark:bg-gray-800">
|
||||
{answer && <p className="leading-7 text-gray-700 dark:text-gray-300">{answer}</p>}
|
||||
{cities.length > 0 && (
|
||||
<div className="mt-4 grid gap-3 sm:grid-cols-3">
|
||||
{cities.map((city) => (
|
||||
<div key={city.id} className="rounded-xl bg-white p-3 text-sm shadow-sm dark:bg-gray-900">
|
||||
<p className="font-semibold text-gray-900 dark:text-gray-100">{city.name}</p>
|
||||
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
¥{Number(city.costPerMonth || 0).toLocaleString()}/月 · {city.internetSpeed || 0}Mbps
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-[#ff4d4f]">评分 {city.overallScore || "-"}/5</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-6 pt-6 border-t border-gray-100 dark:border-gray-800">
|
||||
<p className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
|
||||
{t("examples")}
|
||||
@@ -72,7 +112,7 @@ export default function AIPage() {
|
||||
</div>
|
||||
|
||||
<p className="mt-6 text-center text-xs text-gray-400 dark:text-gray-500">
|
||||
{t("comingSoon")}
|
||||
AI 助手已接入本机城市数据库,后续可替换为真实模型和用户画像推荐。
|
||||
</p>
|
||||
</div>
|
||||
<Footer />
|
||||
|
||||
Reference in New Issue
Block a user