132 lines
3.1 KiB
JavaScript
132 lines
3.1 KiB
JavaScript
import { injectPage } from '@jdmini/api'
|
|
const { getCategories, getWorksheets } = require('../../utils/api.js')
|
|
const { DATA_BASE_URL } = require('../../utils/config.js')
|
|
|
|
Page(injectPage({})({
|
|
data: {
|
|
dataBaseUrl: DATA_BASE_URL,
|
|
categories: [],
|
|
currentCategory: 1,
|
|
currentCategoryName: '',
|
|
worksheets: [],
|
|
page: 1,
|
|
pageSize: 20,
|
|
total: 0,
|
|
hasMore: true,
|
|
loading: false
|
|
},
|
|
|
|
onLoad(options) {
|
|
// 如果传入了分类ID
|
|
if (options.id) {
|
|
this.setData({
|
|
currentCategory: Number(options.id)
|
|
})
|
|
}
|
|
this.loadCategories()
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.setData({
|
|
page: 1,
|
|
hasMore: true,
|
|
worksheets: []
|
|
})
|
|
this.loadWorksheets().finally(() => {
|
|
wx.stopPullDownRefresh()
|
|
})
|
|
},
|
|
|
|
// 加载分类列表
|
|
async loadCategories() {
|
|
try {
|
|
const res = await getCategories()
|
|
if (res.success && res.data.length > 0) {
|
|
const categories = res.data
|
|
// 如果没有设置当前分类,默认选中第一个
|
|
const currentCategory = this.data.currentCategory || categories[0].id
|
|
const currentCategoryObj = categories.find(c => c.id === currentCategory) || categories[0]
|
|
|
|
this.setData({
|
|
categories,
|
|
currentCategory: currentCategoryObj.id,
|
|
currentCategoryName: currentCategoryObj.name
|
|
})
|
|
|
|
// 加载该分类下的练习表
|
|
await this.loadWorksheets()
|
|
}
|
|
} catch (error) {
|
|
console.error('加载分类失败:', error)
|
|
wx.showToast({
|
|
title: '加载失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
// 选择分类
|
|
async selectCategory(e) {
|
|
const categoryId = Number(e.currentTarget.dataset.id)
|
|
if (categoryId === this.data.currentCategory) return
|
|
|
|
const category = this.data.categories.find(c => c.id === categoryId)
|
|
|
|
this.setData({
|
|
currentCategory: categoryId,
|
|
currentCategoryName: category?.name || '',
|
|
page: 1,
|
|
hasMore: true,
|
|
worksheets: []
|
|
})
|
|
|
|
await this.loadWorksheets()
|
|
},
|
|
|
|
// 加载练习表列表
|
|
async loadWorksheets() {
|
|
if (this.data.loading) return
|
|
|
|
try {
|
|
this.setData({ loading: true })
|
|
|
|
const res = await getWorksheets({
|
|
category_id: this.data.currentCategory,
|
|
page: this.data.page,
|
|
pageSize: this.data.pageSize
|
|
})
|
|
|
|
if (res.success) {
|
|
const newWorksheets = res.data.list || []
|
|
this.setData({
|
|
worksheets: this.data.page === 1 ? newWorksheets : [...this.data.worksheets, ...newWorksheets],
|
|
total: res.data.pagination?.total || 0,
|
|
hasMore: newWorksheets.length >= this.data.pageSize,
|
|
loading: false
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('加载练习表失败:', error)
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
// 加载更多
|
|
async loadMore() {
|
|
if (this.data.loading || !this.data.hasMore) return
|
|
|
|
this.setData({
|
|
page: this.data.page + 1
|
|
})
|
|
await this.loadWorksheets()
|
|
},
|
|
|
|
// 跳转详情页
|
|
goDetail(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
wx.navigateTo({
|
|
url: `/pages/detail/detail?id=${id}`
|
|
})
|
|
}
|
|
}))
|