解决启动问题

This commit is contained in:
anlicheng 2026-02-27 11:12:19 +08:00
parent 815f82c27e
commit 58aa779a60
5 changed files with 48 additions and 18 deletions

View File

@ -0,0 +1,18 @@
//
// LoginState.swift
// punchnet
//
// Created by on 2026/1/16.
//
import Foundation
import Observation
@Observable
class AppContext {
var noticePort: Int
init(noticePort: Int) {
self.noticePort = noticePort
}
}

View File

@ -48,16 +48,28 @@ struct Node: Codable {
}
}
//
struct NetworkContext: Codable {
let ip: String
let maskLen: UInt8
let hostname: String
let identityId: UInt32
let resourceList: [Resource]
let nodeList: [Node]
enum CodingKeys: String, CodingKey {
case ip
case maskLen = "mask_len"
case hostname
case identityId = "identity_id"
case resourceList = "resource_list"
case nodeList = "node_list"
}
static func `default`() -> Self {
return .init(ip: "", maskLen: 24, hostname: "", identityId: 0, resourceList: [], nodeList: [])
}
}
//
@ -105,10 +117,7 @@ class NetworkModel {
//
var selectedNode: Node?
var ip: String = ""
var resourceList: [Resource] = []
var nodeList: [Node] = []
var networkContext: NetworkContext = .default()
init() {
@ -116,7 +125,7 @@ class NetworkModel {
func changeSelectedNode(nodeId: Int?) {
if let nodeId {
if let node = self.nodeList.first(where: { $0.id == nodeId}) {
if let node = self.networkContext.nodeList.first(where: { $0.id == nodeId}) {
self.selectedNode = node
}
}
@ -128,10 +137,7 @@ class NetworkModel {
"access_token": networkSession.accessToken
]
let networkContext = try await SDLAPIClient.doPost(path: "/connect", params: params, as: NetworkContext.self)
self.ip = networkContext.ip
self.resourceList = networkContext.resourceList
self.nodeList = networkContext.nodeList
self.networkContext = try await SDLAPIClient.doPost(path: "/connect", params: params, as: NetworkContext.self)
self.connectState = .connected
self.isOn = true
}

View File

@ -95,6 +95,7 @@ struct NetworkView: View {
//
struct NetworkDisconnctedView: View {
@Bindable var networkModel: NetworkModel
@Environment(AppContext.self) var appContext: AppContext
@Environment(UserContext.self) var userContext: UserContext
@State private var showAlert = false
@ -163,14 +164,15 @@ struct NetworkDisconnctedView: View {
return
}
let networkContext = networkModel.networkContext
let options = SystemConfig.getOptions(networkId: UInt32(networkSession.networkId),
networkDomain: networkSession.networkDomain,
ip: "",
maskLen: 24,
ip: networkContext.ip,
maskLen: networkContext.maskLen,
accessToken: networkSession.accessToken,
identityId: 1234,
hostname: "mysql",
noticePort: 1234)
identityId: networkContext.identityId,
hostname: networkContext.hostname,
noticePort: self.appContext.noticePort)
// token使token
try await VPNManager.shared.enableVpn(options: options!)
}
@ -185,7 +187,7 @@ struct NetworkResourceGroupView: View {
var body: some View {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 8) {
ForEach(self.networkModel.resourceList, id: \.id) { resource in
ForEach(self.networkModel.networkContext.resourceList, id: \.id) { resource in
NetworkResourceView(resource: resource)
}
}
@ -219,7 +221,7 @@ struct NetworkDeviceGroupView: View {
var body: some View {
NavigationSplitView {
List(self.networkModel.nodeList, id: \.id, selection: $selectedId) { node in
List(self.networkModel.networkContext.nodeList, id: \.id, selection: $selectedId) { node in
NetworkNodeHeadView(node: node)
}
.listStyle(.sidebar)
@ -228,7 +230,7 @@ struct NetworkDeviceGroupView: View {
}
.onAppear {
if selectedId == nil {
selectedId = self.networkModel.nodeList.first?.id
selectedId = self.networkModel.networkContext.nodeList.first?.id
}
}
} detail: {

View File

@ -36,10 +36,13 @@ struct punchnetApp: App {
@ObservedObject var vpnManager = VPNManager.shared
private var noticeServer: UDPNoticeCenterServer
@State private var appContext: AppContext
init() {
self.noticeServer = UDPNoticeCenterServer()
self.noticeServer.start()
self.appContext = AppContext(noticePort: self.noticeServer.port)
}
var body: some Scene {
@ -64,6 +67,7 @@ struct punchnetApp: App {
}
//.toolbar(.hidden)
.navigationTitle("")
.environment(self.appContext)
}
.commands {
CommandGroup(replacing: .appInfo) {