fix: 消息管理中,分页问题修复。

This commit is contained in:
tianyongbao
2026-03-08 17:31:53 +08:00
parent 5deb6387c4
commit b1c9b6ad02
2 changed files with 93 additions and 63 deletions

View File

@@ -27,7 +27,9 @@ export function msgSwitch(data) {
// 已读一条消息 // 已读一条消息
export function msgRead(data) { export function msgRead(data) {
return httpService.put(API.MESSAGE.READ_ONE(), {data})
// 后端需要id作为URL参数
return httpService.put(`${API.MESSAGE.READ_ONE()}?id=${data.id}`)
} }
// 已读全部 // 已读全部

View File

@@ -67,6 +67,8 @@
refresherThreshold="50" refresherThreshold="50"
:refresherTriggered="refStatus" :refresherTriggered="refStatus"
@RefresherPulling="onPulling" @RefresherPulling="onPulling"
@scrolltolower="lower"
:lower-threshold="100"
> >
<view class="body"> <view class="body">
<view v-if="selectVal === 0"> <view v-if="selectVal === 0">
@@ -246,7 +248,7 @@ const msg_o2 = `${imgUrl}msg_o2.png`;
const pond404 = `${imgUrl}zanwurenwu.png`; const pond404 = `${imgUrl}zanwurenwu.png`;
const selectVal = ref(0); const selectVal = ref(0);
// 告警列表 // 告警列表
const warnList = ref([]); const warnList = ref<any[]>([]);
const warnParams = ref({ const warnParams = ref({
pageSize: 10, pageSize: 10,
pageNum: 1 pageNum: 1
@@ -254,7 +256,7 @@ const warnParams = ref({
const warnTotal = ref(0); const warnTotal = ref(0);
const warnTotalPages = ref(1); const warnTotalPages = ref(1);
// 开关列表 // 开关列表
const switchList = ref([]); const switchList = ref<any[]>([]);
const switchParams = ref({ const switchParams = ref({
pageSize: 10, pageSize: 10,
pageNum: 1 pageNum: 1
@@ -262,7 +264,7 @@ const switchParams = ref({
const switchTotal = ref(0); const switchTotal = ref(0);
const switchTotalPages = ref(1); const switchTotalPages = ref(1);
// 充值列表 // 充值列表
const payList = ref([]); const payList = ref<any[]>([]);
const payParams = ref({ const payParams = ref({
pageSize: 10, pageSize: 10,
pageNum: 1 pageNum: 1
@@ -272,12 +274,22 @@ const payTotalPages = ref(1);
const refStatus = ref(false); const refStatus = ref(false);
const unReadCount = ref<number>(0); const unReadCount = ref<number>(0);
const msgCount = ref<number>(0); const msgCount = ref<number>(0);
// 使用普通变量而不是 ref避免响应式延迟
let isLoadingMore = false;
// 记录当前正在加载的页码,防止重复
let currentLoadingPage = 0;
/** ----------------method start------------------------ */ /** ----------------method start------------------------ */
// 标记是否已经初始化
let isInitialized = false;
Taro.useDidShow(() => { Taro.useDidShow(() => {
selectVal.value = 0; // 只在首次显示时加载数据
// 查询设备列表 if (!isInitialized) {
getWarnMsg(); selectVal.value = 0;
getWarnMsg();
isInitialized = true;
}
}); });
// 头部组件刷新 // 头部组件刷新
function resUsetInfo(e){ function resUsetInfo(e){
@@ -289,6 +301,11 @@ function changeVal(e) {
refStatus.value = false; refStatus.value = false;
}, 200); }, 200);
// 如果是同一个tab不做任何操作保持当前状态
if (selectVal.value === e) {
return;
}
selectVal.value = e; selectVal.value = e;
if (e == 0) { if (e == 0) {
warnParams.value.pageNum = 1; warnParams.value.pageNum = 1;
@@ -302,27 +319,30 @@ function changeVal(e) {
} }
} }
// 查询告警消息 // 查询告警消息
function getWarnMsg() { function getWarnMsg(isLoadMore = false) {
const store = useRootUserStore(); const store = useRootUserStore();
const userId = store.getRootUserId || Taro.getStorageSync('UserId'); const userId = store.getRootUserId || Taro.getStorageSync('UserId');
const params = { const params = {
...warnParams.value, ...warnParams.value,
userId userId
}; };
msgWarn(params).then((res: any) => { msgWarn(params).then((res: any) => {
if (res.code == 200) { if (res.code == 200) {
// 数据直接在 res 上,不是 res.data if (isLoadMore) {
warnList.value = res.rows || []; const newRows = res.rows || [];
warnList.value = [...warnList.value, ...newRows];
} else {
warnList.value = res.rows || [];
}
warnTotal.value = res.total || 0; warnTotal.value = res.total || 0;
// 计算总页数
warnTotalPages.value = Math.ceil(warnTotal.value / warnParams.value.pageSize); warnTotalPages.value = Math.ceil(warnTotal.value / warnParams.value.pageSize);
// 统计未读数量
unReadCount.value = warnList.value.filter(item => !item.isRead).length; unReadCount.value = warnList.value.filter(item => !item.isRead).length;
msgCount.value = warnTotal.value; msgCount.value = warnTotal.value;
if (unReadCount.value) { if (unReadCount.value) {
Taro.setTabBarBadge({ Taro.setTabBarBadge({
index: 1, // tabBar的位置从0开始计数 index: 1,
text: unReadCount.value > 9 ? "9+" : String(unReadCount.value), text: unReadCount.value > 9 ? "9+" : String(unReadCount.value),
}); });
} else { } else {
@@ -331,19 +351,30 @@ function getWarnMsg() {
}); });
} }
} }
}).finally(() => {
// 延迟重置锁,防止立即触发下一次滚动
setTimeout(() => {
isLoadingMore = false;
currentLoadingPage = 0;
}, 500);
}); });
} }
// 查询开关消息 // 查询开关消息
function getSwitchMsg() { function getSwitchMsg(isLoadMore = false) {
const store = useRootUserStore(); const store = useRootUserStore();
const userId = store.getRootUserId || Taro.getStorageSync('UserId'); const userId = store.getRootUserId || Taro.getStorageSync('UserId');
const params = { const params = {
...switchParams.value, ...switchParams.value,
userId userId
}; };
msgSwitch(params).then((res: any) => { msgSwitch(params).then((res: any) => {
if (res.code == 200) { if (res.code == 200) {
switchList.value = res.rows || []; if (isLoadMore) {
const newRows = res.rows || [];
switchList.value = [...switchList.value, ...newRows];
} else {
switchList.value = res.rows || [];
}
switchTotal.value = res.total || 0; switchTotal.value = res.total || 0;
switchTotalPages.value = Math.ceil(switchTotal.value / switchParams.value.pageSize); switchTotalPages.value = Math.ceil(switchTotal.value / switchParams.value.pageSize);
msgCount.value = switchTotal.value; msgCount.value = switchTotal.value;
@@ -351,7 +382,7 @@ function getSwitchMsg() {
}); });
} }
// 查询充值消息 // 查询充值消息
function getPayMsg() { function getPayMsg(isLoadMore = false) {
const store = useRootUserStore(); const store = useRootUserStore();
const userId = store.getRootUserId || Taro.getStorageSync('UserId'); const userId = store.getRootUserId || Taro.getStorageSync('UserId');
const params = { const params = {
@@ -360,7 +391,12 @@ function getPayMsg() {
}; };
msgPay(params).then((res: any) => { msgPay(params).then((res: any) => {
if (res.code == 200) { if (res.code == 200) {
payList.value = res.rows || []; if (isLoadMore) {
const newRows = res.rows || [];
payList.value = [...payList.value, ...newRows];
} else {
payList.value = res.rows || [];
}
payTotal.value = res.total || 0; payTotal.value = res.total || 0;
payTotalPages.value = Math.ceil(payTotal.value / payParams.value.pageSize); payTotalPages.value = Math.ceil(payTotal.value / payParams.value.pageSize);
msgCount.value = payTotal.value; msgCount.value = payTotal.value;
@@ -368,55 +404,46 @@ function getPayMsg() {
}); });
} }
function lower() { function lower() {
// 防止重复加载
if (isLoadingMore) {
return;
}
// 检查是否还有更多数据
if (selectVal.value == 0 && warnParams.value.pageNum >= warnTotalPages.value) {
return;
}
if (selectVal.value == 1 && switchParams.value.pageNum >= switchTotalPages.value) {
return;
}
if (selectVal.value == 2 && payParams.value.pageNum >= payTotalPages.value) {
return;
}
// 计算要加载的页码
const nextPage = warnParams.value.pageNum + 1;
// 检查是否正在加载同一页
if (currentLoadingPage === nextPage) {
return;
}
// 设置加载状态
isLoadingMore = true;
currentLoadingPage = nextPage;
// 更新页码
warnParams.value.pageNum = nextPage;
const userId = Taro.getStorageSync("UserId"); const userId = Taro.getStorageSync("UserId");
if (selectVal.value == 0) { if (selectVal.value == 0) {
if (warnParams.value.pageNum < warnTotalPages.value) { getWarnMsg(true);
warnParams.value.pageNum = warnParams.value.pageNum + 1;
const params = {
...warnParams.value,
userId
};
msgWarn(params).then((res: any) => {
if (res.code == 200) {
const newRows = res.rows || [];
newRows.forEach((r) => {
warnList.value.push(r);
});
}
});
}
} else if (selectVal.value == 1) { } else if (selectVal.value == 1) {
if (switchParams.value.pageNum < switchTotalPages.value) { switchParams.value.pageNum = switchParams.value.pageNum + 1;
switchParams.value.pageNum = switchParams.value.pageNum + 1; getSwitchMsg(true);
const params = {
...switchParams.value,
userId
};
msgSwitch(params).then((res: any) => {
if (res.code == 200) {
const newRows = res.rows || [];
newRows.forEach((r) => {
switchList.value.push(r);
});
}
});
}
} else if (selectVal.value == 2) { } else if (selectVal.value == 2) {
if (payParams.value.pageNum < payTotalPages.value) { payParams.value.pageNum = payParams.value.pageNum + 1;
payParams.value.pageNum = payParams.value.pageNum + 1; getPayMsg(true);
const params = {
...payParams.value,
userId
};
msgPay(params).then((res: any) => {
if (res.code == 200) {
const newRows = res.rows || [];
newRows.forEach((r) => {
payList.value.push(r);
});
}
});
}
} }
} }
/** 上拉触底分页 */ /** 上拉触底分页 */
@@ -564,6 +591,7 @@ function getWarnCode(warnCode) {
} }
.body { .body {
padding: 15px; padding: 15px;
min-height: 81vh; // 确保内容高度超过容器,支持滚动
.title { .title {
font-size: 32px; font-size: 32px;