QSLLoginViewController.swift 14 KB

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