fix(mobile): 🐛 fix error when mobile receives message

distinguish between pc and mobile ports
This commit is contained in:
Dawn
2025-10-08 17:17:15 +08:00
parent 71845124ce
commit ba55a2f7df
8 changed files with 64 additions and 53 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -7,7 +7,7 @@
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"frontendDist": "../dist",
"devUrl": "http://127.0.0.1:6130"
"devUrl": "http://127.0.0.1:1020"
},
"bundle": {
"active": true,
+1 -1
View File
@@ -7,7 +7,7 @@
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"frontendDist": "../dist",
"devUrl": "http://127.0.0.1:6130"
"devUrl": "http://127.0.0.1:1020"
},
"bundle": {
"active": true,
+22 -16
View File
@@ -19,7 +19,6 @@
<script setup lang="ts">
import { emitTo } from '@tauri-apps/api/event'
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
import { type } from '@tauri-apps/plugin-os'
import { useRoute } from 'vue-router'
import SafeAreaPlaceholder from '#/components/placeholders/SafeAreaPlaceholder.vue'
import { NotificationTypeEnum, TauriCommand } from '@/enums'
@@ -31,6 +30,7 @@ import { useGlobalStore } from '@/stores/global'
import { useSettingStore } from '@/stores/setting'
import { useUserStore } from '@/stores/user'
import { audioManager } from '@/utils/AudioManager'
import { isMobile, isWindows } from '@/utils/PlatformConstants'
import { invokeSilently } from '@/utils/TauriInvokeHandler'
const route = useRoute()
@@ -68,26 +68,28 @@ useMitt.on(WsResponseMessageType.RECEIVE_MESSAGE, async (data: MessageType) => {
// 只有非免打扰的会话才发送通知和触发图标闪烁
if (session && session.muteNotification !== NotificationTypeEnum.NOT_DISTURB) {
// 检查 home 窗口状态
const home = await WebviewWindow.getByLabel('mobile-home')
let shouldPlaySound = false
let shouldPlaySound = isMobile()
if (home) {
if (!isMobile()) {
try {
const isVisible = await home.isVisible()
const isMinimized = await home.isMinimized()
const isFocused = await home.isFocused()
const home = await WebviewWindow.getByLabel('mobile-home')
// 如果窗口不可见、被最小化或未聚焦,则播放音效
shouldPlaySound = !isVisible || isMinimized || !isFocused
if (home) {
const isVisible = await home.isVisible()
const isMinimized = await home.isMinimized()
const isFocused = await home.isFocused()
// 如果窗口不可见、被最小化或未聚焦,则播放音效
shouldPlaySound = !isVisible || isMinimized || !isFocused
} else {
// 如果找不到home 窗口,播放音效
shouldPlaySound = true
}
} catch (error) {
console.warn('检查窗口状态失败:', error)
// 如果检查失败,默认播放音效
shouldPlaySound = true
}
} else {
// 如果找不到 home 窗口,播放音效
shouldPlaySound = true
}
// 播放消息音效
@@ -99,12 +101,16 @@ useMitt.on(WsResponseMessageType.RECEIVE_MESSAGE, async (data: MessageType) => {
// useMitt.emit(MittEnum.MESSAGE_ANIMATION, data)
// session.unreadCount++
// 在windows系统下才发送通知
if (type() === 'windows') {
if (!isMobile() && isWindows()) {
globalStore.setTipVisible(true)
}
if (WebviewWindow.getCurrent().label === 'mobile-home') {
await emitTo('notify', 'notify_content', data)
if (!isMobile()) {
const currentWindow = WebviewWindow.getCurrent()
if (currentWindow.label === 'mobile-home') {
await emitTo('notify', 'notify_content', data)
}
}
}
}
+29 -18
View File
@@ -27,7 +27,6 @@
import { emitTo } from '@tauri-apps/api/event'
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
import { info } from '@tauri-apps/plugin-log'
import { type } from '@tauri-apps/plugin-os'
import { useRoute } from 'vue-router'
import SafeAreaPlaceholder from '#/components/placeholders/SafeAreaPlaceholder.vue'
import type { default as TabBarType } from '#/layout/tabBar/index.vue'
@@ -42,6 +41,7 @@ import { useGroupStore } from '@/stores/group'
import { useSettingStore } from '@/stores/setting'
import { useUserStore } from '@/stores/user'
import { audioManager } from '@/utils/AudioManager'
import { isMobile, isWindows } from '@/utils/PlatformConstants'
import { invokeSilently } from '@/utils/TauriInvokeHandler'
import { useContactStore } from '~/src/stores/contacts'
@@ -185,40 +185,49 @@ useMitt.on(WsResponseMessageType.RECEIVE_MESSAGE, async (data: MessageType) => {
// 只有非免打扰的会话才发送通知和触发图标闪烁
if (session && session.muteNotification !== NotificationTypeEnum.NOT_DISTURB) {
// 检查 home 窗口状态
const home = await WebviewWindow.getByLabel('mobile-home')
let shouldPlaySound = false
let shouldPlaySound = isMobile()
if (home) {
if (!isMobile()) {
try {
const isVisible = await home.isVisible()
const isMinimized = await home.isMinimized()
const isFocused = await home.isFocused()
const home = await WebviewWindow.getByLabel('mobile-home')
// 如果窗口不可见、被最小化或未聚焦,则播放音效
shouldPlaySound = !isVisible || isMinimized || !isFocused
if (home) {
const isVisible = await home.isVisible()
const isMinimized = await home.isMinimized()
const isFocused = await home.isFocused()
// 如果窗口不可见、被最小化或未聚焦,则播放音效
shouldPlaySound = !isVisible || isMinimized || !isFocused
} else {
// 如果找不到home 窗口,播放音效
shouldPlaySound = true
}
} catch (error) {
console.warn('检查窗口状态失败:', error)
// 如果检查失败,默认播放音效
shouldPlaySound = true
}
} else {
// 如果找不到 home 窗口,播放音效
shouldPlaySound = true
}
// 播放消息音效
if (shouldPlaySound) {
await playMessageSound()
}
// 设置图标闪烁
// useMitt.emit(MittEnum.MESSAGE_ANIMATION, data)
// session.unreadCount++
// 在windows系统下才发送通知
if (type() === 'windows') {
if (!isMobile() && isWindows()) {
globalStore.setTipVisible(true)
}
if (WebviewWindow.getCurrent().label === 'mobile-home') {
await emitTo('notify', 'notify_content', data)
if (!isMobile()) {
const currentWindow = WebviewWindow.getCurrent()
if (currentWindow.label === 'mobile-home') {
await emitTo('notify', 'notify_content', data)
}
}
}
}
@@ -274,8 +283,10 @@ useMitt.on(WsResponseMessageType.TOKEN_EXPIRED, async (wsTokenExpire: WsTokenExp
if (Number(userUid.value) === Number(wsTokenExpire.uid) && userStore.userInfo!.client === wsTokenExpire.client) {
console.log('收到用户token过期通知', wsTokenExpire)
// 聚焦主窗口
const home = await WebviewWindow.getByLabel('home')
await home?.setFocus()
if (!isMobile()) {
const home = await WebviewWindow.getByLabel('home')
await home?.setFocus()
}
useMitt.emit(MittEnum.LEFT_MODAL_SHOW, {
type: ModalEnum.REMOTE_LOGIN,
props: {
+6 -4
View File
@@ -161,6 +161,7 @@ import { useSettingStore } from '@/stores/setting'
import { useUserStore } from '@/stores/user'
import { AvatarUtils } from '@/utils/AvatarUtils'
import { searchFriend, searchGroup } from '@/utils/ImRequestUtils'
import { isMobile } from '@/utils/PlatformConstants'
import router from '~/src/router'
const contactStore = useContactStore()
@@ -169,7 +170,6 @@ const globalStore = useGlobalStore()
const settingStore = useSettingStore()
const cachedStore = useCachedStore()
const { themes } = storeToRefs(settingStore)
// 定义标签页
const tabs = ref([
{ name: 'recommend', label: '推荐' },
@@ -414,9 +414,11 @@ const handleAddFriend = async (item: any) => {
// 处理编辑个人资料
const handleEditProfile = async () => {
// 获取主窗口
const homeWindow = await WebviewWindow.getByLabel('home')
// 激活主窗口
await homeWindow?.setFocus()
if (!isMobile()) {
const homeWindow = await WebviewWindow.getByLabel('home')
// 激活主窗口
await homeWindow?.setFocus()
}
// 打开个人资料编辑窗口
emitTo('home', 'open_edit_info')
}
-10
View File
@@ -52,10 +52,8 @@ declare module 'vue' {
NaiveProvider: typeof import('./../components/common/NaiveProvider.vue')['default']
NAutoComplete: typeof import('naive-ui')['NAutoComplete']
NAvatar: typeof import('naive-ui')['NAvatar']
NAvatarGroup: typeof import('naive-ui')['NAvatarGroup']
NBadge: typeof import('naive-ui')['NBadge']
NButton: typeof import('naive-ui')['NButton']
NButtonGroup: typeof import('naive-ui')['NButtonGroup']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup']
NCollapse: typeof import('naive-ui')['NCollapse']
@@ -73,7 +71,6 @@ declare module 'vue' {
NFormItem: typeof import('naive-ui')['NFormItem']
NHighlight: typeof import('naive-ui')['NHighlight']
NIcon: typeof import('naive-ui')['NIcon']
NIconWrapper: typeof import('naive-ui')['NIconWrapper']
NImage: typeof import('naive-ui')['NImage']
NImageGroup: typeof import('naive-ui')['NImageGroup']
NInput: typeof import('naive-ui')['NInput']
@@ -83,8 +80,6 @@ declare module 'vue' {
NModalProvider: typeof import('naive-ui')['NModalProvider']
NNotificationProvider: typeof import('naive-ui')['NNotificationProvider']
NPopover: typeof import('naive-ui')['NPopover']
NPopselect: typeof import('naive-ui')['NPopselect']
NProgress: typeof import('naive-ui')['NProgress']
NQrCode: typeof import('naive-ui')['NQrCode']
NRadio: typeof import('naive-ui')['NRadio']
NResult: typeof import('naive-ui')['NResult']
@@ -94,14 +89,9 @@ declare module 'vue' {
NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin']
NSwitch: typeof import('naive-ui')['NSwitch']
NTab: typeof import('naive-ui')['NTab']
NTabPane: typeof import('naive-ui')['NTabPane']
NTabs: typeof import('naive-ui')['NTabs']
NTag: typeof import('naive-ui')['NTag']
NTimeline: typeof import('naive-ui')['NTimeline']
NTimelineItem: typeof import('naive-ui')['NTimelineItem']
NTooltip: typeof import('naive-ui')['NTooltip']
NTransfer: typeof import('naive-ui')['NTransfer']
NUpload: typeof import('naive-ui')['NUpload']
NVirtualList: typeof import('naive-ui')['NVirtualList']
PersonalInfo: typeof import('./../mobile/components/my/PersonalInfo.vue')['default']
+4 -2
View File
@@ -44,6 +44,8 @@ export default defineConfig(({ mode }: ConfigEnv) => {
config.TAURI_ENV_PLATFORM === 'darwin' ||
config.TAURI_ENV_PLATFORM === 'linux'
const serverPort = isPC ? 6130 : 1020
// 根据平台决定host地址
const host = (() => {
if (isPC) {
@@ -175,11 +177,11 @@ export default defineConfig(({ mode }: ConfigEnv) => {
hmr: {
protocol: 'ws',
host: host,
port: 6130
port: serverPort
},
cors: true, // 配置 CORS
host: '0.0.0.0',
port: 6130,
port: serverPort,
strictPort: true,
watch: {
// 3. tell vite to ignore watching `src-tauri`