SettingsViewController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // SettingsViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/3/3.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import NVActivityIndicatorView
  10. import ActionSheetPicker_3_0
  11. import WHToast
  12. /// 系统设置视图容器控制类
  13. class SettingsViewController: UITableViewController {
  14. // MARK: - 属性列表
  15. /// CellIdentifier
  16. let CellIdentifier = "Settings_Cell"
  17. /// titles
  18. let titles = ["极小", "小", "中", "大", "极大"]
  19. /// values
  20. let values: [CGFloat] = [0.8, 0.9, 1.0, 1.1, 1.2]
  21. /// 密码有效期
  22. let pwdDowns: [Int] = [1, 2, 4, 8, 12, 24]
  23. // MARK: - 生命周期相关
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. // Uncomment the following line to preserve selection between presentations
  27. // self.clearsSelectionOnViewWillAppear = false
  28. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  29. // self.navigationItem.rightBarButtonItem = self.editButtonItem
  30. self.tableView.register(SettingsCell.self, forCellReuseIdentifier: CellIdentifier)
  31. }
  32. override func viewDidAppear(_ animated: Bool) {
  33. super.viewDidAppear(animated)
  34. /// 显示
  35. let select = values.firstIndex(of: UserDefaultsUtils.fontSize()) ?? 3
  36. let cell = self.tableView.cellForRow(at: IndexPath(row: 0, section: 1)) as? SettingsCell
  37. cell?.detial.text = titles[select]
  38. /// 密码有效显示
  39. let pwdCell = self.tableView.cellForRow(at: IndexPath(row: 2, section: 0)) as? SettingsCell
  40. pwdCell?.detial.text = "\(UserDefaultsUtils.pwdDown())小时有效"
  41. }
  42. // MARK: - 交互相关
  43. /// onButtonPressed
  44. /// - Parameter sender: sender
  45. @IBAction func onButtonPressed(_ sender: UIButton) {
  46. /// 执行登出操作
  47. requestLoginOut()
  48. }
  49. // MARK: - 系统登出
  50. /// 系统登出的方法
  51. private func requestLoginOut() {
  52. /// 异常情况
  53. guard let loginRsp = MTP2BusinessCore.shared.accountManager?.loginRsp,
  54. let accountManager = MTP2BusinessCore.shared.accountManager else { return }
  55. /// 系统提示
  56. showAlertController(title: "提示", message: "确定要退出登录吗?", cancelTitle: "取消", sureTitle: "确定") {} sureBlock: {
  57. /// startAnimating
  58. NVActivityIndicatorPresenter.sharedInstance.startAnimating(self.animData("退出登录中..."))
  59. guard let ip = MTP2BusinessCore.shared.address?.tradeHost,
  60. let port = MTP2BusinessCore.shared.address?.tradePort,
  61. let UIntPort = UInt32(port) else {
  62. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  63. return
  64. }
  65. /// 目前登出暂时不用等待服务端回调
  66. accountManager.logout(loginID: loginRsp.loginID, token: loginRsp.token, loginIp: ip, loginPort: UIntPort) { (_, _) in }
  67. /// 回到登录页面
  68. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  69. /// 重置管理类
  70. MTP2BusinessCore.shared.resetCore {
  71. /// stopAnimating
  72. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  73. /// 显示登录界面
  74. (UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = self.viewController("Main", "LoginNav")
  75. }
  76. }
  77. }
  78. }
  79. /// app检测版本更新
  80. fileprivate func requestAppVersionUpdate() {
  81. /// 地址信息为空
  82. guard let updateUrl = ConfigUtils.getServiceUrl(resourceType: .UpdateUrl),
  83. updateUrl != "" else { return }
  84. /// startAnimating
  85. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animData("版本获取中..."))
  86. /// 请求更新
  87. HttpUtils.callInterface(url: updateUrl, type: .GET, params: nil) { (isSuccess, response) in
  88. DispatchQueue.main.async {
  89. /// stopAnimating
  90. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  91. /// 数据结息
  92. if isSuccess, let dic = response as? NSDictionary, let arrays = (dic["results"] as? [NSDictionary]), arrays.count != 0 {
  93. if let version = (arrays[0])["version"] as? String, let url = (arrays[0])["trackViewUrl"] as? String {
  94. let notes = (arrays[0])["releaseNotes"] as? String
  95. /// 商店版本比本地版本大
  96. if version > app_Version {
  97. /// 提示更新
  98. AppUpdateAlert.showUpdateAlert(version: version, description: notes ?? "", url)
  99. } else {
  100. WHToast.showMessage("已经是最新版", duration: ToastTimer, finishHandler: {})
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. // MARK: - Table view data source
  108. override func numberOfSections(in tableView: UITableView) -> Int {
  109. // #warning Incomplete implementation, return the number of sections
  110. return 2
  111. }
  112. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  113. // #warning Incomplete implementation, return the number of rows
  114. return section == 0 ? 3 : MTP2BusinessCore.shared.address?.oem == "qhj" ? 2 : 3
  115. }
  116. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  117. switch (indexPath.section, indexPath.row) {
  118. case (0, 1): /// 重置登录密码
  119. /// 进入到重置密码
  120. let resetPwd = UIStoryboard(name: "ResetPwd", bundle: nil).instantiateViewController(withIdentifier: "ResetPwdFirstStepID")
  121. self.navigationController?.pushViewController(resetPwd, animated: true)
  122. case (0, 2): /// 密码有效期限
  123. ActionSheetStringPicker.show(withTitle: "设置登录密码有效时", rows: pwdDowns, initialSelection: 0, doneBlock: { picker, value, index in
  124. /// 小时有效
  125. (self.tableView.cellForRow(at: indexPath) as? SettingsCell)?.detial.text = "\(index as! Int)小时有效"
  126. /// 记录
  127. UserDefaultsUtils.setPwdDown(index as! Int)
  128. },
  129. cancel: { picker in
  130. return
  131. },
  132. origin: self.view)
  133. case (1, 0): /// 字体设置
  134. let select = values.firstIndex(of: UserDefaultsUtils.fontSize()) ?? 3
  135. (self.tableView.cellForRow(at: indexPath) as? SettingsCell)?.detial.text = titles[select]
  136. ActionSheetStringPicker.show(withTitle: "设置字体大小", rows: titles, initialSelection: select, doneBlock: { picker, value, index in
  137. (self.tableView.cellForRow(at: indexPath) as? SettingsCell)?.detial.text = index as? String
  138. UserDefaultsUtils.setFontSize(CGFloat(self.values[value]))
  139. /// startAnimating
  140. NVActivityIndicatorPresenter.sharedInstance.startAnimating(self.animData("提交中..."))
  141. /// asyncAfter
  142. DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
  143. /// stop loding.....
  144. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  145. /// 重置
  146. (UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainTabBar")
  147. let home = (((UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController as? MainTabBarController)?.selectedViewController as? BaseNavigationController)?.viewControllers[0] as? HomeViewController
  148. home?.push("Settings", "Settings")
  149. }
  150. },
  151. cancel: { picker in
  152. return
  153. },
  154. origin: self.view)
  155. case (1, 1): /// 版本更新
  156. self.requestAppVersionUpdate()
  157. (self.tableView.cellForRow(at: indexPath) as? SettingsCell)?.detial.text = "V"+app_Version
  158. case (1, 2): /// 用户协议
  159. self.navigationController?.pushViewController(viewController("Main", "Privacy"), animated: true)
  160. default:
  161. break
  162. }
  163. }
  164. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  165. return section == 0 ? "安全相关" : "其他"
  166. }
  167. }
  168. class SettingsCell: UITableViewCell {
  169. /// 详细
  170. @IBOutlet weak var detial: UILabel!
  171. /// 标题
  172. @IBOutlet weak var title: UILabel!
  173. /// 标题
  174. @IBOutlet weak var ig: UIImageView!
  175. }