From 858e76cf34395330fc4253d7e90d143ff75c4590 Mon Sep 17 00:00:00 2001 From: wenlei Date: Tue, 16 Jan 2024 21:32:50 +0800 Subject: [PATCH] The login logic is modified --- .../IndieMusic.xcodeproj/project.pbxproj | 4 + .../Common/AlertViewController.swift | 12 ++- .../Common/TableViewController.swift | 1 + .../IndieMusic/Common/ViewController.swift | 7 +- .../IndieMusic/Managers/AuthManager.swift | 25 +++--- IndieMusic/IndieMusic/Models/User.swift | 17 ++++ .../Modules/Home/HomeTabBarViewModel.swift | 23 ++++++ .../IndieMusic/Modules/Home/HomeView.swift | 63 +++++++++------ .../Modules/Home/HomeViewController.swift | 80 ++++++++----------- .../Modules/Home/HomeViewModel.swift | 3 - .../Login/AgreementViewController.swift | 14 ++-- .../Modules/Login/LoginViewController.swift | 4 +- .../Modules/Login/PhoneCodeController.swift | 43 +++++----- .../Modules/Login/PhoneCodeViewModel.swift | 47 ++++------- .../Modules/Mine/MineViewModel.swift | 17 +++- .../Player/AudioTrackListViewController.swift | 6 +- IndieMusic/IndieMusic/Networking/Api.swift | 4 + .../Networking/Rest/APIConfig.swift | 20 +++-- .../IndieMusic/Networking/Rest/RestApi.swift | 12 ++- 19 files changed, 236 insertions(+), 166 deletions(-) create mode 100644 IndieMusic/IndieMusic/Models/User.swift diff --git a/IndieMusic/IndieMusic.xcodeproj/project.pbxproj b/IndieMusic/IndieMusic.xcodeproj/project.pbxproj index 052cb3f..7932442 100644 --- a/IndieMusic/IndieMusic.xcodeproj/project.pbxproj +++ b/IndieMusic/IndieMusic.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 770228E92B55169C00E07F7A /* InternationalNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770228E82B55169C00E07F7A /* InternationalNumber.swift */; }; 770228EB2B55174D00E07F7A /* InternationalNumberViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770228EA2B55174D00E07F7A /* InternationalNumberViewModel.swift */; }; 770228ED2B55284F00E07F7A /* Login.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770228EC2B55284F00E07F7A /* Login.swift */; }; + 770228EF2B56142E00E07F7A /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770228EE2B56142E00E07F7A /* User.swift */; }; 77165D742B464493002AE0A5 /* BarButtonItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77165D732B464493002AE0A5 /* BarButtonItem.swift */; }; 7736FF442B4CECF2008D5DAD /* CommentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7736FF432B4CECF2008D5DAD /* CommentViewModel.swift */; }; 7736FF462B4CF0E6008D5DAD /* CommentDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7736FF452B4CF0E6008D5DAD /* CommentDetailViewController.swift */; }; @@ -214,6 +215,7 @@ 770228E82B55169C00E07F7A /* InternationalNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternationalNumber.swift; sourceTree = ""; }; 770228EA2B55174D00E07F7A /* InternationalNumberViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternationalNumberViewModel.swift; sourceTree = ""; }; 770228EC2B55284F00E07F7A /* Login.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Login.swift; sourceTree = ""; }; + 770228EE2B56142E00E07F7A /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = ""; }; 77165D732B464493002AE0A5 /* BarButtonItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarButtonItem.swift; sourceTree = ""; }; 7736FF432B4CECF2008D5DAD /* CommentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentViewModel.swift; sourceTree = ""; }; 7736FF452B4CF0E6008D5DAD /* CommentDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentDetailViewController.swift; sourceTree = ""; }; @@ -622,6 +624,7 @@ 77FA0B352B0C50D800404C5E /* Share.swift */, 77FA0B3D2B0C573600404C5E /* Filter.swift */, 77FA0B532B0F447400404C5E /* Mine.swift */, + 770228EE2B56142E00E07F7A /* User.swift */, 7751D3552B42AE4600F1F2BD /* Setting.swift */, 7751D35D2B42B61100F1F2BD /* PersonInfo.swift */, 7751D3712B43B9ED00F1F2BD /* Search.swift */, @@ -1148,6 +1151,7 @@ 7751D3802B45271600F1F2BD /* BindPhoneViewModel.swift in Sources */, 774A17F52B045F1C00F56DF1 /* Player.swift in Sources */, 7751D3882B45584000F1F2BD /* LaunchADManager.swift in Sources */, + 770228EF2B56142E00E07F7A /* User.swift in Sources */, 7751D37A2B450BB100F1F2BD /* AgreementViewController.swift in Sources */, 778B8AAB2AF8ED0E0034AFD4 /* UIView+Borders.swift in Sources */, 778B8AC12AF8ED280034AFD4 /* TableView.swift in Sources */, diff --git a/IndieMusic/IndieMusic/Common/AlertViewController.swift b/IndieMusic/IndieMusic/Common/AlertViewController.swift index 430bfb1..affc8f3 100644 --- a/IndieMusic/IndieMusic/Common/AlertViewController.swift +++ b/IndieMusic/IndieMusic/Common/AlertViewController.swift @@ -56,7 +56,6 @@ class AlertViewController: UIViewController { cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 16) cancelButton.setTitle("取消", for: .normal) cancelButton.setTitleColor(UIColor.tertiaryText(), for: .normal) - cancelButton.addTarget(AlertViewController.self, action: #selector(cancelButtonClicked), for: .touchUpInside) return cancelButton }() @@ -66,13 +65,13 @@ class AlertViewController: UIViewController { confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 16) confirmButton.setTitle("确定", for: .normal) confirmButton.setTitleColor(UIColor.primaryText(), for: .normal) - confirmButton.addTarget(AlertViewController.self, action: #selector(confirmButtonClick), for: .touchUpInside) return confirmButton }() var confirmClosures: (()->())? + var cancelClosures: (()->())? override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { @@ -94,6 +93,10 @@ class AlertViewController: UIViewController { super.viewDidLoad() makeUI() + + cancelButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside) + confirmButton.addTarget(self, action: #selector(confirmButtonClick), for: .touchUpInside) + } @@ -158,7 +161,10 @@ class AlertViewController: UIViewController { } @objc func cancelButtonClicked() { - self.dismiss(animated: true) + if let cancelClosures = self.cancelClosures { + cancelClosures() + self.dismiss(animated: true) + } } @objc func confirmButtonClick() { diff --git a/IndieMusic/IndieMusic/Common/TableViewController.swift b/IndieMusic/IndieMusic/Common/TableViewController.swift index da27ca5..07e6776 100644 --- a/IndieMusic/IndieMusic/Common/TableViewController.swift +++ b/IndieMusic/IndieMusic/Common/TableViewController.swift @@ -63,6 +63,7 @@ class TableViewController: ViewController, UIScrollViewDelegate { }.disposed(by: rx.disposeBag) + isHeaderLoading.bind(to: self.tableView.rx.endRefresh).disposed(by: rx.disposeBag) isFooterLoading.bind(to: self.tableView.rx.endRefresh).disposed(by: rx.disposeBag) diff --git a/IndieMusic/IndieMusic/Common/ViewController.swift b/IndieMusic/IndieMusic/Common/ViewController.swift index edcd60f..067d6e6 100644 --- a/IndieMusic/IndieMusic/Common/ViewController.swift +++ b/IndieMusic/IndieMusic/Common/ViewController.swift @@ -116,10 +116,11 @@ class ViewController: UIViewController, Navigatable { public override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + + updateUI() if automaticallyAdjustsLeftBarButtonItem { adjustLeftBarButtonItem() } - updateUI() } public override func viewDidAppear(_ animated: Bool) { @@ -132,6 +133,8 @@ class ViewController: UIViewController, Navigatable { deinit { logDebug("\(type(of: self)): Deinited") logResourcesCount() + + print("\(type(of: self)): Deinited") } override public func didReceiveMemoryWarning() { @@ -194,6 +197,8 @@ class ViewController: UIViewController, Navigatable { self.navigationItem.leftBarButtonItem = backBarButton } else if self.presentingViewController != nil { // presented self.navigationItem.leftBarButtonItem = closeBarButton + } else { + self.navigationItem.leftBarButtonItem = nil } } diff --git a/IndieMusic/IndieMusic/Managers/AuthManager.swift b/IndieMusic/IndieMusic/Managers/AuthManager.swift index 8a9503c..312fbf9 100644 --- a/IndieMusic/IndieMusic/Managers/AuthManager.swift +++ b/IndieMusic/IndieMusic/Managers/AuthManager.swift @@ -29,18 +29,21 @@ class AuthManager { var token: Token? { get { - guard let jsonString = keychain[tokenKey]?.data(using: .utf8), - let token = try? JSONDecoder().decode(Token.self, from: jsonString) else { return nil } - return token + let token = UserDefaults.AccountInfo.string(forKey: .userToken) + return Token.init(isValid: token != nil ? true : false , basicToken: token) +// guard let jsonString = keychain[tokenKey]?.data(using: .utf8), +// let token = try? JSONDecoder().decode(Token.self, from: jsonString) else { return nil } +// return token } set { - if let token = newValue, - let data = try? JSONEncoder().encode(token), - let jsonString = String.init(data: data, encoding: .utf8) { - keychain[tokenKey] = jsonString - } else { - keychain[tokenKey] = nil - } +// if let token = newValue, +// let data = try? JSONEncoder().encode(token), +// let jsonString = String.init(data: data, encoding: .utf8) { +// keychain[tokenKey] = jsonString +// } else { +// keychain[tokenKey] = nil +// } + UserDefaults.AccountInfo.set(newValue?.basicToken, forKey: .userToken) tokenChanged.onNext(newValue) loggedIn.accept(hasValidToken) } @@ -61,4 +64,6 @@ class AuthManager { class func tokenValidated() { AuthManager.shared.token?.isValid = true } + + } diff --git a/IndieMusic/IndieMusic/Models/User.swift b/IndieMusic/IndieMusic/Models/User.swift new file mode 100644 index 0000000..79f9e6d --- /dev/null +++ b/IndieMusic/IndieMusic/Models/User.swift @@ -0,0 +1,17 @@ +// +// User.swift +// IndieMusic +// +// Created by WenLei on 2024/1/16. +// + +import Foundation + +struct User: Codable { + let sex: Bool? + let avatar: String? + let nickname: String? + let personality: String? + let id: String? + +} diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeTabBarViewModel.swift b/IndieMusic/IndieMusic/Modules/Home/HomeTabBarViewModel.swift index 58cbaf6..a8eef78 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeTabBarViewModel.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeTabBarViewModel.swift @@ -8,6 +8,7 @@ import Foundation import RxCocoa import RxSwift +import SVProgressHUD class HomeTabBarViewModel: ViewModel, ViewModelType { @@ -30,6 +31,13 @@ class HomeTabBarViewModel: ViewModel, ViewModelType { func transform(input: Input) -> Output { let tabBarItems = Observable.just([HomeTabBarItem.home, HomeTabBarItem.search, HomeTabBarItem.mine]).asDriver(onErrorJustReturn: []) + + + if authorized { + renewalToken() + } + + return Output(tabBarItems: tabBarItems) } @@ -47,4 +55,19 @@ class HomeTabBarViewModel: ViewModel, ViewModelType { return viewModel } } + + + func renewalToken() { + self.provider.autoLogin().subscribe { login in + print("autoLogin: success") + AuthManager.setToken(token: Token.init(isValid: true, basicToken: login.token)) + + } onFailure: { error in + print("autoLogin: error") +// if case HTTPServiceError.errorJudge(let err) = error { +// SVProgressHUD.showText(withStatus: err.message) +// } + + }.disposed(by: rx.disposeBag) + } } diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeView.swift b/IndieMusic/IndieMusic/Modules/Home/HomeView.swift index f1522e7..12d2bdd 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeView.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeView.swift @@ -159,14 +159,24 @@ class HomeViewCell: UITableViewCell { return commentCountButton }() + + + let homeNoLoginView: HomeNoLoginView = { + let homeNoLoginView = HomeNoLoginView.init() +// noLoginBottomView.backgroundColor = .systemBlue + homeNoLoginView.isHidden = true + return homeNoLoginView + }() + + var loginClosures: (()->())? + var homeJournal: HomeJournal? { didSet { titleLabel.text = homeJournal?.title subTitleLabel.text = homeJournal?.subTitle commentLabel.text = homeJournal?.comment - - + } } @@ -178,6 +188,8 @@ class HomeViewCell: UITableViewCell { makeUI() + homeNoLoginView.loginButton.addTarget(self, action: #selector(loginButtonClick), for: .touchUpInside) + titleLabel.text = "标题标题标题标题标题标题" subTitleLabel.text = "子标题子标题子标题子标题子标题子标题子标题子标题" commentLabel.text = "评论评论评论评论评论评论评论" @@ -206,6 +218,7 @@ class HomeViewCell: UITableViewCell { containerView.addSubview(commentLabel) containerView.addSubview(commentCountButton) + contentView.addSubview(homeNoLoginView) containerView.snp.makeConstraints { make in make.left.equalTo(contentView).offset(18) @@ -258,27 +271,23 @@ class HomeViewCell: UITableViewCell { } + homeNoLoginView.snp.makeConstraints { make in + make.top.equalTo(contentView) + make.left.equalTo(contentView) + make.right.equalTo(contentView) + make.bottom.equalTo(contentView) + } + + + } -// override var frame:CGRect{ -// didSet { -// let insetX: CGFloat = 18 -// let insetY: CGFloat = 0.0 -// -// var newFrame = frame -// newFrame.origin.x += insetX -// newFrame.origin.y += insetY -// -// newFrame.size.width -= 2*insetX -// newFrame.size.height -= 2*insetY -// -// -// super.frame = newFrame -// } -// } -// - - + @objc func loginButtonClick() { + if let loginClosures = self.loginClosures { + loginClosures() + } + } + } @@ -446,7 +455,7 @@ class HomeFilterButton: UIControl { -class NoLoginBottomView: UIView { +class HomeNoLoginView: UIView { var loginButton: UIButton = { let loginButton = UIButton.init() loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium) @@ -462,9 +471,9 @@ class NoLoginBottomView: UIView { var gradientLayer: CAGradientLayer = { let gradientLayer = CAGradientLayer.init() - gradientLayer.colors = [UIColor.init(hex: 0xFFFFFF, alpha: 0).cgColor, - UIColor.init(hex: 0xFFFFFF, alpha: 0.8).cgColor] - gradientLayer.locations = [0.0, 0.9] + gradientLayer.colors = [UIColor.init(hex: 0xF2F3F7, alpha: 0.9).cgColor, + UIColor.init(hex: 0xF3F4F8, alpha: 1).cgColor] + gradientLayer.locations = [0.0, 1] gradientLayer.startPoint = CGPoint.init(x: 0 , y: 0) gradientLayer.endPoint = CGPoint.init(x: 0, y: 1) @@ -473,11 +482,11 @@ class NoLoginBottomView: UIView { }() - override init(frame: CGRect) { super.init(frame: frame) makeUI() + } required init?(coder: NSCoder) { @@ -504,4 +513,6 @@ class NoLoginBottomView: UIView { make.size.equalTo(CGSize.init(width: 118, height: 32)) } } + + } diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift b/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift index 9153aee..38cf68d 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift @@ -28,11 +28,7 @@ class HomeViewController: TableViewController { var bannerArray = [String]() - let noLoginBottomView: NoLoginBottomView = { - let noLoginBottomView = NoLoginBottomView.init() -// noLoginBottomView.backgroundColor = .systemBlue - return noLoginBottomView - }() + override func viewDidLoad() { super.viewDidLoad() @@ -44,27 +40,18 @@ class HomeViewController: TableViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) -// let vm = JournalDetailViewModel.init(provider: viewModel!.provider) -// let vc = JournalDetailController.init(viewModel: vm, navigator: navigator) -// -// self.navigationController?.pushViewController(vc, animated: true) if let tabbar = self.tabBarController as? HomeTabBarController { tabbar.showAllBar(true, animated: true) } - guard let viewModel = viewModel as? HomeViewModel else { return } - + self.navigationItem.leftBarButtonItem = nil -// let loginViewModel = LoginViewModel.init(provider: viewModel.provider) -// let login = LoginViewController.init(viewModel: loginViewModel, navigator: navigator) -// self.navigationController?.pushViewController(login, animated: true) } override func makeUI() { super.makeUI() - let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: BaseDimensions.screenWidth, height: 120)) pagerView.frame = headerView.bounds pagerView.backgroundColor = .red @@ -74,10 +61,8 @@ class HomeViewController: TableViewController { headerView.addSubview(pagerView) tableView.tableHeaderView = headerView + tableView.separatorColor = .clear tableView.register(HomeViewCell.self, forCellReuseIdentifier: "HomeViewCell") - - - view.addSubview(noLoginBottomView) } @@ -88,41 +73,40 @@ class HomeViewController: TableViewController { guard let viewModel = viewModel as? HomeViewModel else { return } let input = HomeViewModel.Input.init(viewWillAppear: rx.viewWillAppear, - selection: tableView.rx.itemSelected.asDriver(), loginButtonTrigger: self.noLoginBottomView.loginButton.rx.tap.asDriver()) + selection: tableView.rx.itemSelected.asDriver()) let output = viewModel.transform(input: input) - let dataSource = HomeViewController.dataSource() + let dataSource = HomeViewController.dataSource { cell, item in + + let loginViewModel = LoginViewModel.init(provider: viewModel.provider) + self.navigator.show(segue: .login(viewModel: loginViewModel), sender: self) + } output.items.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag) output.itemSelected.subscribe { [weak self] sectionItem in switch sectionItem { - - default: - - - let journalDetailViewModel = JournalDetailViewModel.init(provider: viewModel.provider) self?.navigator.show(segue: .journalDetail(viewModel: journalDetailViewModel), sender: self) - } - }.disposed(by: rx.disposeBag) + + isFooterLoading.accept((.loadMore, false)) - self.noLoginBottomView.loginButton.rx.tap.subscribe { _ in - - let loginViewModel = LoginViewModel.init(provider: viewModel.provider) - self.navigator.show(segue: .login(viewModel: loginViewModel), sender: self) + loggedIn.subscribe { isloggedIn in + if isloggedIn.element == true { + + } else { + self.tableView.mj_footer?.rx.refreshFooterState.onNext(RxMJRefreshFooterState.noMoreData) + } - }.disposed(by: rx.disposeBag) - - - + self.tableView.reloadData() + }.disposed(by: rx.disposeBag) } @@ -130,14 +114,7 @@ class HomeViewController: TableViewController { super.viewDidLayoutSubviews() - noLoginBottomView.snp.makeConstraints { make in - make.bottom.equalTo(view).offset( -BaseDimensions.tabBarHeight) - make.left.equalTo(view) - make.right.equalTo(view) - make.height.equalTo(100) - - } - + } override func touchesBegan(_ touches: Set, with event: UIEvent?) { @@ -171,7 +148,6 @@ extension HomeViewController: FSPagerViewDataSource { cell.imageView?.kf.setImage(with: URL.init(string: "")) cell.imageView?.backgroundColor = .red - return cell } @@ -180,7 +156,7 @@ extension HomeViewController: FSPagerViewDataSource { extension HomeViewController { - static func dataSource() -> RxTableViewSectionedReloadDataSource { + static func dataSource(_ buttonTapHandler: @escaping (UITableViewCell, HomeSectionItem) -> Void) -> RxTableViewSectionedReloadDataSource { return RxTableViewSectionedReloadDataSource { dataSource, tableView, indexPath, item in @@ -189,9 +165,17 @@ extension HomeViewController { let cell: HomeViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeViewCell", for: indexPath) as! HomeViewCell cell.homeJournal = model - return cell - default: UITableViewCell() + if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 { + cell.homeNoLoginView.isHidden = AuthManager.shared.token?.isValid ?? false + } + + + cell.loginClosures = { + buttonTapHandler(cell, item) + } + + return cell } } } @@ -219,7 +203,7 @@ extension HomeViewController { let navigator = self?.navigator else { return } let filterViewModel = FilterViewModel.init(provider: viewModel.provider) - let filter = FilterViewController.init(viewModel: filterViewModel, navigator: navigator) +// let filter = FilterViewController.init(viewModel: filterViewModel, navigator: navigator) // self.present(filter, animated: true) navigator.show(segue: .filter(viewModel: filterViewModel), sender: self, transition: .navigationPresent(type: .filter)) } diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeViewModel.swift b/IndieMusic/IndieMusic/Modules/Home/HomeViewModel.swift index 6a64b36..d5ad9f9 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeViewModel.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeViewModel.swift @@ -14,9 +14,6 @@ class HomeViewModel: ViewModel, ViewModelType { struct Input { let viewWillAppear: ControlEvent let selection: Driver - let loginButtonTrigger: Driver - - } struct Output { diff --git a/IndieMusic/IndieMusic/Modules/Login/AgreementViewController.swift b/IndieMusic/IndieMusic/Modules/Login/AgreementViewController.swift index 139c568..a8cb495 100644 --- a/IndieMusic/IndieMusic/Modules/Login/AgreementViewController.swift +++ b/IndieMusic/IndieMusic/Modules/Login/AgreementViewController.swift @@ -57,7 +57,6 @@ class AgreementViewController: UIViewController { cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 16) cancelButton.setTitle("取消", for: .normal) cancelButton.setTitleColor(UIColor.tertiaryText(), for: .normal) - cancelButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside) return cancelButton }() @@ -67,7 +66,7 @@ class AgreementViewController: UIViewController { confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 16) confirmButton.setTitle("已阅读并同意", for: .normal) confirmButton.setTitleColor(UIColor.primaryText(), for: .normal) - confirmButton.addTarget(self, action: #selector(confirmButtonClick), for: .touchUpInside) +// confirmButton.addTarget(self, action: #selector(confirmButtonClick), for: .touchUpInside) return confirmButton }() @@ -91,6 +90,9 @@ class AgreementViewController: UIViewController { super.viewDidLoad() makeUI() + + cancelButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside) + } @@ -155,12 +157,12 @@ class AgreementViewController: UIViewController { } @objc func cancelButtonClicked() { - + self.dismiss(animated: true) } - @objc func confirmButtonClick() { - - } +// @objc func confirmButtonClick() { +// +// } } diff --git a/IndieMusic/IndieMusic/Modules/Login/LoginViewController.swift b/IndieMusic/IndieMusic/Modules/Login/LoginViewController.swift index ea87122..dc42cd8 100644 --- a/IndieMusic/IndieMusic/Modules/Login/LoginViewController.swift +++ b/IndieMusic/IndieMusic/Modules/Login/LoginViewController.swift @@ -119,9 +119,9 @@ class LoginViewController: ViewController { }.disposed(by: rx.disposeBag) - self.phoneView.codeButton.rx.tap.subscribe { _ in + self.phoneView.codeButton.rx.tap.subscribe { [weak self] _ in let internationalNumberViewModel = InternationalNumberViewModel.init(number: viewModel.number, provider: viewModel.provider) - self.navigator.show(segue: .internationalNumber(viewModel: internationalNumberViewModel), sender: self, transition: .modal) + self?.navigator.show(segue: .internationalNumber(viewModel: internationalNumberViewModel), sender: self, transition: .modal) }.disposed(by: rx.disposeBag) diff --git a/IndieMusic/IndieMusic/Modules/Login/PhoneCodeController.swift b/IndieMusic/IndieMusic/Modules/Login/PhoneCodeController.swift index 358b8e2..b48793c 100644 --- a/IndieMusic/IndieMusic/Modules/Login/PhoneCodeController.swift +++ b/IndieMusic/IndieMusic/Modules/Login/PhoneCodeController.swift @@ -58,20 +58,25 @@ class PhoneCodeController: ViewController { return codeButton }() + let errorLabel: UILabel = { + let errorLabel = UILabel.init() + errorLabel.font = UIFont.systemFont(ofSize: 17, weight: .medium) + errorLabel.textColor = .primary() + + return errorLabel + }() + + + let boxInputText = PublishSubject<(String?, Bool)>.init() override func viewDidLoad() { super.viewDidLoad() - boxInputView.textDidChangeblock = { (text, isFinish) in - if isFinish { - - - - } - - self.boxInputText.onNext((text, isFinish)) + boxInputView.textDidChangeblock = { [weak self] (text, isFinish) in + + self?.boxInputText.onNext((text, isFinish)) } } @@ -84,6 +89,7 @@ class PhoneCodeController: ViewController { view.addSubview(tipsLabel) view.addSubview(boxInputView) view.addSubview(codeButton) + view.addSubview(errorLabel) } @@ -111,7 +117,7 @@ class PhoneCodeController: ViewController { output.tipsText.drive(self.tipsLabel.rx.text).disposed(by: rx.disposeBag) - output.toAlertView.subscribe { _ in + output.toAlertView.subscribe { [weak self] _ in let agree = AlertViewController.init() agree.titleLabel.text = "阅读并同意以下协议" agree.cancelButton.setTitle("取消", for: .normal) @@ -125,22 +131,17 @@ class PhoneCodeController: ViewController { (" 并授权获取本机号码", [NSAttributedString.Key.foregroundColor: UIColor.secondaryText(), NSAttributedString.Key.paragraphStyle: paragraphStyle]) ]) + agree.cancelClosures = {[weak self] in + AuthManager.removeToken() + } agree.confirmClosures = {[weak self] in self?.dismiss(animated: true) self?.navigator.pop(sender: self, toRoot: true) - } - - self.present(agree, animated: true) - -// self.navigator.pop(sender: self, toRoot: true) - - + self?.present(agree, animated: true) }.disposed(by: rx.disposeBag) - - } @@ -169,6 +170,12 @@ class PhoneCodeController: ViewController { make.left.equalTo(view).offset(24) make.top.equalTo(boxInputView.snp.bottom).offset(24) } + + errorLabel.snp.makeConstraints { make in + make.right.equalTo(view).offset(-24) + make.centerY.equalTo(view) + } + } } diff --git a/IndieMusic/IndieMusic/Modules/Login/PhoneCodeViewModel.swift b/IndieMusic/IndieMusic/Modules/Login/PhoneCodeViewModel.swift index 7bdfaf9..6c9d14d 100644 --- a/IndieMusic/IndieMusic/Modules/Login/PhoneCodeViewModel.swift +++ b/IndieMusic/IndieMusic/Modules/Login/PhoneCodeViewModel.swift @@ -22,15 +22,13 @@ class PhoneCodeViewModel: ViewModel, ViewModelType { let isButtonEnabled: Driver let tipsText: Driver let toAlertView: PublishRelay + let errorMessage: Driver } private var remainingSeconds = 59 private var tipsText = "验证码已发送至 " - - -// let mobileCheckCode = BehaviorRelay.init(value: "") let toAlertView = PublishRelay.init() var phone: String @@ -43,7 +41,8 @@ class PhoneCodeViewModel: ViewModel, ViewModelType { func transform(input: Input) -> Output { - + let errorMessages = PublishSubject() + let startImmediately = Observable.just(Void()) let startCountdownAfterRequest = input.codeButtonTrigger @@ -58,26 +57,8 @@ class PhoneCodeViewModel: ViewModel, ViewModelType { .do(onNext: { [weak self] _ in self?.remainingSeconds = 59 }) - -// // 倒计时逻辑 -// let timer = startCountdown -// .flatMapLatest { _ in -// Observable.interval(.seconds(1), scheduler -// - - -// -// let startCountdown = Observable.merge(startImmediately, input.codeButtonTrigger) -// .do(onNext: { [weak self] _ in -// self?.remainingSeconds = 59 -// }).flatMapLatest { [weak self] _ -> Observable in -// guard let self = self else { return Observable.just(false) } -// return self.requestSMSData() -// } -// -// - + // 倒计时逻辑 let timer = startCountdown .flatMapLatest { _ in Observable.interval(.seconds(1), scheduler: MainScheduler.instance) @@ -105,19 +86,18 @@ class PhoneCodeViewModel: ViewModel, ViewModelType { - input.boxInputText.subscribe(onNext: { (text, isFinish) in + input.boxInputText.subscribe(onNext: { [weak self] (text, isFinish) in - guard let text = text, isFinish == true else { return } + guard let self = self, let text = text, isFinish == true else { return } print("text: \(text) isFinish: \(isFinish) ") - self.requestLoginData(mobileCheckCode: text).subscribe(onNext: { login in - - UserDefaults.AccountInfo.set(login.token, forKey: .userToken) - UserDefaults.AccountInfo.set(login.id, forKey: .userID) - UserDefaults.AccountInfo.set(login.nickname, forKey: .userName) - - self.toAlertView.accept(()) + + self.requestLoginData(mobileCheckCode: text).subscribe(onNext: { [weak self] login in + AuthManager.setToken(token: Token.init(isValid: true, basicToken: login.token)) +// AuthManager.tokenValidated() + + self?.toAlertView.accept(()) }, onError: { error in if case HTTPServiceError.errorJudge(let err) = error { SVProgressHUD.showText(withStatus: err.message) @@ -131,7 +111,8 @@ class PhoneCodeViewModel: ViewModel, ViewModelType { return Output(countdownText: countdownText, isButtonEnabled: isButtonEnabled, tipsText: tipsText, - toAlertView: toAlertView) + toAlertView: toAlertView, + errorMessage: errorMessages.asDriver(onErrorJustReturn: "")) } diff --git a/IndieMusic/IndieMusic/Modules/Mine/MineViewModel.swift b/IndieMusic/IndieMusic/Modules/Mine/MineViewModel.swift index b9551f8..7c992fe 100644 --- a/IndieMusic/IndieMusic/Modules/Mine/MineViewModel.swift +++ b/IndieMusic/IndieMusic/Modules/Mine/MineViewModel.swift @@ -23,7 +23,6 @@ class MineViewModel: ViewModel, ViewModelType { let selection: Driver let itemSelected: PublishSubject - } let itemSelected = PublishSubject() @@ -41,6 +40,15 @@ class MineViewModel: ViewModel, ViewModelType { items.accept([MineSection.init(items: [single, journal, download])]) + requestUserInfoData().subscribe { user in + print(user) + + } onError: { error in + + }.disposed(by: rx.disposeBag) + + + input.selection.drive { indexPath in guard let sectionItem = self.items.value.first?.items[indexPath.row] else { return } @@ -54,4 +62,11 @@ class MineViewModel: ViewModel, ViewModelType { itemSelected: itemSelected) } + + func requestUserInfoData() -> Observable { + return provider.getUserInfo() + .trackActivity(loading) + .trackError(error) + } + } diff --git a/IndieMusic/IndieMusic/Modules/Player/AudioTrackListViewController.swift b/IndieMusic/IndieMusic/Modules/Player/AudioTrackListViewController.swift index 125630d..834007c 100644 --- a/IndieMusic/IndieMusic/Modules/Player/AudioTrackListViewController.swift +++ b/IndieMusic/IndieMusic/Modules/Player/AudioTrackListViewController.swift @@ -91,9 +91,9 @@ extension AudioTrackListViewController { if indexPath.row == 0 || indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 { cell.lineView.isHidden = true - } else { - cell.lineView.isHidden = false - } + } else { + cell.lineView.isHidden = false + } return cell } diff --git a/IndieMusic/IndieMusic/Networking/Api.swift b/IndieMusic/IndieMusic/Networking/Api.swift index 22ebcd4..62f9b42 100644 --- a/IndieMusic/IndieMusic/Networking/Api.swift +++ b/IndieMusic/IndieMusic/Networking/Api.swift @@ -19,5 +19,9 @@ protocol IndieMusicAPI { func sendsms(mobile: String) -> Single /// 登录注册 func loginOrRegister(mobile: String, mobileCheckCode: String) -> Single + /// Token续期 + func autoLogin() -> Single + /// 个人信息 + func getUserInfo() -> Single } diff --git a/IndieMusic/IndieMusic/Networking/Rest/APIConfig.swift b/IndieMusic/IndieMusic/Networking/Rest/APIConfig.swift index caed2f9..79c831f 100644 --- a/IndieMusic/IndieMusic/Networking/Rest/APIConfig.swift +++ b/IndieMusic/IndieMusic/Networking/Rest/APIConfig.swift @@ -18,8 +18,9 @@ protocol ProductAPIType { enum APIConfig { case wechatAccessToken([String: Any]) case login(String, String, [String: Any]) - case sendsms(mobil: String, [String: Any]) + case autoLogin([String: Any]) + case getUserInfo([String: Any]) } extension APIConfig: TargetType { @@ -33,6 +34,10 @@ extension APIConfig: TargetType { case .sendsms(let mobil, _): return "user/user/sendsms/\(mobil)" + case .autoLogin: + return "user/user/autoLogin" + case .getUserInfo: + return "user/my/getUserInfo" } } @@ -40,7 +45,7 @@ extension APIConfig: TargetType { switch self { case .wechatAccessToken: return .get - case .sendsms, .login: + case .sendsms, .login, .autoLogin, .getUserInfo: return .post } } @@ -52,7 +57,7 @@ extension APIConfig: TargetType { case .login: return JSONEncoding.default - case .sendsms: + case .sendsms, .autoLogin, .getUserInfo: return JSONEncoding.default } } @@ -64,7 +69,7 @@ extension APIConfig: TargetType { case .wechatAccessToken: return .requestPlain - case .login(_, _, let dic), .sendsms(_, let dic): + case .login(_, _, let dic), .sendsms(_, let dic), .autoLogin(let dic), .getUserInfo(let dic): parameters = dic return .requestParameters(parameters: parameters, encoding: parameterEncoding) @@ -78,7 +83,7 @@ extension APIConfig: TargetType { switch self { case .wechatAccessToken: return URL(string: Configs.App.wechatURL)! - case .login, .sendsms: + case .login, .sendsms, .autoLogin, .getUserInfo: return URL(string: Configs.App.environmentType.baseUrl)! } @@ -86,9 +91,8 @@ extension APIConfig: TargetType { var headers : [String : String]? { switch self { - case .wechatAccessToken: - return nil - + case .autoLogin, .getUserInfo: + return ["token": AuthManager.shared.token?.basicToken ?? ""] default: return nil } diff --git a/IndieMusic/IndieMusic/Networking/Rest/RestApi.swift b/IndieMusic/IndieMusic/Networking/Rest/RestApi.swift index c4e0771..32069b4 100644 --- a/IndieMusic/IndieMusic/Networking/Rest/RestApi.swift +++ b/IndieMusic/IndieMusic/Networking/Rest/RestApi.swift @@ -72,18 +72,22 @@ extension RestApi { return requestObject(.wechatAccessToken(dic), with: "data", type: EmptyModel.self) } - - - func loginOrRegister(mobile: String, mobileCheckCode: String) -> Single { return requestObject(.login(mobile, mobileCheckCode, ["": ""]), with: "data", type: Login.self) } + func autoLogin() -> Single { + return requestObject(.autoLogin(["": ""]), with: "data", type: Login.self) + } func sendsms(mobile: String) -> Single { let dic = ["": ""] return requestObject(.sendsms(mobil: mobile, dic), with: "data", type: EmptyModel.self) - + } + + func getUserInfo() -> Single { + let dic = ["": ""] + return requestObject(.getUserInfo(dic), with: "data", type: User.self) }