- 端口 17311 → **17300**(桌面端常量、localhost/127.0.0.1 双前缀、前端 LOCAL_BASE、
插件默认目标与说明、两个 README 全部同步)。
- `/api/health` 返回值加上 `loggedIn` / `user`(由 App 提供:IsLoggedInProvider、UserNameProvider),
前端只认 `ok`(探活),再用 `loggedIn` 决定要不要唤起登录。
- 新增 `POST /api/activate-login`:叫本机软件把登录窗(已登录则主界面)带到前台。
用 POST 而不是 GET,避免被浏览器/聊天工具的链接预取误触发;CORS Allow-Methods 加了 POST。
- `/api/query/{code}` 未登录时返回 401(前端会转去唤起登录)。
- 桌面端:App 记住登录窗引用,收到 LoginRequested 时切回 UI 线程激活它(托盘里也能叫出来)。
- 前端 `/local?code=`:
- 路由加 alias `/local/`;
- 新增 need-login 状态:探活到「软件在跑但没登录」→ 自动 POST activate-login →
每秒探活等登录(30s 上限)→ 登录完成后自动查询;期间有「再叫一次登录窗口」按钮与托盘提示;
- api/local.ts 增加 localHealth()(online/loggedIn/user)、requestLocalLogin()、waitForLoggedIn();
waitForHealth() 改为返回状态而非 bool;老版本软件没有 loggedIn 字段时按已登录处理(向后兼容)。
- 实测(7301 + 模拟的 17300):打开 /local/?code=… → 「本机软件未登录 / 正在等待登录…」→
mock 翻成已登录后自动切到查询结果(5 行);杀掉 mock 后显示「服务暂不可用」且探测地址为 17300。
121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace YuncangTrace.LocalService.Services;
|
|
|
|
/// <summary>
|
|
/// 单实例 + 参数转发(命名管道)
|
|
///
|
|
/// 场景:窗口已经开着,用户又点了一次 WMS 页面上的「查看打包视频」→ 系统按协议再启动一次本程序。
|
|
/// 这时第二个实例**不应该**去抢 17300 端口,而是把 `yuncang://open?code=xxx` 通过管道送给第一个实例,
|
|
/// 由它把窗口激活并查询那个运单号。
|
|
/// </summary>
|
|
public sealed class SingleInstance : IDisposable
|
|
{
|
|
private readonly Mutex _mutex;
|
|
private readonly string _pipeName;
|
|
private readonly Action<string[]> _onArgs;
|
|
private readonly CancellationTokenSource _cts = new();
|
|
private readonly bool _ownsMutex;
|
|
|
|
/// <summary>本次是否是第一个实例(不是的话调用 <see cref="SendArgs"/> 后就可以退出了)</summary>
|
|
public bool IsFirstInstance { get; }
|
|
|
|
public SingleInstance(string name, Action<string[]> onArgs)
|
|
{
|
|
_onArgs = onArgs;
|
|
_pipeName = $"{name}.pipe";
|
|
|
|
// 全局命名互斥体:跨会话也能认出来(同一个用户下就够了,用 Local\ 前缀)
|
|
_mutex = new Mutex(initiallyOwned: true, $@"Local\{name}.mutex", out var createdNew);
|
|
_ownsMutex = createdNew;
|
|
IsFirstInstance = createdNew;
|
|
}
|
|
|
|
/// <summary>第一个实例调用:开一个后台线程等第二个实例送参数过来</summary>
|
|
public void StartListening()
|
|
{
|
|
var thread = new Thread(ListenLoop)
|
|
{
|
|
IsBackground = true,
|
|
Name = "single-instance-pipe",
|
|
};
|
|
thread.Start();
|
|
}
|
|
|
|
/// <summary>第二个实例调用:把命令行参数送给已运行的实例(连不上就算了,不影响退出)</summary>
|
|
public void SendArgs(string[] args)
|
|
{
|
|
if (args.Length == 0) return;
|
|
|
|
try
|
|
{
|
|
using var client = new NamedPipeClientStream(".", _pipeName, PipeDirection.Out);
|
|
client.Connect(1500);
|
|
|
|
using var writer = new StreamWriter(client, new UTF8Encoding(false)) { AutoFlush = true };
|
|
foreach (var arg in args) writer.WriteLine(arg);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// 拿不到第一个实例(可能正在退出):静默忽略,用户重新点一次即可
|
|
}
|
|
}
|
|
|
|
private void ListenLoop()
|
|
{
|
|
while (!_cts.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
using var server = new NamedPipeServerStream(
|
|
_pipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances,
|
|
PipeTransmissionMode.Byte, PipeOptions.None);
|
|
|
|
server.WaitForConnection();
|
|
|
|
var args = new List<string>();
|
|
using (var reader = new StreamReader(server, new UTF8Encoding(false)))
|
|
{
|
|
string? line;
|
|
while ((line = reader.ReadLine()) is not null)
|
|
{
|
|
if (line.Length > 0) args.Add(line);
|
|
}
|
|
}
|
|
|
|
if (args.Count > 0) _onArgs(args.ToArray());
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (_cts.IsCancellationRequested) return;
|
|
Thread.Sleep(200);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_cts.Cancel();
|
|
|
|
if (_ownsMutex)
|
|
{
|
|
try
|
|
{
|
|
_mutex.ReleaseMutex();
|
|
}
|
|
catch (ApplicationException)
|
|
{
|
|
// 不持有互斥体(理论上不会发生)
|
|
}
|
|
}
|
|
|
|
_mutex.Dispose();
|
|
_cts.Dispose();
|
|
}
|
|
}
|