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.

index.vue 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. <template>
  2. <div>
  3. <div class="w-full h-[45px] sm:h-[55px] md:h-[65px] lg:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-8 md:py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-6 w-6 md:h-8 md:w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <div class="w-full mb-8 md:mb-12 lg:mb-16 relative">
  13. <div class="absolute top-0 left-0 w-full h-full z-10">
  14. <div
  15. class="max-w-screen-2xl mx-auto h-full flex flex-col justify-center gap-2 md:gap-4 p-4 md:p-6 lg:p-8"
  16. >
  17. <div
  18. class="text-white text-xl sm:text-2xl md:text-3xl lg:text-4xl xl:text-6xl font-normal"
  19. >
  20. {{ t("products.product_list") }}
  21. </div>
  22. <div
  23. class="text-white text-xs sm:text-sm md:text-base lg:text-lg font-normal leading-relaxed md:leading-loose"
  24. >
  25. {{ t("products.product_list_description") }}
  26. </div>
  27. </div>
  28. </div>
  29. <img
  30. :src="banner"
  31. alt="products-banner"
  32. class="w-full object-cover h-40 sm:h-48 md:h-56 lg:h-64 xl:h-80"
  33. />
  34. </div>
  35. <div class="max-w-full mb-4 md:mb-6 lg:mb-8 xl:px-2 lg:px-2 md:px-4 px-4">
  36. <div class="max-w-screen-2xl mx-auto">
  37. <nuxt-link
  38. to="/"
  39. class="text-white/60 text-sm md:text-base font-normal hover:text-white transition-colors duration-300"
  40. >{{ t("common.home") }}</nuxt-link
  41. >
  42. <span class="text-white/60 text-sm md:text-base font-normal px-2"> / </span>
  43. <nuxt-link
  44. to="/products"
  45. class="text-white text-sm md:text-base font-normal"
  46. >{{ t("products.product_list") }}</nuxt-link
  47. >
  48. </div>
  49. </div>
  50. <div
  51. class="max-w-full mb-8 md:mb-12 lg:mb-16 xl:mb-20 xl:px-2 lg:px-2 md:px-4 px-4"
  52. >
  53. <div class="max-w-screen-2xl mx-auto">
  54. <div class="w-full grid grid-cols-1 md:grid-cols-10 gap-4 md:gap-6 lg:gap-8">
  55. <div
  56. class="col-span-1 md:col-span-2 flex flex-col gap-8 md:gap-12 lg:gap-16 mb-6 md:mb-0"
  57. >
  58. <div class="flex flex-col gap-3 md:gap-4">
  59. <div class="flex justify-between items-center">
  60. <div class="text-white text-xl md:text-2xl lg:text-3xl font-medium">
  61. {{ t("products.product_categories_title") }}
  62. </div>
  63. <button
  64. v-if="selectedCategory"
  65. @click="clearCategory"
  66. class="flex items-center gap-1 px-2 py-1 md:px-3 md:py-1.5 text-xs md:text-sm text-white/60 hover:text-white bg-zinc-800/50 hover:bg-zinc-700/50 rounded-lg transition-all duration-300"
  67. >
  68. <span>{{ t("common.clear") }}</span>
  69. <svg
  70. xmlns="http://www.w3.org/2000/svg"
  71. class="h-3 w-3 md:h-4 md:w-4"
  72. viewBox="0 0 20 20"
  73. fill="currentColor"
  74. >
  75. <path
  76. fill-rule="evenodd"
  77. d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
  78. clip-rule="evenodd"
  79. />
  80. </svg>
  81. </button>
  82. </div>
  83. <div class="flex flex-col gap-2 md:gap-3 w-fit">
  84. <div
  85. v-for="category in categories"
  86. :key="category"
  87. @click="handleCategoryFilter(category)"
  88. class="opacity-80 select-none text-white text-sm md:text-base font-normal cursor-pointer hover:opacity-100 transition-all duration-300 px-3 py-1.5 md:px-4 md:py-2 rounded-lg inline-block"
  89. :class="{
  90. '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':
  91. selectedCategory === category,
  92. 'hover:bg-zinc-800/50': selectedCategory !== category,
  93. }"
  94. >
  95. {{ category }}
  96. </div>
  97. </div>
  98. </div>
  99. <div class="flex flex-col gap-3 md:gap-4">
  100. <div class="flex justify-between items-center">
  101. <div class="text-white text-xl md:text-2xl lg:text-3xl font-medium">
  102. {{ t("products.product_categories_usage") }}
  103. </div>
  104. <button
  105. v-if="selectedUsage"
  106. @click="clearUsage"
  107. class="flex items-center gap-1 px-2 py-1 md:px-3 md:py-1.5 text-xs md:text-sm text-white/60 hover:text-white bg-zinc-800/50 hover:bg-zinc-700/50 rounded-lg transition-all duration-300"
  108. >
  109. <span>{{ t("common.clear") }}</span>
  110. <svg
  111. xmlns="http://www.w3.org/2000/svg"
  112. class="h-3 w-3 md:h-4 md:w-4"
  113. viewBox="0 0 20 20"
  114. fill="currentColor"
  115. >
  116. <path
  117. fill-rule="evenodd"
  118. d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
  119. clip-rule="evenodd"
  120. />
  121. </svg>
  122. </button>
  123. </div>
  124. <div class="flex flex-col gap-2 md:gap-3 w-fit">
  125. <div
  126. v-for="usage in usages"
  127. :key="usage"
  128. @click="handleUsageFilter(usage)"
  129. class="opacity-80 select-none text-white text-sm md:text-base font-normal cursor-pointer hover:opacity-100 transition-all duration-300 px-3 py-1.5 md:px-4 md:py-2 rounded-lg inline-block"
  130. :class="{
  131. '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':
  132. selectedUsage === usage,
  133. 'hover:bg-zinc-800/50': selectedUsage !== usage,
  134. }"
  135. >
  136. {{ usage }}
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <div class="col-span-1 md:col-span-8">
  142. <div class="flex flex-col gap-8 md:gap-12 lg:gap-16">
  143. <template v-for="category in categories" :key="category">
  144. <div
  145. v-if="
  146. filteredProducts.filter((p) => {
  147. const categoryObj = allCategories.find(
  148. (c) => c.title === category
  149. );
  150. return categoryObj && p.categoryId === categoryObj.id;
  151. }).length > 0
  152. "
  153. class="flex flex-col gap-4 md:gap-6"
  154. >
  155. <div class="w-full text-white text-2xl md:text-3xl lg:text-4xl font-normal mb-4 md:mb-6">
  156. {{ category }}
  157. </div>
  158. <!-- 添加系列分组 -->
  159. <template v-for="[seriesName, seriesProducts] in Array.from(productsBySeries.entries())" :key="seriesName">
  160. <div v-if="seriesProducts.some(p => {
  161. const categoryObj = allCategories.find(c => c.title === category);
  162. return categoryObj && p.categoryId === categoryObj.id;
  163. })" class="mb-8 md:mb-12">
  164. <div class="relative">
  165. <!-- 系列标题背景 -->
  166. <div class="absolute inset-0 bg-gradient-to-r from-blue-900/20 to-blue-500/20 rounded-lg transform -skew-x-6"></div>
  167. <!-- 系列标题内容 -->
  168. <div class="relative flex items-center gap-3 md:gap-4 py-3 md:py-4 px-4 md:px-6">
  169. <!-- 装饰线 -->
  170. <div class="w-1 h-6 md:h-8 bg-gradient-to-b from-blue-500 to-blue-300 rounded-full"></div>
  171. <!-- 系列名称 -->
  172. <h3 class="text-white text-lg md:text-xl lg:text-2xl font-medium tracking-wide">
  173. {{ seriesName }}
  174. </h3>
  175. <!-- 产品数量标签 -->
  176. <div class="ml-auto px-2 py-0.5 md:px-3 md:py-1 bg-blue-500/20 rounded-full text-blue-300 text-xs md:text-sm">
  177. {{ seriesProducts.length }} {{ t("products.product_count") }}
  178. </div>
  179. </div>
  180. </div>
  181. <!-- 产品网格 -->
  182. <div class="mt-4 md:mt-6">
  183. <transition-group
  184. name="fade"
  185. tag="div"
  186. class="w-full grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 md:gap-4"
  187. >
  188. <nuxt-link
  189. v-for="product in seriesProducts.filter(p => {
  190. const categoryObj = allCategories.find(c => c.title === category);
  191. return categoryObj && p.categoryId === categoryObj.id;
  192. })"
  193. :key="product.id"
  194. :to="`/products/${product.name}`"
  195. class="group bg-zinc-900 rounded-lg transition-all duration-300 hover:bg-zinc-800 hover:scale-105 hover:shadow-lg hover:ring-2 hover:ring-blue-400 relative overflow-hidden"
  196. >
  197. <div class="w-full p-4 md:p-6 lg:p-8">
  198. <div class="relative w-full aspect-square mb-3 md:mb-4">
  199. <img
  200. v-if="!isImageError(product.id)"
  201. :src="product.image"
  202. :alt="product.name"
  203. class="w-full h-full object-cover rounded-lg transition-transform duration-300 group-hover:scale-110"
  204. @error="handleImageError($event, product)"
  205. @load="handleImageLoad($event, product)"
  206. loading="lazy"
  207. />
  208. <div
  209. v-if="isImageLoading(product.id)"
  210. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg"
  211. >
  212. <div
  213. class="animate-spin h-6 w-6 md:h-8 md:w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  214. ></div>
  215. </div>
  216. <div
  217. v-if="
  218. !product.image ||
  219. product.image === '' ||
  220. isImageError(product.id)
  221. "
  222. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg"
  223. >
  224. <svg
  225. xmlns="http://www.w3.org/2000/svg"
  226. class="h-12 w-12 md:h-16 md:w-16 text-white/40"
  227. fill="none"
  228. viewBox="0 0 24 24"
  229. stroke="currentColor"
  230. >
  231. <path
  232. stroke-linecap="round"
  233. stroke-linejoin="round"
  234. stroke-width="2"
  235. d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
  236. />
  237. </svg>
  238. </div>
  239. </div>
  240. <div class="text-center text-white text-base md:text-lg lg:text-xl font-normal mb-2">
  241. {{ product.name }}
  242. </div>
  243. <div class="text-center text-stone-400 text-sm md:text-base font-normal leading-normal">
  244. {{ product.capacities.join(" / ") }}
  245. </div>
  246. <!-- Summary 悬浮层 -->
  247. <div class="absolute inset-0 bg-gradient-to-t from-black/90 via-black/50 to-transparent opacity-0 group-hover:opacity-100 transition-all duration-300 flex items-end">
  248. <div class="p-4 md:p-6 w-full">
  249. <div class="text-white text-sm md:text-base line-clamp-3">
  250. {{ product.summary }}
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. </nuxt-link>
  256. </transition-group>
  257. </div>
  258. </div>
  259. </template>
  260. <!-- 展示没有系列的产品 -->
  261. <transition-group
  262. v-if="filteredProducts.filter(p => {
  263. const categoryObj = allCategories.find(c => c.title === category);
  264. return categoryObj && p.categoryId === categoryObj.id && (!p.series || p.series.length === 0);
  265. }).length > 0"
  266. name="fade"
  267. tag="div"
  268. class="w-full grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 md:gap-4"
  269. >
  270. <nuxt-link
  271. v-for="product in filteredProducts.filter(p => {
  272. const categoryObj = allCategories.find(c => c.title === category);
  273. return categoryObj && p.categoryId === categoryObj.id && (!p.series || p.series.length === 0);
  274. })"
  275. :key="product.id"
  276. :to="`/products/${product.name}`"
  277. class="group bg-zinc-900 rounded-lg transition-all duration-300 hover:bg-zinc-800 hover:scale-105 hover:shadow-lg hover:ring-2 hover:ring-blue-400 relative overflow-hidden"
  278. >
  279. <div class="w-full p-4 md:p-6 lg:p-8">
  280. <div class="relative w-full aspect-square mb-3 md:mb-4">
  281. <img
  282. v-if="!isImageError(product.id)"
  283. :src="product.image"
  284. :alt="product.name"
  285. class="w-full h-full object-cover rounded-lg transition-transform duration-300 group-hover:scale-110"
  286. @error="handleImageError($event, product)"
  287. @load="handleImageLoad($event, product)"
  288. loading="lazy"
  289. />
  290. <div
  291. v-if="isImageLoading(product.id)"
  292. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg"
  293. >
  294. <div
  295. class="animate-spin h-6 w-6 md:h-8 md:w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  296. ></div>
  297. </div>
  298. <div
  299. v-if="
  300. !product.image ||
  301. product.image === '' ||
  302. isImageError(product.id)
  303. "
  304. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg"
  305. >
  306. <svg
  307. xmlns="http://www.w3.org/2000/svg"
  308. class="h-12 w-12 md:h-16 md:w-16 text-white/40"
  309. fill="none"
  310. viewBox="0 0 24 24"
  311. stroke="currentColor"
  312. >
  313. <path
  314. stroke-linecap="round"
  315. stroke-linejoin="round"
  316. stroke-width="2"
  317. d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
  318. />
  319. </svg>
  320. </div>
  321. </div>
  322. <div class="text-center text-white text-base md:text-lg lg:text-xl font-normal mb-2">
  323. {{ product.name }}
  324. </div>
  325. <div class="text-center text-stone-400 text-sm md:text-base font-normal leading-normal">
  326. {{ product.capacities.join(" / ") }}
  327. </div>
  328. <!-- Summary 悬浮层 -->
  329. <div class="absolute inset-0 bg-gradient-to-t from-black/90 via-black/50 to-transparent opacity-0 group-hover:opacity-100 transition-all duration-300 flex items-end">
  330. <div class="p-4 md:p-6 w-full">
  331. <div class="text-white text-sm md:text-base line-clamp-3">
  332. {{ product.summary }}
  333. </div>
  334. </div>
  335. </div>
  336. </div>
  337. </nuxt-link>
  338. </transition-group>
  339. </div>
  340. </template>
  341. </div>
  342. </div>
  343. </div>
  344. </div>
  345. </div>
  346. </div>
  347. </ErrorBoundary>
  348. </div>
  349. </template>
  350. <script setup lang="ts">
  351. /**
  352. * 产品列表页面
  353. * 展示所有产品,支持按分类和用途筛选
  354. */
  355. import { useErrorHandler } from "~/composables/useErrorHandler";
  356. import banner from "@/assets/images/product-banner.webp";
  357. import { useI18n } from "vue-i18n";
  358. import { queryCollection } from "#imports";
  359. import { useRoute, useRouter } from "vue-router";
  360. import { useDebounceFn } from "@vueuse/core";
  361. const { t, locale } = useI18n();
  362. const route = useRoute();
  363. const router = useRouter();
  364. // 产品接口定义
  365. interface Product {
  366. id: number;
  367. name: string;
  368. title: string;
  369. categoryId: number;
  370. usage: string[];
  371. series: string[];
  372. capacities: string[];
  373. image: string;
  374. description?: string;
  375. summary: string;
  376. gallery: string[];
  377. sort: number;
  378. }
  379. interface Category {
  380. id: number;
  381. title: string;
  382. description?: string;
  383. image: string;
  384. summary: string;
  385. capacities: string[];
  386. sort: number;
  387. }
  388. // 分页配置
  389. const PAGE_SIZE = 100;
  390. const currentPage = ref(1);
  391. const totalPages = ref(1);
  392. // 使用防抖优化路由更新
  393. const debouncedRouterPush = useDebounceFn(
  394. (query: Record<string, string | undefined>) => {
  395. router.push({ query });
  396. },
  397. 300
  398. );
  399. const { error, isLoading, wrapAsync } = useErrorHandler();
  400. const allProducts = ref<Product[]>([]);
  401. const allCategories = ref<Category[]>([]);
  402. const filteredProducts = ref<Product[]>([]);
  403. const categories = ref<string[]>([]);
  404. const usages = ref<string[]>([]);
  405. const selectedCategory = ref(route.query.category?.toString() || "");
  406. const selectedUsage = ref(route.query.usage?.toString() || "");
  407. // 使用缓存优化数据加载
  408. const { data: productData } = await useAsyncData("products", async () => {
  409. try {
  410. const content = await queryCollection("content").all();
  411. const products = content
  412. .filter((item: any) => {
  413. const isProduct =
  414. item.path?.includes("/products/") &&
  415. !item.path?.includes("/categories/") &&
  416. item.path?.includes(`/${locale.value}/`);
  417. return isProduct;
  418. })
  419. .map((item: any, index: number) => {
  420. const meta = item.meta || item.frontmatter || {};
  421. const pathId = item.path?.match(/\/products\/(\d+)/)?.[1];
  422. return {
  423. id: pathId ? parseInt(pathId) : index + 1,
  424. name: meta.name || item.title || "",
  425. title: item.title || "",
  426. categoryId: parseInt(meta.categoryId?.toString() || "0"),
  427. usage: meta.usage || [],
  428. series: meta.series || [],
  429. capacities: meta.capacities || [],
  430. image: meta.image || "",
  431. description: meta.description || "",
  432. summary: meta.summary || "",
  433. gallery: meta.gallery || [],
  434. sort: meta.sort || 0,
  435. };
  436. })
  437. .sort((a, b) => a.sort - b.sort);
  438. // 计算总页数
  439. totalPages.value = Math.ceil(products.length / PAGE_SIZE);
  440. return products;
  441. } catch (error) {
  442. console.error("Error loading product content:", error);
  443. return [];
  444. }
  445. });
  446. // 使用缓存优化类别数据加载
  447. const { data: categoryData } = await useAsyncData("categories", async () => {
  448. try {
  449. const content = await queryCollection("content").all();
  450. return content
  451. .filter((item: any) => {
  452. const isCategory =
  453. item.path?.includes("/categories/") &&
  454. item.path?.includes(`/${locale.value}/`);
  455. return isCategory;
  456. })
  457. .map((item: any, index: number) => {
  458. const meta = item.meta || item.frontmatter || {};
  459. const pathId = item.path?.match(/\/categories\/(\d+)/)?.[1];
  460. return {
  461. id: pathId ? parseInt(pathId) : index + 1,
  462. title: meta.title || item.title || "",
  463. description: meta.description || "",
  464. image: meta.image || "",
  465. summary: meta.summary || "",
  466. capacities: meta.capacities || [],
  467. sort: meta.sort || 0,
  468. };
  469. })
  470. .sort((a, b) => a.sort - b.sort);
  471. } catch (error) {
  472. console.error("Error loading category content:", error);
  473. return [];
  474. }
  475. });
  476. // 使用计算属性优化数据过滤
  477. const filteredProductsByCategory = computed(() => {
  478. if (!selectedCategory.value) return allProducts.value;
  479. const category = allCategories.value.find(
  480. (c: Category) => c.title === selectedCategory.value
  481. );
  482. if (!category) return allProducts.value;
  483. return allProducts.value.filter((p: Product) => p.categoryId === category.id);
  484. });
  485. const filteredProductsByUsage = computed(() => {
  486. if (!selectedUsage.value) return filteredProductsByCategory.value;
  487. return filteredProductsByCategory.value.filter((p: Product) =>
  488. p.usage.includes(selectedUsage.value)
  489. );
  490. });
  491. // 分页后的产品列表
  492. const paginatedProducts = computed(() => {
  493. const start = (currentPage.value - 1) * PAGE_SIZE;
  494. const end = start + PAGE_SIZE;
  495. return filteredProductsByUsage.value.slice(start, end);
  496. });
  497. // 使用计算属性优化用途列表
  498. const uniqueUsages = computed(() => {
  499. const usageSet = new Set<string>();
  500. allProducts.value.forEach((product: Product) => {
  501. product.usage.forEach((u: string) => usageSet.add(u));
  502. });
  503. return Array.from(usageSet);
  504. });
  505. // 使用计算属性优化类别列表
  506. const categoryTitles = computed(() =>
  507. allCategories.value.map((c: Category) => c.title)
  508. );
  509. // 使用计算属性优化系列分组
  510. const productsBySeries = computed(() => {
  511. const seriesMap = new Map<string, Product[]>();
  512. filteredProducts.value.forEach((product: Product) => {
  513. if (product.series && product.series.length > 0) {
  514. const series = product.series[0]; // 取第一个系列作为分组依据
  515. if (!seriesMap.has(series)) {
  516. seriesMap.set(series, []);
  517. }
  518. seriesMap.get(series)?.push(product);
  519. }
  520. });
  521. return seriesMap;
  522. });
  523. // 处理数据变化
  524. watchEffect(() => {
  525. if (!productData.value || !categoryData.value) return;
  526. isLoading.value = true;
  527. try {
  528. allProducts.value = productData.value;
  529. allCategories.value = categoryData.value;
  530. usages.value = uniqueUsages.value;
  531. categories.value = categoryTitles.value;
  532. filteredProducts.value = paginatedProducts.value;
  533. } catch (err) {
  534. console.error("Error processing data:", err);
  535. error.value = new Error(t("products.processError"));
  536. } finally {
  537. isLoading.value = false;
  538. }
  539. });
  540. /**
  541. * 处理分类筛选
  542. */
  543. function handleCategoryFilter(category: string) {
  544. selectedCategory.value = selectedCategory.value === category ? "" : category;
  545. currentPage.value = 1; // 重置页码
  546. filteredProducts.value = paginatedProducts.value;
  547. }
  548. /**
  549. * 处理用途筛选
  550. */
  551. function handleUsageFilter(usage: string) {
  552. selectedUsage.value = selectedUsage.value === usage ? "" : usage;
  553. currentPage.value = 1; // 重置页码
  554. filteredProducts.value = paginatedProducts.value;
  555. }
  556. /**
  557. * 处理页码变化
  558. */
  559. function handlePageChange(page: number) {
  560. currentPage.value = page;
  561. filteredProducts.value = paginatedProducts.value;
  562. }
  563. // 监听分类变化
  564. watch(selectedCategory, (newValue) => {
  565. debouncedRouterPush({
  566. ...route.query,
  567. category: newValue || undefined,
  568. });
  569. });
  570. // 监听用途变化
  571. watch(selectedUsage, (newValue) => {
  572. debouncedRouterPush({
  573. ...route.query,
  574. usage: newValue || undefined,
  575. });
  576. });
  577. // 监听路由变化
  578. watch(
  579. () => route.query,
  580. (newQuery) => {
  581. selectedCategory.value = newQuery.category?.toString() || "";
  582. selectedUsage.value = newQuery.usage?.toString() || "";
  583. const page = parseInt(newQuery.page?.toString() || "1");
  584. if (page !== currentPage.value) {
  585. currentPage.value = page;
  586. }
  587. },
  588. { deep: true }
  589. );
  590. // 重置筛选条件
  591. const resetFilters = () => {
  592. selectedCategory.value = "";
  593. selectedUsage.value = "";
  594. currentPage.value = 1;
  595. debouncedRouterPush({});
  596. };
  597. // SEO优化
  598. useHead({
  599. title: "产品列表 - Hanye",
  600. meta: [
  601. {
  602. name: "description",
  603. content: "浏览我们的产品列表,找到适合您的解决方案。",
  604. },
  605. ],
  606. });
  607. const fallbackImage = "/images/placeholder.jpg";
  608. // 图片加载状态管理
  609. const loadingImages = ref<Set<number>>(new Set());
  610. const errorImages = ref<Set<number>>(new Set());
  611. // 检查图片是否正在加载
  612. const isImageLoading = (productId: number) => {
  613. return loadingImages.value.has(productId);
  614. };
  615. // 检查图片是否加载错误
  616. const isImageError = (productId: number) => {
  617. return errorImages.value.has(productId);
  618. };
  619. // 处理图片加载开始
  620. const handleImageLoadStart = (productId: number) => {
  621. loadingImages.value.add(productId);
  622. errorImages.value.delete(productId);
  623. };
  624. // 处理图片加载完成
  625. const handleImageLoad = (event: Event, product: Product) => {
  626. loadingImages.value.delete(product.id);
  627. errorImages.value.delete(product.id);
  628. };
  629. // 处理图片加载错误
  630. const handleImageError = (event: Event, product: Product) => {
  631. console.warn(`Image load failed for product: ${product.title}`);
  632. if (event.target instanceof HTMLImageElement) {
  633. loadingImages.value.delete(product.id);
  634. errorImages.value.add(product.id);
  635. }
  636. };
  637. // 清除分类筛选
  638. const clearCategory = () => {
  639. selectedCategory.value = "";
  640. currentPage.value = 1;
  641. filteredProducts.value = paginatedProducts.value;
  642. debouncedRouterPush({
  643. ...route.query,
  644. category: undefined,
  645. });
  646. };
  647. // 清除用途筛选
  648. const clearUsage = () => {
  649. selectedUsage.value = "";
  650. currentPage.value = 1;
  651. filteredProducts.value = paginatedProducts.value;
  652. debouncedRouterPush({
  653. ...route.query,
  654. usage: undefined,
  655. });
  656. };
  657. // 组件卸载时清理资源
  658. onBeforeUnmount(() => {
  659. allProducts.value = [];
  660. allCategories.value = [];
  661. filteredProducts.value = [];
  662. categories.value = [];
  663. usages.value = [];
  664. loadingImages.value.clear();
  665. errorImages.value.clear();
  666. });
  667. </script>
  668. <style scoped>
  669. .fade-enter-active,
  670. .fade-leave-active {
  671. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  672. }
  673. .fade-enter-from,
  674. .fade-leave-to {
  675. opacity: 0;
  676. transform: translateY(20px);
  677. }
  678. /* 系列标题悬停效果 */
  679. .relative:hover .bg-gradient-to-r {
  680. @apply from-blue-900/30 to-blue-500/30;
  681. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  682. }
  683. /* 产品数量标签悬停效果 */
  684. .relative:hover .bg-blue-500\/20 {
  685. @apply bg-blue-500/30;
  686. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  687. }
  688. /* 装饰线动画效果 */
  689. .relative:hover .w-1 {
  690. @apply h-10;
  691. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  692. }
  693. .products-page {
  694. min-height: 100vh;
  695. background-color: #f9fafb;
  696. }
  697. /* 添加过渡动画 */
  698. .bg-primary {
  699. @apply bg-blue-600;
  700. }
  701. button {
  702. transition: all 0.3s ease;
  703. }
  704. .rounded-full {
  705. @apply transition-all duration-300 ease-in-out;
  706. }
  707. .rounded-full:hover {
  708. @apply bg-gray-200;
  709. }
  710. /* 添加清除按钮悬停效果 */
  711. button:hover {
  712. transform: translateY(-1px);
  713. }
  714. /* 添加图片容器过渡效果 */
  715. .aspect-square {
  716. transition: all 0.3s ease;
  717. background-color: rgb(39, 39, 42); /* 添加背景色 */
  718. }
  719. .aspect-square:hover {
  720. transform: scale(1.02);
  721. }
  722. /* 加载动画优化 */
  723. .animate-spin {
  724. animation: spin 1s linear infinite;
  725. }
  726. @keyframes spin {
  727. from {
  728. transform: rotate(0deg);
  729. }
  730. to {
  731. transform: rotate(360deg);
  732. }
  733. }
  734. /* 响应式间距调整 */
  735. @media (max-width: 640px) {
  736. .gap-4 {
  737. gap: 0.75rem;
  738. }
  739. .p-4 {
  740. padding: 0.75rem;
  741. }
  742. }
  743. @media (min-width: 768px) {
  744. .gap-4 {
  745. gap: 1rem;
  746. }
  747. .p-4 {
  748. padding: 1rem;
  749. }
  750. }
  751. @media (min-width: 1024px) {
  752. .gap-4 {
  753. gap: 1.25rem;
  754. }
  755. .p-4 {
  756. padding: 1.5rem;
  757. }
  758. }
  759. /* 添加新的样式 */
  760. .line-clamp-3 {
  761. display: -webkit-box;
  762. -webkit-line-clamp: 3;
  763. -webkit-box-orient: vertical;
  764. overflow: hidden;
  765. }
  766. /* 优化过渡动画 */
  767. .group {
  768. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  769. }
  770. .group:hover {
  771. transform: translateY(-4px);
  772. }
  773. /* 图片缩放效果 */
  774. .group img {
  775. transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  776. }
  777. /* Summary 悬浮层动画 */
  778. .group .absolute {
  779. transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  780. }
  781. /* 确保文字在悬浮层上清晰可见 */
  782. .group .text-white {
  783. text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  784. }
  785. /* 响应式调整 */
  786. @media (max-width: 640px) {
  787. .group .absolute {
  788. padding: 0.75rem;
  789. }
  790. }
  791. @media (min-width: 768px) {
  792. .group .absolute {
  793. padding: 1rem;
  794. }
  795. }
  796. @media (min-width: 1024px) {
  797. .group .absolute {
  798. padding: 1.5rem;
  799. }
  800. }
  801. </style>