KMRequestServer.swift 13 KB

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