解决启动问题
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 {
|
struct NetworkContext: Codable {
|
||||||
let ip: String
|
let ip: String
|
||||||
|
let maskLen: UInt8
|
||||||
|
let hostname: String
|
||||||
|
let identityId: UInt32
|
||||||
let resourceList: [Resource]
|
let resourceList: [Resource]
|
||||||
let nodeList: [Node]
|
let nodeList: [Node]
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case ip
|
case ip
|
||||||
|
case maskLen = "mask_len"
|
||||||
|
case hostname
|
||||||
|
case identityId = "identity_id"
|
||||||
case resourceList = "resource_list"
|
case resourceList = "resource_list"
|
||||||
case nodeList = "node_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 selectedNode: Node?
|
||||||
|
var networkContext: NetworkContext = .default()
|
||||||
var ip: String = ""
|
|
||||||
var resourceList: [Resource] = []
|
|
||||||
var nodeList: [Node] = []
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
|
||||||
@ -116,7 +125,7 @@ class NetworkModel {
|
|||||||
|
|
||||||
func changeSelectedNode(nodeId: Int?) {
|
func changeSelectedNode(nodeId: Int?) {
|
||||||
if let nodeId {
|
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
|
self.selectedNode = node
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,10 +137,7 @@ class NetworkModel {
|
|||||||
"access_token": networkSession.accessToken
|
"access_token": networkSession.accessToken
|
||||||
]
|
]
|
||||||
|
|
||||||
let networkContext = try await SDLAPIClient.doPost(path: "/connect", params: params, as: NetworkContext.self)
|
self.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.connectState = .connected
|
self.connectState = .connected
|
||||||
self.isOn = true
|
self.isOn = true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,6 +95,7 @@ struct NetworkView: View {
|
|||||||
// 网络处于未连接状态
|
// 网络处于未连接状态
|
||||||
struct NetworkDisconnctedView: View {
|
struct NetworkDisconnctedView: View {
|
||||||
@Bindable var networkModel: NetworkModel
|
@Bindable var networkModel: NetworkModel
|
||||||
|
@Environment(AppContext.self) var appContext: AppContext
|
||||||
@Environment(UserContext.self) var userContext: UserContext
|
@Environment(UserContext.self) var userContext: UserContext
|
||||||
|
|
||||||
@State private var showAlert = false
|
@State private var showAlert = false
|
||||||
@ -163,14 +164,15 @@ struct NetworkDisconnctedView: View {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let networkContext = networkModel.networkContext
|
||||||
let options = SystemConfig.getOptions(networkId: UInt32(networkSession.networkId),
|
let options = SystemConfig.getOptions(networkId: UInt32(networkSession.networkId),
|
||||||
networkDomain: networkSession.networkDomain,
|
networkDomain: networkSession.networkDomain,
|
||||||
ip: "",
|
ip: networkContext.ip,
|
||||||
maskLen: 24,
|
maskLen: networkContext.maskLen,
|
||||||
accessToken: networkSession.accessToken,
|
accessToken: networkSession.accessToken,
|
||||||
identityId: 1234,
|
identityId: networkContext.identityId,
|
||||||
hostname: "mysql",
|
hostname: networkContext.hostname,
|
||||||
noticePort: 1234)
|
noticePort: self.appContext.noticePort)
|
||||||
// token存在则优先使用token
|
// token存在则优先使用token
|
||||||
try await VPNManager.shared.enableVpn(options: options!)
|
try await VPNManager.shared.enableVpn(options: options!)
|
||||||
}
|
}
|
||||||
@ -185,7 +187,7 @@ struct NetworkResourceGroupView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 8) {
|
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)
|
NetworkResourceView(resource: resource)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +221,7 @@ struct NetworkDeviceGroupView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationSplitView {
|
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)
|
NetworkNodeHeadView(node: node)
|
||||||
}
|
}
|
||||||
.listStyle(.sidebar)
|
.listStyle(.sidebar)
|
||||||
@ -228,7 +230,7 @@ struct NetworkDeviceGroupView: View {
|
|||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
if selectedId == nil {
|
if selectedId == nil {
|
||||||
selectedId = self.networkModel.nodeList.first?.id
|
selectedId = self.networkModel.networkContext.nodeList.first?.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} detail: {
|
} detail: {
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct RootView: View {
|
struct RootView: View {
|
||||||
@State private var userContext = UserContext()
|
@State private var userContext = UserContext()
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
Group {
|
||||||
if userContext.isLogined {
|
if userContext.isLogined {
|
||||||
|
|||||||
@ -36,10 +36,13 @@ struct punchnetApp: App {
|
|||||||
@ObservedObject var vpnManager = VPNManager.shared
|
@ObservedObject var vpnManager = VPNManager.shared
|
||||||
|
|
||||||
private var noticeServer: UDPNoticeCenterServer
|
private var noticeServer: UDPNoticeCenterServer
|
||||||
|
@State private var appContext: AppContext
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
self.noticeServer = UDPNoticeCenterServer()
|
self.noticeServer = UDPNoticeCenterServer()
|
||||||
self.noticeServer.start()
|
self.noticeServer.start()
|
||||||
|
|
||||||
|
self.appContext = AppContext(noticePort: self.noticeServer.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
@ -64,6 +67,7 @@ struct punchnetApp: App {
|
|||||||
}
|
}
|
||||||
//.toolbar(.hidden)
|
//.toolbar(.hidden)
|
||||||
.navigationTitle("")
|
.navigationTitle("")
|
||||||
|
.environment(self.appContext)
|
||||||
}
|
}
|
||||||
.commands {
|
.commands {
|
||||||
CommandGroup(replacing: .appInfo) {
|
CommandGroup(replacing: .appInfo) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user