iOS Moya + RxSwift + ObjectMapper + MVVM + CryptoSwift 的使用

76BC10BD-A876-4180-9328-9874091516CF.png

导入框架

pod 'Moya/RxSwift'
pod 'RxSwift'
pod 'RxCocoa'
pod 'NSObject+Rx'
pod 'Moya-ObjectMapper/RxSwift'
pod 'CryptoSwift'

通用请求 并转模型

import Foundation
import Moya
import CryptoSwift
import RxCocoa
import RxSwift
import NSObject_Rx
import SVProgressHUD
import Moya_ObjectMapper
import ObjectMapper

//初始化360°智能科技请求的provider
let intelligentProvider = MoyaProvider<Intelligent>(requestClosure : timeoutClosure,plugins:[RequestHudPlugin])

let appKey = "加密后的拼接的key"


//定义请求分类
public enum Intelligent {
    
    // MARK: - 轮播图接口
    case shufflingFigure(version : Any)
    
    // MARK: - 登录接口
    case verifyAccount(loginName : String,passwd : String)
    
    //MARK: - 上传用户头像接口
    case replacePicture(photo : Data,version : Any,uid : Int,token : String)
    
    case functionModule(version : Any,uid : Int?,token : String?,id : Int?)
}

//设置请求配置
extension Intelligent : TargetType {
    
    //服务器地址
    public var baseURL: URL {
        return URL(string:"此处填地址")!
    }
    
    //各个请求的具体路径
    public var path: String {
        
        switch self {
            
        case .shufflingFigure:
            return "/baseconfig/index"
            
        case .verifyAccount:
            return "/user/login"
            
        case .replacePicture:
            return "/user/userPhotoUpdate"
        case .functionModule:
            return "/baseconfig/functionLink"
        }
        
    }
    
    //请求类型
    public var method: Moya.Method {
        
        return .post
    }
    
    //这个就是做单元测试模拟的数据,只会在单元测试文件中有作用
    public var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }
    
    //请求任务事件
    public var task: Task {
        // 请求通用参数(未登录)
        var parameterDict = ["osType":"IOS","version":"1.0.0"] as [String : Any];
        switch self {
            
        case .shufflingFigure(let version):
            parameterDict["appVersion"] = version
            break
            
        case .verifyAccount(let loginName, let passwd):
            parameterDict["loginName"] = loginName
            parameterDict["passwd"] = passwd
            parameterDict["appVersion"] = LCZVersion
            break
            
        case .replacePicture(let photo,let version, let uid, let token):
            let gifData = MultipartFormData(provider: .data(photo), name: "photo", fileName: "file.jpg", mimeType: "image/jpg")
            let multipartData = [gifData]
            parameterDict["version"] = version
            parameterDict["uid"] = uid
            parameterDict["token"] = token
            
            return .uploadCompositeMultipart(multipartData, urlParameters: md5Encryption(dict: parameterDict))
            
        case .functionModule(let version, let uid, let token, let id):
            parameterDict["version"] = version
            parameterDict["uid"] = uid
            parameterDict["token"] = token
            parameterDict["id"] = id
        }
        
        
        
        return  .requestParameters(parameters: md5Encryption(dict: parameterDict), encoding: URLEncoding.default)
    }
    
    
    //请求头
    public var headers: [String : String]? {
        return nil
    }
    
    //md5加密
    func md5Encryption(dict : Dictionary<String,Any>) -> Dictionary<String,Any> {
        var parameterDict = dict
        
        //ASCII码排序
        let keyAry = parameterDict.keys.sorted()
        
        var urlStr : String! = String()
 
        for key in keyAry {
            //拼接字符串
            urlStr.append(key + "=" + String(describing: parameterDict[key]!) + "&")
        }
        
        //删除最后一个 & 符号
        urlStr.removeLast()
        
        //在字符串最后在拼接appKey
        urlStr.append(appKey)
        
        //把字符串MD5加密 并转化为大写
        let hash : String = urlStr.md5().uppercased()
        
        //添加加密的键值对
        parameterDict["sign"] = hash
        
        return parameterDict
    }
    
}

/// 设置接口的超时时间
let timeoutClosure = { (endpoint : Endpoint<Intelligent>,closure : MoyaProvider<Intelligent>.RequestResultClosure) -> Void in
    
    if var urlRequest = try? endpoint.urlRequest() {
        urlRequest.timeoutInterval = 10
        closure(.success(urlRequest))
    }
    else{
        closure(.failure(MoyaError.requestMapping(endpoint.url)))
    }
}


/// 管理网络状态的插件
let RequestHudPlugin = NetworkActivityPlugin { change, target  in
    switch change {
        case .began:
            //根据不同的请求,是否显示加载框
            switch target as! Intelligent {
                
                case .shufflingFigure(let version):
                    break
                case .verifyAccount(let loginName, let passwd):
                    LCZHUDTool.show()
                    break
                case .replacePicture(let photo, let version, let uid, let token):
                    break
                case .functionModule(let version, let uid, let token, let id):
                    break
                
            }
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
        
        case .ended:
            LCZHUDTool.dismiss()
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

}


//与数据相关的错误类型
enum DataError: Error {
    case abnormalAccount //账户异常需要重新登录
    case otherError //其它错误
}


struct defaultModel<T: Mappable>: Mappable {
    
    var code: Int?
    var data: T?
    var msg: String?
    
    init?(map: Map) {
        
    }
    
    mutating func mapping(map: Map) {
        code     <- map["code"]
        data     <- map["data"]
        msg      <- map["msg"]
    }
}



extension MoyaProvider {
    public func requestService<T: Mappable>(_ target : Target, _ model: T.Type) -> Single<T> {
        
        return Single<T>.create(subscribe: { (single) -> Disposable in
            
            let request = self.rx.request(target).filterSuccessfulStatusCodes().mapObject(defaultModel<T>.self)
                .subscribe(onSuccess: { (result) in
    
                    //根据code值 来实现不同的操作
                    if result.code == 0 { //成功 //返回deta中的模型
                        single(.success(result.data!))
                    }else if result.code! == 110 { //让用户去登录
                        single(.error(DataError.abnormalAccount))
                    }else { //提示msg
                        LCZHUDTool.showError(title: result.msg!)
                        single(.error(DataError.otherError))
                    }

                }) { (error) in
                    LCZHUDTool.showError(title: "网络请求超时,请检查网络状态!")
                }
            return Disposables.create([request])
        })
        
    }
    
}

View

import UIKit

class LoginView: BaseView {
    
    /// 手机号输入框
    var phoneTextField: UITextField!
    
    /// 密码输入框
    var passwordTextField: UITextField!
    
    /// 忘记密码按钮
    var forgotPassword: UIButton!
    
    /// 登录按钮
    var loginButton: UIButton!
    
    
    

    override func config() {
        
        //logo标题
        let logoTitleLabel = UILabel()
        self.addSubview(logoTitleLabel)
        logoTitleLabel.text = "360°智能科技"
        logoTitleLabel.font = UIFont.boldFontSize(value: 18)
        logoTitleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(50 + LCZNaviBarHeight + LCZStatusBarHeight)
        }
        
        //logo图标
        let logoImageView = UIImageView()
        self.addSubview(logoImageView)
        logoImageView.image = UIImage(named: "login")
        logoImageView.snp.makeConstraints { (make) in
            make.right.equalTo(logoTitleLabel.snp.left).offset(-10)
            make.centerY.equalTo(logoTitleLabel)
        }
        
        //手机号输入框
        self.phoneTextField = UITextField()
        self.addSubview(self.phoneTextField)
        self.phoneTextField.placeholder = "请输入手机号"
        self.phoneTextField.keyboardType = .numberPad
        self.phoneTextField.snp.makeConstraints { (make) in
            make.top.equalTo(logoTitleLabel.snp.bottom).offset(40)
            make.left.equalToSuperview().offset(40)
            make.right.equalToSuperview().offset(-40)
            make.height.equalTo(40)
        }
        
        //手机号下的分割线
        let phoneLine = UIView()
        self.addSubview(phoneLine)
        phoneLine.backgroundColor = UIColor.RGBColor(245, 245, 245, 1)
        phoneLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.phoneTextField)
            make.height.equalTo(1)
        }
        
        //密码输入框
        self.passwordTextField = UITextField()
        self.addSubview(self.passwordTextField)
        self.passwordTextField.placeholder = "请输入密码"
        self.passwordTextField.keyboardType = .asciiCapable
        self.passwordTextField.isSecureTextEntry = true
        self.passwordTextField.snp.makeConstraints { (make) in
            make.top.equalTo(phoneLine.snp.bottom).offset(30)
            make.left.right.equalTo(phoneLine)
            make.height.equalTo(40)
        }
        
        //密码分割线
        let passwordLine = UIView()
        self.addSubview(passwordLine)
        passwordLine.backgroundColor = UIColor.RGBColor(245, 245, 245, 1)
        passwordLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.passwordTextField)
            make.height.equalTo(1)
        }
        
        //忘记密码按钮
        self.forgotPassword = UIButton()
        self.addSubview(self.forgotPassword)
        self.forgotPassword.setTitle("忘记密码?", for: .normal)
        self.forgotPassword.setTitleColor(UIColor.RGBColor(153, 153, 153, 1), for: .normal)
        self.forgotPassword.titleLabel?.font = UIFont.fontSize(value: 14)
        self.forgotPassword.snp.makeConstraints { (make) in
            make.top.equalTo(passwordLine.snp.bottom).offset(20)
            make.right.equalTo(passwordLine)
        }
        
        //登录按钮
        self.loginButton = UIButton()
        self.addSubview(self.loginButton)
        self.loginButton.setTitle("登录", for: .normal)
        self.loginButton.setTitleColor(UIColor.white, for: .normal)
        self.loginButton.titleLabel?.font = UIFont.fontSize(value: 18)
        self.loginButton.backgroundColor = UIColor.RGBColor(221, 59, 59, 0.5)
        self.loginButton.endEditing(true)
        self.loginButton.layer.cornerRadius = 5
        self.loginButton.clipsToBounds = true
        self.loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(self.forgotPassword.snp.bottom).offset(30)
            make.left.right.equalTo(passwordLine)
            make.height.equalTo(40)
        }
    }

}

ViewModel

import Foundation
import RxSwift
import RxCocoa
import ObjectMapper

struct LoginModel: Mappable {
    init?(map: Map) {
        
    }
    
    mutating func mapping(map: Map) {
        uid       <- map["uid"]
        token     <- map["token"]
        nickName  <- map["nickName"]
        phone     <- map["phone"]
        photo     <- map["photo"]
    }
    
    
    var uid: Int?
    var token: String?
    var nickName: String?
    var phone: String?
    var photo: String?
    
}



class LoginViewModel {
    
    /// 用户名验证结果
    var validatedUserPhone: Observable<Bool>
    
    /// 密码验证结果
    var validatedPassword: Observable<Bool>
    
    /// 登录结果
    var loginResult: Observable<LoginModel>
    
    /// 初始化
    init(input: (
            userPhone: Observable<String>,
            password: Observable<String>,
            loginTaps: Signal<Void>
            ),
         dependency: (
            networkService: IntelligentNetworkService,
            loginService: LoginService
         )) {
        
        //验证手机号        
        validatedUserPhone = dependency.loginService.validateUserPhone(input.userPhone)
        
        //验证密码
        validatedPassword = dependency.loginService.validatePassword(input.password)
        
        //获取最新的用户名和密码
        let usernameAndPassword = Observable.combineLatest(input.userPhone, input.password) {
            (username: $0, password: $1) }
        
        //获取登录结果
        loginResult = input.loginTaps.asObservable().withLatestFrom(usernameAndPassword)
            .flatMapLatest{ (arg) -> Single<LoginModel> in
                
                return dependency.networkService.verifyLogin(arg.username, arg.password, LoginModel.self)
                
        }
        
    }
}

Controller

import UIKit
import RxCocoa
import RxSwift
import NSObject_Rx

class LoginViewController: BaseViewController {
    
    var loginView: LoginView!
    
    //视图将要显示
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //设置状态栏颜色
        UIApplication.shared.statusBarStyle = .default
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "登录"
        
        //设置X图标
        let spacer = UIBarButtonItem(barButtonSystemItem: .stop, target: self,action: #selector(self.BasePopViewController))
        spacer.tintColor = UIColor.black
        self.navigationItem.leftBarButtonItems = [spacer]
        
        //注册按钮
        let rightBarBtn = UIBarButtonItem(title: "注册", style: .plain, target: self, action: nil)
        rightBarBtn.tintColor = UIColor.RGBColor(153, 153, 153, 1)
        self.navigationItem.rightBarButtonItem = rightBarBtn
        
        //注册响应事件
        rightBarBtn.rx.tap.subscribe { _ in
            let register = RegisterViewController()
            self.BasePushViewController(register)
        }.disposed(by: rx.disposeBag)
        
        //主视图
        self.loginView = LoginView(frame: self.view.bounds)
        self.view.addSubview(self.loginView)
        
        //视图模型
        let viewModel = LoginViewModel.init(
            input: (self.loginView.phoneTextField.rx.text.orEmpty.asObservable(),
                    self.loginView.passwordTextField.rx.text.orEmpty.asObservable(),
                    self.loginView.loginButton.rx.tap.asSignal()
            ),
            dependency:(IntelligentNetworkService(),
                        LoginService()
            )
        )
        
        //订阅手机号码和密码的验证结果。
        Observable.combineLatest(viewModel.validatedUserPhone,viewModel.validatedPassword) {
                        $0 && $1
            }.subscribe { [weak self] in
                self?.loginView.loginButton.backgroundColor = $0.element! ? UIColor.RGBColor(221, 59, 59, 1) : UIColor.RGBColor(221, 59, 59, 0.5)
                self?.loginView.loginButton.isEnabled = $0.element!
        }.disposed(by: rx.disposeBag)
        
        //登录结果
        viewModel.loginResult.subscribe { (result) in
            print(result.element)
        }.disposed(by: rx.disposeBag)
        
    }
    
    

}

登录验证层

import Foundation
import RxSwift
import RxCocoa
import NSObject_Rx

class LoginService {
    
    //账号为11位数
    let phoneCount = 11

    //密码最少位数
    let minPasswordCount = 6
    
    /// 验证用户手机号是否符合要求
    ///
    /// - Parameter userPhone: 手机号码
    /// - Returns: true / false
    func validateUserPhone(_ userPhone: Observable<String>) -> Observable<Bool> {
        return userPhone.map{
             $0.count == 11
        }
    }
    
    
    /// 验证用户密码是否符合要求
    ///
    /// - Parameter password: 密码
    /// - Returns: true / false
    func validatePassword(_ password: Observable<String>) -> Observable<Bool> {
        return password.map{
            $0.count >= 6
        }
    }
}

网络服务层

import Foundation
import RxCocoa
import RxSwift
import NSObject_Rx
import ObjectMapper

class IntelligentNetworkService {
    
    
    /// 登录
    func verifyLogin(_ phoneNum: String, _ password: String, _ model: LoginModel.Type) -> Single<LoginModel> {
       
        return Single<LoginModel>.create(subscribe: { (single) -> Disposable in
            let verifyLogin = intelligentProvider.requestService(.verifyAccount(loginName: phoneNum, passwd: password.md5()), model.self).subscribe(onSuccess: { (model) in
                
                single(.success(model))
                
            }) { (error) in
                print(error);
            }
            return Disposables.create([verifyLogin])
        })

    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 210,978评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,954评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,623评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,324评论 1 282
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,390评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,741评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,892评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,655评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,104评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,451评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,569评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,254评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,834评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,725评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,950评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,260评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,446评论 2 348

推荐阅读更多精彩内容