Преглед на файлове

Merge branch 'develop_PDFReaderPro_V4.7.0' of git.kdan.cc:Mac_PDF/PDF_Office into develop_PDFReaderPro_V4.7.0

zenghong преди 1 седмица
родител
ревизия
acb1e5e79b
променени са 1 файла, в които са добавени 118 реда и са изтрити 23 реда
  1. 118 23
      PDF Office/PDF Master/Class/AIInfo/KMAIRequestServerManager.swift

+ 118 - 23
PDF Office/PDF Master/Class/AIInfo/KMAIRequestServerManager.swift

@@ -389,34 +389,66 @@ class ResultWrapper: NSObject {
      @param state  类型
      */
     private func aiRewriting(content: String, state: KMAIRewritingState, complete: @escaping KMRequestServerComplete) {
+        let aiInfo: AIInfo = AIInfoManager.default().aiInfo
         
-        var urlString = AIInfoConfig().aiActionURL + "/api/AI/rewrite"
-        if state == .correctTypos {
-            urlString = AIInfoConfig().aiActionURL + "/api/AI/correctTypos"
-        }
-        let params: [String: Any] = ["content": content,
-                                     "uuid": uuid(),
-                                     "platform": platform(),
-                                     "app_name": app_name()]
-        
-        KMAIRequestServer.requestServer.aiRewriting(urlString: urlString, params: params) { formData in
+        if aiInfo.totalToken - aiInfo.usedTimes > 1 {
+            var urlString = AIInfoConfig().aiActionURL + "/api/AI/rewrite"
+            if state == .correctTypos {
+                urlString = AIInfoConfig().aiActionURL + "/api/AI/correctTypos"
+            }
+            let params: [String: Any] = ["content": content,
+                                         "uuid": uuid(),
+                                         "platform": platform(),
+                                         "app_name": app_name()]
             
-        } requestSerializer: { requestSerializer in
-            requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
-        } completion: { task, responseObject, error in
-            if responseObject != nil {
-                let data: NSDictionary = responseObject!["data"] as? NSDictionary ?? [:]
-                let code: String = responseObject!["code"] as? String ?? "06005"
-                let message: String = responseObject!["message"] as? String ?? "unknown error"
-                let error = NSError(domain: message, code: Int(code)!)
+            KMAIRequestServer.requestServer.aiRewriting(urlString: urlString, params: params) { formData in
+                
+            } requestSerializer: { requestSerializer in
+                requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
+            } completion: { task, responseObject, error in
+                if responseObject != nil {
+                    let data: NSDictionary = responseObject!["data"] as? NSDictionary ?? [:]
+                    let code: String = responseObject!["code"] as? String ?? "06005"
+                    let message: String = responseObject!["message"] as? String ?? "unknown error"
  
-                if Int(code)! == 200 {
-                    complete(ResultWrapper(success: true, content: data["content"] as! String))
+                    if Int(code)! == 200 {
+                        complete(ResultWrapper(success: true, content: data["content"] as! String))
+                    } else {
+                        complete(ResultWrapper(success: false, content: message))
+                    }
                 } else {
-                    complete(ResultWrapper(success: false, content: message))
+                    complete(ResultWrapper(success: false, content: "unknown error"))
+                }
+            }
+        } else {
+            let token: String = KMMemberInfo.shared.access_token
+            if token == "" {
+                return
+            }
+            var urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/api/rewrite"
+            if state == .correctTypos {
+                urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/api/correct-typos"
+            }
+            let params: [String: Any] = ["content": content]
+            
+            KMAIRequestServer.requestServer.aiMemberRewriting(urlString: urlString, params: params) { formData in
+                
+            } requestSerializer: { requestSerializer in
+                requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
+                requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
+            } completion: { task, responseObject, error in
+                if responseObject is KMMemberCenterResult {
+                    let resultObjct = responseObject as! KMMemberCenterResult
+                    let code = resultObjct.code
+                    let message: String = resultObjct.msg ?? ""
+                    if code == 200 {
+                        complete(ResultWrapper(success: true, content: message))
+                    } else {
+                        complete(ResultWrapper(success: false, content: message))
+                    }
+                } else {
+                    complete(ResultWrapper(success: false, content: "unknown error"))
                 }
-            } else { 
-                complete(ResultWrapper(success: false, content: "unknown error"))
             }
         }
     }
@@ -610,4 +642,67 @@ class KMAIRequestServer {
             }
         }
     }
+    
+    func aiMemberRewriting(urlString: String,
+                           params: Dictionary<String, Any>?,
+                           body: ((_ formData:AFMultipartFormData) -> Void)?,
+                           requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
+                           completion: KMHttpRequestServerComplete?) -> Void {
+        
+        let token: String = KMMemberInfo.shared.access_token
+        
+        let urlString = urlString
+        let paraDict: [String: Any] = params!
+        let postData  = try! JSONSerialization.data(withJSONObject: paraDict)
+        
+        var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
+        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
+        
+        request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
+        request.addValue("Bearer " + token, forHTTPHeaderField: "Authorization")
+        request.httpMethod = "POST"
+        request.httpBody = postData
+        
+        let task = URLSession.shared.dataTask(with: request) { data, response, error in
+           guard let data = data else {
+               print(String(describing: error))
+               return
+           }
+            let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
+            if jsonObject != nil {
+                let codeStr: String = jsonObject!["code"] as! String
+                var code: Int = 0
+                if let numCode = Int(codeStr) {
+                    code = numCode
+                }
+                var result_bool: Bool = true
+                
+                var message: String = ""
+                if let string = jsonObject!["data"] {
+                    let dataDic = jsonObject!["data"] as? NSDictionary ?? [:]
+                    if let dataStr = dataDic["content"] {
+                        message = dataStr as! String
+                        result_bool = true
+                    }
+                }
+                let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
+                if code == 200 {
+                    if (completion != nil) {
+                        completion!(nil, result, nil);
+                    }
+                } else {
+                    if (completion != nil) {
+                        completion!(nil, nil, error as? NSError);
+                    }
+                }
+            } else {
+                if (completion != nil) {
+                    completion!(nil, nil, error as? NSError);
+                }
+            }
+        }
+        task.resume()
+        
+        
+    }
 }