Browse Source

【综合】整体代码

tangchao 3 months ago
parent
commit
8e79a8599e

+ 2 - 38
PDF Office/PDF Master/AppDelegate.swift

@@ -203,44 +203,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, iRateDelegate{
     }
     
     func initAccountRights() {
-        if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
-            let header = ["Token" : token]
-            KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
-                if success == false { // App 启动 Token 过期 退出登陆 & 解除设备绑定,需用户重新登陆再绑定
-                    KMDataManager.ud_set("", forKey: kAccountTokenKey)
-                    AccountManager.manager.saveRights(model: nil)
-                    
-                    NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
-                    
-                    VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
-                        VerificationManager.default().verification {status1 , infoDict1, err1 in
-                            
-                        }
-                    }
-                    return
-                }
-                let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
-                AccountManager.manager.saveRights(model: model)
-                
-                NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
-                
-                if let data = model.memberInfo?.email, data.isEmpty == false {
-                    self.activateDevice(email: data)
-                }
-            }
-        }
-    }
-    
-    func activateDevice(email: String) {
-//        if IAPProductsManager.default().isAvailableAllFunction() {
-//            return
-//        }
-        
-        VerificationManager.default().activateDevice(withInfo: ["email" : email]) { status, info, err in
-            VerificationManager.default().verification { status, info, err in
-                
-            }
-        }
+        // App 启动 Token 过期 退出登陆 & 解除设备绑定,需用户重新登陆再绑定
+        AccountTools.refreshRights(isActivaDevice: true, isDealCode: false) { model in }
     }
     
     func initiRateData() {

+ 75 - 0
PDF Office/PDF Master/Class/Account/Tools/AccountTools.swift

@@ -85,6 +85,81 @@ import Cocoa
         }
     }
     
+    public class func refreshRights(isActivaDevice: Bool, isDealCode: Bool, callback: ((_ model: KMRequestResultModel?)->Void)?) {
+        guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
+            callback?(nil)
+            return
+        }
+        let header = ["Token" : token]
+        KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
+            if success == false {
+                // 清空 Token
+                KMDataManager.ud_set("", forKey: kAccountTokenKey)
+                // 清空 登陆 & 权益 数据
+                AccountManager.manager.saveRights(model: nil)
+                // 发布通知 登陆状态改变
+                NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
+                // 解除设备激活
+                if isActivaDevice {
+                    self.unactivateDevice {}
+                }
+                // 处理 Code
+                if let codeV = dataModel?.code, isDealCode {
+                    self.dealCode(.init(rawValue: codeV))
+                }
+                
+                callback?(nil)
+                return
+            }
+            let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
+            // 保存 登陆 & 权益 数据
+            AccountManager.manager.saveRights(model: model)
+            // 发布通知 登陆状态改变
+            NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
+            
+            if isActivaDevice == false {
+                callback?(dataModel)
+            }
+            
+            // 设备激活
+            if let data = model.memberInfo?.email, data.isEmpty == false {
+                self.activateDevice(email: data) {
+                    callback?(dataModel)
+                }
+            } else {
+                callback?(dataModel)
+            }
+        }
+    }
+    
+    // MARK: - 处理 Code
+    
+    public class func dealCode(_ code: KMHttpReponseCode?) {
+        if code == .passwordChanged {
+            self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
+        } else if code == .accountAtOtherDeviceLogin {
+//                    self.pwdChangedAction()
+        }
+    }
+    
+    // MARK: - 激活设备
+    
+    public class func activateDevice(email: String, callback: (()->Void)?) {
+        VerificationManager.default().activateDevice(withInfo: ["email" : email]) { status, info, err in
+            VerificationManager.default().verification { status, info, err in
+                callback?()
+            }
+        }
+    }
+    
+    public class func unactivateDevice(callback: (()->Void)?) {
+        VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
+            VerificationManager.default().verification {status1 , infoDict1, err1 in
+                callback?()
+            }
+        }
+    }
+    
     class func gotoCompare() {
         KMMainThreadExecute {
             let winC = KMPurchaseCompareWindowController.sharedInstance()

+ 1 - 45
PDF Office/PDF Master/Class/Account/Window/AccountCenterWindowController.swift

@@ -98,11 +98,7 @@ class AccountCenterWindowController: NSWindowController {
                 }
             }
         } else {
-//            if let token = KMDataManager.ud_string(forKey: kAccountEmailKey), token.isEmpty == false {
-                self.gotoLogin()
-//            } else {
-//                self.gotoSignin()
-//            }
+            self.gotoLogin()
         }
     }
     
@@ -277,46 +273,6 @@ class AccountCenterWindowController: NSWindowController {
         }
     }
     
-//    func rightDatas() {
-//        let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
-//        KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataDict, err in
-//            let model = AccountRightModel(dict: dataDict ?? [:])
-//            KMPrint("")
-//        }
-//    }
-    
-//    func productDatas() {
-//        let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
-//        KMHTTP.OEM_POST(urlString: kURLAPI_oemGetProductData, parameter: nil, headers: header) { success, dataDict, err in
-//            let array = dataDict?["list"] as? [Any]
-//            var datas: [ProductListModel] = []
-//            for data in array ?? [] {
-//                guard let dict = data as? [String : Any] else {
-//                    continue
-//                }
-//                let model = ProductListModel(dict: dict)
-//                datas.append(model)
-//            }
-//            KMPrint("")
-//        }
-//    }
-//    
-//    func loginIn() {
-//        let params = ["email" : "aaaa1@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
-//        KMHTTP.OEM_POST(urlString: kURLAPI_oemLogin, parameter: params, headers: nil) { success , dataDict, err in
-//            let model = AccountInfoModel(dict: dataDict ?? [:])
-//            KMPrint("")
-//        }
-//    }
-//    
-//    func signIn() {
-//        let params = ["email" : "aaaa5@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
-//        KMHTTP.OEM_POST(urlString: kURLAPI_oemRegister, parameter: params, headers: nil) { success , dataDict, err in
-//            let model = AccountInfoModel(dict: dataDict ?? [:])
-//            KMPrint("")
-//        }
-//    }
-    
     // MARK: - Private Methods
     
     @objc private func _closeAction() {

+ 1 - 0
PDF Office/PDF Master/Class/Common/Tools/HTTP/KMHTTP+OEM.swift

@@ -20,6 +20,7 @@ let kURLAPI_oemLogout                   = "/pdf/logout"
 let kURLAPI_oemBind                   = "/pdf/bind"
 
 enum KMHttpReponseCode: Int {
+    case none = 0
     case passwordChanged = 405
     case accountAtOtherDeviceLogin = 402
 }