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 36KB

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