Swift 网络请求 Moya/HandyJSON使用

2021-02-23  本文已影响0人  Aliv丶Zz

    Moya的简介及时间逻辑就不在赘述,网上有很多。记录一下在实际项目中如何使用Moya进行网络请求。

1. Moya相关设置

Code、Msg、Data节点为接口出参通配字段,所有接口都会包含这三个字段,用来校验接口是否成功返回,或提示错误信息

  1. Code:用来判断接口是否正常返回
  2. Msg:错误信息
  3. Data: 接口返回的数据
import Moya
import SwiftyJSON

//请求超时设置
let requestTimeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<PerformanceAPI>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 20
        done(.success(request))
    } catch {
        return
    }
}

class  ZLBaseNetwork : NSObject {
    public static func request<T: TargetType>(provider: MoyaProvider<T>,
                               target: T,
                               success successCallback: @escaping (JSON) -> Void,
                               error errorCallback: @escaping (Int, String) -> Void,
                               failure failureCallback: @escaping (MoyaError) -> Void) {
        provider.request(target) { (result) in
            switch result {
            case .success(let response):
                do {
                    let json = try JSON(response.filterSuccessfulStatusCodes().mapJSON())
                    //解析数据 200 == 接口正常返回
                    if let code = json["Code"].int, code != 200 {
                        if code == 201 {// 201 token过期
                         //重新登录(重新获取token)
                        }
                        uLog("\(code)--\(json["Msg"].string!)")
                        errorCallback(code, json["Msg"].string!)
                        return
                    }
                    successCallback(json["Data"])
                    
                } catch  {
                    uLog(error)
                    errorCallback(1, "出现错误")
                }
                break
                
            case .failure(let error):
                
                failureCallback(error)
                break
                
            }
        }
    }
    
    public static func request<T: TargetType>(target: T,
                               success successCallback: @escaping (JSON) -> Void,
                               error errorCallback: @escaping (Int, String) -> Void,
                               failure failureCallback: @escaping (MoyaError) -> Void) {
        
        let provider = MoyaProvider<T>(requestClosure: requestTimeoutClosure)
        request(provider: provider, target: target, success: successCallback, error: errorCallback, failure: failureCallback)
    }
}

ps: 可以在处理code时,进行错误信息的处理,比如:token过期,重新登录(重新获取token)

在实际项目开发中,会将不同模块的网路请求api单独进行封装,如1. 用户信息模块2. 我的关注模块 等等,这时需要分别创建 UserInfoAPIMyFocusAPI 去进行配置。

1. 用户信息模块配置

import UIKit
import Moya
import SwiftyJSON

// 用户相关请求枚举
public enum UserInfoAPI {
    case UserList(page: Int = 1, size: Int = 10)
    case UserDetail(userID: String)

}

// 继承自基类,如果特殊请求,可以单独处理
class UserInfoNetwork : ZLBaseNetwork {
    
}

// 用户相关请求API需继承TargetType
extension UserInfoAPI: TargetType {
    public var baseURL: URL {
        return URL.init(string: baseUrlString)!
    }
    
// 接口名称配置
    public var path: String {
        switch self {
        case .UserList:
            return "Common/UserInfo"
        case .UserDetail:
            return "Common/UserDetail"

        }
    }
    //接口请求方式
    public var method: Moya.Method {
        switch self {
        case .UserList:
            return .post
        case .UserDetail:
            return .post
        }
    }
    //接口参数配置
    public var task: Task {
        var params: [String : Any] = [:]
        switch self {
        case .UserList(let page, let size):
            params["CurrentPage"]   = page
            params["PageSize"]      = size
        case .UserDetail(let userID):
            params["userID"]        = userID

        }
                
        let jsonStr = self.convertDictionaryToJSONString(dict: params as NSDictionary)
        // 加密处理
        let desStr = DES.encrypt(withContent: jsonStr, type: CCOperation(kCCEncrypt), key: DesKey)
        let dict = ["EncryptMsg": desStr]
        uLog("\n======\(self.path)======\n\(jsonStr)\n")

        return .requestParameters(parameters:dict as [String : Any], encoding: JSONEncoding.default)
    }
    
   // 请求头配置
    public var headers: [String : String]? {
        
        let tokens = ""
        let md5Sign = ""
        let timeStamp = ""
        
        uLog(["timeStamp":timeStamp,"token":tokens,"sign":md5Sign])
        
        return ["timeStamp":timeStamp,"token":tokens,"sign":md5Sign]
    }
    
    public var sampleData: Data {
        return Data()
    }
    
    func convertDictionaryToJSONString(dict:NSDictionary?)->String {
        let data = try? JSONSerialization.data(withJSONObject: dict!, options: JSONSerialization.WritingOptions.init(rawValue: 0))
        let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        return jsonStr! as String
    }
}

ps: 在项目配置文件中,需设置

 ///测试环境
 let baseUrlString = "https://192.168.xxx.com.cn/dev/Api/"
// 正式环境
//let baseUrlString = "https://192.168.xxx.com.cn/Api/"

2. 我的关注模块 参考用户信息模块

 // MARK: -  加载数据
    private func loadUserList(){
        let provider = MoyaProvider<UserInfoAPI>(requestClosure: requestTimeoutClosure)
        let target = UserInfoAPI.UserList(page: 1, size: 20)

        UserInfoNetwork.request(provider: provider, target: target) { (json) in
            //数据处理
        } error: { (type:Int, msg:String) in
            //异常处理
        } failure: { (error) in
            //失败处理
        }

    }

2. HandyJSON的使用(待补充)

上一篇 下一篇

猜你喜欢

热点阅读