137 lines
3.8 KiB
Swift
137 lines
3.8 KiB
Swift
//
|
|
// URLManager.swift
|
|
// SHECommunity
|
|
//
|
|
// Created by 林 on 2024/11/26.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftyUserDefaults
|
|
import RxSwift
|
|
|
|
extension DefaultsKeys {
|
|
// 接口环境 0:测试环境 1:正式环境
|
|
var apiEnvKey: DefaultsKey<Int> { .init("ApiEnvKey", defaultValue: -99) }
|
|
// 接口地址
|
|
var apiServerURL: DefaultsKey<String> { .init("ApiServerURL", defaultValue: "") }
|
|
}
|
|
|
|
@objcMembers class URLManager: NSObject {
|
|
static let shared = URLManager()
|
|
|
|
// -1: UAT 0: 开发 1: 正式
|
|
var apiEnv: Int = 0
|
|
/// 接口地址
|
|
var apiServerURL: String = "" {
|
|
didSet {
|
|
AppNetworkConfig.shared.baseURL = apiServerURL
|
|
}
|
|
}
|
|
|
|
/// OpenIM SDK
|
|
var openIM_API = ""
|
|
var openIM_WS = ""
|
|
|
|
/// 用户协议
|
|
var userAgreementUrl: String { "https://smartdrive.zuom8.cn/jisu/Agreement.html" }
|
|
/// 隐私政策
|
|
var privacyPolicyUrl: String { "https://smartdrive.zuom8.cn/jisu/Privacy.html" }
|
|
/// 儿童
|
|
var kidsPrivacyUrl: String { "https://smartdrive.zuom8.cn/jisu/KidsPrivacy.html" }
|
|
/// 注销须知
|
|
var cancellationNoticeUrl: String { "https://smartdrive.zuom8.cn/jisu/Cancellation.html" }
|
|
|
|
private override init() {
|
|
super.init()
|
|
self.setupNetworkMode()
|
|
}
|
|
|
|
func setupNetworkMode() {
|
|
#if DEBUG
|
|
setupApiEnv(0)
|
|
openIM_API = "https://imapi.zuom8.cn" // http://38.207.176.65:10002
|
|
openIM_WS = "ws://imws.zuom8.cn" // ws://38.207.176.65:10010
|
|
#elseif AdHoc
|
|
setupApiEnv(0)
|
|
openIM_API = "https://imapi.zuom8.cn"
|
|
openIM_WS = "ws://imws.zuom8.cn"
|
|
#else
|
|
setupApiEnv(1)
|
|
openIM_API = "https://imapi.zuom8.cn"
|
|
openIM_WS = "ws://imws.zuom8.cn"
|
|
#endif
|
|
}
|
|
|
|
private func setupApiEnv(_ env: Int) {
|
|
// env
|
|
if Defaults[\.apiEnvKey] != -99 {
|
|
apiEnv = Defaults[\.apiEnvKey]
|
|
}
|
|
else {
|
|
apiEnv = env
|
|
}
|
|
// api
|
|
if Defaults[\.apiServerURL].isEmpty {
|
|
apiServerURL = apiServerURL(apiEnv)
|
|
} else {
|
|
apiServerURL = Defaults[\.apiServerURL]
|
|
}
|
|
}
|
|
|
|
// MARK: - 接口地址
|
|
func apiServerURL(_ env: Int) -> String {
|
|
switch env {
|
|
case 1: // 正式
|
|
return "https://jsapi.zuom8.cn/"
|
|
case -1: // UAT
|
|
return "https://jsapi.zuom8.cn/"
|
|
default: // SIT
|
|
return "http://172.16.10.23:9243/"
|
|
}
|
|
}
|
|
|
|
static let presetServers: [(title: String, url: String, env: Int)] = [
|
|
("开发", "http://172.16.10.23:9243/", 0),
|
|
("正式", "https://jsapi.zuom8.cn/", 1)
|
|
]
|
|
|
|
func switchServer(url: String, env: Int? = nil) {
|
|
let normalized = Self.normalizedServerURL(url)
|
|
guard !normalized.isEmpty else { return }
|
|
let resolvedEnv = env ?? Self.env(for: normalized)
|
|
Defaults[\.apiEnvKey] = resolvedEnv
|
|
Defaults[\.apiServerURL] = normalized
|
|
apiEnv = resolvedEnv
|
|
apiServerURL = normalized
|
|
}
|
|
|
|
static func normalizedServerURL(_ raw: String) -> String {
|
|
var url = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !url.isEmpty else { return "" }
|
|
if !url.contains("://") {
|
|
url = "http://" + url
|
|
}
|
|
if !url.hasSuffix("/") {
|
|
url += "/"
|
|
}
|
|
return url
|
|
}
|
|
|
|
static func env(for url: String) -> Int {
|
|
let normalized = normalizedServerURL(url)
|
|
if let preset = presetServers.first(where: { normalizedServerURL($0.url) == normalized }) {
|
|
return preset.env
|
|
}
|
|
return 99
|
|
}
|
|
|
|
// MARK: - 大对象服务器地址
|
|
func uploadServerURL() -> String {
|
|
//#if DEBUG
|
|
// return ""
|
|
//#else
|
|
return ""
|
|
//#endif
|
|
}
|
|
}
|