| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //
- // LNLoginCaptchaInputViewController.swift
- // Gami
- //
- // Created by OneeChan on 2026/1/16.
- //
- import Foundation
- import UIKit
- import SnapKit
- extension UIView {
- func pushToLoginCaptcha(code: String, phone: String) {
- let vc = LNLoginCaptchaInputViewController(code: code, phone: phone)
- navigationController?.pushViewController(vc, animated: true)
- }
- }
- class LNLoginCaptchaInputViewController: LNViewController {
- private let code: String
- private let phone: String
-
- private let captchaInput = LNCaptchaInputView()
- private let tipsView = LNAutoSizeTextView()
-
- init(code: String, phone: String) {
- self.code = code
- self.phone = phone
-
- super.init(nibName: nil, bundle: nil)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- setupViews()
-
- LNEventDeliver.addObserver(self)
- }
- }
- extension LNLoginCaptchaInputViewController: LNAccountManagerNotify {
- func onCaptchaCoolDownChanged(time: Int) {
- if time == 0 {
- let text = NSMutableAttributedString(string: .init(key: "B00028"), attributes: [
- .foregroundColor: UIColor.text_5
- ])
- let resend = NSMutableAttributedString(string: .init(key: "B00029"), attributes: [
- .font: UIFont.heading_h4,
- .link: "Gami:resend",
- ])
- text.append(.init(string: " "))
- text.append(resend)
- tipsView.attributedText = text
- } else {
- let text: String = .init(key: "B00027") + " (\(time)s)"
- tipsView.attributedText = .init(string: text, attributes: [
- .foregroundColor: UIColor.text_3
- ])
- }
- }
-
- func onUserLogin() {
- navigationController?.popToRootViewController(animated: true)
- }
- }
- extension LNLoginCaptchaInputViewController: UITextViewDelegate {
- func textView(_ textView: UITextView, shouldInteractWith URL: URL,
- in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
- showLoading()
- LNAccountManager.shared.getLoginCaptcha(code: code, phone: phone) { [weak self] success in
- dismissLoading()
- guard self != nil else { return }
- guard success else { return }
- showToast(.init(key: "B00030"))
- }
-
- return false
- }
-
- func textViewDidChangeSelection(_ textView: UITextView) {
- textView.selectedTextRange = nil
- }
- }
- extension LNLoginCaptchaInputViewController: LNCaptchaInputViewDelegate {
- func onCaptchaInputChange(view: LNCaptchaInputView) {
- guard view.hasDone else { return }
-
- LNAccountManager.shared.loginByPhone(code: code, num: phone, captcha: view.curInput)
- }
- }
- extension LNLoginCaptchaInputViewController {
- private func setupViews() {
- showNavigationBar = false
-
- let cover = UIImageView(image: .icLoginProfileBg)
- view.addSubview(cover)
- cover.snp.makeConstraints { make in
- make.top.horizontalEdges.equalToSuperview()
- }
-
- let fakeNav = LNFakeNaviBar()
- fakeNav.showBackButton()
- view.addSubview(fakeNav)
- fakeNav.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- let textView = buildTextView()
- view.addSubview(textView)
- textView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22)
- make.top.equalTo(fakeNav.snp.bottom).offset(20)
- }
-
- captchaInput.delegate = self
- view.addSubview(captchaInput)
- captchaInput.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22)
- make.top.equalTo(textView.snp.bottom).offset(24)
- make.height.equalTo(64)
- }
-
- let tipsView = buildCoolDown()
- view.addSubview(tipsView)
- tipsView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22 - 4)
- make.top.equalTo(captchaInput.snp.bottom).offset(24 - 7) // 7 是 UITextView 自身的内容缩进
- }
-
- view.onTap { [weak self] in
- guard let self else { return }
- view.endEditing(true)
- }
- }
-
- private func buildTextView() -> UIView {
- let container = UIView()
-
- let titleLabel = UILabel()
- titleLabel.text = .init(key: "B00025")
- titleLabel.font = .heading_h1
- titleLabel.textColor = .text_5
- container.addSubview(titleLabel)
- titleLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- let descLabel = UILabel()
- descLabel.text = .init(key: "B00026") + "\n\(code) \(phone)"
- descLabel.font = .body_m
- descLabel.textColor = .text_4
- descLabel.numberOfLines = 0
- container.addSubview(descLabel)
- descLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalTo(titleLabel.snp.bottom).offset(13)
- make.bottom.equalToSuperview()
- }
-
- return container
- }
-
- private func buildCoolDown() -> UIView {
- tipsView.font = .body_m
- tipsView.isEditable = false
- tipsView.delegate = self
- tipsView.showsVerticalScrollIndicator = false
- tipsView.showsHorizontalScrollIndicator = false
- tipsView.backgroundColor = .clear
- tipsView.linkTextAttributes = [.foregroundColor: UIColor.text_6]
-
- return tipsView
- }
- }
|