宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
//  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)"])
        }
    }
}