KMRequestServer.swift 12 KB

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