解决启动问题
This commit is contained in:
parent
815f82c27e
commit
58aa779a60
18
punchnet/Views/AppContext.swift
Normal file
18
punchnet/Views/AppContext.swift
Normal 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
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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: {
|
||||
|
||||
@ -9,7 +9,7 @@ import SwiftUI
|
||||
|
||||
struct RootView: View {
|
||||
@State private var userContext = UserContext()
|
||||
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if userContext.isLogined {
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user