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

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