1 line
4.1 KiB
Plaintext
1 line
4.1 KiB
Plaintext
{"version":3,"file":"notificationService.js","sources":["../../../../src/service/notificationService.js"],"sourcesContent":["import { useUserStore } from '@/store/user'\r\nimport { useMessageStore } from '@/store/message'\r\nimport { httpGet, httpPost } from '@/utils/http'\r\n\r\nconst POLLING_INTERVAL = 60000 // 6秒轮询一次\r\nlet pollingTimer = null\r\n\r\nconst startPollingUnreadMessages = () => {\r\n try {\r\n const userStore = useUserStore()\r\n const messageStore = useMessageStore()\r\n\r\n // 检查用户是否已登录\r\n if (!userStore.userInfo.token) {\r\n console.log('用户未登录,停止轮询请求')\r\n return\r\n }\r\n\r\n // 防止多次调用导致重复轮询\r\n if (pollingTimer) return\r\n\r\n pollingTimer = setInterval(() => {\r\n if (!userStore.userInfo.token) {\r\n console.log('用户未登录,停止轮询请求')\r\n stopPollingUnreadMessages()\r\n return\r\n }\r\n\r\n httpGet('/notifications')\r\n .then((response) => {\r\n if (response.code === 200 && response.data.length > 0) {\r\n console.log('未读消息:', response.data)\r\n // 将获取到的未读消息存储到 message store 中\r\n messageStore.setUnreadMessages(response.data)\r\n }\r\n })\r\n .catch((error) => {\r\n console.error('获取未读消息失败:', error)\r\n })\r\n }, POLLING_INTERVAL)\r\n } catch (error) {\r\n console.error('Pinia 未初始化或其他错误:', error)\r\n }\r\n}\r\n\r\nconst stopPollingUnreadMessages = () => {\r\n if (pollingTimer) {\r\n clearInterval(pollingTimer)\r\n pollingTimer = null\r\n }\r\n}\r\n\r\nconst markMessageAsRead = (messageId) => {\r\n try {\r\n const userStore = useUserStore()\r\n const messageStore = useMessageStore()\r\n\r\n // 检查用户是否已登录\r\n if (!userStore.userInfo.token) {\r\n console.log('用户未登录,无法标记消息为已读')\r\n return Promise.reject('用户未登录')\r\n }\r\n\r\n return httpPost('/notifications', {}, { messageId })\r\n .then((response) => {\r\n if (response.code === 200) {\r\n console.log('消息已读:', messageId)\r\n // 在成功标记消息为已读后,将消息从未读列表移到已读列表中\r\n messageStore.markMessageAsRead(messageId)\r\n }\r\n })\r\n .catch((error) => {\r\n console.error('标记消息为已读失败:', error)\r\n })\r\n } catch (error) {\r\n console.error('Pinia 未初始化或其他错误:', error)\r\n }\r\n}\r\n\r\n// 将服务导出,供全局使用\r\nexport const notificationService = {\r\n startPollingUnreadMessages,\r\n stopPollingUnreadMessages,\r\n markMessageAsRead,\r\n}\r\n"],"names":["useUserStore","useMessageStore","httpGet","httpPost"],"mappings":";;;;AAIA,MAAM,mBAAmB;AACzB,IAAI,eAAe;AAEnB,MAAM,6BAA6B,MAAM;AACvC,MAAI;AACF,UAAM,YAAYA,WAAAA,aAAc;AAChC,UAAM,eAAeC,cAAAA,gBAAiB;AAGtC,QAAI,CAAC,UAAU,SAAS,OAAO;AAC7B,cAAQ,IAAI,cAAc;AAC1B;AAAA,IACD;AAGD,QAAI;AAAc;AAElB,mBAAe,YAAY,MAAM;AAC/B,UAAI,CAAC,UAAU,SAAS,OAAO;AAC7B,gBAAQ,IAAI,cAAc;AAC1B,kCAA2B;AAC3B;AAAA,MACD;AAEDC,iBAAAA,QAAQ,gBAAgB,EACrB,KAAK,CAAC,aAAa;AAClB,YAAI,SAAS,SAAS,OAAO,SAAS,KAAK,SAAS,GAAG;AACrD,kBAAQ,IAAI,SAAS,SAAS,IAAI;AAElC,uBAAa,kBAAkB,SAAS,IAAI;AAAA,QAC7C;AAAA,MACX,CAAS,EACA,MAAM,CAAC,UAAU;AAChB,gBAAQ,MAAM,aAAa,KAAK;AAAA,MAC1C,CAAS;AAAA,IACJ,GAAE,gBAAgB;AAAA,EACpB,SAAQ,OAAO;AACd,YAAQ,MAAM,oBAAoB,KAAK;AAAA,EACxC;AACH;AAEA,MAAM,4BAA4B,MAAM;AACtC,MAAI,cAAc;AAChB,kBAAc,YAAY;AAC1B,mBAAe;AAAA,EAChB;AACH;AAEA,MAAM,oBAAoB,CAAC,cAAc;AACvC,MAAI;AACF,UAAM,YAAYF,WAAAA,aAAc;AAChC,UAAM,eAAeC,c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