This commit is contained in:
2026-09-13 01:51:09 +08:00
parent b1a664a768
commit afd0973ce5
32 changed files with 935 additions and 171 deletions
+70 -20
View File
@@ -1,5 +1,6 @@
import { request } from '../../utils/request'
import { OutfitItem, ClothesItem, PagedResult } from '../../types/models'
import { normalizePaged, hasMorePaged } from '../../utils/paged'
const CATEGORIES = [
{ name: '全部', value: '' },
@@ -11,6 +12,8 @@ const CATEGORIES = [
{ name: '配饰', value: '配饰' },
]
const PAGE_SIZE = 50
Page({
data: {
uuid: '',
@@ -25,6 +28,10 @@ Page({
activeCategory: 0,
allClothes: [] as ClothesItem[],
selectLoading: true,
loadingMore: false,
hasMore: true,
page: 1,
saving: false,
},
onLoad(query: Record<string, string>) {
@@ -48,6 +55,11 @@ Page({
}
})
)
// 编辑态下也要填充已选快照,避免确认选衣时跨分类数据丢失
const map = this.getSelectedMap()
clothes.forEach((it) => {
map[it.clothes_uuid] = it
})
this.setData({
outfitName: outfit.outfit_name,
outfitDesc: outfit.outfit_desc,
@@ -58,6 +70,12 @@ Page({
} catch (e) {}
},
// 已选衣物快照:跨分类切换时列表只保留当前分类,快照保留全部已选项
getSelectedMap(): Record<string, ClothesItem> {
if (!(this as any).selectedMap) (this as any).selectedMap = {}
return (this as any).selectedMap
},
onNameInput(e: WechatMiniprogram.Input) {
this.setData({ outfitName: e.detail.value })
},
@@ -69,49 +87,75 @@ Page({
},
openSelect() {
this.setData({ view: 'select', selectLoading: true })
this.fetchAllClothes()
this.setData({ view: 'select', selectLoading: true, page: 1, hasMore: true })
this.fetchAllClothes(1)
},
backToForm() {
this.setData({ view: 'form' })
},
fetchAllClothes() {
fetchAllClothes(page = 1) {
const cat = CATEGORIES[this.data.activeCategory].value
const params: Record<string, any> = { page: 1, page_size: 50 }
const params: Record<string, any> = { page, page_size: PAGE_SIZE }
if (cat) params.category_main = cat
// 请求序号:切换分类时旧响应可能后到,序号不匹配直接丢弃
const seq = ((this as any).reqSeq || 0) + 1
;(this as any).reqSeq = seq
if (page > 1) this.setData({ loadingMore: true })
request<PagedResult<ClothesItem>>({ url: '/clothes', data: params })
.then((res) => {
const list = res.list.map((it) => ({
if ((this as any).reqSeq !== seq) return
const { list: raw, total } = normalizePaged<ClothesItem>(res)
const list = raw.map((it) => ({
...it,
_selected: this.data.selectedUuids.indexOf(it.clothes_uuid) >= 0,
}))
this.setData({ allClothes: list, selectLoading: false })
const merged = page === 1 ? list : this.data.allClothes.concat(list)
this.setData({
allClothes: merged,
selectLoading: false,
loadingMore: false,
page,
hasMore: hasMorePaged(merged, total, PAGE_SIZE),
})
})
.catch(() => {
if ((this as any).reqSeq !== seq) return
this.setData({ selectLoading: false, loadingMore: false })
})
.catch(() => this.setData({ selectLoading: false }))
},
onCategoryTap(e: WechatMiniprogram.TouchEvent) {
const index = Number(e.currentTarget.dataset.index)
if (index === this.data.activeCategory) return
this.setData({ activeCategory: index, selectLoading: true })
this.fetchAllClothes()
this.setData({ activeCategory: index, selectLoading: true, page: 1, hasMore: true })
this.fetchAllClothes(1)
},
onReachBottom() {
if (this.data.view !== 'select') return
if (!this.data.hasMore || this.data.selectLoading || this.data.loadingMore) return
this.fetchAllClothes(this.data.page + 1)
},
onToggleSelect(e: WechatMiniprogram.CustomEvent) {
const item = e.detail.item as ClothesItem
const uuids = this.data.selectedUuids
const idx = uuids.indexOf(item.clothes_uuid)
if (idx >= 0) {
const map = this.getSelectedMap()
if (uuids.indexOf(item.clothes_uuid) >= 0) {
delete map[item.clothes_uuid]
this.syncSelect(uuids.filter((u) => u !== item.clothes_uuid))
} else {
if (uuids.length >= 10) {
wx.showToast({ title: '最多选择 10 件衣物', icon: 'none' })
return
}
this.syncSelect([...uuids, item.clothes_uuid])
return
}
if (uuids.length >= 10) {
wx.showToast({ title: '最多选择 10 件衣物', icon: 'none' })
return
}
map[item.clothes_uuid] = item
this.syncSelect([...uuids, item.clothes_uuid])
},
syncSelect(uuids: string[]) {
@@ -125,13 +169,16 @@ Page({
},
confirmSelect() {
const selectedItems = this.data.allClothes.filter(
(it) => this.data.selectedUuids.indexOf(it.clothes_uuid) >= 0
const map = this.getSelectedMap()
// 以快照为准:只从当前分类列表里过滤会丢掉其它分类已选的衣物
const selectedItems = this.data.selectedUuids.map(
(u) => map[u] || ({ clothes_uuid: u } as ClothesItem)
)
this.setData({ view: 'form', selectedItems })
},
onSave() {
if (this.data.saving) return
const name = this.data.outfitName.trim()
if (!name) {
wx.showToast({ title: '请填写穿搭名称', icon: 'none' })
@@ -148,15 +195,18 @@ Page({
scene: this.data.scene,
items: this.data.selectedUuids.map((u) => ({ clothes_uuid: u })),
}
this.setData({ saving: true })
const req = this.data.isEdit
? request({ url: `/outfit/${this.data.uuid}`, method: 'PUT', data: body })
: request({ url: '/outfit', method: 'POST', data: body })
req
.then(() => {
this.setData({ saving: false })
wx.showToast({ title: '已保存', icon: 'success' })
setTimeout(() => wx.navigateBack(), 500)
})
.catch(() => {})
.catch(() => this.setData({ saving: false }))
},
onDelete() {