Sfoglia il codice sorgente

【会员系统】AI功能完善

niehaoyu 1 settimana fa
parent
commit
cecad62bc1

+ 147 - 2
PDF Office/PDF Master/Class/AIInfo/KMAIRequestServerManager.swift

@@ -71,9 +71,19 @@ class ResultWrapper: NSObject {
                 }
             })
         } else if state == .textTranslate { // 文本翻译
-            aiTranslationTextTrans(q: content, from: languageAbbreviation(from), to: languageAbbreviation(to), complete: complete)
+            let aiInfo: AIInfo = AIInfoManager.default().aiInfo
+            if aiInfo.totalToken - aiInfo.usedTimes > 1 {
+                aiTranslationTextTrans(q: content, from: languageAbbreviation(from), to: languageAbbreviation(to), complete: complete)
+            } else {
+                aiMemberTranslationTextTrans(q: content, from: languageAbbreviation(from), to: languageAbbreviation(to), complete: complete)
+            }
         } else if state == .extractSummaryFile {    // 提取摘要
-            extractSummaryFile(file: content, complete: complete)
+            let aiInfo: AIInfo = AIInfoManager.default().aiInfo
+            if aiInfo.totalToken - aiInfo.usedTimes > 1 {
+                extractSummaryFile(file: content, complete: complete)
+            } else {
+                
+            }
         }
     }
     
@@ -271,6 +281,58 @@ class ResultWrapper: NSObject {
         }
     }
     
+    private func aiMemberTranslationFileUpload(file: String, complete: @escaping KMRequestServerComplete) {
+        let token: String = KMMemberInfo.shared.access_token
+        if token == "" {
+            return
+        }
+        
+        let infoDictionary = Bundle .main.infoDictionary!
+        let urlString = AIInfoConfig().aiActionURL + "/api/AI/uploadTranslate"
+        let fileData = FileManager.default.contents(atPath: file)
+        let params: [String: Any] = ["file": fileData,
+                                     "uuid": uuid(),
+                                     "platform": platform(),
+                                     "app_name": app_name(),
+                                     "calculate":"1"]
+        KMAIRequestServer.requestServer.uploadFile(urlString: urlString, params: params) { formData in
+            let fileURL = URL(fileURLWithPath: file)
+            try? formData.appendPart(withFileURL: fileURL, name: "file", fileName: fileURL.lastPathComponent, mimeType: "application/octet-stream")
+        } 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 ?? [:]
+                var code: String = responseObject!["code"] as? String ?? "06005"
+                if code == "06005" {
+                    let tempCode: Int = responseObject!["code"] as? Int ?? 0
+                    if tempCode == 501 {
+                        code = "501"
+                    }
+                }
+                
+                let message: String = responseObject!["message"] as? String ?? "unknown error"
+                let error = NSError(domain: message, code: Int(code)!)
+ 
+                if Int(code)! == 200 {
+                    let wrapper = ResultWrapper(success: true, resultData: data)
+                    wrapper.content = data["fileKey"] as! String
+                    complete(wrapper)
+                } else {
+                    let wrapper = ResultWrapper(success: false, resultData: data)
+                    if Int(code)! == 501 {
+                        wrapper.content = "501"
+                    } else {
+                        wrapper.content = message
+                    }
+                    complete(wrapper)
+                }
+            } else {
+                 complete(ResultWrapper(success: false, content: "unknown error"))
+            }
+        }
+    }
+    
     /**
      @abstract 文件翻译
      @param fileKey  文件Key
@@ -383,6 +445,45 @@ class ResultWrapper: NSObject {
         task.resume()
     }
     
+    private func aiMemberTranslationTextTrans(q: String, from: String, to: String, complete: @escaping KMRequestServerComplete) {
+        let token: String = KMMemberInfo.shared.access_token
+        if token == "" {
+            return
+        }
+        
+        let urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/v1/translate/textTrans"
+        
+        var escapedString = q
+        escapedString = String(data: escapedString.data(using: .utf8)!, encoding: .utf8)!
+        let paraDict = ["text":escapedString,
+                        "from":from,
+                        "to":to,
+                        "uuid":uuid(),
+                        "platform": platform(),
+                        "app_name": app_name()]
+        var postData  = try! JSONSerialization.data(withJSONObject: paraDict)
+ 
+        KMAIRequestServer.requestServer.aiMemberRewriting(urlString: urlString, params: paraDict) { 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"))
+            }
+        }
+    }
+    
     /**
      @abstract重写 & 纠错
      @param content  内容
@@ -552,6 +653,50 @@ class KMAIRequestServer {
         }
     }
     
+    func memberUploadFile(urlString: String,
+                        params: Dictionary<String, Any>?,
+                       body: ((_ formData:AFMultipartFormData) -> Void)?,
+          requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
+                     completion: KMHttpRequestServerComplete?) -> Void {
+        
+        sessionManager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
+        sessionManager.requestSerializer.timeoutInterval = 60
+        sessionManager.responseSerializer = AFJSONResponseSerializer()
+        sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
+        if (requestSerializer != nil) {
+            requestSerializer!(sessionManager.requestSerializer);
+        }
+        if (body != nil) {
+            self.task = sessionManager.post(urlString, parameters: params, headers: nil) { formData in
+                body!(formData)
+            } progress: { progress in
+                
+            } success: { task, responseObject in
+                let responseObject = responseObject as AnyObject
+                if (completion != nil) {
+                    completion!(task,responseObject,nil);
+                }
+            } failure: { task, error in
+                if (completion != nil) {
+                    completion!(task, nil, error as NSError);
+                }
+            }
+        } else {
+            self.task = sessionManager.post(urlString, parameters: params, headers: nil) { progress in
+            
+            } success: { task, responseObject in
+                if (completion != nil) {
+                    let responseObject = responseObject as AnyObject
+                    completion!(task,responseObject,nil);
+                }
+            } failure: { task, error in
+                if (completion != nil) {
+                    completion!(task, nil, error as NSError);
+                }
+            }
+        }
+    }
+    
     /**
      文件翻译
      */