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

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