237 lines
7.7 KiB
Swift
237 lines
7.7 KiB
Swift
|
|
|
|
import Foundation
|
|
import Network
|
|
import Kingfisher
|
|
import Alamofire
|
|
import SVProgressHUD
|
|
|
|
|
|
class XINGYStartVC: UIViewController {
|
|
|
|
var presentXY : XYStartPresent = {
|
|
let presentXY = XYStartPresent()
|
|
return presentXY
|
|
}()
|
|
var isShowXYFreeTimeTip : Bool = false
|
|
|
|
let XYmonitor = NWPathMonitor()
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
self.navigationController?.navigationBar.isHidden = true
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
setNeedsStatusBarAppearanceUpdate()
|
|
SVProgressHUD.show(withStatus: "正在加载...")
|
|
presentXY.attechView(self)
|
|
XYInitNetWorlk()
|
|
}
|
|
func XYInitNetWorlk() {
|
|
if !UserDefaults.standard.bool(forKey: XINGYUserField.IsXINGYRegister) {
|
|
XYmonitor.pathUpdateHandler = { [weak self] path in
|
|
guard let self = self else { return } // 使用 guard let 安全解包 self
|
|
if path.status == .satisfied {
|
|
self.XINGYgetHostInfo()
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
|
self.presentXY.fetchRepXYVpnVersion()
|
|
}
|
|
self.XYmonitor.cancel()
|
|
} else {
|
|
print("网络不可用")
|
|
// self.initXYRep()
|
|
}
|
|
}
|
|
|
|
let queue = DispatchQueue(label: "NetworkMonitor")
|
|
XYmonitor.start(queue: queue)
|
|
}
|
|
else{
|
|
self.XINGYgetHostInfo()
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
|
self.presentXY.fetchRepXYVpnVersion()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func XINGYgetHostInfo(){
|
|
|
|
|
|
AF.request(XINGYUserField.CurrentNewXYUrlHostOne, method: .get).cacheResponse(using: ResponseCacher.doNotCache).responseString{ (XINGYreturnResult) in
|
|
switch XINGYreturnResult.result{
|
|
case .success(_):
|
|
if let XINGYrspData = XINGYreturnResult.value {
|
|
if !XINGYrspData.isEmpty {
|
|
let XINGYdataStr = AESUtils.aesXYDecrypt(XINGYrspData)
|
|
let XINGYhostArray = XINGYdataStr?.split(separator: "+")
|
|
UserDefaults.standard.set(XINGYhostArray, forKey: XINGYUserField.XYHostArr)
|
|
}
|
|
|
|
}
|
|
case .failure(_):
|
|
|
|
self.XINGYgetHostInfoOne()
|
|
}
|
|
}
|
|
}
|
|
|
|
func XINGYgetHostInfoOne(){
|
|
|
|
AF.request(XINGYUserField.CurrentNewXYUrlHostTwo, method: .get).cacheResponse(using: ResponseCacher.doNotCache).responseString{ (XINGYreturnResult) in
|
|
switch XINGYreturnResult.result{
|
|
case .success(_):
|
|
if let XINGYrspData = XINGYreturnResult.value {
|
|
if !XINGYrspData.isEmpty {
|
|
let XINGYdataStr = AESUtils.aesXYDecrypt(XINGYrspData)
|
|
let XINGYhostArray = XINGYdataStr?.split(separator: "+")
|
|
UserDefaults.standard.set(XINGYhostArray, forKey: XINGYUserField.XYHostArr)
|
|
}
|
|
}
|
|
case .failure(_):
|
|
self.XINGYgetHostInfoTwo()
|
|
}
|
|
}
|
|
}
|
|
func XINGYgetHostInfoTwo(){
|
|
|
|
AF.request(XINGYUserField.CurrentNewXYUrlHostThree, method: .get).cacheResponse(using: ResponseCacher.doNotCache).responseString{ (XINGYreturnResult) in
|
|
switch XINGYreturnResult.result{
|
|
case .success(_):
|
|
if let XINGYrspData = XINGYreturnResult.value {
|
|
if !XINGYrspData.isEmpty {
|
|
let XINGYdataStr = AESUtils.aesXYDecrypt(XINGYrspData)
|
|
let XINGYhostArray = XINGYdataStr?.split(separator: "+")
|
|
UserDefaults.standard.set(XINGYhostArray, forKey: XINGYUserField.XYHostArr)
|
|
}
|
|
}
|
|
case .failure(_):
|
|
print("获取主机失败3")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func initXYRep(){
|
|
if UserDefaults.standard.bool(forKey: XINGYUserField.IsXINGYRegister) {
|
|
let tokenXY = UserDefaults.standard.string(forKey: XINGYUserField.VpnToken)
|
|
if tokenXY != nil && !tokenXY!.isEmpty {
|
|
isShowXYFreeTimeTip = false
|
|
getXYUserInfo()
|
|
} else {
|
|
XINGYgoLoginCt()
|
|
}
|
|
} else {
|
|
if(isMoRenYuYanXYZhongWen()){
|
|
self.registerXYReq()
|
|
} else {
|
|
XINGYgoLoginCt()
|
|
}
|
|
|
|
}
|
|
}
|
|
func isMoRenYuYanXYZhongWen() -> Bool {
|
|
if let preferredLanguage = Locale.preferredLanguages.first {
|
|
return preferredLanguage.hasPrefix("zh")
|
|
}
|
|
return false
|
|
}
|
|
|
|
func XINGYgoLoginCt(){
|
|
let LVC = XINGYLoginVC()
|
|
self.navigationController?.pushViewController(LVC, animated: true)
|
|
}
|
|
|
|
func goToXYLogin(_ userName:String,_ password:String){
|
|
presentXY.fetchRepXYLogin(userName, password)
|
|
}
|
|
func getXYUserInfo( ){
|
|
presentXY.fetchRepXYUserInfo()
|
|
}
|
|
func goDrawerMain(){
|
|
let XYVC = XYMainVC()
|
|
// XYVC.isShowXYFreeTimeTip = isShowXYFreeTimeTip
|
|
self.navigationController?.pushViewController(XYVC, animated: true)
|
|
}
|
|
|
|
func registerXYReq(){
|
|
presentXY.fetchRepXuRegister()
|
|
}
|
|
override var preferredStatusBarStyle: UIStatusBarStyle {
|
|
return .lightContent
|
|
}
|
|
func switcXINGYhHost() -> Bool {
|
|
let userXINGYInfo = UserDefaults.standard
|
|
let currentSelHost = userXINGYInfo.string(forKey: XINGYUserField.CurrentXYHost) ?? XINGYUserField.LocalDefaultXINGYHost
|
|
if var routerONESelHost = userXINGYInfo.stringArray(forKey:XINGYUserField.XYHostArr) {
|
|
if !routerONESelHost.isEmpty {
|
|
for (index,item) in routerONESelHost.enumerated().reversed() {
|
|
if !item.contains(currentSelHost) {
|
|
routerONESelHost.remove(at: index)
|
|
userXINGYInfo.setValue(item, forKey: XINGYUserField.CurrentXYHost)
|
|
userXINGYInfo.set(routerONESelHost, forKey: XINGYUserField.XYHostArr)
|
|
return true
|
|
} else {
|
|
routerONESelHost.remove(at: index)
|
|
userXINGYInfo.set(routerONESelHost, forKey: XINGYUserField.XYHostArr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
}
|
|
extension XINGYStartVC : XYStartProtocol{
|
|
func repErrorXYVpnVersion(_ msg: String) {
|
|
|
|
if self.switcXINGYhHost() {
|
|
self.presentXY.fetchRepXYVpnVersion()
|
|
}
|
|
else{
|
|
self.initXYRep()
|
|
SVProgressHUD.showInfo(withStatus: msg)
|
|
SVProgressHUD.dismiss(withDelay: 1.5)
|
|
}
|
|
}
|
|
|
|
func repXYLogin() {
|
|
self.getXYUserInfo()
|
|
}
|
|
|
|
func repErrorXYLogin(_ msg: String) {
|
|
self.XINGYgoLoginCt()
|
|
SVProgressHUD.showInfo(withStatus: msg)
|
|
SVProgressHUD.dismiss(withDelay: 1.5)
|
|
}
|
|
|
|
func repXYRegister(_ userName: String) {
|
|
UserDefaults.standard.setValue(true, forKey: "IsXYSavePsw")
|
|
UserDefaults.standard.setValue("123456", forKey: "XYSavePassWord")
|
|
isShowXYFreeTimeTip = true
|
|
self.goToXYLogin(userName,XINGYUserField.DefaultPsw)
|
|
}
|
|
func repErrorXYRegister() {
|
|
XINGYgoLoginCt()
|
|
}
|
|
|
|
func repXYUserInfo() {
|
|
self.goDrawerMain()
|
|
}
|
|
|
|
func repErrorXYUserInfo() {
|
|
self.XINGYgoLoginCt()
|
|
}
|
|
|
|
func repXYVpnVersion() {
|
|
self.initXYRep()
|
|
}
|
|
|
|
|
|
}
|