Files
code-bang/frontend/miniapp/pages/demand/list/index.js
T
2026-09-29 16:24:19 +08:00

295 lines
7.5 KiB
JavaScript

// pages/demand/list/index.js
import { fetchDemandList } from '../../../services/demand/fetchDemand';
import { fetchConfig } from '../../../services/home/home';
Page({
/**
* 页面的初始数据
*/
data: {
page: 1,
opType: 0,
keywords: '',
demandList: [],
isEmpty: false,
status: -1,
freshPage: false,
fromPreview: false, // 预览锁
hasMore: true, // 新增字段,标记是否还有更多数据
refreshing: false, // 刷新按钮是否处于加载中
tabList: [
{ text: '全部', key: -1 },
{ text: '招标中', key: 1 },
{ text: '签约中', key: 2 },
{ text: '工作中', key: 3 },
{ text: '验收中', key: 4 },
{ text: '结算中', key: 5 },
{ text: '交易完成', key: 6 },
{ text: '交易关闭', key: 7 },
]
},
// 标签页切换事件处理
tabChangeHandle(e) {
const { value } = e.detail;
this.setData({
status: value,
page: 1,
hasMore: true
});
// 根据不同标签加载不同数据
this.loadDemandList(true);
},
//搜索
onChangeInput(e) {
const { value } = e.detail;
this.setData({
keywords: value
});
if (!value) {
this.loadDemandList(true);
}
},
onChangeSearch(e) {
const { value } = e.detail;
if (value !== undefined) {
this.setData({
keywords: value
});
}
this.loadDemandList(true);
},
// 点击右侧刷新按钮
async onRefreshTap() {
if (this.data.refreshing) return;
this.setData({ refreshing: true });
await this.loadDemandList(true);
this.setData({ refreshing: false });
},
//图片预览
onImageView(e) {
const { images, index } = e.currentTarget.dataset;
console.log('images', images);
console.log('index', index);
wx.previewImage({
current: images[index], // 当前显示图片的http链接
urls: images, // 需要预览的图片http链接列表
success: (res) => {
console.log('预览成功');
},
complete: (res) => {
this.setData({ fromPreview: true });
}
});
},
// 加载需求列表数据
async loadDemandList(fresh = false, options = {}) {
const { silent = false } = options;
if (fresh) {
this.setData({
page: 1,
hasMore: true, // 刷新时重置
});
wx.pageScrollTo({
scrollTop: 0,
});
}
wx.nextTick(() => {
console.log(this.data.hasMore, fresh);
if (!this.data.hasMore && !fresh) return;
});
try {
const { page, status, keywords, opType } = this.data;
let params = { page, status, keywords, opType };
// 下拉刷新自带 loading 指示,不再叠加 showLoading
if (!silent) {
wx.showLoading({
title: '加载中',
});
}
const res = await fetchDemandList(params);
const nextList = res.data.data;
console.log(res)
console.log(nextList)
if (!silent) {
wx.hideLoading();
}
// 判断是否还有更多数据
const { current_page, last_page } = res.data;
let newDemandList = fresh ? nextList : this.data.demandList.concat(nextList);
const hasMore = current_page < last_page;
// 只有在还有更多页时才自增page
if (hasMore) {
this.setData({
demandList: newDemandList,
page: page + 1,
hasMore
});
} else {
this.setData({
demandList: newDemandList,
hasMore: false
});
}
this.setData({
isEmpty: newDemandList.length > 0 ? false : true
});
// 检查内容是否超出最大高度,放到 nextTick 里
wx.nextTick(() => {
this.checkDescOverflow();
});
} catch (err) {
console.error('Failed to load demand list:', err);
}
},
// 检查每条内容是否超出最大高度,决定是否显示“查看全文”
checkDescOverflow() {
const query = this.createSelectorQuery();
const maxHeight = 60; // 120rpx ≈ 60px,实际可根据机型调整
this.data.demandList.forEach((item, index) => {
query.select(`#desc-${index}`).boundingClientRect();
});
query.exec((rects) => {
const newList = this.data.demandList.map((item, idx) => {
// 只有内容高度大于 maxHeight+1 时才显示“查看全文”
const showToggle = rects[idx] && rects[idx].height > (maxHeight + 1);
return {
...item,
expanded: false,
showToggle
};
});
this.setData({ demandList: newList });
});
},
// 切换展开/收起
toggleDesc(e) {
const index = e.currentTarget.dataset.index;
const key = `demandList[${index}].expanded`;
this.setData({ [key]: !this.data.demandList[index].expanded });
},
loadConfig() {
fetchConfig().then(
(res) => {
console.log(res);
if (res.code == 1) {
let resData = res.data;
this.setData({
tabList: resData.developTypes
});
}
}
);
},
demandListClickHandle(e) {
const { index } = e.currentTarget.dataset;
const { id } = this.data.demandList[index];
let type = this.data.opType;
const opType = type == 'all' || type == 'myQuote' ? 'quote' : 'view';
console.log(index, id);
wx.navigateTo({
url: `/pages/demand/detail/index?id=${id}&opType=${opType}`,
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const opType = options.opType || '';
this.setData({ opType });
wx.setNavigationBarTitle({
title: opType == 'all' ? '需求大厅' : (opType == 'myQuote' ? '我投标的需求' : '我发布的需求')
});
opType == 'all' && this.loadConfig();
this.loadDemandList();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
if (this.data.fromPreview) {
// 刚从预览回来,直接跳过
this.setData({ fromPreview: false });
return;
}
const bidDemandId = wx.getStorageSync('bidDemandId');
if (bidDemandId) {
// 先清除标记,避免同一条被重复处理
wx.removeStorageSync('bidDemandId');
const index = this.data.demandList.findIndex(item => item.id === Number(bidDemandId));
if (index < 0) {
// 当前列表里没有这条(例如「我投标的需求」页),交给接口重新拉取
this.loadDemandList(true);
} else {
this.setData({
[`demandList[${index}].is_bid`]: true,
// 按钮渲染的是 item.button.name,只改 btnName 不会刷新文案
[`demandList[${index}].button`]: { name: '已投标', theme: 'light' },
[`demandList[${index}].demand_biddings_count`]: this.data.demandList[index].demand_biddings_count + 1,
});
}
return;
}
if (this.data.freshPage) {
this.setData({ freshPage: false });
this.loadDemandList(true);
}
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
async onPullDownRefresh() {
await this.loadDemandList(true, { silent: true });
wx.stopPullDownRefresh();
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
if (this.data.hasMore) {
this.loadDemandList();
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})