fix
杨锴
2025-04-16 f646fbebad928833d15546a36e37768d00fe6173
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
//  AddStudentVC.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/12.
//
 
import UIKit
import QMUIKit
import JQTools
 
class AddStudentVC: BaseVC {
 
                @IBOutlet weak var img_profile: UIImageView!
                @IBOutlet weak var view_profile: UIView!
                @IBOutlet weak var tf_name: QMUITextField!
                @IBOutlet weak var tf_gender: UITextField!
                @IBOutlet weak var tf_idCard: UITextField!
                @IBOutlet weak var tf_height: QMUITextField!
                @IBOutlet weak var view_height: UIView!
                @IBOutlet weak var view_weight: UIView!
                @IBOutlet weak var view_phone: UIView!
                @IBOutlet weak var tf_weight: QMUITextField!
                @IBOutlet weak var tf_phone: QMUITextField!
                @IBOutlet weak var tf_birthday: UITextField!
                @IBOutlet weak var img_idCardSingle: UIImageView!
                @IBOutlet weak var img_phoneSingle: UIImageView!
 
                var profileImg:UIImage?
 
                var studentModel = StudentProfileModel()
                var studentInfo:StudentProfile1Model?
                var activityDetailPartModel:ActivityDetailPartModel?
                var type:ItemType!
 
                var verifyIdCard:Bool = false
                var identity:Bool = false //是否需要实名
                var identityPhone:Bool = false //是否需要实名手机
 
 
                init(type:ItemType,studentInfo:StudentProfile1Model? = nil,identity:Bool = false,identityPhone:Bool = false) {
                                super.init(nibName: nil, bundle: nil)
                                self.type = type
                                self.studentInfo = studentInfo
                                self.identity = identity
                                self.identityPhone = identityPhone
                }
 
                init(type:ItemType,activityDetailPartModel:ActivityDetailPartModel? = nil,identity:Bool = false,identityPhone:Bool = false) {
                                super.init(nibName: nil, bundle: nil)
                                self.type = type
                                self.activityDetailPartModel = activityDetailPartModel
                                self.identity = identity
                                self.identityPhone = identityPhone
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
 
                override func viewDidLoad() {
                                super.viewDidLoad()
 
                                img_idCardSingle.isHidden = !identity
                                img_phoneSingle.isHidden = !identityPhone
 
                                if type == .course{
                                                title = "添加运动营成员"
                                                view_profile.isHidden = false
                                                if studentInfo != nil{
                                                                title = "编辑运动营成员"
                                                                tf_name.placeholder = "请输入运动营成员姓名"
                                                                tf_height.placeholder = "请输入运动营成员身高"
                                                                tf_weight.placeholder = "请输入运动营成员体重"
                                                                tf_phone.placeholder = "请输入运动营成员联系方式"
                                                                tf_idCard.placeholder = "请输入运动营成员身份证号"
                                                                tf_name.text = studentInfo!.stuName
                                                                tf_idCard.text = studentInfo!.idCard
                                                                tf_height.text = String(format: "%.0lf", studentInfo!.stuHeight)
                                                                tf_weight.text = String(format: "%.0lf", studentInfo!.stuWeight)
                                                                tf_phone.text = studentInfo!.stuPhone
                                                                tf_birthday.text = studentInfo!.birthday
                                                                img_profile.sd_setImage(with: URL(string: studentInfo!.stuHeadImg))
                                                                studentModel.headImg = studentInfo!.stuHeadImg
                                                                studentModel.stuId = studentInfo!.stuId
                                                                tf_gender.text = studentInfo!.sex == .man ? "男":"女"
                                                }
                                }else{
                                                view_profile.isHidden = false
                                                title = "添加人员"
                                                tf_name.placeholder = "请输入人员姓名"
                                                tf_height.placeholder = "请输入身高"
                                                tf_weight.placeholder = "请输入体重"
                                                tf_phone.placeholder = "请输入联系方式"
                                                tf_idCard.placeholder = "请输入身份证号"
                                                if let m = activityDetailPartModel{
                                                                title = "编辑人员"
                                                                view_profile.isHidden = identity
                                                                tf_name.text = m.name
                                                                tf_idCard.text = m.idcard
                                                                tf_height.text = String(format: "%.0lf", m.height)
                                                                tf_weight.text = String(format: "%.0lf", m.weight)
                                                                tf_phone.text = m.phone
                                                                tf_birthday.text = m.birthday
                                                                tf_gender.text = m.gender == .man ? "男":"女"
                                                                view_height.isHidden = true
                                                                view_weight.isHidden = true
                                                                view_phone.isHidden = true
 
                                                                tf_name.isEnabled = false
                                                                tf_birthday.isEnabled = false
                                                                tf_gender.isEnabled = false
                                                }
                                }
 
                                let idCardView = IDCardView()
                                idCardView.box.bind { [weak self] text in
                                                self?.tf_idCard.text = text
                                }
                                tf_idCard.inputView = idCardView
 
                                let tap = UITapGestureRecognizer(target: self, action: #selector(userImgUploadAction))
                                img_profile.isUserInteractionEnabled = true
                                img_profile.addGestureRecognizer(tap)
                }
 
                override func setUI() {
                                tf_birthday.delegate = self
                                tf_gender.delegate = self
                }
 
//                override func setRx() {
//                                tf_idCard.rx.text.orEmpty.subscribe(onNext: { text in
//                                                if text.count == 18{
//                                                                print("---->")
//                                                }
//                                }).disposed(by: disposeBag)
//                }
 
                @objc func userImgUploadAction(){
                                JQ_ImagePickerTool.getSharedInstance().singleImage({ [weak self] image in
                                                self?.img_profile.image = image
                                                self?.profileImg = image
                                }, clipSize: CGSize(width: JQ_ScreenW, height: JQ_ScreenW))
                }
 
 
                @IBAction func completeAction(_ sender: UIButton) {
 
                                if !view_profile.isHidden{
                                                guard profileImg != nil || !(studentInfo?.stuHeadImg.isEmpty ?? true) else {
 
                                                                if type == .course{
                                                                                alertError(msg: "请上传运动营成员头像");
                                                                }else{
                                                                                alertError(msg: "请上传人员头像");
                                                                }
 
                                                                return
                                                }
                                }
 
                                guard !tf_name.text!.isEmpty else {alertError(msg: tf_name.placeholder!);return}
                                guard !tf_birthday.text!.isEmpty else {alertError(msg: "请选择生日");return}
                                guard !tf_gender.text!.isEmpty else {alertError(msg: "请选择性别");return}
 
                                guard !tf_height.text!.isEmpty else {alertError(msg: tf_height.placeholder!);return}
                                guard !tf_weight.text!.isEmpty else {alertError(msg: tf_weight.placeholder!);return}
                                guard tf_height.text != "0" else {alertError(msg: "请输入正确的身高");return}
                                guard tf_weight.text != "0" else {alertError(msg: "请输入正确的体重");return}
 
        if !view_phone.isHidden{
                                                guard tf_phone.text!.jq_isPhone else {alertError(msg: "请输入正确的手机号");return}
                                }
 
                                if !tf_idCard.isEmpty || identity{
                                                guard tf_idCard.text!.jq_idCard() else {alertError(msg: "请输入正确的身份证号码");return}
                                }
 
                                if let birthday = tf_idCard.text?.jq_idCardBirthday(){
                                                studentModel.birthday = birthday
                                }
 
                                studentModel.name = tf_name.text!
                                studentModel.height = tf_height.text!.toDouble
                                studentModel.weight = tf_weight.text!.toDouble
                                studentModel.phone = tf_phone.text!
                                studentModel.idCard = tf_idCard.text!
                                studentModel.name = tf_name.text!
                                studentModel.birthday = tf_birthday.text!
 
                                if profileImg != nil{
                                                showHUD("正在上传头像")
                                                profileImg!.uploadImgToService().subscribe(onNext: { [weak self] imgUrl in
                                                                guard let weakSelf = self else { return }
                                                                weakSelf.studentModel.headImg = imgUrl
                                                                hiddenHUD()
                                                                weakSelf.addStudent()
                                                }, onError: { error in
                                                                hiddenHUD()
                                                                alertError(msg: error.localizedDescription)
                                                }).disposed(by: disposeBag)
                                }else{
                                                addStudent()
                                }
                }
 
                private func addStudent(){
                                if type == .course{
                                                if studentInfo != nil{
                                                                Services.editStu(studentModel).subscribe(onNext: {[weak self]data in
                                                                                self?.addStudentSuccess(text: "编辑成功")
                                                                                NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                                                                }) { error in
 
                                                                }.disposed(by: disposeBag)
                                                }else{
                                                                Services.addStudent(studentModel).subscribe(onNext: { [weak self] data in
                                                                                self?.addStudentSuccess(text: "添加成功")
                                                                }).disposed(by: disposeBag)
                                                }
                                }else{
                                                if activityDetailPartModel != nil{
                                                                activityDetailPartModel!.idcard = tf_idCard.text!
                                                                Services.editParticipant(activityDetailPartModel!).subscribe(onNext: {[weak self] data in
                                                                                self?.addStudentSuccess(text: "编辑成功")
                                                                }).disposed(by: disposeBag)
 
                                                }else{
                                                                Services.addParticipant(studentModel).subscribe(onNext: {[weak self] data in
                                                                                self?.addStudentSuccess(text: "添加成功")
                                                                }).disposed(by: disposeBag)
                                                }
                                }
                }
 
                private func addStudentSuccess(text:String){
                                alertSuccess(msg: text)
                                DispatchQueue.main.asyncAfter(deadline: .now()+1) {
                                                self.navigationController?.popViewController()
                                                NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                                }
                }
 
                deinit{
                                JQ_ImagePickerTool.destroy()
                }
}
 
extension AddStudentVC:UITextFieldDelegate{
                func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
                                if textField == tf_birthday{
                                                view.endEditing(true)
                                                CommonDatePickerView.show(before: 60, after: 0, type: .YMD) { year, month, day,_,_ in
                                                                textField.text = String(format: "%ld-%02ld-%02ld", year!,month!,day!)
                                                }
                                                return false
                                }
 
                                if textField == tf_gender{
                                                let alertVC = UIAlertController(title: "性别", message: nil, preferredStyle: .actionSheet)
                                                alertVC.addAction(UIAlertAction(title: "男", style: .default) { _ in
                                                                self.tf_gender.text = "男"
                                                                self.studentModel.sex = .man
                                                                self.activityDetailPartModel?.gender = .man
                                                                self.studentInfo?.sex = .man
                                                })
 
                                                alertVC.addAction(UIAlertAction(title: "女", style: .default) { _ in
                                                                self.tf_gender.text = "女"
                                                                self.studentModel.sex = .woman
                                                                self.activityDetailPartModel?.gender = .woman
                                                                self.studentInfo?.sex = .woman
                                                })
 
                                                alertVC.addAction(UIAlertAction(title: "取消", style: .cancel))
                                                present(alertVC, animated: true)
                                                return false
                                }
                                return true
                }
}