فهرست منبع

feat:socket

ljc 1 ماه پیش
والد
کامیت
729625d805
4فایلهای تغییر یافته به همراه160 افزوده شده و 5 حذف شده
  1. 11 0
      components/cwg-notice.vue
  2. 144 0
      components/cwg-page-wrapper.vue
  3. 1 1
      components/cwg-right-drawer.vue
  4. 4 4
      pages/mine/improveImmediately.vue

+ 11 - 0
components/cwg-notice.vue

@@ -75,6 +75,11 @@ const goMore = () => {
     })
     close()
 }
+const openNotice = () => {
+  if (!userToken.value) return
+  getData()
+  getList()
+}
 const getData = async () => {
     const res = await newsApi.newsNoticeRead({
         read: 0
@@ -93,6 +98,12 @@ onMounted(() => {
     })
     getData()
     getList()
+  uni.$on('updateUnreadCount',(value)=>{
+    console.log('updateUnreadCount',value)
+    isRed.value = value > 0
+    if (!userToken.value) return
+    getList()
+  })
 })
 </script>
 

+ 144 - 0
components/cwg-page-wrapper.vue

@@ -60,7 +60,12 @@ import { userApi } from '@/api/user'
 import useGlobalStore from '@/stores/use-global-store'
 import PrefectInfo from '@/components/PrefectInfo.vue'
 import IbInfo from '@/components/IbInfo.vue'
+import { userToken } from "@/composables/config";
+import { useI18n } from 'vue-i18n'
+import Config from '@/config/index'
+import { customApi } from '@/service/custom'
 
+const { t, locale } = useI18n()
 const globalStore = useGlobalStore()
 const router = useRouter()
 const userStore = useUserStore()
@@ -128,16 +133,155 @@ const handleOpenRightDrawer = () => {
   openRightDrawer()
 }
 
+// ========== WebSocket ==========
+let websock: any = null
+let reconnectTimer: any = null
+const pushManager = ref<any>({})
+const reasons = ref<any>({})
+
+const initWebSocket = () => {
+  let token = userToken.value || uni.getStorageSync('token')
+  if (!token) return
+  
+  // 模拟 tool.tokenReplace 移除 Bearer
+  if (token.startsWith('Bearer ')) {
+    token = token.replace('Bearer ', '')
+  }
+
+  const wsUrl = `${Config.HostWs}/webSocket?Access-Token=${token}`
+
+  websock = uni.connectSocket({
+    url: wsUrl,
+    success: () => {
+      // console.log('WebSocket connected successfully')
+    },
+    fail: () => {
+      uni.showToast({ title: t('Msg.socket'), icon: 'none' })
+    }
+  })
+
+  websock.onOpen(() => {
+    console.log('WebSocket opened')
+  })
+
+  websock.onError(() => {
+    clearTimeout(reconnectTimer)
+    reconnectTimer = setTimeout(() => {
+      initWebSocket()
+    }, 3000)
+  })
+
+  websock.onMessage((res: any) => {
+    console.log('接受道消息', res)
+    try {
+      const data = JSON.parse(res.data)
+      if (data.newsType == 3) {
+        pushRes(data)
+      }
+      if (data.newsType == 4) {
+        // 全局广播未读消息数量更新
+        uni.$emit('updateUnreadCount', data.count)
+      }
+    } catch (e) {
+      console.error('WebSocket parse error:', e)
+    }
+  })
+
+  websock.onClose(() => {
+    // console.log('WebSocket closed')
+  })
+}
+
+const pushRes = (data: any) => {
+  const isCn = ['cn', 'zhHant'].includes(locale.value)
+
+  let part1 = ''
+  if (data.pushMessageId && pushManager.value[data.pushMessageId]) {
+    part1 = isCn 
+      ? pushManager.value[data.pushMessageId].content 
+      : pushManager.value[data.pushMessageId].enContent
+  }
+
+  let part2 = ''
+  if (data.approveDesc && reasons.value[data.approveDesc]) {
+    part2 = isCn 
+      ? reasons.value[data.approveDesc].content 
+      : reasons.value[data.approveDesc].enContent
+  }
+
+  const msg = `${part1}\n${part2}`.trim()
+
+  uni.showModal({
+    title: t("news_add_field.Label.Tips"),
+    content: msg || t('news_add_field.Label.Tips'),
+    showCancel: false,
+    confirmText: t('Btn.Confirm'),
+    success: (res) => {
+      if (res.confirm) {
+        pushToSingle(data.newsType)
+      }
+    }
+  })
+}
+
+//获取推送列表
+const  searchPush = async()=> {
+  let res = await customApi.PushMessageList({ type: null });
+  if (res.code == 200) {
+    if (res.data == null) {
+      pushManager.value = {};
+    } else {
+      pushManager.value = res.data;
+    }
+  } else {
+    uni.showToast({title:res.msg,icon:'none'});
+  }
+}
+//获取原因列表
+const  searchReasons = async()=> {
+  let res = await customApi.reasonsRefusalList({ type: null });
+  if (res.code == 200) {
+    if (res.data == null) {
+      reasons.value = {};
+    } else {
+      reasons.value = res.data;
+    }
+  } else {
+    uni.showToast({title:res.msg,icon:'none'});
+  }
+}
+
+
+
+const pushToSingle = (newsType: number) => {
+  switch (newsType) {
+    case 3:
+      router
+        .push({ path: "/pages/customer/recording-history", query: { type: 4 } })
+        .catch((arr) => arr);
+      break;
+  }
+}
+
 onMounted(() => {
   // 只在组件挂载时注册事件监听器
   uni.$once('open-ib', handleOpenIb)
   uni.$on('open-right-drawer', handleOpenRightDrawer)
+  searchPush()
+  searchReasons()
+  initWebSocket()
 })
 
 onUnmounted(() => {
   // 在组件销毁时移除事件监听器
   uni.$off('open-ib', handleOpenIb)
   uni.$off('open-right-drawer', handleOpenRightDrawer)
+  
+  if (websock) {
+    websock.close()
+    websock = null
+  }
+  clearTimeout(reconnectTimer)
 })
 
 function openRightDrawer() {

+ 1 - 1
components/cwg-right-drawer.vue

@@ -100,7 +100,7 @@ watch(() => route, () => {
 
 // 打开抽屉
 function openNotice() {
-    dropdownRef.value?.open()
+    // dropdownRef.value?.open()
 }
 
 // 关闭

+ 4 - 4
pages/mine/improveImmediately.vue

@@ -1447,10 +1447,10 @@
     } else {
       const wsUrl = HostWs.value + '/webSocket?Access-Token=' + token
       websock.value = new WebSocket(wsUrl)
-      websock.value.onmessage = websocketonmessage
-      websock.value.onopen = websocketonopen
-      websock.value.onerror = websocketonerror
-      websock.value.onclose = websocketclose
+      websock.value.onMessage = websocketonmessage
+      websock.value.onOpen = websocketonopen
+      websock.value.onError = websocketonerror
+      websock.value.onClose = websocketclose
     }
   }