Pārlūkot izejas kodu

【会员系统】AI功能完善

niehaoyu 1 nedēļu atpakaļ
vecāks
revīzija
479ed678c9

+ 115 - 57
PDF Office/PDF Master/Class/AIInfo/KMAIRequestServerManager.swift

@@ -59,17 +59,33 @@ class ResultWrapper: NSObject {
         if state == .rewrite || state == .correctTypos {  //重写 && 纠错
             aiRewriting(content: content, state: state, complete: complete)
         } else if state == .uploadTranslate || state == .fileTranslate {    // 上传文件 || 文件翻译
-            aiTranslationFileUpload(file: content, complete: { [weak self] wrapper in
-                let success: Bool = wrapper.success
-                let fileKey = wrapper.content
-                if success {
-                    let fileData = wrapper.result
-                    complete(wrapper)
- 
-                } else {
-                    complete(wrapper)
-                }
-            })
+//            let aiInfo: AIInfo = AIInfoManager.default().aiInfo
+//            if aiInfo.totalToken - aiInfo.usedTimes > 1 {
+//                aiTranslationFileUpload(file: content, complete: { [weak self] wrapper in
+//                    let success: Bool = wrapper.success
+//                    let fileKey = wrapper.content
+//                    if success {
+//                        let fileData = wrapper.result
+//                        complete(wrapper)
+//     
+//                    } else {
+//                        complete(wrapper)
+//                    }
+//                })
+//            } else {
+                memberAITranslationFileUpload(file: content, complete: { [weak self] wrapper in
+                    let success: Bool = wrapper.success
+                    let fileKey = wrapper.content
+                    if success {
+                        let fileData = wrapper.result
+                        complete(wrapper)
+     
+                    } else {
+                        complete(wrapper)
+                    }
+                })  
+//        }
+            
         } else if state == .textTranslate { // 文本翻译
             let aiInfo: AIInfo = AIInfoManager.default().aiInfo
             if aiInfo.totalToken - aiInfo.usedTimes > 1 {
@@ -82,7 +98,7 @@ class ResultWrapper: NSObject {
             if aiInfo.totalToken - aiInfo.usedTimes > 1 {
                 extractSummaryFile(file: content, complete: complete)
             } else {
-                
+                memberExtractSummaryFile(file: content, complete: complete)
             }
         }
     }
@@ -281,6 +297,56 @@ class ResultWrapper: NSObject {
         }
     }
     
+    private func memberAITranslationFileUpload(file: String, complete: @escaping KMRequestServerComplete) {
+        let token: String = KMMemberInfo.shared.access_token
+        if token == "" {
+            return
+        }
+        
+        let urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/v1/translate/fileUpload"
+        let fileData = FileManager.default.contents(atPath: file)
+        let params: [String: Any] = ["file": fileData,
+                                     "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")
+            requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
+        } 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"))
+            }
+        }
+    }
+    
     private func aiMemberTranslationFileUpload(file: String, complete: @escaping KMRequestServerComplete) {
         let token: String = KMMemberInfo.shared.access_token
         if token == "" {
@@ -591,6 +657,42 @@ class ResultWrapper: NSObject {
         }
     }
 
+    private func memberExtractSummaryFile(file: String, complete: @escaping KMRequestServerComplete) {
+        let token: String = KMMemberInfo.shared.access_token
+        if token == "" {
+            return
+        }
+
+        let urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/api/extract-file-summary"
+        let fileData = FileManager.default.contents(atPath: file)
+        let params: [String: Any] = ["file": fileData]
+        
+        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")
+            requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
+        } completion: { task, responseObject, error in
+            if responseObject != nil {
+                let data: NSDictionary = responseObject!["data"] as? NSDictionary ?? [:]
+                let code: Int = responseObject!["code"] as? Int ?? 06005
+                let message: String = responseObject!["message"] as? String ?? "unknown error"
+                let error = NSError(domain: message, code: code)
+ 
+                if code == 200 {
+                    complete(ResultWrapper(success: true, content: data["summary"] as! String))
+                } else {
+                    complete(ResultWrapper(success: false, content: message))
+                }
+            } else {
+                let error = NSError(domain: "unknown error", code: 404)
+                 complete(ResultWrapper(success: false, content: "unknown error"))
+            }
+        }
+    }
+    
+    
 }
 
 //typealias KMHttpRequestServerComplete = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: NSError?) -> Void
@@ -652,51 +754,7 @@ 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);
-                }
-            }
-        }
-    }
-    
+     
     /**
      文件翻译
      */