Browse Source

fix: 修复消息数据处理和跳转逻辑,增强健壮性

- 在路由守卫和首页中统一处理消息数据,确保 data 字段为数组类型
- 新增 getMessageJumpTarget 工具函数,根据消息的 queryId、type 或 status 智能确定跳转目标
- 首页点击消息时优先尝试根据 queryId 查找路由,提高跳转准确性
zhangrenyuan/feature-20260128093451-日常优化1.0
zhangrenyuan 1 month ago
parent
commit
0713b828cd
  1. 9
      src/router/index.js
  2. 42
      src/utils/goToCheck.js
  3. 19
      src/views/home.vue

9
src/router/index.js

@ -579,11 +579,12 @@ router.afterEach(async (to) => { // 接收to参数获取当前路由信息
// 存入全局状态,供所有页面访问 // 存入全局状态,供所有页面访问
const messageStore = useMessageStore(); const messageStore = useMessageStore();
// 过滤 flag=1的消息 // 过滤 flag=1的消息
newMessageRes.data = newMessageRes.data.filter(item => item.flag !== 1);
messageStore.setMessages(newMessageRes.data);
const list = Array.isArray(newMessageRes.data)
? newMessageRes.data
: (Array.isArray(newMessageRes.data?.list) ? newMessageRes.data.list : []);
messageStore.setMessages(list.filter(item => item.flag !== 1));
} catch (error) { } catch (error) {
console.error('获取消息失败:', error); console.error('获取消息失败:', error);
} }
}); });
export default router;
export default router;

42
src/utils/goToCheck.js

@ -33,4 +33,44 @@ export function getOrderPage(status) {
} }
// 未知状态返回工作台 // 未知状态返回工作台
return '/workbench'; return '/workbench';
}
}
function toNumberOrNull(value) {
if (value === null || value === undefined) return null;
const num = typeof value === 'number' ? value : Number(String(value).trim());
return Number.isFinite(num) ? num : null;
}
const queryIdRouteNameMap = {
6: 'rechargeAudit',
7: 'rechargeAudit',
8: 'rechargeAudit',
9: 'rechargeAudit',
10: 'rechargeAudit',
11: 'rechargeAudit',
12: 'rechargeAudit',
13: 'refundAudit',
14: 'refundAudit',
15: 'refundAudit',
16: 'refundAudit',
17: 'refundAudit',
18: 'refundAudit',
19: 'refundAudit'
};
export function getMessageJumpTarget(message) {
const permissionId = toNumberOrNull(message?.queryId ?? message?.menuId ?? message?.permissionId);
if (permissionId !== null) {
const routeName = queryIdRouteNameMap[permissionId];
if (routeName) return { name: routeName };
}
const type = toNumberOrNull(message?.type);
if (type === 0) return { name: 'rechargeAudit' };
if (type === 1) return { name: 'refundAudit' };
const status = toNumberOrNull(message?.status);
if (status !== null) return getOrderPage(status);
return '/workbench';
}

19
src/views/home.vue

@ -211,7 +211,8 @@ const getMessage = async () => {
}); });
if (res?.data) { if (res?.data) {
const cleanList = res.data.filter(i => i.flag !== 1)
const list = Array.isArray(res.data) ? res.data : (Array.isArray(res.data?.list) ? res.data.list : [])
const cleanList = list.filter(i => i.flag !== 1)
messageStore.setMessages(cleanList) messageStore.setMessages(cleanList)
} }
} catch (e) { } catch (e) {
@ -282,6 +283,19 @@ const toggleShowAll = () => showAll.value = !showAll.value
const scrollContainer = ref(null) const scrollContainer = ref(null)
const scrollToTop = () => scrollContainer.value?.scrollTo({top: 0, behavior: 'smooth'}) const scrollToTop = () => scrollContainer.value?.scrollTo({top: 0, behavior: 'smooth'})
const getPathByQueryId = (queryId) => {
const qid = Number(queryId)
if (!Number.isFinite(qid)) return null
const record = router.getRoutes().find(r => {
const pid = r.meta?.permissionId
if (Array.isArray(pid)) return pid.includes(qid)
return pid === qid
})
return record?.path || null
}
// + // +
const handleMessageClick = async (item) => { const handleMessageClick = async (item) => {
const res = await API({ const res = await API({
@ -292,7 +306,8 @@ const handleMessageClick = async (item) => {
if (res.code === 200) { if (res.code === 200) {
closeMessageDialog() closeMessageDialog()
await router.push(getOrderPage(item.status))
const targetPath = item?.queryId ? getPathByQueryId(item.queryId) : null
await router.push(targetPath || getOrderPage(item.status) || '/noPermission')
await getMessage() await getMessage()
ElMessage.success(t('elmessage.jumpSuccess')) ElMessage.success(t('elmessage.jumpSuccess'))
} else { } else {

Loading…
Cancel
Save