//
|
// Check.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/5/20.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import Foundation
|
extension String {
|
|
/// 检查手机号码格式
|
///
|
/// - Throws: 错误原因
|
func checkPhoneNumber() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .phoneNumberEmptyError)
|
}
|
|
guard self.regEx(pattern: .isPhoneNumber) else {
|
throw NSError(localizedDescription: .phoneNumberError)
|
}
|
}
|
|
/// 检查手机号绑定
|
///
|
/// - Throws: 错误原因
|
func checkPhoneNumberBind() throws {
|
|
guard self != app.userInfo.phone else {
|
throw NSError(localizedDescription: "不能绑定当前手机号")
|
}
|
}
|
|
/// 检查验证码格式
|
///
|
/// - Throws: 错误原因
|
func checkSMSCode() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .SMSCodeEmptyError)
|
}
|
|
guard self.regEx(pattern: .isSMSCodeOrPaymentCode) else {
|
throw NSError(localizedDescription: .SMSCodeError)
|
}
|
}
|
|
/// 检查密码格式
|
///
|
/// - Throws: 错误原因
|
func checkPassword() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .passwordEmptyError)
|
}
|
|
guard self.regEx(pattern: .isPassword) else {
|
throw NSError(localizedDescription: .passwordError)
|
}
|
}
|
|
/// 检查原密码格式
|
///
|
/// - Throws: 错误原因
|
func checkOriginalPassword() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .originalPasswordEmptyError)
|
}
|
|
guard self.regEx(pattern: .isPassword) else {
|
throw NSError(localizedDescription: .originalPasswordError)
|
}
|
}
|
|
/// 检查新密码格式
|
///
|
/// - Throws: 错误原因
|
func checkNewPassword() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .newPasswordEmptyError)
|
}
|
|
guard self.regEx(pattern: .isPassword) else {
|
throw NSError(localizedDescription: .newPasswordError)
|
}
|
}
|
|
/// 检查名字
|
///
|
/// - Throws: 错误原因
|
func checkName() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .realNameEmptyError)
|
}
|
|
}
|
|
/// 检查身份证号
|
///
|
/// - Throws: 错误原因
|
func checkIdCode() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .idCodeEmptyError)
|
}
|
|
}
|
|
/// 检查意见反馈格式
|
///
|
/// - Throws: 错误原因
|
func checkFeedback() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .feedbackEmptyError)
|
}
|
}
|
|
/// 比较密码一致性
|
///
|
/// - Parameter anOtherPassword: 需要比较的另一个密码
|
/// - Throws: 错误原因
|
func comparePassword(by anOtherPassword: String) throws {
|
|
guard self == anOtherPassword else {
|
throw NSError(localizedDescription: .comparePasswordError)
|
}
|
|
}
|
|
/// 检查联系电话格式
|
///
|
/// - Throws: 错误原因
|
func checkContactNumber() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .contactNumberEmptyError)
|
}
|
|
guard self.regEx(pattern: .isPhoneNumber) else {
|
throw NSError(localizedDescription: .contactNumberError)
|
}
|
}
|
|
/// 检查详细地址格式
|
///
|
/// - Throws: 错误原因
|
func checkDetailedAddress() throws {
|
|
guard !self.isEmpty else {
|
throw NSError(localizedDescription: .detailedAddressEmptyError)
|
}
|
|
}
|
|
}
|
|
extension Bool {
|
|
/// 检查是否同意协议
|
///
|
/// - Parameter agreementName: 协议名
|
/// - Throws: 错误原因
|
func checkAgreement(agreementName: String) throws {
|
|
guard self else {
|
throw NSError(domain: "com.sdongpo.lvyouyou", code: -9999, userInfo: [NSLocalizedDescriptionKey: "请先同意\(agreementName)"])
|
}
|
}
|
}
|