123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- //
- // KMRequestServer.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/2/22.
- //
- import Cocoa
- import AFNetworking
- @objc public enum KMRequestServerMethodType: Int {
- case post = 0
- case get
- case put
- case delete
- }
- typealias KMHttpRequestServerComplete = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: NSError?) -> Void
- @objcMembers class KMRequestServer: NSObject {
- @objc var sessionManager: AFHTTPSessionManager!
- @objc static let requestServer = KMRequestServer()
- @objc var task: URLSessionTask?
-
- override init() {
- super.init()
-
- let configuration: URLSessionConfiguration = URLSessionConfiguration.default
- sessionManager = AFHTTPSessionManager.init(sessionConfiguration: configuration)
- sessionManager.securityPolicy = AFSecurityPolicy.default()
- }
-
- public func request(urlString: String,
- method: KMRequestServerMethodType,
- params: Dictionary<String, Any>?,
- completion: KMHttpRequestServerComplete?) -> Void
- {
- self.request(urlString: urlString,
- method: method,
- params: params,
- requestSerializer: nil,
- completion: completion)
- }
-
- func request(urlString: String,
- method: KMRequestServerMethodType,
- params: Dictionary<String, Any>?,
- requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
- completion: KMHttpRequestServerComplete?) -> Void {
- self.request(urlString: urlString,
- method: method,
- params: params,
- requestSerializer: requestSerializer,
- body: nil,
- completion: completion)
- }
-
- func request(urlString: String,
- method: KMRequestServerMethodType,
- params: Dictionary<String, Any>?,
- requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
- body: ((_ formData:AFMultipartFormData) -> Void)?,
- completion: KMHttpRequestServerComplete?) -> Void {
- //每次请求需要重新创建
- sessionManager.requestSerializer = AFJSONRequestSerializer()
- sessionManager.responseSerializer = AFJSONResponseSerializer()
- sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
- sessionManager.requestSerializer.timeoutInterval = 60
- // [requestSerializer setValue:@"application/vnd.api+json;version=1" forHTTPHeaderField:@"Accept"];
- if (requestSerializer != nil) {
- requestSerializer!(sessionManager.requestSerializer);
- }
-
- if (method == .post) {
- 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);
- }
- }
- }
- } else if (method == .get) {
- self.task = sessionManager.get(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);
- }
- }
- }
- }
-
- /**
- 下载
- */
- func downLoad(urlString: String,
- filePath: String,
- progress: @escaping(_ progress: Progress) -> Void,
- completion: KMRequestServerComplete) {
- // /* 下载地址 */
- // NSURL *url = [NSURL URLWithString:urlString];
- // NSURLRequest *request = [NSURLRequest requestWithURL:url];
- // /* 开始请求下载 */
- // NSProgress *tProgress;
- // NSURLSessionDownloadTask *downloadTask = [_sessionManager downloadTaskWithRequest:request progress:&tProgress destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
- // dispatch_async(dispatch_get_main_queue(), ^{
- // //如果需要进行UI操作,需要获取主线程进行操作
- // });
- // /* 设定下载到的位置 */
- // return [NSURL fileURLWithPath:filePath];
- //
- // } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- // if (completion) {
- // completion(response,filePath,error);
- // }
- // }];
- //
- // if (progress) {
- // progress(tProgress);
- // }
- //
- // [downloadTask resume];
- }
-
- /**
- 翻译上传文件
- */
- func aiTranslation(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);
- }
- }
- }
- }
-
- /**
- 文件翻译
- */
- func aiFileTranslateHandle(urlString: String,
- params: Dictionary<String, Any>?,
- body: ((_ formData:AFMultipartFormData) -> Void)?,
- requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
- completion: KMHttpRequestServerComplete?) -> Void {
- sessionManager.requestSerializer = AFJSONRequestSerializer()
- 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);
- }
- }
- }
- }
-
- /**
- 重写
- */
- func aiRewriting(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);
- }
- }
- }
- }
-
- /*
- status
- AFNetworkReachabilityStatusUnknown = -1, 不知道监测的是什么
- AFNetworkReachabilityStatusNotReachable = 0, 没有检测到网络
- AFNetworkReachabilityStatusReachableViaWWAN = 1, 蜂窝网
- AFNetworkReachabilityStatusReachableViaWiFi = 2, WIFI
- */
- func reachabilityStatusChange(block: @escaping(AFNetworkReachabilityStatus) ->Void) {
- // 1.创建网络监测单例
- let manager = AFNetworkReachabilityManager.shared()
- manager.setReachabilityStatusChange({ status in
- // 2.实现网络监测的回调
- block(status)
- })
- // 3.开始监测
- manager.startMonitoring()
- }
- }
|