Hanye官网
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

faq.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <div
  7. class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  8. ></div>
  9. </div>
  10. <div v-else>
  11. <div class="max-w-full xl:px-2 lg:px-2 md:px-4 px-4 mt-6 mb-20">
  12. <div class="max-w-screen-2xl mx-auto">
  13. <nuxt-link
  14. to="/"
  15. class="justify-start text-white/60 text-base font-normal"
  16. >{{ t("common.home") }}</nuxt-link
  17. >
  18. <span class="text-white/60 text-base font-normal px-2"> / </span>
  19. <nuxt-link to="/faq" class="text-white text-base font-normal">{{
  20. t("faq.title")
  21. }}</nuxt-link>
  22. </div>
  23. </div>
  24. <div
  25. class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
  26. >
  27. <div class="max-w-screen-2xl mx-auto">
  28. <div class="w-full grid grid-cols-1 md:grid-cols-10 gap-8 md:gap-2">
  29. <!-- 左侧分类导航 -->
  30. <div class="col-span-1 md:col-span-2">
  31. <div class="flex flex-col gap-4">
  32. <div class="text-white text-3xl font-medium">
  33. {{ t("faq.category") }}
  34. </div>
  35. <div class="flex flex-col gap-4 w-fit">
  36. <div
  37. v-for="category in categoriesList"
  38. :key="category"
  39. @click="handleCategoryFilter(category)"
  40. class="opacity-80 justify-start text-white text-base font-normal cursor-pointer hover:opacity-100 transition-all duration-300 px-4 py-2 rounded-lg inline-block"
  41. :class="{
  42. 'opacity-100 font-bold bg-gradient-to-r from-blue-700 to-blue-400 text-white border-0 shadow-lg scale-105 transition-all duration-300':
  43. selectedCategory === category,
  44. 'hover:bg-zinc-800/50': selectedCategory !== category,
  45. }"
  46. >
  47. {{ category }}
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <!-- 右侧FAQ列表 -->
  53. <div class="col-span-1 md:col-span-8">
  54. <!-- 搜索框 -->
  55. <div class="mb-8 relative">
  56. <input
  57. v-model="searchTerm"
  58. ref="searchInputRef"
  59. type="search"
  60. :placeholder="t('faq.searchPlaceholder')"
  61. class="block w-full appearance-none rounded-lg border border-gray-600 bg-zinc-800/70 px-4 py-3 text-base text-gray-100 placeholder-gray-400 shadow-inner transition duration-200 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/50 focus:ring-offset-2 focus:ring-offset-zinc-900"
  62. />
  63. <button
  64. v-if="searchTerm"
  65. @click="clearSearch"
  66. class="absolute right-2 top-1/2 -translate-y-1/2 flex items-center justify-center w-8 h-8 rounded-full bg-zinc-700/80 hover:bg-blue-500/90 text-gray-300 hover:text-white transition-all duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
  67. :aria-label="t('faq.clearSearch')"
  68. tabindex="0"
  69. type="button"
  70. >
  71. <svg
  72. xmlns="http://www.w3.org/2000/svg"
  73. class="h-5 w-5"
  74. fill="none"
  75. viewBox="0 0 24 24"
  76. stroke="currentColor"
  77. stroke-width="2"
  78. >
  79. <path
  80. stroke-linecap="round"
  81. stroke-linejoin="round"
  82. d="M6 18L18 6M6 6l12 12"
  83. />
  84. </svg>
  85. </button>
  86. </div>
  87. <div class="flex flex-col gap-8">
  88. <div
  89. v-for="faq in filteredFaqs"
  90. :key="generateFaqKey(faq)"
  91. class="bg-zinc-900 rounded-lg p-8 transition-all duration-300 hover:bg-zinc-800 hover:shadow-lg"
  92. >
  93. <div
  94. class="flex items-center justify-between cursor-pointer"
  95. @click="toggleFaq(faq)"
  96. >
  97. <div class="text-white text-xl font-medium">
  98. <template
  99. v-for="(part, i) in highlightKeyword(faq.title)"
  100. :key="i"
  101. >
  102. <span v-if="typeof part === 'string'">{{
  103. part
  104. }}</span>
  105. <component v-else :is="part"></component>
  106. </template>
  107. </div>
  108. <div
  109. class="text-white text-2xl transition-transform duration-300"
  110. :class="{ 'rotate-180': isFaqExpanded(faq) }"
  111. >
  112. </div>
  113. </div>
  114. <div
  115. v-if="isFaqExpanded(faq)"
  116. class="mt-4 text-white/80 text-base font-normal leading-relaxed"
  117. >
  118. <ContentRenderer class="text-white" :value="{ body: faq.content }" />
  119. </div>
  120. </div>
  121. <div
  122. v-if="filteredFaqs.length === 0"
  123. class="text-center text-gray-400 py-8"
  124. >
  125. {{ t("faq.noResults") }}
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. </div>
  133. </ErrorBoundary>
  134. </div>
  135. </template>
  136. <script setup lang="ts">
  137. /**
  138. * FAQ页面
  139. * 展示常见问题及其答案
  140. */
  141. import { useErrorHandler } from "~/composables/useErrorHandler";
  142. import { queryCollection } from "#imports";
  143. const { error, isLoading, wrapAsync } = useErrorHandler();
  144. const { t, locale } = useI18n();
  145. // FAQ数据
  146. interface FAQ {
  147. category: string;
  148. title: string;
  149. content: any; // 修改为 any 类型,因为可能是对象或字符串
  150. sort: number;
  151. id?: string;
  152. }
  153. // 从content目录读取FAQ数据
  154. const faqs = ref<FAQ[]>([]);
  155. const categoriesList = ref<string[]>([]);
  156. // 选中的分类
  157. const selectedCategory = ref("");
  158. // 使用 queryCollection 加载FAQ数据
  159. const { data: faqData } = await useAsyncData(
  160. "faqs",
  161. async () => {
  162. try {
  163. // 使用 queryCollection 加载 FAQ 数据
  164. const content = await queryCollection("content")
  165. .where("path", "LIKE", `/faq/${locale.value}/%`)
  166. .all();
  167. console.log("Raw FAQ content:", content);
  168. if (!content || !Array.isArray(content)) {
  169. console.error("No FAQ content found or invalid format:", content);
  170. return [];
  171. }
  172. // 转换数据格式
  173. const faqItems = content.map((item: any) => {
  174. console.log("Processing FAQ item:", item);
  175. return {
  176. category: item.meta?.category || "",
  177. title: item.title || "",
  178. content: item.body || "",
  179. sort: item.meta?.sort || 0,
  180. };
  181. });
  182. console.log("Processed FAQ items:", faqItems);
  183. return faqItems.sort((a, b) => a.sort - b.sort);
  184. } catch (error) {
  185. console.error("Error loading FAQ content:", error);
  186. return [];
  187. }
  188. },
  189. {
  190. server: true,
  191. lazy: false,
  192. immediate: true,
  193. watch: [locale],
  194. }
  195. );
  196. // 处理FAQ数据变化
  197. watchEffect(() => {
  198. if (faqData.value) {
  199. isLoading.value = true;
  200. try {
  201. console.log("Processing FAQ data:", faqData.value);
  202. // 设置分类列表和默认选中的分类
  203. const allOption: string =
  204. locale.value === "en"
  205. ? "All"
  206. : locale.value === "zh"
  207. ? "全部"
  208. : "すべて";
  209. // 从FAQ数据中提取所有不同的分类
  210. const uniqueCategories = [
  211. ...new Set(faqData.value.map((faq: FAQ) => faq.category)),
  212. ]
  213. .filter((category) => category)
  214. .sort(); // 过滤掉空分类并排序
  215. console.log("Unique categories:", uniqueCategories);
  216. // 设置分类列表和默认选中的分类
  217. categoriesList.value = [allOption, ...uniqueCategories];
  218. selectedCategory.value = categoriesList.value[0];
  219. } catch (err) {
  220. console.error("Error processing FAQ data:", err);
  221. error.value = new Error(t("faq.processError"));
  222. } finally {
  223. isLoading.value = false;
  224. }
  225. }
  226. });
  227. // 展开的FAQ标识
  228. const expandedFaqKey = ref<string | null>(null);
  229. // 搜索关键词
  230. const searchTerm = ref("");
  231. const searchInputRef = ref<HTMLInputElement | null>(null);
  232. // 过滤后的FAQ列表
  233. const filteredFaqs = computed(() => {
  234. if (!faqData.value) {
  235. console.log("No FAQ data available");
  236. return [];
  237. }
  238. let result = faqData.value;
  239. // 分类过滤
  240. if (selectedCategory.value !== categoriesList.value[0]) {
  241. result = result.filter(
  242. (faq: FAQ) => faq.category === selectedCategory.value
  243. );
  244. }
  245. // 搜索过滤 - 只搜索标题
  246. if (searchTerm.value.trim()) {
  247. const keyword = searchTerm.value.trim().toLowerCase();
  248. result = result.filter((faq: FAQ) => {
  249. const title = String(faq.title || "").toLowerCase();
  250. return title.includes(keyword);
  251. });
  252. }
  253. return result;
  254. });
  255. /**
  256. * 高亮显示匹配的关键字
  257. * @param text 原始文本
  258. * @returns 高亮后的VNode数组
  259. */
  260. function highlightKeyword(text: any): (string | any)[] {
  261. const keyword = searchTerm.value.trim();
  262. if (!keyword) return [text];
  263. // 确保 text 是字符串
  264. const textStr = typeof text === "string" ? text : String(text?.body || "");
  265. // 构建正则,忽略大小写,转义特殊字符
  266. const escaped = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  267. const reg = new RegExp(escaped, "gi");
  268. const parts = textStr.split(reg);
  269. const matches = textStr.match(reg);
  270. if (!matches) return [textStr];
  271. // 组装高亮
  272. const result: (string | any)[] = [];
  273. parts.forEach((part, i) => {
  274. result.push(part);
  275. if (i < matches.length) {
  276. result.push(
  277. h(
  278. "span",
  279. { class: "text-blue-400 bg-blue-400/10 font-bold" },
  280. matches[i]
  281. )
  282. );
  283. }
  284. });
  285. return result;
  286. }
  287. /**
  288. * 生成FAQ的唯一标识
  289. * @param faq FAQ对象
  290. * @returns string 唯一标识
  291. */
  292. function generateFaqKey(faq: FAQ): string {
  293. return `${faq.category}-${faq.title}`;
  294. }
  295. /**
  296. * 切换FAQ展开状态
  297. * @param faq FAQ对象
  298. * @returns void
  299. */
  300. function toggleFaq(faq: FAQ): void {
  301. if (!faq) return;
  302. const faqKey = generateFaqKey(faq);
  303. // 如果点击的是当前展开的FAQ,则关闭它
  304. if (expandedFaqKey.value === faqKey) {
  305. expandedFaqKey.value = null;
  306. } else {
  307. // 否则展开新的FAQ
  308. expandedFaqKey.value = faqKey;
  309. }
  310. }
  311. /**
  312. * 检查FAQ是否处于展开状态
  313. * @param faq FAQ对象
  314. * @returns boolean
  315. */
  316. function isFaqExpanded(faq: FAQ): boolean {
  317. if (!faq) return false;
  318. return expandedFaqKey.value === generateFaqKey(faq);
  319. }
  320. /**
  321. * 处理分类筛选
  322. */
  323. function handleCategoryFilter(category: string) {
  324. selectedCategory.value = category;
  325. }
  326. // 自动展开匹配项
  327. watch(
  328. [filteredFaqs, searchTerm],
  329. ([faqs, keyword]: [FAQ[], string]) => {
  330. if (!faqs?.length) return;
  331. if (keyword.trim()) {
  332. // 搜索时展开第一个匹配项
  333. expandedFaqKey.value = faqs[0] ? generateFaqKey(faqs[0]) : null;
  334. } else {
  335. // 清除搜索时关闭所有展开项
  336. expandedFaqKey.value = null;
  337. }
  338. },
  339. { deep: true }
  340. );
  341. function clearSearch() {
  342. searchTerm.value = "";
  343. // 让输入框重新聚焦
  344. searchInputRef.value?.focus();
  345. }
  346. // SEO优化
  347. useHead({
  348. title: t("faq.title") + " - Hanye",
  349. meta: [
  350. {
  351. name: "description",
  352. content: t("faq.description"),
  353. },
  354. {
  355. name: "keywords",
  356. content: t("faq.keywords"),
  357. },
  358. ],
  359. });
  360. </script>
  361. <style scoped>
  362. /* 添加过渡动画 */
  363. .fade-enter-active,
  364. .fade-leave-active {
  365. transition: opacity 0.3s;
  366. }
  367. .fade-enter-from,
  368. .fade-leave-to {
  369. opacity: 0;
  370. }
  371. </style>