功能(小程序):同步用户体验流程到小程序

This commit is contained in:
xiaowang
2026-05-23 21:56:52 +08:00
parent 861eea933b
commit b67dae502f
38 changed files with 2163 additions and 126 deletions
+50 -6
View File
@@ -78,6 +78,8 @@ Page({
loading: false,
loadingMore: false,
categories: [ALL_CATEGORY],
filterGroups: [],
selectedTagId: '',
selectedCategory: ALL_CATEGORY,
searchKeyword: '',
products: [],
@@ -197,10 +199,16 @@ Page({
},
async loadTags() {
const tags = await cache.remember('tags:all', CACHE_TTL.tags, () => tagApi.getAllTags())
const names = Array.isArray(tags) ? tags.map(item => item.tagName).filter(Boolean) : []
const groups = await cache.remember('tags:filter-options', CACHE_TTL.tags, () => tagApi.getFilterOptions())
const normalizedGroups = (Array.isArray(groups) ? groups : []).map(group => ({
...group,
tags: (Array.isArray(group.tags) ? group.tags : []).map(tag => ({
...tag,
idText: String(tag.id)
}))
}))
this.setData({
categories: [ALL_CATEGORY].concat(names)
filterGroups: normalizedGroups
})
},
@@ -225,10 +233,13 @@ Page({
async applyFilter(filter) {
const keyword = String(filter.keyword || '').trim()
const tag = String(filter.tag || '').trim()
const tagId = String(filter.tagId || '').trim()
const matchedTag = this.resolveFilterTag(tagId, tag)
this.setData({
searchKeyword: keyword,
selectedCategory: tag && this.data.categories.includes(tag) ? tag : ALL_CATEGORY,
selectedTagId: matchedTag ? String(matchedTag.id) : '',
selectedCategory: matchedTag ? (matchedTag.displayName || matchedTag.tagName) : ALL_CATEGORY,
page: 1,
total: 0,
hasMore: false
@@ -246,11 +257,13 @@ Page({
try {
const keyword = String(this.data.searchKeyword || '').trim()
const tag = this.data.selectedCategory === ALL_CATEGORY ? '' : this.data.selectedCategory
const tagIds = this.data.selectedTagId || ''
const result = await productApi.getProductPage({
page: nextPage,
size: this.data.pageSize,
keyword: keyword || undefined,
tag: tag || undefined
tag: tagIds ? undefined : (tag || undefined),
tagIds: tagIds || undefined
})
const cards = await buildProductCards(result && result.list ? result.list : [])
const displayCards = await this.attachCartState(cards)
@@ -333,6 +346,33 @@ Page({
return chips.join(' \u00b7 ')
},
/**
* Purpose: flattenFilterTags returns all scene-filter tags for query matching.
* Usage: call before applying route or pending filters.
*/
flattenFilterTags() {
return (Array.isArray(this.data.filterGroups) ? this.data.filterGroups : [])
.reduce((list, group) => list.concat(Array.isArray(group.tags) ? group.tags : []), [])
},
/**
* Purpose: resolveFilterTag matches route filter text or id to a visible scene tag.
* Usage: call from applyFilter so old tag query links still select the right chip.
*/
resolveFilterTag(tagId, tagName) {
const tags = this.flattenFilterTags()
if (tagId) {
const byId = tags.find(item => String(item.id) === tagId)
if (byId) {
return byId
}
}
if (!tagName) {
return null
}
return tags.find(item => item.tagName === tagName || item.displayName === tagName) || null
},
/**
* `normalizeId` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
@@ -447,6 +487,7 @@ Page({
await actionGuard.run(this, 'products:search', async () => {
this.setData({
selectedCategory: ALL_CATEGORY,
selectedTagId: '',
page: 1
})
await runWithLoading(() => this.loadProducts(true))
@@ -466,11 +507,13 @@ Page({
async handleSelectCategory(event) {
const category = event.currentTarget.dataset.category
const tagId = event.currentTarget.dataset.tagId ? String(event.currentTarget.dataset.tagId) : ''
resetKeyboardSafe(this)
try {
await actionGuard.run(this, `products:category:${category}`, async () => {
await actionGuard.run(this, `products:category:${category}:${tagId}`, async () => {
this.setData({
selectedCategory: category,
selectedTagId: tagId,
searchKeyword: '',
page: 1
})
@@ -495,6 +538,7 @@ Page({
await actionGuard.run(this, 'products:reset', async () => {
this.setData({
selectedCategory: ALL_CATEGORY,
selectedTagId: '',
searchKeyword: '',
page: 1
})
+18 -5
View File
@@ -25,13 +25,26 @@
<scroll-view class="category-scroll" scroll-x enable-flex show-scrollbar="false">
<view class="category-list">
<view
wx:for="{{categories}}"
wx:key="*this"
class="category-item {{selectedCategory === item ? 'active' : ''}}"
data-category="{{item}}"
class="category-item {{selectedCategory === '全部' ? 'active' : ''}}"
data-category="全部"
data-tag-id=""
bindtap="handleSelectCategory"
>
{{item}}
全部
</view>
<view wx:for="{{filterGroups}}" wx:for-item="group" wx:key="groupName" class="filter-group">
<view class="filter-group-title">{{group.groupName}}</view>
<view
wx:for="{{group.tags}}"
wx:for-item="tag"
wx:key="id"
class="category-item {{selectedTagId === tag.idText ? 'active' : ''}}"
data-category="{{tag.displayName || tag.tagName}}"
data-tag-id="{{tag.idText}}"
bindtap="handleSelectCategory"
>
{{tag.displayName || tag.tagName}}
</view>
</view>
</view>
</scroll-view>
+13
View File
@@ -83,6 +83,19 @@
font-weight: 600;
}
.filter-group {
display: inline-flex;
align-items: center;
gap: 12rpx;
flex-shrink: 0;
}
.filter-group-title {
color: #a4939c;
font-size: 22rpx;
font-weight: 700;
}
.category-item.active {
background: var(--theme-primary-soft);
color: var(--theme-primary);