|
@@ -59,21 +59,9 @@ class ResultWrapper: NSObject {
|
|
|
if state == .rewrite || state == .correctTypos { //重写 && 纠错
|
|
|
aiRewriting(content: content, state: state, complete: complete)
|
|
|
} else if state == .uploadTranslate || state == .fileTranslate { // 上传文件 || 文件翻译
|
|
|
-// 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 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 {
|
|
@@ -83,9 +71,20 @@ class ResultWrapper: NSObject {
|
|
|
} 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 {
|
|
@@ -305,9 +304,9 @@ class ResultWrapper: NSObject {
|
|
|
|
|
|
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"]
|
|
|
-
|
|
|
+ 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")
|
|
@@ -316,84 +315,19 @@ class ResultWrapper: NSObject {
|
|
|
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 data: NSDictionary = responseObject as? NSDictionary ?? [:]
|
|
|
+ if data.count > 0 {
|
|
|
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
|
|
|
+ if let value = data["fileKey"] {
|
|
|
+ wrapper.content = value as! String
|
|
|
}
|
|
|
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 == "" {
|
|
|
- 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)
|
|
|
+ let error = NSError(domain: "unknown error", code: 404)
|
|
|
+ complete(ResultWrapper(success: false, content: "unknown error"))
|
|
|
}
|
|
|
} else {
|
|
|
+ let error = NSError(domain: "unknown error", code: 404)
|
|
|
complete(ResultWrapper(success: false, content: "unknown error"))
|
|
|
}
|
|
|
}
|
|
@@ -406,26 +340,81 @@ class ResultWrapper: NSObject {
|
|
|
@param to 结束语言
|
|
|
*/
|
|
|
func aiTranslationFileTranslateHandle(fileKey: String, from: String, to: String, complete: @escaping KMRequestServerComplete) {
|
|
|
-
|
|
|
- let infoDictionary = Bundle .main.infoDictionary!
|
|
|
- let urlString = AIInfoConfig().aiActionURL + "/api/AI/fileTranslate"
|
|
|
+ let aiInfo: AIInfo = AIInfoManager.default().aiInfo
|
|
|
+ if aiInfo.totalToken - aiInfo.usedTimes > 1 {
|
|
|
+ let infoDictionary = Bundle .main.infoDictionary!
|
|
|
+ let urlString = AIInfoConfig().aiActionURL + "/api/AI/fileTranslate"
|
|
|
+
|
|
|
+ let paraDict = ["fileKey": fileKey,
|
|
|
+ "from": languageAbbreviation(from),
|
|
|
+ "to": languageAbbreviation(to),
|
|
|
+ "uuid": uuid(),
|
|
|
+ "platform": platform(),
|
|
|
+ "app_name": app_name()]
|
|
|
+ var postData = try! JSONSerialization.data(withJSONObject: paraDict)
|
|
|
+
|
|
|
+
|
|
|
+ var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
|
|
|
+ request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
|
|
|
+ request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
+
|
|
|
+ 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 data1: NSDictionary = jsonObject!["data"] as? NSDictionary ?? [:]
|
|
|
+ let code: String = jsonObject!["code"] as? String ?? "06005"
|
|
|
+ let message: String = jsonObject!["message"] as? String ?? "unknown error"
|
|
|
+ let error = NSError(domain: message, code: Int(code)!)
|
|
|
+
|
|
|
+ if Int(code)! == 200 {
|
|
|
+ let fileUrl = data1["fileUrl"]
|
|
|
+ let downFileUrl = data1["downFileUrl"]
|
|
|
+ let ossDownUrl = data1["ossDownUrl"]
|
|
|
+ let fileName = data1["fileName"]
|
|
|
+ let downFileName = data1["downFileName"]
|
|
|
+ let fromStr = data1["from"]
|
|
|
+ let toStr = data1["to"]
|
|
|
+
|
|
|
+ self.downFileName = downFileName as! String
|
|
|
+ complete(ResultWrapper(success: true, content: ossDownUrl as! String))
|
|
|
+ } else {
|
|
|
+ complete(ResultWrapper(success: false, content: message))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ complete(ResultWrapper(success: false, content: "unknown error"))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ task.resume()
|
|
|
+ } else {
|
|
|
+ memberAITranslationFileTranslateHandle(fileKey: fileKey, from: from, to: to, complete: complete)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func memberAITranslationFileTranslateHandle(fileKey: String, from: String, to: String, complete: @escaping KMRequestServerComplete) {
|
|
|
+ let token: String = KMMemberInfo.shared.access_token
|
|
|
+
|
|
|
+ let urlString = AIInfoConfig().memberAIActionURL + "/member-system-website/v1/translate/fileTranslateHandle"
|
|
|
|
|
|
let paraDict = ["fileKey": fileKey,
|
|
|
"from": languageAbbreviation(from),
|
|
|
- "to": languageAbbreviation(to),
|
|
|
- "uuid": uuid(),
|
|
|
- "platform": platform(),
|
|
|
- "app_name": app_name()]
|
|
|
- var postData = try! JSONSerialization.data(withJSONObject: paraDict)
|
|
|
-
|
|
|
-
|
|
|
+ "to": languageAbbreviation(to)]
|
|
|
+ let postData = try! JSONSerialization.data(withJSONObject: paraDict)
|
|
|
+
|
|
|
var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
|
|
|
- request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
|
|
|
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))
|
|
@@ -433,27 +422,35 @@ class ResultWrapper: NSObject {
|
|
|
}
|
|
|
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
|
|
|
if jsonObject != nil {
|
|
|
- let data1: NSDictionary = jsonObject!["data"] as? NSDictionary ?? [:]
|
|
|
- let code: String = jsonObject!["code"] as? String ?? "06005"
|
|
|
- let message: String = jsonObject!["message"] as? String ?? "unknown error"
|
|
|
- let error = NSError(domain: message, code: Int(code)!)
|
|
|
-
|
|
|
- if Int(code)! == 200 {
|
|
|
- let fileUrl = data1["fileUrl"]
|
|
|
- let downFileUrl = data1["downFileUrl"]
|
|
|
- let ossDownUrl = data1["ossDownUrl"]
|
|
|
- let fileName = data1["fileName"]
|
|
|
- let downFileName = data1["downFileName"]
|
|
|
- let fromStr = data1["from"]
|
|
|
- let toStr = data1["to"]
|
|
|
-
|
|
|
- self.downFileName = downFileName as! String
|
|
|
- complete(ResultWrapper(success: true, content: ossDownUrl as! String))
|
|
|
+ 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 = ""
|
|
|
+
|
|
|
+ let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
|
|
|
+ if code == 200 {
|
|
|
+ if jsonObject!["data"] != nil {
|
|
|
+ let dataDic = jsonObject!["data"] as? NSDictionary ?? [:]
|
|
|
+
|
|
|
+ let fileUrl = dataDic["fileUrl"]
|
|
|
+ let downFileUrl = dataDic["downFileUrl"]
|
|
|
+ let ossDownUrl = dataDic["ossDownUrl"]
|
|
|
+ let fileName = dataDic["fileName"]
|
|
|
+ let downFileName = dataDic["downFileName"]
|
|
|
+ let fromStr = dataDic["from"]
|
|
|
+ let toStr = dataDic["to"]
|
|
|
+
|
|
|
+ self.downFileName = downFileName as! String
|
|
|
+ complete(ResultWrapper(success: true, content: ossDownUrl as! String))
|
|
|
+ }
|
|
|
} else {
|
|
|
- complete(ResultWrapper(success: false, content: message))
|
|
|
+ complete(ResultWrapper(success: false, content: ""))
|
|
|
}
|
|
|
} else {
|
|
|
- complete(ResultWrapper(success: false, content: "unknown error"))
|
|
|
+ complete(ResultWrapper(success: false, content: ""))
|
|
|
}
|
|
|
}
|
|
|
task.resume()
|
|
@@ -905,7 +902,6 @@ class KMAIRequestServer {
|
|
|
}
|
|
|
}
|
|
|
task.resume()
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|