KMRequestServer.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // KMRequestServer.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/22.
  6. //
  7. import Cocoa
  8. import AFNetworking
  9. enum KMRequestServerMethodType: String, CaseIterable {
  10. case post = "POST"
  11. case get = "GET"
  12. case put = "PUT"
  13. case delete = "DELETE"
  14. }
  15. typealias KMHttpRequestServerComplete = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: NSError?) -> Void
  16. class KMRequestServer {
  17. var sessionManager: AFHTTPSessionManager!
  18. static let requestServer = KMRequestServer()
  19. var task: URLSessionTask?
  20. init() {
  21. let configuration: URLSessionConfiguration = URLSessionConfiguration.default
  22. sessionManager = AFHTTPSessionManager.init(sessionConfiguration: configuration)
  23. sessionManager.securityPolicy = AFSecurityPolicy.default()
  24. }
  25. func request(urlString: String,
  26. method: KMRequestServerMethodType,
  27. params: Dictionary<String, Any>?,
  28. completion: KMHttpRequestServerComplete?) -> Void {
  29. self.request(urlString: urlString,
  30. method: method,
  31. params: params,
  32. requestSerializer: nil,
  33. completion: completion)
  34. }
  35. func request(urlString: String,
  36. method: KMRequestServerMethodType,
  37. params: Dictionary<String, Any>?,
  38. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  39. completion: KMHttpRequestServerComplete?) -> Void {
  40. self.request(urlString: urlString,
  41. method: method,
  42. params: params,
  43. requestSerializer: requestSerializer,
  44. body: nil,
  45. completion: completion)
  46. }
  47. func request(urlString: String,
  48. method: KMRequestServerMethodType,
  49. params: Dictionary<String, Any>?,
  50. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  51. body: ((_ formData:AFMultipartFormData) -> Void)?,
  52. completion: KMHttpRequestServerComplete?) -> Void {
  53. //每次请求需要重新创建
  54. sessionManager.requestSerializer = AFJSONRequestSerializer()
  55. sessionManager.responseSerializer = AFJSONResponseSerializer()
  56. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  57. sessionManager.requestSerializer.timeoutInterval = 10
  58. // [requestSerializer setValue:@"application/vnd.api+json;version=1" forHTTPHeaderField:@"Accept"];
  59. if (requestSerializer != nil) {
  60. requestSerializer!(sessionManager.requestSerializer);
  61. }
  62. if (method == .post) {
  63. if (body != nil) {
  64. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { formData in
  65. body!(formData)
  66. } progress: { progress in
  67. } success: { task, responseObject in
  68. let responseObject = responseObject as AnyObject
  69. if (completion != nil) {
  70. completion!(task,responseObject,nil);
  71. }
  72. } failure: { task, error in
  73. if (completion != nil) {
  74. completion!(task, nil, error as NSError);
  75. }
  76. }
  77. } else {
  78. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { progress in
  79. } success: { task, responseObject in
  80. if (completion != nil) {
  81. let responseObject = responseObject as AnyObject
  82. completion!(task,responseObject,nil);
  83. }
  84. } failure: { task, error in
  85. if (completion != nil) {
  86. completion!(task, nil, error as NSError);
  87. }
  88. }
  89. }
  90. } else if (method == .get) {
  91. self.task = sessionManager.get(urlString, parameters: params, headers: nil) { progress in
  92. } success: { task, responseObject in
  93. if (completion != nil) {
  94. let responseObject = responseObject as AnyObject
  95. completion!(task,responseObject,nil);
  96. }
  97. } failure: { task, error in
  98. if (completion != nil) {
  99. completion!(task, nil, error as NSError);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. 下载
  106. */
  107. func downLoad(urlString: String,
  108. filePath: String,
  109. progress: @escaping(_ progress: Progress) -> Void,
  110. completion: KMRequestServerComplete) {
  111. // /* 下载地址 */
  112. // NSURL *url = [NSURL URLWithString:urlString];
  113. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  114. // /* 开始请求下载 */
  115. // NSProgress *tProgress;
  116. // NSURLSessionDownloadTask *downloadTask = [_sessionManager downloadTaskWithRequest:request progress:&tProgress destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  117. // dispatch_async(dispatch_get_main_queue(), ^{
  118. // //如果需要进行UI操作,需要获取主线程进行操作
  119. // });
  120. // /* 设定下载到的位置 */
  121. // return [NSURL fileURLWithPath:filePath];
  122. //
  123. // } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  124. // if (completion) {
  125. // completion(response,filePath,error);
  126. // }
  127. // }];
  128. //
  129. // if (progress) {
  130. // progress(tProgress);
  131. // }
  132. //
  133. // [downloadTask resume];
  134. }
  135. /**
  136. 翻译上传文件
  137. */
  138. func aiTranslation(urlString: String,
  139. params: Dictionary<String, Any>?,
  140. body: ((_ formData:AFMultipartFormData) -> Void)?,
  141. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  142. completion: KMHttpRequestServerComplete?) -> Void {
  143. sessionManager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
  144. sessionManager.requestSerializer.timeoutInterval = 60
  145. sessionManager.responseSerializer = AFJSONResponseSerializer()
  146. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  147. if (requestSerializer != nil) {
  148. requestSerializer!(sessionManager.requestSerializer);
  149. }
  150. if (body != nil) {
  151. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { formData in
  152. body!(formData)
  153. } progress: { progress in
  154. } success: { task, responseObject in
  155. let responseObject = responseObject as AnyObject
  156. if (completion != nil) {
  157. completion!(task,responseObject,nil);
  158. }
  159. } failure: { task, error in
  160. if (completion != nil) {
  161. completion!(task, nil, error as NSError);
  162. }
  163. }
  164. } else {
  165. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { progress in
  166. } success: { task, responseObject in
  167. if (completion != nil) {
  168. let responseObject = responseObject as AnyObject
  169. completion!(task,responseObject,nil);
  170. }
  171. } failure: { task, error in
  172. if (completion != nil) {
  173. completion!(task, nil, error as NSError);
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. 文件翻译
  180. */
  181. func aiFileTranslateHandle(urlString: String,
  182. params: Dictionary<String, Any>?,
  183. body: ((_ formData:AFMultipartFormData) -> Void)?,
  184. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  185. completion: KMHttpRequestServerComplete?) -> Void {
  186. sessionManager.requestSerializer = AFJSONRequestSerializer()
  187. sessionManager.responseSerializer = AFJSONResponseSerializer()
  188. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  189. if (requestSerializer != nil) {
  190. requestSerializer!(sessionManager.requestSerializer);
  191. }
  192. if (body != nil) {
  193. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { formData in
  194. body!(formData)
  195. } progress: { progress in
  196. } success: { task, responseObject in
  197. let responseObject = responseObject as AnyObject
  198. if (completion != nil) {
  199. completion!(task,responseObject,nil);
  200. }
  201. } failure: { task, error in
  202. if (completion != nil) {
  203. completion!(task, nil, error as NSError);
  204. }
  205. }
  206. } else {
  207. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { progress in
  208. } success: { task, responseObject in
  209. if (completion != nil) {
  210. let responseObject = responseObject as AnyObject
  211. completion!(task,responseObject,nil);
  212. }
  213. } failure: { task, error in
  214. if (completion != nil) {
  215. completion!(task, nil, error as NSError);
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. 重写
  222. */
  223. func aiRewriting(urlString: String,
  224. params: Dictionary<String, Any>?,
  225. body: ((_ formData:AFMultipartFormData) -> Void)?,
  226. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  227. completion: KMHttpRequestServerComplete?) -> Void {
  228. sessionManager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
  229. sessionManager.requestSerializer.timeoutInterval = 60
  230. sessionManager.responseSerializer = AFJSONResponseSerializer()
  231. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  232. if (requestSerializer != nil) {
  233. requestSerializer!(sessionManager.requestSerializer);
  234. }
  235. if (body != nil) {
  236. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { formData in
  237. body!(formData)
  238. } progress: { progress in
  239. } success: { task, responseObject in
  240. let responseObject = responseObject as AnyObject
  241. if (completion != nil) {
  242. completion!(task,responseObject,nil);
  243. }
  244. } failure: { task, error in
  245. if (completion != nil) {
  246. completion!(task, nil, error as NSError);
  247. }
  248. }
  249. } else {
  250. self.task = sessionManager.post(urlString, parameters: params, headers: nil) { progress in
  251. } success: { task, responseObject in
  252. if (completion != nil) {
  253. let responseObject = responseObject as AnyObject
  254. completion!(task,responseObject,nil);
  255. }
  256. } failure: { task, error in
  257. if (completion != nil) {
  258. completion!(task, nil, error as NSError);
  259. }
  260. }
  261. }
  262. }
  263. /*
  264. status
  265. AFNetworkReachabilityStatusUnknown = -1, 不知道监测的是什么
  266. AFNetworkReachabilityStatusNotReachable = 0, 没有检测到网络
  267. AFNetworkReachabilityStatusReachableViaWWAN = 1, 蜂窝网
  268. AFNetworkReachabilityStatusReachableViaWiFi = 2, WIFI
  269. */
  270. func reachabilityStatusChange(block: @escaping(AFNetworkReachabilityStatus) ->Void) {
  271. // 1.创建网络监测单例
  272. let manager = AFNetworkReachabilityManager.shared()
  273. manager.setReachabilityStatusChange({ status in
  274. // 2.实现网络监测的回调
  275. block(status)
  276. })
  277. // 3.开始监测
  278. manager.startMonitoring()
  279. }
  280. }