QSLLoginViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // QSLLoginViewController.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/11/26.
  6. //
  7. import UIKit
  8. import YYText
  9. enum QSLLoginJumpType: Int {
  10. case mine // 我的
  11. case add // 添加好友
  12. case road // 查看轨迹
  13. case contact // 添加紧急联系人
  14. }
  15. class QSLLoginViewController: QSLBaseController {
  16. var type: QSLLoginJumpType?
  17. lazy var loginBg: UIImageView = {
  18. let imageView = UIImageView()
  19. imageView.image = UIImage(named: "mine_bg")
  20. return imageView
  21. }()
  22. lazy var backButton: UIButton = {
  23. let button = UIButton()
  24. button.image(UIImage(named: "public_back_btn"))
  25. button.title("登录")
  26. button.mediumFont(17)
  27. button.textColor(QSLColor.Color_202020)
  28. button.setImageTitleLayout(.imgLeft, spacing: 4.rpx)
  29. button.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
  30. return button
  31. }()
  32. lazy var logoImageView: UIImageView = {
  33. let imageView = UIImageView()
  34. imageView.image = UIImage(named: "mine_about_logo")
  35. return imageView
  36. }()
  37. lazy var phoneView: UIView = {
  38. let view = UIView()
  39. view.addRadius(radius: 6.rpx)
  40. view.backgroundColor = .hexStringColor(hexString: "#FAFAFA")
  41. return view
  42. }()
  43. lazy var phonePreLabel: UILabel = {
  44. let label = UILabel()
  45. label.text("+86")
  46. label.mediumFont(16)
  47. label.textColor = QSLColor.Color_202020
  48. return label
  49. }()
  50. lazy var phoneLineView: UIView = {
  51. let view = UIView()
  52. view.backgroundColor = .hexStringColor(hexString: "#E2E2E2")
  53. return view
  54. }()
  55. lazy var phoneTextField: UITextField = {
  56. let textField = UITextField()
  57. textField.maxTextNumber = 11
  58. textField.delegate = self
  59. textField.keyboardType = .numberPad
  60. textField.textColor = QSLColor.Color_202020
  61. textField.font = UIFont.textF(16)
  62. textField.placeholder = "请输入11位手机号码"
  63. textField.setPlaceholderAttribute(font: UIFont.textF(16), color: UIColor.hexStringColor(hexString: "#A7A7A7"))
  64. return textField
  65. }()
  66. lazy var codeView: UIView = {
  67. let view = UIView()
  68. view.addRadius(radius: 6.rpx)
  69. view.backgroundColor = .hexStringColor(hexString: "#FAFAFA")
  70. return view
  71. }()
  72. lazy var codeTextField: UITextField = {
  73. let textField = UITextField()
  74. textField.delegate = self
  75. textField.keyboardType = .numberPad
  76. textField.textColor = QSLColor.Color_202020
  77. textField.font = UIFont.textF(16)
  78. textField.placeholder = "请输入验证码"
  79. textField.setPlaceholderAttribute(font: UIFont.textF(16), color: UIColor.hexStringColor(hexString: "#A7A7A7"))
  80. return textField
  81. }()
  82. lazy var codeButton: UIButton = {
  83. let button = UIButton()
  84. button.addRadius(radius: 4.rpx)
  85. button.setBackgroundColor(.hexStringColor(hexString: "#15CBA1", alpha: 0.2), forState: .disabled)
  86. button.setBackgroundColor(.hexStringColor(hexString: "#15CBA1", alpha: 1), forState: .normal)
  87. button.title("发送验证码")
  88. button.textColor(.white)
  89. button.font(14)
  90. button.addTarget(self, action: #selector(codeButtonAction), for: .touchUpInside)
  91. return button
  92. }()
  93. lazy var selectBtn: UIButton = {
  94. let btn = UIButton()
  95. btn.image(UIImage(named: "public_select_btn_false"), .normal)
  96. btn.image(UIImage(named: "public_select_btn_true"), .selected)
  97. btn.addTarget(self, action: #selector(selectButtonAction), for: .touchUpInside)
  98. return btn
  99. }()
  100. lazy var serviceLabel: YYLabel = {
  101. let label = YYLabel()
  102. let attr = NSMutableAttributedString()
  103. let firstAttr = NSMutableAttributedString(string: "已阅读并同意")
  104. firstAttr.font(12)
  105. firstAttr.color(.hexStringColor(hexString: "#A7A7A7"))
  106. attr.append(firstAttr)
  107. let blankAttr = NSMutableAttributedString(string: " ")
  108. blankAttr.font(12)
  109. attr.append(blankAttr)
  110. let privacyHL = YYTextHighlight()
  111. var privacyStr = "《隐私权政策》"
  112. let privacyText = NSMutableAttributedString(string: privacyStr)
  113. privacyText.font(12)
  114. privacyText.color(.hexStringColor(hexString: "#2F79FF"))
  115. privacyText.yy_setTextHighlight(privacyHL, range: NSRange(location: 0, length: privacyStr.count))
  116. privacyHL.tapAction = { [weak self] containerView, text, range, rect in
  117. self?.privacyAction()
  118. }
  119. attr.append(privacyText)
  120. attr.append(blankAttr)
  121. let andAttr = NSMutableAttributedString(string: "和")
  122. andAttr.font(12)
  123. andAttr.color(.hexStringColor(hexString: "#A7A7A7"))
  124. attr.append(andAttr)
  125. attr.append(blankAttr)
  126. let serviceHL = YYTextHighlight()
  127. var serviceStr = "《用户协议》"
  128. let serviceText = NSMutableAttributedString(string: serviceStr)
  129. serviceText.font(12)
  130. serviceText.color(.hexStringColor(hexString: "#2F79FF"))
  131. serviceText.yy_setTextHighlight(serviceHL, range: NSRange(location: 0, length: serviceStr.count))
  132. serviceHL.tapAction = { [weak self] containerView, text, range, rect in
  133. self?.serviceAction()
  134. }
  135. attr.append(serviceText)
  136. label.attributedText = attr
  137. return label
  138. }()
  139. lazy var loginBtn: UIButton = {
  140. let btn = UIButton()
  141. btn.addRadius(radius: 22.rpx)
  142. if let image = UIImage.gradient([.hexStringColor(hexString: "#15CBA1", alpha: 0.2), .hexStringColor(hexString: "#1FE0BA", alpha: 0.2)], size: CGSize(width: 280.rpx, height: 44.rpx), locations: [0, 1], direction: .horizontal) {
  143. btn.setBackgroundColor(UIColor(patternImage: image), forState: .disabled)
  144. }
  145. if let image = UIImage.gradient([.hexStringColor(hexString: "#15CBA1"), .hexStringColor(hexString: "#1FE0BA")], size: CGSize(width: 280.rpx, height: 44.rpx), locations: [0, 1], direction: .horizontal) {
  146. btn.setBackgroundColor(UIColor(patternImage: image), forState: .normal)
  147. }
  148. btn.title("登录")
  149. btn.textColor(.white)
  150. btn.mediumFont(16)
  151. btn.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
  152. return btn
  153. }()
  154. override func viewDidLoad() {
  155. super.viewDidLoad()
  156. initializeView()
  157. if let type = self.type {
  158. switch type {
  159. case .mine:
  160. gravityInstance?.track(QSLGravityConst.login_show, properties: ["id": 1001])
  161. case .add:
  162. gravityInstance?.track(QSLGravityConst.login_show, properties: ["id": 1002])
  163. case .road:
  164. gravityInstance?.track(QSLGravityConst.login_show, properties: ["id": 1003])
  165. case .contact:
  166. gravityInstance?.track(QSLGravityConst.login_show, properties: ["id": 1004])
  167. }
  168. }
  169. }
  170. }
  171. extension QSLLoginViewController {
  172. @objc func privacyAction() {
  173. let vc = QSLWebViewController()
  174. vc.webUrl = QSLConfig.AppPrivacyAgreementLink
  175. vc.title = "隐私政策"
  176. self.navigationController?.pushViewController(vc, animated: true)
  177. }
  178. @objc func serviceAction() {
  179. let vc = QSLWebViewController()
  180. vc.webUrl = QSLConfig.AppServiceAgreementLink
  181. vc.title = "服务协议"
  182. self.navigationController?.pushViewController(vc, animated: true)
  183. }
  184. @objc func selectButtonAction() {
  185. gravityInstance?.track(QSLGravityConst.login_agree)
  186. self.selectBtn.isSelected = !self.selectBtn.isSelected
  187. }
  188. // 验证码
  189. @objc func codeButtonAction() {
  190. gravityInstance?.track(QSLGravityConst.login_code)
  191. if let phone = self.phoneTextField.text, phone.count == 11 {
  192. codeButton.countDown(60)
  193. QSLNetwork().request(.userCode(dict: ["phone": phone]), success: { response in
  194. }, fail: { code, error in
  195. gravityInstance?.track(QSLGravityConst.login_code_fail, properties: ["id": 1001])
  196. self.view.toast(text: "发送失败")
  197. })
  198. } else {
  199. gravityInstance?.track(QSLGravityConst.login_code_fail, properties: ["id": 1002])
  200. self.view.toast(text: "请输入正确的手机号码")
  201. }
  202. }
  203. // 登录按钮
  204. @objc func loginButtonAction() {
  205. gravityInstance?.track(QSLGravityConst.login_click)
  206. guard let phone = self.phoneTextField.text, phone.count == 11 else {
  207. self.view.toast(text: "请输入正确的手机号码")
  208. return
  209. }
  210. guard let code = self.codeTextField.text else {
  211. self.view.toast(text: "请输入验证码")
  212. return
  213. }
  214. if !self.selectBtn.isSelected {
  215. gravityInstance?.track(QSLGravityConst.login_fail, properties: ["id": 1001])
  216. self.view.toast(text: "请先阅读并同意《隐私权政策》和《用户协议》")
  217. return
  218. }
  219. QSLLoading.show()
  220. QSLNetwork().request(.userLogin(dict: ["phone": phone, "code": code]), success: { response in
  221. QSLLoading.hide()
  222. let authToken = response.toJSON(modelKey: "data>authToken").stringValue
  223. QSLBaseManager.shared.loginUpdateUser(authToken: authToken, phone: phone)
  224. UIApplication.keyWindow?.toast(text: "登录成功")
  225. gravityInstance?.track(QSLGravityConst.login_success)
  226. // 发送通知
  227. NotificationCenter.default.post(name: QSLNotification.QSLLogin, object: nil)
  228. // 发送通知
  229. NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
  230. // 返回
  231. self.backBtnAction()
  232. }, fail: { code, error in
  233. if code == 1005 {
  234. gravityInstance?.track(QSLGravityConst.login_fail, properties: ["id": 1002])
  235. } else {
  236. gravityInstance?.track(QSLGravityConst.login_fail, properties: ["id": 1003])
  237. }
  238. QSLLoading.hide()
  239. self.view.toast(text: error)
  240. })
  241. }
  242. }
  243. extension QSLLoginViewController: UITextFieldDelegate {
  244. }
  245. extension QSLLoginViewController {
  246. func initializeView() {
  247. self.view.backgroundColor = .white
  248. self.view.addSubview(loginBg)
  249. loginBg.snp.makeConstraints { make in
  250. make.left.top.right.equalToSuperview()
  251. }
  252. self.view.addSubview(backButton)
  253. backButton.snp.makeConstraints { make in
  254. make.size.equalTo(CGSize(width: 65.rpx, height: 25.rpx))
  255. make.left.equalTo(12.rpx)
  256. make.top.equalTo(QSLConst.qsl_kStatusBarFrameH)
  257. }
  258. self.view.addSubview(logoImageView)
  259. logoImageView.snp.makeConstraints { make in
  260. make.size.equalTo(CGSize(width: 70.rpx, height: 70.rpx))
  261. make.centerX.equalToSuperview()
  262. make.top.equalTo(backButton.snp.bottom).offset(30.rpx)
  263. }
  264. self.view.addSubview(phoneView)
  265. phoneView.addSubview(phonePreLabel)
  266. phoneView.addSubview(phoneLineView)
  267. phoneView.addSubview(phoneTextField)
  268. phoneView.snp.makeConstraints { make in
  269. make.left.equalTo(24.rpx)
  270. make.right.equalTo(-24.rpx)
  271. make.height.equalTo(50.rpx)
  272. make.top.equalTo(logoImageView.snp.bottom).offset(40.rpx)
  273. }
  274. phonePreLabel.snp.makeConstraints { make in
  275. make.left.equalTo(12.rpx)
  276. make.centerY.equalToSuperview()
  277. }
  278. phoneLineView.snp.makeConstraints { make in
  279. make.top.equalTo(15.rpx)
  280. make.bottom.equalTo(-15.rpx)
  281. make.left.equalTo(49.rpx)
  282. make.width.equalTo(1.rpx)
  283. }
  284. phoneTextField.snp.makeConstraints { make in
  285. make.left.equalTo(phoneLineView.snp.right).offset(25.rpx)
  286. make.right.equalTo(-12.rpx)
  287. make.top.bottom.equalTo(0)
  288. }
  289. self.view.addSubview(codeView)
  290. codeView.addSubview(codeTextField)
  291. codeView.addSubview(codeButton)
  292. codeView.snp.makeConstraints { make in
  293. make.left.equalTo(24.rpx)
  294. make.right.equalTo(-24.rpx)
  295. make.height.equalTo(50.rpx)
  296. make.top.equalTo(phoneView.snp.bottom).offset(12.rpx)
  297. }
  298. codeButton.snp.makeConstraints { make in
  299. make.size.equalTo(CGSize(width: 90.rpx, height: 32.rpx))
  300. make.right.equalTo(-12.rpx)
  301. make.centerY.equalToSuperview()
  302. }
  303. codeTextField.snp.makeConstraints { make in
  304. make.left.equalTo(12.rpx)
  305. make.top.bottom.equalTo(0)
  306. make.right.equalTo(codeButton.snp.left).offset(-12.rpx)
  307. }
  308. self.view.addSubview(selectBtn)
  309. self.view.addSubview(serviceLabel)
  310. selectBtn.snp.makeConstraints { make in
  311. make.size.equalTo(CGSize(width: 12.rpx, height: 12.rpx))
  312. make.left.equalTo(25.rpx)
  313. make.top.equalTo(codeView.snp.bottom).offset(14.rpx)
  314. }
  315. serviceLabel.snp.makeConstraints { make in
  316. make.centerY.equalTo(selectBtn.snp.centerY)
  317. make.left.equalTo(selectBtn.snp.right).offset(4.rpx)
  318. }
  319. self.view.addSubview(loginBtn)
  320. loginBtn.snp.makeConstraints { make in
  321. make.size.equalTo(CGSize(width: 280.rpx, height: 44.rpx))
  322. make.centerX.equalToSuperview()
  323. make.bottom.equalTo(-QSLConst.qsl_kTabbarBottom - 95.rpx)
  324. }
  325. }
  326. }