yingsa/miniprogram/pages/index/index.js
2026-01-20 08:50:50 +08:00

103 lines
2.1 KiB
JavaScript

const app = getApp()
const util = require('../../utils/util')
Page({
data: {
currentStore: null,
gender: '',
list: [],
loading: false,
page: 1,
pageSize: 20,
hasMore: true
},
onLoad() {
this.initData()
},
onShow() {
if (app.globalData.currentStore) {
this.setData({ currentStore: app.globalData.currentStore })
}
},
onPullDownRefresh() {
this.setData({ page: 1, hasMore: true })
this.fetchData().then(() => {
wx.stopPullDownRefresh()
})
},
onReachBottom() {
if (this.data.hasMore && !this.data.loading) {
this.loadMore()
}
},
async initData() {
// 确保已登录
if (!app.globalData.token) {
try {
await app.login()
} catch (e) {
console.error('登录失败:', e)
}
}
// 获取当前门店
try {
const store = await app.getCurrentStore()
this.setData({ currentStore: store })
this.fetchData()
} catch (e) {
console.error('获取门店失败:', e)
}
},
async fetchData() {
if (!this.data.currentStore?.storeId) return
this.setData({ loading: true })
try {
const res = await app.request('/api/ladder/ranking', {
store_id: this.data.currentStore.storeId,
gender: this.data.gender,
page: this.data.page,
pageSize: this.data.pageSize
})
const list = res.data.list || []
this.setData({
list: this.data.page === 1 ? list : [...this.data.list, ...list],
hasMore: list.length >= this.data.pageSize
})
} catch (e) {
console.error('获取排名失败:', e)
} finally {
this.setData({ loading: false })
}
},
loadMore() {
this.setData({ page: this.data.page + 1 })
this.fetchData()
},
setGender(e) {
const gender = e.currentTarget.dataset.gender
this.setData({ gender, page: 1, hasMore: true })
this.fetchData()
},
selectStore() {
wx.navigateTo({ url: '/pages/store/index' })
},
viewPlayer(e) {
const id = e.currentTarget.dataset.id
wx.navigateTo({ url: `/pages/player/index?id=${id}` })
}
})