-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
297 lines (266 loc) · 13.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PCBUYBUY</title>
<!-- 引入 Element Plus -->
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/element-plus/dist/index.css"
/>
<!-- Import Vue 3 -->
<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<!-- Import component library -->
<script src="//cdn.jsdelivr.net/npm/element-plus"></script>
<script src="//cdn.jsdelivr.net/npm/@element-plus/icons-vue"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="app">
<div id="loading">
<div v-if="error" class="alert alert-danger" role="alert">
無法載入資料,請稍後再試
<br>
<small class="text-muted">{{ error }}</small>
</div>
<div v-if="loading" class="loading-container">
<div class="loading-spinner"></div>
<div>{{loadingStatus}}</div>
</div>
<div v-if="componentsData">
<div v-if="updateTime" class="update-time">更新時間: {{ updateTime }}</div>
<el-row v-for="component in componentsData"
:key="component.title"
class="component-row">
<el-col :xs="4" :sm="6" class="component-title">
<span>{{ component.title }}</span>
</el-col>
<el-col :xs="20" :sm="18">
<div class="component-container" v-for="(selection, index) in getComponentSelections(component.title)" :key="index">
<el-select
v-model="selection.value"
:placeholder="'請選擇' + component.title"
filterable
class="component-select"
clearable
value-key="id"
@change="() => handleSelectionChange(component.title, index)">
<template #default="{ item }">
<el-option
v-for="item in component.data"
:key="item.id"
:label="item.name"
:value="item"
:disabled="item.price === null || item.isGroup"
:class="{
'bundle-price': item.name.includes('搭購價'),
'option-group': item.isGroup,
[`color-group-${item.colorGroup}`]: item.colorGroup > 0
}">
<div style="display: flex; justify-content: space-between; align-items: center;">
<span>{{ item.name }}</span>
<span v-if="item.price" style="color: #67C23A; font-weight: bold;">
${{ item.price.toLocaleString() }}
</span>
</div>
</el-option>
</template>
</el-select>
<div class="price-display" v-if="selection.value && selection.value.price">
${{ selection.value.price.toLocaleString() }}
</div>
<div class="selection-controls">
<el-icon
@click="addSelection(component.title)"
v-if="index === getComponentSelections(component.title).length - 1"
color="#67C23A"
>
<Plus />
</el-icon>
<el-icon
@click="removeSelection(component.title, index)"
v-if="getComponentSelections(component.title).length > 1"
color="red"
>
<Minus />
</el-icon>
</div>
</div>
<div class="total-price-display" v-if="calculateComponentTotal(component.title) > 0">
總額: ${{ calculateComponentTotal(component.title).toLocaleString() }}
</div>
</el-col>
</el-row>
<!-- 新增總金額顯示 -->
<el-row class="grand-total-row" v-if="calculateGrandTotal() > 0">
<el-col :span="24">
<div class="grand-total-display">
總金額: ${{ calculateGrandTotal().toLocaleString() }}
</div>
</el-col>
</el-row>
</div>
</div>
</div>
<script>
const { createApp, ref, onMounted} = Vue;
const app = createApp({
setup() {
const API_URL = "https://pcbuybuy.aliceric27.workers.dev/coolpc";
const componentsData = ref(null);
const error = ref(null);
const loading = ref(true);
const selectedComponents = ref({});
const updateTime = ref(null);
const processComponentData = (html, selector, componentType) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const select = doc.querySelector(selector);
const data = [];
let currentColorGroup = 0;
if (!select) {
console.error(`找不到 ${componentType} 的選擇器`);
return null;
}
// 處理所有子元素
select.childNodes.forEach(node => {
if (node.nodeName === 'OPTGROUP') {
// 更新顏色組
currentColorGroup = (currentColorGroup % 4) + 1;
// 添加組標題
data.push({
id: `group_${node.label}`,
name: node.label,
price: null,
isGroup: true,
colorGroup: currentColorGroup
});
// 處理組內的選項
node.querySelectorAll('option').forEach(option => {
const value = option.value;
if (value === '0' || !value) return;
const text = option.textContent;
const priceMatch = text.match(/\$([0-9,]+)/);
const price = priceMatch ? parseInt(priceMatch[1].replace(',', '')) : null;
data.push({
id: value,
name: text.split(',')[0].trim(),
price: price,
isGroup: false,
colorGroup: currentColorGroup
});
});
} else if (node.nodeName === 'OPTION') {
const value = node.value;
if (value === '0' || !value) return;
const text = node.textContent;
const priceMatch = text.match(/\$([0-9,]+)/);
const price = priceMatch ? parseInt(priceMatch[1].replace(',', '')) : null;
data.push({
id: value,
name: text.split(',')[0].trim(),
price: price,
isGroup: false,
colorGroup: 0 // 非組內選項不設置顏色
});
}
});
return { [componentType]: data };
};
const fetchData = async () => {
try {
loading.value = true;
error.value = null;
const response = await fetch(API_URL);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const html = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const categories = doc.querySelectorAll('.t');
console.log(doc.querySelector('#Mdy').innerText);
const updateTimeText = doc.querySelector('#Mdy').innerText;
const dateTimeMatch = updateTimeText.match(/(\d{4})\/(\d{1,2})\/(\d{1,2}) (\d{1,2}):(\d{2})/);
updateTime.value = dateTimeMatch ?
`${dateTimeMatch[1]}/${String(dateTimeMatch[2]).padStart(2, '0')}/${String(dateTimeMatch[3]).padStart(2, '0')} ${dateTimeMatch[4]}:${dateTimeMatch[5]}` :
'更新時間格式錯誤';
const allData = [];
for (let i = 0; i < categories.length; i++) {
const componentType = categories[i].textContent.trim();
const selector = `#tbdy > tr:nth-child(${i+1}) > td:nth-child(3) > select`;
const componentData = processComponentData(html, selector, componentType);
if(componentData!==null){
allData.push({
title: componentType,
data: componentData[componentType]
});
}
}
componentsData.value = allData;
} catch (err) {
error.value = err.message;
console.error('Error fetching data:', err);
} finally {
loading.value = false;
}
};
onMounted(() => {
fetchData();
});
// 獲取組件的所有選擇
const getComponentSelections = (componentTitle) => {
if (!selectedComponents.value[componentTitle]) {
selectedComponents.value[componentTitle] = [{value: null}];
}
return selectedComponents.value[componentTitle];
};
// 添加新的選擇欄位
const addSelection = (componentTitle) => {
selectedComponents.value[componentTitle].push({value: null});
};
// 移除選擇欄位
const removeSelection = (componentTitle, index) => {
selectedComponents.value[componentTitle].splice(index, 1);
};
// 處理選擇變更
const handleSelectionChange = (componentTitle, index) => {
// 可以在這裡添加額外的邏輯
};
// 計算單個組件的總金額
const calculateComponentTotal = (componentTitle) => {
const selections = selectedComponents.value[componentTitle] || [];
return selections.reduce((total, selection) => {
return total + (selection.value?.price || 0);
}, 0);
};
// 計算所有組件的總金額
const calculateGrandTotal = () => {
if (!componentsData.value) return 0;
return componentsData.value.reduce((total, component) => {
return total + calculateComponentTotal(component.title);
}, 0);
};
return {
componentsData,
error,
loading,
selectedComponents,
updateTime,
getComponentSelections,
addSelection,
removeSelection,
handleSelectionChange,
calculateComponentTotal,
calculateGrandTotal
};
}
});
// 使用 Element Plus
app.use(ElementPlus);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.mount('#app');
</script>
</body>
</html>