QSQuickLogonHandle.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // QSQuickLogonHandle.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/7/18.
  6. //
  7. import Foundation
  8. import NTESQuickPass
  9. import UIKit
  10. class QSQuickLogonHandle: NSObject {
  11. //单例
  12. static let share = QSQuickLogonHandle()
  13. static var isGetPhoneNumSuccess = false
  14. var currentVC: UIViewController?
  15. // 易盾 token
  16. var token: String?
  17. // 运营商授权码
  18. var accessToken: String?
  19. var loginSuccess: (() -> ())?
  20. func starOneQuickLogin(loginSuccess: @escaping () -> ()) {
  21. self.loginSuccess = loginSuccess
  22. QSLLoading.show()
  23. NTESQuickLoginManager.sharedInstance().register(withBusinessID:QSLConfig.quick_logon_key_id)
  24. NTESQuickLoginManager.sharedInstance().getPhoneNumberCompletion { [weak self] resultDic in
  25. guard let self = self else { return }
  26. if let isSuccess = resultDic["success"] as? Bool {
  27. QSQuickLogonHandle.isGetPhoneNumSuccess = isSuccess
  28. if isSuccess && NTESQuickLoginManager.sharedInstance().shouldQuickLogin() {
  29. if let token = resultDic["token"] as? String {
  30. QSQuickLogonHandle.share.token = token
  31. }
  32. NTESQuickLoginManager.sharedInstance().delegate = self
  33. self.layoutQuickLoginUI()
  34. self.obtionAuthorizeLogin()
  35. } else {
  36. DispatchQueue.main.async {
  37. QSLLoading.hide()
  38. ///去登录
  39. if !QSLBaseManager.shared.isLogin() {
  40. QSEventHandle.eventPush( eventName: QSLGravityConst.mine_login)
  41. QSLJumpManager.shared.pushToLogin(type: .mine)
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. func layoutQuickLoginUI() {
  49. let model = NTESQuickLoginModel()
  50. // print("curredfdjf---\(String(describing: currentVC))");
  51. model.currentVC = currentVC
  52. model.presentDirectionType = .push
  53. model.authWindowPop = .fullScreen
  54. model.backgroundColor = .white
  55. // 导航栏
  56. model.navBarHidden = false
  57. model.navBgColor = .white
  58. model.navText = ""
  59. model.navTextFont = UIFont.systemFont(ofSize: 14)
  60. model.navTextColor = .white
  61. model.navTextHidden = false
  62. if let image = UIImage(named: "public_back_btn") {
  63. model.navReturnImg = image
  64. }
  65. model.navReturnImgLeftMargin = 15
  66. model.navReturnImgBottomMargin = 0
  67. model.navReturnImgWidth = 44
  68. model.navReturnImgHeight = 44
  69. // logo
  70. if let image = UIImage(named: "mine_about_logo") {
  71. model.logoImg = image
  72. }
  73. model.logoWidth = 80
  74. model.logoHeight = 80
  75. model.logoOffsetTopY = 50
  76. model.logoOffsetX = 0
  77. model.logoHidden = false
  78. // 手机号
  79. model.numberColor = QSLColor.Color_202020
  80. model.numberFont = UIFont.systemFont(ofSize: 18)
  81. model.numberOffsetTopY = 160
  82. model.numberOffsetX = 0
  83. model.numberHeight = 27
  84. // 品牌标语
  85. model.brandColor = .hexStringColor(hexString: "#8F8F8F")
  86. model.brandFont = UIFont.systemFont(ofSize: 12)
  87. model.brandWidth = 200
  88. model.brandHeight = 16
  89. model.brandOffsetX = 0
  90. model.brandOffsetTopY = 200
  91. model.brandHidden = false
  92. // 登录按钮
  93. model.logBtnText = "本机号码一键登录"
  94. model.logBtnTextFont = UIFont.systemFont(ofSize: 18)
  95. model.logBtnTextColor = UIColor.white
  96. model.logBtnOffsetTopY = 260
  97. model.logBtnRadius = 8
  98. model.logBtnUsableBGColor = QSLColor.themeMainColor
  99. model.logBtnOriginLeft = 24
  100. model.logBtnOriginRight = 24
  101. model.logBtnHeight = 56
  102. // 协议
  103. if let image = UIImage(named: "login_agree_n") {
  104. model.uncheckedImg = image
  105. }
  106. if let image = UIImage(named: "login_agree_s") {
  107. model.checkedImg = image
  108. }
  109. model.uncheckedImg = UIImage(named: "public_select_btn_false")?.withRenderingMode(.alwaysOriginal) ?? UIImage(named: "")!
  110. model.checkedImg = UIImage(named: "public_select_btn_true")?.withRenderingMode(.alwaysOriginal) ?? UIImage(named: "")!
  111. model.checkboxWH = 22
  112. model.privacyState = false
  113. model.checkBoxAlignment = .center
  114. model.checkedSelected = false
  115. model.checkBoxMargin = 5
  116. // model.appPrivacyOriginLeftMargin = 32
  117. // model.appPrivacyOriginRightMargin = 12
  118. model.appPrivacyOriginBottomMargin = 40
  119. if let image = UIImage(named: "public_back_btn") {
  120. model.privacyNavReturnImg = image
  121. }
  122. // model.privacyNavReturnImg = UIImage(named: "navigation_back") ?? UIImage(named: "")
  123. model.appPrivacyText = "已阅读并同意《隐私权政策》、《用户协议》"
  124. model.appFPrivacyText = "《隐私权政策》"
  125. model.appFPrivacyURL = QSLConfig.AppPrivacyAgreementLink
  126. model.appSPrivacyText = "《用户协议》"
  127. model.appSPrivacyURL = QSLConfig.AppServiceAgreementLink
  128. model.shouldHiddenPrivacyMarks = false
  129. model.privacyColor = .hexStringColor(hexString: "#404040")//.hexStringColor(hexString: "#2F79FF")
  130. model.privacyFont = UIFont.systemFont(ofSize: 12)
  131. model.protocolColor = .hexStringColor(hexString: "#2F79FF")
  132. model.appPrivacyLineSpacing = 1
  133. model.appPrivacyWordSpacing = 0
  134. model.progressColor = .hexStringColor(hexString: "#D9D9D9")
  135. model.toastBackgroundColor = .white//.hexStringColor(hexString: "#171717")
  136. model.toastTitleText = "隐私政策及权限说明"
  137. // model.toastTitleFontSize = 14
  138. model.toastTitleTextColor = QSLColor.Color_202020//.white
  139. //同意按钮
  140. model.toastConfirmText = "同意并继续"
  141. model.toastConfirmBorderColor = .clear
  142. model.toastConfirmTextColor = .white
  143. model.toastConfirmBackgroundColor = .hexStringColor(hexString: "#15CBA1")
  144. model.toastConfirmCornerRadius = 18
  145. //不同意按钮
  146. model.toastCancelText = "不同意"
  147. model.toastCancelBorderColor = .clear
  148. model.toastCancelBackgroundColor = .hexStringColor(hexString: "#F8F8F8")
  149. model.toastCancelTextColor = .hexStringColor(hexString: "#A7A7A7")
  150. model.toastEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
  151. model.toastContentTextColor = .hexStringColor(hexString: "#404040")
  152. model.toastContentFontWeight = .regular
  153. model.toastContentPrefixText = "需同意"
  154. model.toastContentSuffixText = "才能继续享受我们的服务,您的信任对我们非常重要!如您仍不同意本隐私政策与服务条例,很遗憾我们难以为您继续服务~"
  155. model.toastProtocolTextColor = .hexStringColor(hexString: "#6B9CF2")
  156. // 添加自定义按钮 “验证码登录”
  157. model.customViewBlock = { customView in
  158. let codeLoginBtn = UIButton(type: .system)
  159. codeLoginBtn.setTitle("验证码登录", for: .normal)
  160. codeLoginBtn.setTitleColor(QSLColor.Color_202020, for: .normal)
  161. codeLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  162. codeLoginBtn.backgroundColor = .clear
  163. codeLoginBtn.layer.cornerRadius = 20
  164. codeLoginBtn.addTarget(self, action: #selector(self.codeLoginAction), for: .touchUpInside)
  165. customView?.addSubview(codeLoginBtn)
  166. codeLoginBtn.translatesAutoresizingMaskIntoConstraints = false
  167. NSLayoutConstraint.activate([
  168. codeLoginBtn.topAnchor.constraint(equalTo: customView!.topAnchor, constant: 340),
  169. codeLoginBtn.leadingAnchor.constraint(equalTo: customView!.leadingAnchor, constant: 80),
  170. codeLoginBtn.trailingAnchor.constraint(equalTo: customView!.trailingAnchor, constant: -80),
  171. codeLoginBtn.heightAnchor.constraint(equalToConstant: 30)
  172. ])
  173. }
  174. model.backActionBlock = { backType in
  175. if backType == 2 {
  176. self.currentVC?.navigationController?.popViewController(animated: true)
  177. }
  178. }
  179. model.loginActionBlock = { isCheck in
  180. if isCheck {
  181. QSLLoading.show()
  182. }
  183. }
  184. NTESQuickLoginManager.sharedInstance().setupModel(model)
  185. }
  186. func obtionAuthorizeLogin() {
  187. NTESQuickLoginManager.sharedInstance().cucmctAuthorizeLoginCompletion({ resultDic in
  188. if let isSuccess = resultDic["success"] as? Bool {
  189. if isSuccess {
  190. if let accessToken = resultDic["accessToken"] as? String {
  191. self.accessToken = accessToken
  192. }
  193. Task {
  194. await self.requestOneClickLogin()
  195. }
  196. } else {
  197. QSLLoading.hide()
  198. Task { @MainActor in
  199. self.showMyToast(showText: "一键登录失败,请使用验证码登录")
  200. }
  201. }
  202. }
  203. })
  204. }
  205. func requestOneClickLogin() async {
  206. guard let token = self.token else {
  207. return
  208. }
  209. guard let accessToken = self.accessToken else {
  210. return
  211. }
  212. QSLLoading.show()
  213. QSLNetwork().request(.loginOneclick(dict:["token": token, "accessToken": accessToken]), success: { response in
  214. QSLLoading.hide()
  215. let authToken = response.toJSON(modelKey: "data>authToken").stringValue
  216. let phone = response.toJSON(modelKey: "data>phone").stringValue
  217. QSLBaseManager.shared.loginUpdateUser(authToken: authToken, phone: phone)
  218. // 使用时必须异步调用
  219. Task { @MainActor in
  220. self.showMyToast(showText: "登录成功")
  221. }
  222. QSEventHandle.eventPush( eventName: QSLGravityConst.login_success)
  223. // 发送通知
  224. NotificationCenter.default.post(name: QSLNotification.QSLLogin, object: nil)
  225. // 发送通知
  226. NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
  227. // 返回
  228. Task { @MainActor in
  229. self.popCurrentViewController()
  230. }
  231. }, fail: { code, error in
  232. if code == 1005 {
  233. QSEventHandle.gravityPush(eventName: QSLGravityConst.login_fail, eventProps: ["id": 1002])
  234. } else {
  235. QSEventHandle.gravityPush(eventName: QSLGravityConst.login_fail, eventProps: ["id": 1003])
  236. }
  237. QSLLoading.hide()
  238. //self.currentVC?.view.toast(text: error)
  239. Task { @MainActor in
  240. self.showMyToast(showText: error)
  241. }
  242. })
  243. }
  244. @objc @MainActor func popCurrentViewController() {
  245. self.currentVC?.navigationController?.popViewController(animated: true)
  246. }
  247. @objc @MainActor func showMyToast(showText: String) {
  248. UIApplication.keyWindow?.toast(text: showText)
  249. }
  250. // MARK: - 你需要实现的按钮点击方法
  251. @objc func codeLoginAction() {
  252. // 实现你的验证码登录逻辑
  253. print("验证码登录按钮点击")
  254. ///去登录
  255. if !QSLBaseManager.shared.isLogin() {
  256. QSEventHandle.eventPush( eventName: QSLGravityConst.mine_login)
  257. let longinvc = QSLLoginViewController()
  258. longinvc.type = .onQucikLogin
  259. self.currentVC?.pushVC(vc: longinvc)
  260. }
  261. }
  262. }
  263. extension QSQuickLogonHandle: NTESQuickLoginManagerDelegate {
  264. func authViewDidAppear() {
  265. }
  266. func authViewDealloc() {
  267. }
  268. func authViewDidLoad() {
  269. }
  270. func authViewWillAppear() {
  271. QSLLoading.hide()
  272. }
  273. func authViewDidDisappear() {
  274. }
  275. func authViewWillDisappear() {
  276. }
  277. }