Hanye官网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FaqContent.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div class="faq-content">
  3. <div v-if="isLoading" class="flex justify-center py-12">
  4. <div class="animate-spin h-8 w-8 border-2 border-blue-400 border-t-transparent rounded-full"></div>
  5. </div>
  6. <div v-else class="p-8">
  7. <!-- 分类和搜索区域 -->
  8. <div class="mb-8 space-y-6">
  9. <!-- 分类选择 -->
  10. <div class="flex flex-wrap gap-2">
  11. <button
  12. v-for="category in categoriesList"
  13. :key="category"
  14. @click="handleCategoryFilter(category)"
  15. class="px-4 py-2 rounded-md text-sm font-medium transition-colors duration-200 border"
  16. :class="{
  17. 'bg-blue-600 text-white border-blue-600': selectedCategory === category,
  18. 'bg-gray-700 text-gray-300 border-gray-600 hover:bg-gray-600': selectedCategory !== category,
  19. }"
  20. >
  21. {{ category }}
  22. </button>
  23. </div>
  24. <!-- 搜索框 -->
  25. <div class="relative max-w-md">
  26. <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
  27. <svg class="h-5 w-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  28. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
  29. </svg>
  30. </div>
  31. <input
  32. v-model="searchTerm"
  33. type="search"
  34. :placeholder="t('faq.searchPlaceholder')"
  35. class="w-full pl-10 pr-10 py-3 border border-gray-600 rounded-md text-white bg-gray-700 placeholder-gray-400 focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none"
  36. />
  37. <button
  38. v-if="searchTerm"
  39. @click="clearSearch"
  40. class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-500 hover:text-gray-300"
  41. >
  42. <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  43. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
  44. </svg>
  45. </button>
  46. </div>
  47. </div>
  48. <!-- FAQ列表 -->
  49. <div class="space-y-3">
  50. <div
  51. v-for="faq in filteredFaqs"
  52. :key="generateFaqKey(faq)"
  53. class="border border-gray-600 rounded-lg overflow-hidden"
  54. >
  55. <div
  56. class="flex items-center justify-between p-6 cursor-pointer hover:bg-gray-700 transition-colors duration-200"
  57. @click="toggleFaq(faq)"
  58. >
  59. <div class="text-white font-medium flex-1 text-left">
  60. <template
  61. v-for="(part, i) in highlightKeyword(faq.title)"
  62. :key="i"
  63. >
  64. <span v-if="typeof part === 'string'">{{ part }}</span>
  65. <component v-else :is="part"></component>
  66. </template>
  67. </div>
  68. <div
  69. class="text-gray-400 transition-transform duration-200 ml-4 flex-shrink-0"
  70. :class="{ 'rotate-180': isFaqExpanded(faq) }"
  71. >
  72. <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  73. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
  74. </svg>
  75. </div>
  76. </div>
  77. <div
  78. v-if="isFaqExpanded(faq)"
  79. class="px-6 pb-6 border-t border-gray-600 bg-gray-750"
  80. >
  81. <div class="pt-6">
  82. <ContentRenderer
  83. class="prose prose-invert prose-sm max-w-none"
  84. :value="{ body: faq.content }"
  85. v-highlight="searchTerm.trim()"
  86. />
  87. </div>
  88. </div>
  89. </div>
  90. <div
  91. v-if="filteredFaqs.length === 0"
  92. class="text-center text-gray-400 py-16"
  93. >
  94. <div class="mb-4">
  95. <svg class="mx-auto h-12 w-12 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  96. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
  97. </svg>
  98. </div>
  99. <p class="text-lg font-medium text-white mb-2">{{ t("faq.noResults") }}</p>
  100. <p class="text-sm text-gray-500">尝试调整搜索条件或选择不同的分类</p>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </template>
  106. <script setup lang="ts">
  107. /**
  108. * FAQ内容组件
  109. * 专业的常见问题解答展示组件
  110. */
  111. import { useErrorHandler } from "~/composables/useErrorHandler";
  112. import { queryCollection } from "#imports";
  113. const { error, isLoading, wrapAsync } = useErrorHandler();
  114. const { t, locale } = useI18n();
  115. // FAQ数据接口
  116. interface FAQ {
  117. category: string;
  118. title: string;
  119. content: any;
  120. sort: number;
  121. id?: string;
  122. }
  123. // 响应式数据
  124. const faqs = ref<FAQ[]>([]);
  125. const categoriesList = ref<string[]>([]);
  126. const selectedCategory = ref("");
  127. const expandedFaqKeys = ref<Set<string>>(new Set());
  128. const searchTerm = ref("");
  129. // 使用 queryCollection 加载FAQ数据
  130. const { data: faqData } = await useAsyncData(
  131. "faqs",
  132. async () => {
  133. try {
  134. const content = await queryCollection("content")
  135. .where("path", "LIKE", `/faq/${locale.value}/%`)
  136. .all();
  137. if (!content || !Array.isArray(content)) {
  138. console.error("No FAQ content found or invalid format:", content);
  139. return [];
  140. }
  141. const faqItems = content.map((item: any) => {
  142. return {
  143. category: item.meta?.category || "",
  144. title: item.title || "",
  145. content: item.body || "",
  146. sort: item.meta?.sort || 0,
  147. };
  148. });
  149. return faqItems.sort((a, b) => a.sort - b.sort);
  150. } catch (error) {
  151. console.error("Error loading FAQ content:", error);
  152. return [];
  153. }
  154. },
  155. {
  156. server: true,
  157. lazy: false,
  158. immediate: true,
  159. watch: [locale],
  160. }
  161. );
  162. // 处理FAQ数据变化
  163. watchEffect(() => {
  164. if (faqData.value) {
  165. isLoading.value = true;
  166. try {
  167. const allOption: string =
  168. locale.value === "en"
  169. ? "All"
  170. : locale.value === "zh"
  171. ? "全部"
  172. : "すべて";
  173. const uniqueCategories = [
  174. ...new Set(faqData.value.map((faq: FAQ) => faq.category)),
  175. ]
  176. .filter((category) => category)
  177. .sort();
  178. categoriesList.value = [allOption, ...uniqueCategories];
  179. selectedCategory.value = categoriesList.value[0];
  180. } catch (err) {
  181. console.error("Error processing FAQ data:", err);
  182. error.value = new Error(t("faq.processError"));
  183. } finally {
  184. isLoading.value = false;
  185. }
  186. }
  187. });
  188. // 过滤后的FAQ列表
  189. const filteredFaqs = computed(() => {
  190. if (!faqData.value) {
  191. return [];
  192. }
  193. let filtered = [...faqData.value];
  194. // 按分类过滤
  195. const allOption: string =
  196. locale.value === "en"
  197. ? "All"
  198. : locale.value === "zh"
  199. ? "全部"
  200. : "すべて";
  201. if (selectedCategory.value && selectedCategory.value !== allOption) {
  202. filtered = filtered.filter((faq) => faq.category === selectedCategory.value);
  203. }
  204. // 按搜索词过滤
  205. if (searchTerm.value.trim()) {
  206. const search = searchTerm.value.trim().toLowerCase();
  207. filtered = filtered.filter((faq) =>
  208. faq.title.toLowerCase().includes(search)
  209. );
  210. }
  211. return filtered;
  212. });
  213. /**
  214. * 生成FAQ的唯一标识
  215. */
  216. const generateFaqKey = (faq: FAQ): string => {
  217. return `${faq.category}-${faq.title}-${faq.sort}`;
  218. };
  219. /**
  220. * 检查FAQ是否展开
  221. */
  222. const isFaqExpanded = (faq: FAQ): boolean => {
  223. return expandedFaqKeys.value.has(generateFaqKey(faq));
  224. };
  225. /**
  226. * 切换FAQ展开状态
  227. */
  228. const toggleFaq = (faq: FAQ): void => {
  229. const key = generateFaqKey(faq);
  230. if (expandedFaqKeys.value.has(key)) {
  231. expandedFaqKeys.value.delete(key);
  232. } else {
  233. expandedFaqKeys.value.add(key);
  234. }
  235. };
  236. /**
  237. * 处理分类过滤
  238. */
  239. const handleCategoryFilter = (category: string): void => {
  240. selectedCategory.value = category;
  241. // 清空展开状态
  242. expandedFaqKeys.value.clear();
  243. };
  244. /**
  245. * 清除搜索
  246. */
  247. const clearSearch = (): void => {
  248. searchTerm.value = "";
  249. };
  250. /**
  251. * 高亮关键词
  252. */
  253. const highlightKeyword = (text: string) => {
  254. if (!searchTerm.value.trim()) {
  255. return [text];
  256. }
  257. const keyword = searchTerm.value.trim();
  258. const regex = new RegExp(`(${keyword})`, "gi");
  259. const parts = text.split(regex);
  260. return parts.map((part) => {
  261. if (part.toLowerCase() === keyword.toLowerCase()) {
  262. return h("mark", { class: "bg-yellow-500/30 text-yellow-200 px-1 rounded" }, part);
  263. }
  264. return part;
  265. });
  266. };
  267. // 自定义指令:高亮搜索结果
  268. const vHighlight = {
  269. mounted(el: HTMLElement, binding: { value: string }) {
  270. highlightText(el, binding.value);
  271. },
  272. updated(el: HTMLElement, binding: { value: string }) {
  273. highlightText(el, binding.value);
  274. },
  275. };
  276. /**
  277. * 高亮文本中的关键词
  278. */
  279. const highlightText = (el: HTMLElement, keyword: string): void => {
  280. if (!keyword || !keyword.trim()) {
  281. return;
  282. }
  283. const walker = document.createTreeWalker(
  284. el,
  285. NodeFilter.SHOW_TEXT,
  286. null
  287. );
  288. const nodes: Text[] = [];
  289. let node: Text | null;
  290. while ((node = walker.nextNode() as Text)) {
  291. nodes.push(node);
  292. }
  293. nodes.forEach((textNode) => {
  294. const parent = textNode.parentNode;
  295. if (!parent || parent.nodeName === "MARK") return;
  296. const text = textNode.textContent || "";
  297. const regex = new RegExp(`(${keyword.trim()})`, "gi");
  298. if (regex.test(text)) {
  299. const fragment = document.createDocumentFragment();
  300. const parts = text.split(regex);
  301. parts.forEach((part) => {
  302. if (part.toLowerCase() === keyword.trim().toLowerCase()) {
  303. const mark = document.createElement("mark");
  304. mark.className = "bg-yellow-500/30 text-yellow-200 px-1 rounded";
  305. mark.textContent = part;
  306. fragment.appendChild(mark);
  307. } else if (part) {
  308. fragment.appendChild(document.createTextNode(part));
  309. }
  310. });
  311. parent.replaceChild(fragment, textNode);
  312. }
  313. });
  314. };
  315. </script>
  316. <style scoped>
  317. /* 过渡效果 */
  318. .transition-colors {
  319. transition-property: color, background-color, border-color;
  320. transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  321. }
  322. .transition-transform {
  323. transition-property: transform;
  324. transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  325. }
  326. .duration-200 {
  327. transition-duration: 200ms;
  328. }
  329. /* 悬停效果 */
  330. .hover\:bg-gray-700:hover {
  331. background-color: #374151;
  332. }
  333. .hover\:bg-gray-600:hover {
  334. background-color: #4b5563;
  335. }
  336. .hover\:text-gray-300:hover {
  337. color: #d1d5db;
  338. }
  339. /* 焦点样式 */
  340. input:focus {
  341. box-shadow: 0 0 0 1px #3b82f6;
  342. }
  343. /* 高亮样式 */
  344. mark {
  345. background-color: rgba(234, 179, 8, 0.3);
  346. color: #fef3c7;
  347. padding: 2px 4px;
  348. border-radius: 4px;
  349. }
  350. /* Prose样式覆盖 - 深色主题 */
  351. :deep(.prose) {
  352. color: #d1d5db;
  353. max-width: none;
  354. }
  355. :deep(.prose h1),
  356. :deep(.prose h2),
  357. :deep(.prose h3),
  358. :deep(.prose h4),
  359. :deep(.prose h5),
  360. :deep(.prose h6) {
  361. color: #f9fafb;
  362. font-weight: 600;
  363. }
  364. :deep(.prose p) {
  365. margin-top: 1rem;
  366. margin-bottom: 1rem;
  367. line-height: 1.625;
  368. color: #d1d5db;
  369. }
  370. :deep(.prose ul),
  371. :deep(.prose ol) {
  372. margin-top: 1rem;
  373. margin-bottom: 1rem;
  374. }
  375. :deep(.prose li) {
  376. margin-top: 0.5rem;
  377. margin-bottom: 0.5rem;
  378. color: #d1d5db;
  379. }
  380. :deep(.prose strong) {
  381. color: #f9fafb;
  382. font-weight: 600;
  383. }
  384. :deep(.prose code) {
  385. color: #f9fafb;
  386. background-color: #374151;
  387. padding: 0.125rem 0.25rem;
  388. border-radius: 0.25rem;
  389. font-size: 0.875em;
  390. }
  391. :deep(.prose a) {
  392. color: #60a5fa;
  393. text-decoration: underline;
  394. }
  395. :deep(.prose a:hover) {
  396. color: #93c5fd;
  397. }
  398. /* 自定义gray-750背景色 */
  399. .bg-gray-750 {
  400. background-color: #2d3748;
  401. }
  402. /* 响应式优化 */
  403. @media (max-width: 768px) {
  404. .flex-wrap {
  405. gap: 0.5rem;
  406. }
  407. .px-6 {
  408. padding-left: 1rem;
  409. padding-right: 1rem;
  410. }
  411. }
  412. </style>