bigfix
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { request } from '../../utils/request'
|
||||
import { ClothesItem, PagedResult, UserInfo } from '../../types/models'
|
||||
import { STORAGE_KEYS } from '../../utils/config'
|
||||
import { normalizePaged, hasMorePaged } from '../../utils/paged'
|
||||
|
||||
const CATEGORIES = [
|
||||
{ name: '全部', value: '' },
|
||||
@@ -19,6 +20,8 @@ const EXPIRE_OPTIONS = [
|
||||
{ label: '长期有效', days: 0 },
|
||||
]
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
Page({
|
||||
data: {
|
||||
view: 'config' as 'config' | 'select' | 'success',
|
||||
@@ -30,6 +33,10 @@ Page({
|
||||
activeCategory: 0,
|
||||
allClothes: [] as ClothesItem[],
|
||||
selectLoading: true,
|
||||
loadingMore: false,
|
||||
hasMore: true,
|
||||
page: 1,
|
||||
submitting: false,
|
||||
shareUuid: '',
|
||||
nickname: '微信用户',
|
||||
avatar: '',
|
||||
@@ -66,30 +73,54 @@ Page({
|
||||
},
|
||||
|
||||
openSelect() {
|
||||
this.setData({ view: 'select', selectLoading: true })
|
||||
this.fetchAllClothes()
|
||||
this.setData({ view: 'select', selectLoading: true, page: 1, hasMore: true })
|
||||
this.fetchAllClothes(1)
|
||||
},
|
||||
|
||||
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) {
|
||||
@@ -122,6 +153,7 @@ Page({
|
||||
},
|
||||
|
||||
onConfirm() {
|
||||
if (this.data.submitting) return
|
||||
const body: Record<string, any> = { share_type: this.data.shareType }
|
||||
if (this.data.shareType === 1) {
|
||||
if (!this.data.selectedUuids.length) {
|
||||
@@ -133,9 +165,11 @@ Page({
|
||||
const days = this.data.expireDays
|
||||
body.expire_time = days > 0 ? Math.floor(Date.now() / 1000) + days * 86400 : 0
|
||||
|
||||
// 提交加锁:连点会重复创建分享、重复扣额度
|
||||
this.setData({ submitting: true })
|
||||
request<{ share_uuid: string }>({ url: '/closet-share', method: 'POST', data: body })
|
||||
.then((res) => this.setData({ shareUuid: res.share_uuid, view: 'success' }))
|
||||
.catch(() => {})
|
||||
.then((res) => this.setData({ shareUuid: res.share_uuid, view: 'success', submitting: false }))
|
||||
.catch(() => this.setData({ submitting: false }))
|
||||
},
|
||||
|
||||
onViewShare() {
|
||||
|
||||
Reference in New Issue
Block a user