modify:优化

This commit is contained in:
ECRZ
2026-01-07 10:13:21 +08:00
parent da4a055c1c
commit 60b5aba7f8
10 changed files with 3148 additions and 280 deletions

View File

@@ -59,11 +59,16 @@ Page({
today: '',
// 加载状态
loading: false,
loadingMore: false, // 加载更多状态
// 是否已搜索
searched: false,
// 查询结果
priceList: [],
total: 0,
// 分页参数
currentPage: 1,
pageSize: 20, // 每页数量优化为20首屏加载更快
hasMore: true, // 是否还有更多数据
// 统计信息
stats: null,
// Picker 显示状态
@@ -74,7 +79,10 @@ Page({
// Picker value (数组形式)
regionPickerValue: [],
materialPickerValue: [],
partsnamePickerValue: []
partsnamePickerValue: [],
// 价格详情弹窗
detailVisible: false,
detailItem: null
},
/**
@@ -206,7 +214,7 @@ Page({
},
/**
* 查询价格
* 查询价格(首次查询)
*/
async onSearch() {
const {
@@ -222,8 +230,11 @@ Page({
return
}
// 开始加载
// 重置分页状态
this.setData({
currentPage: 1,
priceList: [],
hasMore: true,
loading: true,
searched: false
})
@@ -232,7 +243,8 @@ Page({
// 构建搜索参数
const searchParams = {
region: selectedRegion,
pageSize: 100
page: 1,
pageSize: this.data.pageSize
}
// 添加可选参数
@@ -274,35 +286,8 @@ Page({
console.log('JSON 数据:', JSON.stringify(statsResult.data, null, 2))
console.log('====================================================')
// 更新数据
const priceList = searchResult.data || []
const total = searchResult.total || searchResult.pagination?.total || priceList.length || 0
// 格式化日期字段
const formattedList = priceList.map(item => {
let dateStr = ''
if (item.price_date) {
const date = new Date(item.price_date)
dateStr = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
return {
...item,
price_date_str: dateStr
}
})
this.setData({
priceList: formattedList,
total,
stats: statsResult.data || null,
searched: true,
loading: false
})
// 显示结果提示
if (searchResult.data && searchResult.data.length > 0) {
api.showSuccess(`查询成功,共找到 ${searchResult.data.length} 条数据`)
}
// 处理查询结果
this.processSearchResult(searchResult, statsResult)
} catch (error) {
console.error('查询失败:', error)
@@ -311,12 +296,114 @@ Page({
searched: true,
priceList: [],
total: 0,
stats: null
stats: null,
hasMore: false
})
// API 错误已在 request.js 中处理
}
},
/**
* 处理查询结果(首次查询和加载更多共用)
*/
processSearchResult(searchResult, statsResult) {
const priceList = searchResult.data || []
const total = searchResult.total || searchResult.pagination?.total || priceList.length || 0
// 格式化日期字段
const formattedList = priceList.map(item => {
let dateStr = ''
if (item.price_date) {
const date = new Date(item.price_date)
dateStr = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
return {
...item,
price_date_str: dateStr
}
})
// 判断是否还有更多数据
const hasMore = formattedList.length >= this.data.pageSize && this.data.priceList.length + formattedList.length < total
// 合并数据(首次查询或加载更多)
const newList = this.data.currentPage === 1 ? formattedList : [...this.data.priceList, ...formattedList]
this.setData({
priceList: newList,
total,
stats: statsResult?.data || null,
searched: true,
loading: false,
loadingMore: false,
hasMore
})
// 显示结果提示
if (this.data.currentPage === 1 && searchResult.data && searchResult.data.length > 0) {
api.showSuccess(`查询成功,共找到 ${total} 条数据`)
}
},
/**
* 触底加载更多
*/
async onReachBottom() {
const { loading, loadingMore, hasMore, searched, total, priceList } = this.data
// 如果正在加载、没有更多数据、或未搜索过,则不处理
if (loading || loadingMore || !hasMore || !searched) {
return
}
// 如果已加载全部数据
if (priceList.length >= total) {
this.setData({ hasMore: false })
return
}
console.log('触底加载更多...')
// 开始加载更多
this.setData({
loadingMore: true,
currentPage: this.data.currentPage + 1
})
try {
// 构建搜索参数
const searchParams = {
region: this.data.selectedRegion,
page: this.data.currentPage,
pageSize: this.data.pageSize
}
// 添加可选参数
if (this.data.selectedMaterial) searchParams.material = this.data.selectedMaterial
if (this.data.selectedPartsname) searchParams.partsname = this.data.selectedPartsname
if (this.data.selectedDate) searchParams.startDate = this.data.selectedDate
if (this.data.selectedDate) searchParams.endDate = this.data.selectedDate
console.log('加载更多参数:', searchParams)
// 调用搜索接口
const searchResult = await api.searchPrices(searchParams)
console.log('加载更多结果:', searchResult)
// 处理结果(不需要再次获取统计数据)
this.processSearchResult(searchResult, { data: this.data.stats })
} catch (error) {
console.error('加载更多失败:', error)
this.setData({
loadingMore: false,
currentPage: this.data.currentPage - 1 // 恢复页码
})
api.showError('加载更多失败,请重试')
}
},
/**
* 重置表单
*/
@@ -335,7 +422,10 @@ Page({
searched: false,
priceList: [],
total: 0,
stats: null
stats: null,
currentPage: 1,
hasMore: true,
loadingMore: false
})
},
@@ -345,39 +435,30 @@ Page({
onPriceDetail(e) {
const item = e.currentTarget.dataset.item
// 格式化日期
const formatDate = (dateStr) => {
if (!dateStr) return '-'
const date = new Date(dateStr)
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
// 构建详情信息
let detail = `地区:${item.price_region || '-'}\n`
detail += `品名:${item.partsname_name || '-'}\n`
detail += `材质:${item.goods_material || '-'}\n`
if (item.goods_spec) {
detail += `规格:${item.goods_spec}\n`
}
const price = item.hang_price || item.make_price || '-'
detail += `价格:¥${price}\n`
detail += `日期:${formatDate(item.price_date)}\n`
if (item.price_source) {
detail += `来源:${item.price_source}\n`
}
if (item.productarea_name) {
detail += `产地:${item.productarea_name}\n`
}
detail += `单位:元/吨`
wx.showModal({
title: '价格详情',
content: detail,
showCancel: false,
confirmText: '关闭'
// 显示详情弹窗
this.setData({
detailVisible: true,
detailItem: item
})
},
/**
* 关闭详情弹窗
*/
onCloseDetail() {
this.setData({
detailVisible: false,
detailItem: null
})
},
/**
* 阻止事件冒泡
*/
stopPropagation() {
// 阻止点击弹窗内容时关闭弹窗
},
/**
* 格式化日期为 YYYY-MM-DD
*/