diff --git a/composables/configJson.ts b/composables/configJson.ts index a0ce63a..d9ee8e8 100644 --- a/composables/configJson.ts +++ b/composables/configJson.ts @@ -22,6 +22,9 @@ export const updateConfigJson = async (configJsonSeverUrl: Array | strin saveErrorLog, logLevel, serverUrl, + port, + enableCustomListener, + customListenerData, ...otherConfig } = mainStore.config; let writeServerUrl: Array | string = serverUrl; diff --git a/composables/netcard.ts b/composables/netcard.ts index c84833a..4baf8ec 100644 --- a/composables/netcard.ts +++ b/composables/netcard.ts @@ -6,51 +6,55 @@ import { BaseDirectory } from "@tauri-apps/api/path"; import { exists } from "@tauri-apps/plugin-fs"; const getWinIpBroadcastPid = async () => { - const mainStore = useMainStore(); - const pid = await invoke("search_pid_by_pname", { target_process_name: "WinIPBroadcast" }); - mainStore.winipBcPid = (pid as number) || 0; - if (mainStore.winipBcPid && mainStore.winipBcPid > 0) { - mainStore.winipBcStart = true; - } else { - mainStore.winipBcStart = false; - } + const mainStore = useMainStore(); + const pid = await invoke("search_pid_by_pname", { target_process_name: "WinIPBroadcast" }); + mainStore.winipBcPid = (pid as number) || 0; + mainStore.$patch({ + winipBcStart: !!(mainStore.winipBcPid && mainStore.winipBcPid > 0) + }); }; export const handleWinipBcStart = async () => { - const mainStore = useMainStore(); - if (!mainStore.winipBcStart) { - try { - await invoke("stop_command", { child_id: mainStore.winipBcPid || 0 }); - const isExists = await exists("easytier/tool/WinIPBroadcast.exe", { baseDir: BaseDirectory.Resource }); - if(!isExists) { - ElMessage.error(`WinipBc不存在`); - console.error(`WinipBc不存在`); - return; - } - const child = await Command.create("WinIPBroadcast", ["run"]).spawn(); - mainStore.winipBcPid = child.pid || 0; - if (mainStore.winipBcPid) { - mainStore.winipBcStart = true; - mainStore.winIpBcAutoStart = true; - } else { - ElMessage.error(`WinipBc失败`); - } - } catch (err) { - ElMessage.error(`WinipBc失败`); - console.error(err); - } - } else { - mainStore.winIpBcAutoStart = false; - await invoke("stop_command", { child_id: mainStore.winipBcPid || 0 }); - await getWinIpBroadcastPid(); - } + const mainStore = useMainStore(); + if (!mainStore.winipBcStart) { + try { + await invoke("stop_command", { child_id: mainStore.winipBcPid || 0 }); + const isExists = await exists("easytier/tool/WinIPBroadcast.exe", { baseDir: BaseDirectory.Resource }); + if (!isExists) { + ElMessage.error(`WinipBc不存在`); + console.error(`WinipBc不存在`); + return; + } + const child = await Command.create("WinIPBroadcast", ["run"]).spawn(); + mainStore.winipBcPid = child.pid || 0; + await getWinIpBroadcastPid(); + if (mainStore.winipBcPid) { + mainStore.$patch({ + winIpBcAutoStart: true + }); + } else { + ElMessage.error(`WinipBc失败`); + } + } catch (err) { + ElMessage.error(`WinipBc失败`); + console.error(err); + } + } else { + await invoke("stop_command", { child_id: mainStore.winipBcPid || 0 }); + await getWinIpBroadcastPid(); + if (mainStore.winipBcPid <= 0) { + mainStore.$patch({ + winIpBcAutoStart: true + }); + } + } }; export const initStartWinIpBroadcast = async () => { - const mainStore = useMainStore(); - await getWinIpBroadcastPid(); - // console.error(mainStore.winipBcStart, mainStore.winIpBcAutoStart) - if (mainStore.winIpBcAutoStart && !mainStore.winipBcStart) { - await handleWinipBcStart(); - } -}; \ No newline at end of file + const mainStore = useMainStore(); + await getWinIpBroadcastPid(); + // console.error(mainStore.winipBcStart, mainStore.winIpBcAutoStart) + if (mainStore.winIpBcAutoStart && !mainStore.winipBcStart) { + await handleWinipBcStart(); + } +}; diff --git a/composables/server.ts b/composables/server.ts index 6a282ed..11c53ff 100644 --- a/composables/server.ts +++ b/composables/server.ts @@ -14,9 +14,8 @@ export const getServerArgs = () => { .split("\n") .map(el => el.trim()) .filter(el => el) - .join(" "); if (whiteList && mainStore.serverConfig.enableWhiteList) { - args.push("--relay-network-whitelist", whiteList); + args.push("--relay-network-whitelist", ...whiteList); } if (!whiteList && mainStore.serverConfig.enableWhiteList) { args.push("--relay-network-whitelist"); diff --git a/composables/windows.ts b/composables/windows.ts index df382f7..f344f5e 100644 --- a/composables/windows.ts +++ b/composables/windows.ts @@ -2,17 +2,22 @@ import { type UnlistenFn } from "@tauri-apps/api/event"; import { getCurrentWindow, PhysicalPosition, type WindowOptions, Window, currentMonitor } from "@tauri-apps/api/window"; import { WebviewWindow } from "@tauri-apps/api/webviewWindow"; import type { WebviewLabel, WebviewOptions } from "@tauri-apps/api/webview"; +import useMainStore from "@/stores/index"; +import { onBeforeUnmount } from "vue"; + +const _listenersMaps: { [key: string]: [UnlistenFn | null, UnlistenFn | null] } = {}; + export default async ( label: WebviewLabel, options?: Omit & WindowOptions, afterCreatedFunc?: (webviewWindow: WebviewWindow, appWindow: Window) => void | null, beforeCloseFunc?: () => void | null ) => { - let unListenlogClose: UnlistenFn | null = null; - let unlistenLogCreated: UnlistenFn | null = null; + const unlistenFnList: [UnlistenFn | null, UnlistenFn | null] = _listenersMaps[label] || [null, null]; + let [unlistenLogCreated, unListenlogClose] = unlistenFnList; let dialog = await WebviewWindow.getByLabel(label); if (!dialog) { - let defaultOpts: {[key:string]: any, parent: Window | undefined} = { + let defaultOpts: { [key: string]: any; parent: Window | undefined } = { parent: undefined, closable: true, resizable: true, @@ -25,17 +30,16 @@ export default async ( const appWindow = getCurrentWindow(); if (appWindow) { const appSize = await appWindow.outerSize(); - // const monitor = await currentMonitor(); - // console.error(monitor) const factor = await appWindow.scaleFactor(); const appPosition = await appWindow.outerPosition(); - // console.error((appPosition.x + appSize.width) / 1.25); const logicalPosition = new PhysicalPosition(appPosition.x + appSize.width, appPosition.y).toLogical(factor); defaultOpts.parent = appWindow; // console.error(logicalPosition); defaultOpts.x = logicalPosition.x; defaultOpts.y = logicalPosition.y; } + unlistenLogCreated && (await (unlistenLogCreated as Function)()); + unListenlogClose && (await unListenlogClose()); dialog = new WebviewWindow(label, { ...defaultOpts, ...options }); unlistenLogCreated = await dialog.listen("tauri://webview-created", async () => { if (dialog) { @@ -43,10 +47,14 @@ export default async ( await dialog.show(); } }); + unlistenFnList[0] = unlistenLogCreated; unListenlogClose = await dialog.onCloseRequested(async () => { beforeCloseFunc && (await beforeCloseFunc()); unListenlogClose && (await unListenlogClose()); + unlistenLogCreated && (await (unlistenLogCreated as Function)()); }); + unlistenFnList[1] = unlistenLogCreated; + _listenersMaps[label] = unlistenFnList; } else { const visible = await dialog.isVisible(); if (visible) { @@ -55,3 +63,25 @@ export default async ( } } }; + +export const dataSubscribe = async (cb?: (...args: any) => any) => { + if (!cb) return; + const mainStore = useMainStore(); + const currentWindow = getCurrentWindow(); + let removeSubscribe = mainStore.$subscribe(async (...args) => { + const emitData = await cb(args); + await currentWindow.emitTo({ kind: "Window", label: "main" }, "config", emitData); + }); + const unlisten = currentWindow.listen("global-main-store", event => { + removeSubscribe && removeSubscribe(); + mainStore.$patch({ ...event.payload.store }); // 更新全局状态 + removeSubscribe = mainStore.$subscribe(async (...args) => { + const emitData = await cb(args); + await currentWindow.emitTo({ kind: "Window", label: "main" }, "config", emitData); + }); + }); + onBeforeUnmount(async () => { + unlisten && (await unlisten)(); + removeSubscribe && removeSubscribe(); + }); +}; diff --git a/package.json b/package.json index b88f9f2..16de156 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "private": true, "author": "leizi97", "description": "A simple network initiator based on Easytier", - "version": "1.2.8", + "version": "1.2.9", "scripts": { "dev": "nuxt dev --dotenv env/.env.dev --host 0.0.0.0", "build": "nuxt generate --dotenv env/.env.prod", @@ -16,7 +16,6 @@ "@pinia/nuxt": "0.8.0", "@tauri-apps/api": "^2.1.1", "@tauri-apps/cli": "^2.1.0", - "@tauri-apps/plugin-autostart": "~2", "@tauri-apps/plugin-cli": "^2.0.0", "@tauri-apps/plugin-clipboard-manager": "^2.0.0", "@tauri-apps/plugin-fs": "~2", diff --git a/pages/advance.vue b/pages/advance.vue index 03cb785..c870307 100644 --- a/pages/advance.vue +++ b/pages/advance.vue @@ -1,7 +1,29 @@ diff --git a/pages/index.vue b/pages/index.vue index 6ad0269..3f5957d 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -10,7 +10,7 @@ class="full-label" >