|
|
@@ -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() {
|