sanmu
authored
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<template>
<v-container>
<div class="tw-border tw-border-solid tw-border-[#1f88e5]">
<v-row
class="ma-0 pl-4 bg-grey-lighten-3 tw-border-0 tw-border-b tw-border-solid tw-border-[#1f88e5] md:tw-leading-[64px]"
>
<div
class="tw-pr-0 tw-font-bold tw-w-[160px] tw-h-[36px] tw-leading-[64px] text-grey-darken-3"
>
CATEGORY:
</div>
<v-col class="flex pa-0" cols="12" sm="9">
<span
|
|
14
15
|
:class="
'tw-leading-[50px] tw-inline-flex tw-cursor-pointer px-4 mb-1 mr-1 tw-font-medium rounded hover:tw-text-[#fff] hover:tw-bg-[#1e88e5] ' +
|
sanmu
authored
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
(categoryStore.selectedCategory === item.categoryDisplayName &&
'tw-text-[#fff] tw-bg-[#1e88e5]')
"
v-for="(item, index) in categoryStore.list"
:key="index"
@click="handleCategoryClick(item)"
>
<b class="tw-m-0 tw-inline">{{ item.categoryDisplayName }}</b>
</span>
</v-col>
</v-row>
<v-row class="pa-4 ma-0 bg-grey-lighten-4">
<div
class="tw-pr-0 tw-font-bold tw-w-[130px] tw-h-[36px] tw-leading-[36px] text-grey-darken-3"
>
DEVICE TYPE:
</div>
<v-col class="pa-0" cols="12" sm="9">
<span
|
|
35
36
37
38
|
:class="
'px-4 py-1 mb-1 mr-1 tw-font-medium rounded tw-inline-flex tw-cursor-pointer hover:tw-text-[#fff] hover:tw-bg-[#1e88e5] ' +
(categoryStore.selectedSubCategory === item.id &&
'tw-text-[#fff] tw-bg-[#1e88e5]')
|
sanmu
authored
|
39
40
41
|
"
v-for="(item, index) in subCategoryList"
:key="index"
|
|
42
|
@click="handleSubCategoryClick(item)"
|
sanmu
authored
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
>
{{ item.name }}
</span>
</v-col>
</v-row>
<v-row
v-if="funcCategoryList.length"
class="pa-4 ma-0 bg-grey-lighten-4 tw-border-0 tw-border-t tw-border-dashed tw-border-[rgb(178, 178, 178)]"
>
<div
class="tw-pr-0 tw-font-bold tw-w-[210px] tw-h-[36px] tw-leading-[36px] text-grey-darken-3"
>
MATERIAL FUNCTION:
</div>
<v-col class="pa-0" cols="12" sm="9">
<span
|
|
59
60
61
62
|
:class="
'px-4 py-1 mb-1 mr-1 tw-font-medium rounded tw-inline-flex tw-cursor-pointer hover:tw-text-[#fff] hover:tw-bg-[#1e88e5] ' +
(categoryStore.selectedFuncCategory === item.id &&
'tw-text-[#fff] tw-bg-[#1e88e5]')
|
sanmu
authored
|
63
64
65
|
"
v-for="(item, index) in funcCategoryList"
:key="index"
|
|
66
|
@click="handleFuncCategoryClick(item)"
|
sanmu
authored
|
67
68
69
70
71
72
73
74
75
76
|
>
{{ item.name }}
</span>
</v-col>
</v-row>
</div>
</v-container>
</template>
<script setup lang="ts">
|
|
77
78
79
80
81
82
83
84
85
86
87
88
|
import { useCategoryStore } from "@/stores/category";
import { useProductListStore } from "@/stores/product_list";
import type { CategoryRootType } from "@/type";
import { computed } from "vue";
import { useRouter, useRoute } from "vue-router";
const route = useRoute();
const router = useRouter();
const categoryStore = useCategoryStore();
const productStore = useProductListStore();
watchEffect(async () => {
|
|
89
|
if (route.query.categories) {
|
|
90
|
// 1. 提取 query.category 的内容
|
|
91
|
productStore.updateKeyword("");
|
|
92
|
const categories = route.query.categories.split(",");
|
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
const mainCategory = categories[0].trim(); // 取第一个值
const subCategoryName = categories[1] ? categories[1].trim() : null; // 取第二个值(如果存在)
// 2. 更新选中的主分类
categoryStore.updateCategory(mainCategory);
// 3. 如果有子分类名称,查找其对应的 ID
if (subCategoryName) {
const subCategoryList = computed(() => {
if (categoryStore.selectedCategory) {
const tmp = categoryStore.list.filter(
(item) =>
item.categoryDisplayName === categoryStore.selectedCategory
);
return tmp?.[0]?.list || [];
}
return [];
});
|
sanmu
authored
|
111
|
|
|
112
113
114
115
|
// 4. 查找对应的子分类 ID
const foundFuncCategory = subCategoryList.value.find(
(func) => func.name === subCategoryName
);
|
sanmu
authored
|
116
|
|
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
if (foundFuncCategory) {
const funcCategoryId = foundFuncCategory.id;
// 你可以在这里使用找到的 funcCategoryId
categoryStore.updateSubCategory(funcCategoryId);
}
}
// 5. 判断 query 中是否存在 function,并查找对应的 ID
if (route.query.function) {
const functionName = route.query.function.trim();
const funcCategoryList = computed(() => {
if (categoryStore.selectedCategory) {
const tmp = categoryStore.list.filter(
(item) =>
item.categoryDisplayName === categoryStore.selectedCategory
);
return tmp?.[0]?.productFunctions || [];
}
return [];
});
const foundFuncCategory = funcCategoryList.value.find(
(func) => func.name === functionName
);
|
sanmu
authored
|
139
|
|
|
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
if (foundFuncCategory) {
const funcCategoryId = foundFuncCategory.id;
// 你可以在这里使用找到的 funcCategoryId
categoryStore.updateFuncCategory(funcCategoryId);
}
// // 6. 查找对应的功能分类 ID
// const foundFuncCategory = funcCategories.find(
// (func) => func.name === functionName
// );
// if (foundFuncCategory) {
// const funcCategoryId = foundFuncCategory.id;
// // 使用找到的 funcCategoryId
// categoryStore.updateFuncCategory(funcCategoryId);
// }
}
|
|
156
|
} else if (Object.keys(route.query).length === 0) {
|
|
157
158
|
// 检查是否有默认的分类
const defaultCategory = categoryStore.list[0]; // 假设第一个分类是默认的
|
|
159
|
|
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
if (defaultCategory) {
const defaultCategoryName = defaultCategory.categoryDisplayName;
const defaultSubCategory = defaultCategory.list[0]; // 假设第一个子分类为默认子分类
const defaultFuncCategory = defaultCategory.productFunctions[0]; // 假设第一个功能分类为默认功能分类
// 更新 store 和 URL
categoryStore.updateCategory(defaultCategoryName);
productStore.updatePageNo(1);
if (defaultSubCategory) {
categoryStore.updateSubCategory(defaultSubCategory.id);
// 如果有之前的值则使用之前的值,拼接新的子分类名
const updatedCategory =
defaultCategoryName + "," + defaultSubCategory.name;
// 拼接设备类型到 URL
router.push({
query: {
|
|
179
|
categories: defaultCategoryName + "," + defaultSubCategory.name,
|
|
180
181
182
183
184
185
186
187
188
|
},
});
}
if (defaultFuncCategory) {
categoryStore.updateFuncCategory(defaultFuncCategory.id);
// 拼接功能类型到 URL
router.push({
query: {
|
|
189
|
categories: defaultCategoryName + "," + defaultSubCategory.name,
|
|
190
191
192
193
194
195
196
|
function: defaultFuncCategory.name,
},
});
}
}
}
});
|
|
197
198
199
200
201
202
203
204
205
206
|
const seo = {
"Energy materials":
"Energy materials,Not specified,Battery accessories,Lithium-ion batteries,Capacitors,Sodium-ion batteries,Lithium-sulfur batteries,Potassium/magnesium/aluminum/zinc batteries,Air/fuel/solar,Analytical electrodes,Complete battery accessories",
"Laboratory consumables":
"Laboratory consumables,Not specified,Glass materials,Plastic materials,Metal materials,Ceramic materials,Paper film materials,Chemical materials,Tetrafluoro materials,Safety protection,Office supplies,Tools,Others",
"Low-dimensional materials":
",Low-dimensional materialsNot specified,Zero-dimensional carbon materials,One-dimensional carbon materials,Two-dimensional carbon materials,Three-dimensional carbon materials,Inorganic nanomaterials,Organic nanomaterials,Metal nanomaterials,Others",
Equipment:
"Equipment,Not specified,Equipment,Accessories & fixtures,Fuel cell manufacturing and testing equipment",
};
|
sanmu
authored
|
207
208
|
const handleCategoryClick = (item: CategoryRootType) => {
|
|
209
210
211
212
|
categoryStore.updateCategory(item.categoryDisplayName);
categoryStore.updateSubCategory(item.list[0].id);
productStore.updatePageNo(1);
const defaultSubCategory = item.list[0]; // 假设第一个子分类为默认子分类
|
sanmu
authored
|
213
|
|
|
214
215
216
|
if (item.categoryDisplayName === "Energy materials") {
router.push({
query: {
|
|
217
|
categories: item.categoryDisplayName + "," + defaultSubCategory.name,
|
|
218
219
220
221
222
223
|
function: item.productFunctions[0].name,
},
});
} else {
router.push({
query: {
|
|
224
|
categories: item.categoryDisplayName + "," + defaultSubCategory.name,
|
|
225
226
227
|
},
});
}
|
sanmu
authored
|
228
229
230
231
232
233
234
235
236
|
// const doc = document as any
// const head = doc.getElementsByTagName('head')
// const meta = doc.createElement('meta')
// document.title = seo[item.categoryDisplayName as keyof typeof seo]
// doc
// .querySelector('meta[name="keywords"]')
// .setAttribute('content', seo[item.categoryDisplayName as keyof typeof seo])
// head[0].appendChild(meta)
|
|
237
238
239
240
241
242
243
244
|
};
const handleSubCategoryClick = (value: CategoryRootType) => {
categoryStore.updateSubCategory(value.id);
productStore.updatePageNo(1);
// 获取当前的查询参数
const currentQuery = router.currentRoute.value.query;
|
|
245
|
const currentCategory = currentQuery.categories || "";
|
|
246
247
248
249
250
251
252
253
|
// 如果有之前的值则使用之前的值,拼接新的子分类名
const updatedCategory = currentCategory.split(",")[0] + "," + value.name;
// 更新路由,保持 handleCategoryClick 的值不变
// router.push({ query: { category: updatedCategory } });
// 更新路由,保持 function 参数不变
router.push({
|
|
254
|
query: { categories: updatedCategory, function: currentQuery.function },
|
|
255
256
257
258
259
260
261
262
|
});
};
const handleFuncCategoryClick = (value: CategoryRootType) => {
categoryStore.updateFuncCategory(value.id);
productStore.updatePageNo(1);
// 获取当前的查询参数
const currentQuery = router.currentRoute.value.query;
|
sanmu
authored
|
263
|
|
|
264
265
266
267
268
|
// 将 value.name 作为新的查询参数加入到现有的 query 中
const updatedQuery = {
...currentQuery, // 保持当前的查询参数
function: value.name, // 将 value.name 添加为新的查询参数 funcCategory
};
|
sanmu
authored
|
269
|
|
|
270
271
272
|
// 更新路由
router.push({ query: updatedQuery });
};
|
sanmu
authored
|
273
274
275
276
277
|
const subCategoryList = computed(() => {
if (categoryStore.selectedCategory) {
const tmp = categoryStore.list.filter(
(item) => item.categoryDisplayName === categoryStore.selectedCategory
|
|
278
279
|
);
return tmp?.[0]?.list || [];
|
sanmu
authored
|
280
|
}
|
|
281
282
|
return [];
});
|
sanmu
authored
|
283
284
285
286
287
|
const funcCategoryList = computed(() => {
if (categoryStore.selectedCategory) {
const tmp = categoryStore.list.filter(
(item) => item.categoryDisplayName === categoryStore.selectedCategory
|
|
288
289
|
);
return tmp?.[0]?.productFunctions || [];
|
sanmu
authored
|
290
|
}
|
|
291
292
|
return [];
});
|
sanmu
authored
|
293
|
</script>
|