index.html
17.4 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Search - for Shoplazza</title>
<link rel="stylesheet" href="/static/css/style.css">
<style>
.suggestions-list {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
max-height: 300px;
overflow-y: auto;
z-index: 1000;
margin-top: 2px;
}
.suggestion-item {
padding: 10px 15px;
cursor: pointer;
border-bottom: 1px solid #f0f0f0;
transition: background-color 0.2s;
}
.suggestion-item:hover,
.suggestion-item.highlighted {
background-color: #f5f5f5;
}
.suggestion-item:last-child {
border-bottom: none;
}
.suggestion-text {
font-size: 14px;
color: #333;
}
.suggestion-meta {
font-size: 12px;
color: #999;
margin-top: 2px;
}
</style>
</head>
<body>
<div class="page-container">
<!-- Header -->
<header class="top-header">
<div class="header-left">
<span class="logo">Product</span>
<span class="product-count" id="productCount">0 products found</span>
</div>
<div class="header-right">
</div>
</header>
<!-- Search Bar -->
<div class="search-bar">
<div class="tenant-input-wrapper">
<label for="langSelect">lang:</label>
<select id="langSelect">
<option value="zh" selected>zh</option>
<option value="en">en</option>
<option value="ru">ru</option>
<option value="es">es</option>
<option value="fr">fr</option>
<option value="de">de</option>
<option value="it">it</option>
<option value="ja">ja</option>
</select>
</div>
<div class="tenant-input-wrapper">
<label for="tenantSelect">tenant ID:</label>
<input type="text" id="tenantSelect" list="tenantList" placeholder="选择或输入 tenant ID" onchange="onTenantIdChange()">
<datalist id="tenantList">
<!-- 选项将通过 JavaScript 动态填充 -->
</datalist>
</div>
<div class="tenant-input-wrapper">
<label for="skuFilterDimension">sku_filter_dimension:</label>
<input type="text" id="skuFilterDimension" placeholder="SKU筛选维度" value="color">
</div>
<div class="search-input-wrapper" style="position: relative;">
<input type="text" id="searchInput" placeholder="输入搜索关键词... (支持中文、英文、俄文)"
onkeypress="handleKeyPress(event)" oninput="handleSearchInput(event)">
<div id="suggestionsList" class="suggestions-list" style="display: none;"></div>
</div>
<button onclick="performSearch()" class="search-btn">Search</button>
</div>
<!-- Filter Section -->
<div class="filter-section" id="filterSection">
<!-- 分面面板将通过 JavaScript 动态生成 -->
<div id="facetsContainer">
<!-- Category Filter (一级分类) - 固定显示 -->
<div class="filter-row" data-facet-field="category1_name">
<div class="filter-label">Category:</div>
<div class="filter-tags" id="category1Tags"></div>
</div>
<!-- Sub Category Filter (二级分类) - 固定显示 -->
<div class="filter-row" data-facet-field="category2_name">
<div class="filter-label">Sub Category:</div>
<div class="filter-tags" id="category2Tags"></div>
</div>
<!-- Third Category Filter (三级分类) - 固定显示 -->
<div class="filter-row" data-facet-field="category3_name">
<div class="filter-label">Third Category:</div>
<div class="filter-tags" id="category3Tags"></div>
</div>
<!-- 规格分面将通过 JavaScript 动态添加到这里 -->
<div id="specificationFacetsContainer"></div>
</div>
<!-- Dropdown Filters -->
<div class="filter-row">
<div class="filter-label">Others:</div>
<div class="filter-dropdowns">
<select id="priceFilter" onchange="handlePriceFilter(this.value)">
<option value="">Price</option>
<option value="0-50">0-50</option>
<option value="50-100">50-100</option>
<option value="100-200">100-200</option>
<option value="200+">200+</option>
</select>
<select id="timeFilter" onchange="handleTimeFilter(this.value)">
<option value="">Listing Time</option>
<option value="today">Today</option>
<option value="week">This Week</option>
<option value="month">This Month</option>
<option value="3months">Last 3 Months</option>
<option value="6months">Last 6 Months</option>
</select>
<button class="clear-filters-btn" onclick="clearAllFilters()" style="display: none;" id="clearFiltersBtn">Clear Filters</button>
</div>
</div>
</div>
<!-- Sort Section -->
<div class="sort-section">
<button class="sort-btn active" data-sort="" onclick="setSortByDefault()">By default</button>
<button class="sort-btn" data-sort="create_time">
By New Products
<span class="sort-arrows">
<span class="arrow-up" data-field="create_time" data-order="desc" onclick="sortByField('create_time', 'desc')">▲</span>
<span class="arrow-down" data-field="create_time" data-order="asc" onclick="sortByField('create_time', 'asc')">▼</span>
</span>
</button>
<button class="sort-btn" data-sort="price">
By Price
<span class="sort-arrows">
<span class="arrow-up" data-field="price" data-order="asc" onclick="sortByField('price', 'asc')">▲</span>
<span class="arrow-down" data-field="price" data-order="desc" onclick="sortByField('price', 'desc')">▼</span>
</span>
</button>
<button class="sort-btn" data-sort="sales">
By Sales
<span class="sort-arrows">
<span class="arrow-up" data-field="sales" data-order="desc" onclick="sortByField('sales', 'desc')">▲</span>
<span class="arrow-down" data-field="sales" data-order="asc" onclick="sortByField('sales', 'asc')">▼</span>
</span>
</button>
<div class="sort-right">
<select id="resultSize" onchange="performSearch()">
<option value="20">20 per page</option>
<option value="50" selected>50 per page</option>
<option value="100">100 per page</option>
<option value="200">200 per page</option>
</select>
</div>
</div>
<!-- Loading -->
<div id="loading" class="loading" style="display: none;">
<div class="spinner"></div>
<p>Loading...</p>
</div>
<!-- Product Grid -->
<div class="product-grid" id="productGrid"></div>
<!-- Pagination -->
<div class="pagination" id="pagination" style="display: none;"></div>
<!-- Debug Info -->
<details class="debug-info">
<summary>调试信息</summary>
<div id="debugInfo"></div>
</details>
</div>
<footer>
<p>saas-search © 2025 | API: <span id="apiUrl">Loading...</span></p>
</footer>
<script src="/static/js/tenant_facets_config.js?v=1.4"></script>
<script src="/static/js/app.js?v=1.0"></script>
<script>
// 自动补全功能(使用后端 /search/suggestions 接口)
const SUGGEST_API = API_BASE_URL + '/search/suggestions';
const LANG_OPTIONS = ['zh', 'en', 'ru', 'es', 'fr', 'de', 'it', 'ja'];
let debounceTimer = null;
let currentSuggestions = [];
let selectedIndex = -1;
let abortController = null;
// 读取当前语言,默认 zh
function getSelectedLang() {
const langSelect = document.getElementById('langSelect');
if (!langSelect) return 'zh';
return langSelect.value || 'zh';
}
// 初始化语言下拉,尝试使用浏览器语言(匹配到列表才生效)
(function initLangSelect() {
const langSelect = document.getElementById('langSelect');
if (!langSelect) return;
const browserLang = (navigator.language || '').slice(0, 2).toLowerCase();
if (LANG_OPTIONS.includes(browserLang)) {
langSelect.value = browserLang;
} else {
langSelect.value = 'zh';
}
})();
// 防抖函数
function debounce(func, wait) {
return function(...args) {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(this, args), wait);
};
}
// 获取映射后的 tenant_id(旧外部 suggest API 使用,当前后端自有 suggest 已不再需要)
function getMappedTenantIdForSuggest(tenantId) {
if (!tenantId) {
return null;
}
// 使用 tenant_facets_config.js 中的映射函数(如果已加载)
if (typeof getMappedTenantId === 'function') {
return getMappedTenantId(tenantId);
}
// 如果函数未加载,使用内联映射逻辑
const tenantIdStr = String(tenantId);
const mapping = {
"170": "170",
"171": "170",
"162": "0"
};
return mapping[tenantIdStr] !== undefined ? mapping[tenantIdStr] : tenantIdStr;
}
// 获取建议
async function fetchSuggestions(query) {
if (!query || query.trim().length === 0) {
hideSuggestions();
return;
}
// 取消之前的请求
if (abortController) {
abortController.abort();
}
abortController = new AbortController();
try {
// 获取当前 tenant_id(搜索后端使用真实 tenant_id)
let tenantId = null;
if (typeof getTenantId === 'function') {
tenantId = getTenantId();
}
// 若未选择租户,则不发起后端请求,避免 400
if (!tenantId) {
console.warn('No tenant ID selected, skip suggestion request');
hideSuggestions();
return;
}
// SUGGEST_API 可能是相对路径(例如 "/search/suggestions"),
// 直接 new URL(SUGGEST_API) 在浏览器环境下会因缺少 base URL 报错。
// 显式指定 window.location.origin 作为 base,保证同源调用正常工作。
const url = new URL(SUGGEST_API, window.location.origin);
url.searchParams.set('q', query);
url.searchParams.set('size', '40');
url.searchParams.set('language', getSelectedLang());
// 同时通过 query 参数传 tenant_id,方便在代理层丢失 header 时仍能识别租户
url.searchParams.set('tenant_id', tenantId);
const headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': window.location.origin + '/'
};
headers['X-Tenant-ID'] = tenantId;
const response = await fetch(url.toString(), {
signal: abortController.signal,
headers: headers
});
if (!response.ok) {
throw new Error('请求失败');
}
const data = await response.json();
if (Array.isArray(data.suggestions) && data.suggestions.length > 0) {
currentSuggestions = data.suggestions;
showSuggestions(data.suggestions);
} else {
hideSuggestions();
}
} catch (error) {
if (error.name !== 'AbortError') {
console.error('获取建议失败:', error);
hideSuggestions();
}
}
}
// 显示建议列表
function showSuggestions(suggestions) {
const list = document.getElementById('suggestionsList');
if (!suggestions || suggestions.length === 0) {
hideSuggestions();
return;
}
list.innerHTML = '';
suggestions.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'suggestion-item';
div.dataset.index = index;
const text = item.text || '';
const lang = item.lang || '';
const score = typeof item.rank_score === 'number'
? item.rank_score.toFixed(2)
: (typeof item.score === 'number' ? item.score.toFixed(2) : '');
div.innerHTML = `
<div class="suggestion-text">${escapeHtml(text)}</div>
<div class="suggestion-meta">${escapeHtml(lang)}${score ? ' | ' + score : ''}</div>
`;
div.onclick = () => selectSuggestion(text);
div.onmouseenter = () => {
selectedIndex = index;
updateHighlight();
};
list.appendChild(div);
});
list.style.display = 'block';
selectedIndex = -1;
}
// 隐藏建议列表
function hideSuggestions() {
const list = document.getElementById('suggestionsList');
list.style.display = 'none';
currentSuggestions = [];
selectedIndex = -1;
}
// 选择建议
function selectSuggestion(text) {
const input = document.getElementById('searchInput');
input.value = text;
hideSuggestions();
performSearch();
}
// 更新高亮
function updateHighlight() {
const items = document.querySelectorAll('.suggestion-item');
items.forEach((item, index) => {
if (index === selectedIndex) {
item.classList.add('highlighted');
} else {
item.classList.remove('highlighted');
}
});
}
// HTML转义
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 处理输入事件
const debouncedFetch = debounce(fetchSuggestions, 300);
function handleSearchInput(event) {
const query = event.target.value;
debouncedFetch(query);
}
// 键盘导航
document.addEventListener('keydown', (e) => {
const list = document.getElementById('suggestionsList');
if (list.style.display === 'none' || currentSuggestions.length === 0) {
return;
}
if (e.key === 'ArrowDown') {
e.preventDefault();
selectedIndex = (selectedIndex + 1) % currentSuggestions.length;
updateHighlight();
} else if (e.key === 'ArrowUp') {
e.preventDefault();
selectedIndex = selectedIndex <= 0 ? currentSuggestions.length - 1 : selectedIndex - 1;
updateHighlight();
} else if (e.key === 'Enter' && selectedIndex >= 0) {
e.preventDefault();
selectSuggestion(currentSuggestions[selectedIndex].text);
} else if (e.key === 'Escape') {
hideSuggestions();
}
});
// 点击外部关闭建议列表
document.addEventListener('click', (e) => {
const list = document.getElementById('suggestionsList');
const input = document.getElementById('searchInput');
if (!list.contains(e.target) && e.target !== input) {
hideSuggestions();
}
});
</script>
</body>
</html>