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