重构项目文件夹目录

This commit is contained in:
2026-06-15 15:53:55 +08:00
parent 8093e07190
commit 7cbe5b3de5
1098 changed files with 34 additions and 4441 deletions
+329
View File
@@ -0,0 +1,329 @@
/**
* Purpose: wishlistApi supports index.js by holding or executing the wishlist api page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const wishlistApi = require('../../api/wishlist')
/**
* Purpose: cartApi supports index.js by holding or executing the cart api page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const cartApi = require('../../api/cart')
/**
* Purpose: actionGuard supports index.js by holding or executing the action guard page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const actionGuard = require('../../utils/action-guard')
/**
* Purpose: auth supports index.js by holding or executing the auth page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const auth = require('../../utils/auth')
/**
* Purpose: navigation supports index.js by holding or executing the navigation page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const navigation = require('../../utils/navigation')
/**
* Purpose: theme supports index.js by holding or executing the theme page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const theme = require('../../utils/theme')
const { buildProductCard } = require('../../utils/product')
const { runWithLoading } = require('../../utils/loading')
/**
* wishlist page logic.
* Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks.
*/
Page({
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
loading: false,
isLoggedIn: false,
wishlist: [],
visibleWishlist: [],
groups: [],
activeGroupId: ''
}),
/**
* `onShow` 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.
*/
onShow() {
this.loadWishlist()
},
async loadWishlist() {
const isLoggedIn = auth.isLoggedIn()
this.setData({
isLoggedIn,
loading: isLoggedIn
})
/**
* `if` 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.
*/
if (!isLoggedIn) {
this.cartProductIdSet = new Set()
this.setData({
wishlist: [],
visibleWishlist: [],
groups: [],
activeGroupId: '',
loading: false
})
return
}
try {
const [wishlist, groups] = await Promise.all([
wishlistApi.getWishlist(),
wishlistApi.getGroups()
])
const list = Array.isArray(wishlist) ? wishlist : []
const groupList = Array.isArray(groups) ? groups : []
const cartProductIdSet = await this.fetchCartProductIdSet()
const cards = await Promise.all(list.map(async item => ({
id: item.id,
productId: item.productId,
groupId: this.normalizeId(item.groupId),
groupName: item.groupName || '\u6700\u8fd1\u60f3\u505a',
note: item.note || '',
productCard: this.markCardInCart(
await buildProductCard(item.product || {}),
cartProductIdSet
)
})))
this.setData({
wishlist: cards,
groups: this.buildGroupTabs(groupList, this.data.activeGroupId),
visibleWishlist: this.filterWishlist(cards, this.data.activeGroupId)
})
} catch (error) {
this.setData({
wishlist: [],
visibleWishlist: []
})
wx.showToast({
title: error.message || '\u6536\u85cf\u5217\u8868\u52a0\u8f7d\u5931\u8d25',
icon: 'none'
})
} finally {
this.setData({
loading: false
})
}
},
/**
* `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.
*/
normalizeId(value) {
return value === undefined || value === null ? '' : String(value)
},
filterWishlist(list, groupId) {
const normalizedGroupId = this.normalizeId(groupId)
if (!normalizedGroupId) {
return list
}
return list.filter(item => this.normalizeId(item && item.groupId) === normalizedGroupId)
},
buildGroupTabs(groups, activeGroupId) {
const normalizedActiveGroupId = this.normalizeId(activeGroupId)
return (Array.isArray(groups) ? groups : []).map(group => {
const id = this.normalizeId(group && group.id)
return {
id,
name: group.name,
itemCount: group.itemCount || 0,
active: id === normalizedActiveGroupId
}
})
},
handleGroupSelect(event) {
const groupId = this.normalizeId(event.currentTarget.dataset.groupId)
this.setData({
activeGroupId: groupId,
groups: this.buildGroupTabs(this.data.groups, groupId),
visibleWishlist: this.filterWishlist(this.data.wishlist, groupId)
})
},
async fetchCartProductIdSet() {
try {
const cartList = await cartApi.getCartList()
const idSet = new Set(
(Array.isArray(cartList) ? cartList : [])
.map(item => this.normalizeId(item && item.productId))
.filter(Boolean)
)
this.cartProductIdSet = idSet
return idSet
} catch (error) {
this.cartProductIdSet = new Set()
return this.cartProductIdSet
}
},
/**
* `markCardInCart` 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.
*/
markCardInCart(card, idSet) {
return {
...card,
inCart: (idSet || new Set()).has(this.normalizeId(card && card.id))
}
},
/**
* `markWishlistProductInCart` 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.
*/
markWishlistProductInCart(productId) {
const normalizedId = this.normalizeId(productId)
/**
* `if` 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.
*/
if (!normalizedId) {
return
}
this.cartProductIdSet = this.cartProductIdSet || new Set()
this.cartProductIdSet.add(normalizedId)
const nextWishlist = this.data.wishlist.map(item => (
this.normalizeId(item && item.productId) === normalizedId
? {
...item,
productCard: {
...item.productCard,
inCart: true
}
}
: item
))
this.setData({
wishlist: nextWishlist,
visibleWishlist: this.filterWishlist(nextWishlist, this.data.activeGroupId)
})
},
/**
* `handleOpenAuth` 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.
*/
handleOpenAuth() {
navigation.openAuth('/pages/wishlist/index')
},
/**
* `handleProductSelect` 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.
*/
handleProductSelect(event) {
const product = event.detail || {}
/**
* `if` 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.
*/
if (!product.id) {
return
}
navigation.navigate('/pages/product-detail/index', {
id: product.id
})
},
async handleQuickAdd(event) {
const product = event.detail || {}
const productId = this.normalizeId(product.id)
/**
* `if` 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.
*/
if (!productId) {
return
}
if (!navigation.ensureLogin('\u52a0\u5165\u8d2d\u7269\u8f66')) {
return
}
if (this.cartProductIdSet && this.cartProductIdSet.has(productId)) {
wx.showToast({
title: '\u8d2d\u7269\u8f66\u5df2\u6709',
icon: 'none'
})
return
}
try {
await actionGuard.run(this, `cart:add:${productId}`, () => runWithLoading(() => cartApi.addToCart({
productId,
quantity: 1
})), {
message: '\u8d2d\u7269\u8f66\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
})
this.markWishlistProductInCart(productId)
await getApp().updateCartBadge()
wx.showToast({
title: '\u5df2\u52a0\u5165\u8d2d\u7269\u8f66',
icon: 'none'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '\u52a0\u5165\u8d2d\u7269\u8f66\u5931\u8d25',
icon: 'none'
})
}
},
async handleRemove(event) {
const productId = event.currentTarget.dataset.productId
/**
* `if` 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.
*/
if (!productId) {
return
}
try {
await actionGuard.run(this, `wishlist:remove:${productId}`, () => runWithLoading(() => wishlistApi.removeFromWishlist(productId)), {
message: '\u64cd\u4f5c\u8fdb\u884c\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u70b9\u51fb'
})
wx.showToast({
title: '\u5df2\u53d6\u6d88\u6536\u85cf',
icon: 'none'
})
await this.loadWishlist()
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '\u53d6\u6d88\u6536\u85cf\u5931\u8d25',
icon: 'none'
})
}
},
/**
* `handleBack` 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.
*/
handleBack() {
navigation.goBack('/pages/profile/index')
}
})
@@ -0,0 +1,6 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar",
"product-card": "/components/product-card/product-card"
}
}
@@ -0,0 +1,65 @@
<navigation-bar title="我的收藏" showBack="{{true}}" fallbackPath="/pages/profile/index" color="{{navThemeColor || '#4A443F'}}" background="{{navThemeBackground || '#FDFBF9'}}"></navigation-bar>
<view class="page-shell wishlist-page" style="{{themeVars}}">
<view class="page-content">
<view wx:if="{{!isLoggedIn}}" class="page-card empty-card">
<text class="empty-title">登录后查看收藏</text>
<text class="empty-desc">登录后查看收藏作品。</text>
<view class="ghost-btn" bindtap="handleOpenAuth">去登录</view>
</view>
<block wx:elif="{{wishlist.length}}">
<view class="page-hero wishlist-hero">
<text class="hero-kicker">收藏夹</text>
<text class="hero-title">已收藏 {{wishlist.length}} 个作品</text>
<text class="hero-desc">喜欢的款式先收藏。</text>
</view>
<scroll-view wx:if="{{groups.length}}" class="wishlist-groups" scroll-x enhanced show-scrollbar="{{false}}">
<view
class="wishlist-group-chip {{!activeGroupId ? 'wishlist-group-chip--active' : ''}}"
data-group-id=""
bindtap="handleGroupSelect"
>全部</view>
<view
wx:for="{{groups}}"
wx:key="id"
class="wishlist-group-chip {{item.active ? 'wishlist-group-chip--active' : ''}}"
data-group-id="{{item.id}}"
bindtap="handleGroupSelect"
>{{item.name}}</view>
</scroll-view>
<view wx:for="{{visibleWishlist}}" wx:key="id" class="wishlist-item">
<product-card
item="{{item.productCard}}"
show-quick-add="{{true}}"
in-cart="{{item.productCard.inCart}}"
bindselect="handleProductSelect"
bindquickadd="handleQuickAdd"
></product-card>
<view class="wishlist-meta">
<text class="wishlist-group-name">{{item.groupName || '最近想做'}}</text>
<text wx:if="{{item.note}}" class="wishlist-note">{{item.note}}</text>
</view>
<view class="wishlist-actions">
<view class="wishlist-action wishlist-action--secondary" data-product-id="{{item.productId}}" bindtap="handleRemove">取消收藏</view>
</view>
</view>
<view wx:if="{{!visibleWishlist.length}}" class="page-card empty-card">
<text class="empty-title">当前分组还没有作品</text>
</view>
</block>
<view wx:elif="{{loading}}" class="page-card empty-card">
<text class="empty-title">正在加载收藏内容</text>
</view>
<view wx:else class="page-card empty-card">
<text class="empty-title">你还没有收藏作品</text>
<text class="empty-desc">先去逛逛作品。</text>
<view class="ghost-btn" bindtap="handleBack">去逛作品</view>
</view>
</view>
</view>
@@ -0,0 +1,83 @@
.wishlist-hero {
margin-bottom: 20rpx;
}
.wishlist-item + .wishlist-item {
margin-top: 22rpx;
}
.wishlist-groups {
display: block;
white-space: nowrap;
margin-bottom: 20rpx;
}
.wishlist-group-chip {
display: inline-flex;
align-items: center;
justify-content: center;
height: 58rpx;
min-width: 92rpx;
margin-right: 14rpx;
padding: 0 24rpx;
border: 2rpx solid var(--theme-border);
border-radius: 16rpx;
background: var(--theme-card-bg);
color: var(--text-secondary);
font-size: 24rpx;
white-space: nowrap;
}
.wishlist-group-chip--active {
border-color: var(--theme-primary);
background: var(--theme-primary-soft);
color: var(--theme-primary);
font-weight: 600;
}
.wishlist-meta {
display: flex;
flex-direction: column;
gap: 8rpx;
margin-top: 12rpx;
}
.wishlist-group-name {
align-self: flex-start;
padding: 4rpx 16rpx;
border-radius: 14rpx;
background: var(--theme-primary-soft);
color: var(--theme-accent);
font-size: 22rpx;
}
.wishlist-note {
color: var(--text-secondary);
font-size: 24rpx;
line-height: 34rpx;
word-break: break-word;
}
.wishlist-actions {
display: flex;
gap: 16rpx;
margin-top: 12rpx;
}
.wishlist-action {
display: flex;
align-items: center;
justify-content: center;
height: 74rpx;
flex: 1;
border-radius: 20rpx;
background: var(--theme-primary-soft);
color: var(--theme-primary);
font-size: 26rpx;
font-weight: 600;
}
.wishlist-action--secondary {
background: var(--theme-primary-soft);
color: var(--theme-accent);
}