QSLTools.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // QSLTools.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/10/28.
  6. //
  7. import UIKit
  8. class QSLTools: NSObject {
  9. static let shared = QSLTools()
  10. var isOpen = false
  11. var urlScheme = ""
  12. }
  13. extension QSLTools {
  14. /// 获取设备令牌
  15. func getDeviceToken() -> String {
  16. if let token = UserDefaults.standard.string(forKey: "deviceToken") {
  17. return token
  18. } else {
  19. return ""
  20. }
  21. }
  22. /// 请求推送权限
  23. func requestNotificationPermission() {
  24. UNUserNotificationCenter.current().requestAuthorization(
  25. options: [.alert, .sound, .badge]
  26. ) { granted, error in
  27. DispatchQueue.main.async {
  28. if granted {
  29. DispatchQueue.main.async {
  30. UIApplication.shared.registerForRemoteNotifications()
  31. }
  32. } else if let error = error {
  33. print("Error: \(error.localizedDescription)")
  34. }
  35. }
  36. }
  37. }
  38. func dealwithScheme(_ scheme: String) {
  39. // 将字符串转换为 URL 对象
  40. guard let url = URL(string: scheme) else {
  41. print("无效的 URL scheme")
  42. return
  43. }
  44. // 检查 scheme 是否为 location
  45. guard url.scheme == "location" else {
  46. print("不支持的 scheme 类型")
  47. return
  48. }
  49. if #available(iOS 16.0, *) {
  50. let host = url.host()
  51. let pathComponents = url.lastPathComponent
  52. let query = url.query()
  53. jumpToPath(path: host ?? "", pathName: pathComponents, param: query ?? "")
  54. } else {
  55. let currentPath = scheme.replacingOccurrences(of: "location://", with: "")
  56. let pathComponents = url.lastPathComponent
  57. let queryList = currentPath.components(separatedBy: "?")
  58. if(queryList.count > 1){
  59. let host = currentPath.replacingOccurrences(of: pathComponents, with: "").replacingOccurrences(of: "/", with: "")
  60. let query = currentPath.replacingOccurrences(of: host, with: "")
  61. jumpToPath(path: host, pathName: pathComponents.replacingOccurrences(of: "/", with: ""), param: query)
  62. }else{
  63. let host = currentPath.replacingOccurrences(of: pathComponents, with: "").replacingOccurrences(of: "/", with: "")
  64. jumpToPath(path: host, pathName: pathComponents.replacingOccurrences(of: "/", with: ""), param: "")
  65. }
  66. }
  67. }
  68. func jumpToPath(path:String, pathName:String, param:String = ""){
  69. if path == "dialog" {
  70. let dialogType = pathName
  71. switch dialogType {
  72. case "member_trial":
  73. QSLJumpManager.shared.pushToVipTrail(type: .notiPush)
  74. break;
  75. default:
  76. print("未知的对话框类型: \(dialogType)")
  77. }
  78. }else if path == "page" {
  79. if(pathName == "QSLVipController"){
  80. QSLJumpManager.shared.pushToVip(type: .shortcut)
  81. return
  82. }
  83. let controllerName = pathName
  84. navigateToViewController(controllerName)
  85. }else if path == "shortcut" {
  86. if(pathName == "com.manbu.shouji.member_or_trial"){
  87. checkMemeberOrTrial()
  88. return
  89. }
  90. }else{
  91. print("未知的路径类型")
  92. }
  93. }
  94. func checkMemeberOrTrial(){
  95. QSLNetwork().request(.wakeUpPressCheck(dict: [String: Any]())) { response in
  96. let model: QSLWakeupModel = response.mapObject(QSLWakeupModel.self, modelKey: "data")
  97. var scheme = "location://page/QSLVipController"
  98. if(model.functionType == "trial"){
  99. scheme = "location://page/QSLVipTrialVC"
  100. }
  101. if(QSLTools.shared.isOpen){
  102. //处理urlScheme
  103. QSLTools.shared.urlScheme = ""
  104. QSLTools.shared.dealwithScheme(scheme)
  105. return
  106. }
  107. QSLTools.shared.urlScheme = scheme
  108. } fail: { code, msg in
  109. }
  110. }
  111. func navigateToViewController(_ controllerName: String) {
  112. // 获取命名空间(Swift 类名通常是 "YourAppName.ViewController")
  113. let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String ?? ""
  114. let fullClassName = "\(namespace).\(controllerName)"
  115. // 通过类名创建控制器实例
  116. guard let controllerClass = NSClassFromString(fullClassName) as? UIViewController.Type else {
  117. print("找不到控制器类: \(controllerName)")
  118. return
  119. }
  120. let viewController = controllerClass.init()
  121. self.rootViewController()?.pushVC(vc: viewController)
  122. }
  123. func requestShortcutItem(){
  124. //判断登录过没有
  125. if QSLBaseManager.shared.isLogin() {
  126. var isAttr = false
  127. if((UserDefaults.standard.value(forKey: "QSAttribution")) != nil){
  128. if let attr = UserDefaults.standard.value(forKey: "QSAttribution") as? Bool {
  129. isAttr = attr
  130. }
  131. }
  132. QSLNetwork().request(.wakeUpPressInfo(dict: ["attribution":isAttr])) { response in
  133. let model: QSLWakeupModel = response.mapObject(QSLWakeupModel.self, modelKey: "data")
  134. var iconName = "mine_func_gift"
  135. if(model.functionType == "feedback"){
  136. iconName = "mine_func_advice"
  137. }
  138. self.setupShortcutItem(typeName: model.functionType, title: model.title, subTitle: model.subTitle, icon: iconName)
  139. } fail: { code, msg in
  140. }
  141. }
  142. }
  143. func setupShortcutItem(typeName:String, title: String, subTitle:String, icon:String ){
  144. let shortcutItem = UIApplicationShortcutItem(
  145. type: "com.manbu.shouji." + typeName,
  146. localizedTitle: title,
  147. localizedSubtitle: subTitle,
  148. icon: UIApplicationShortcutIcon(templateImageName: icon),
  149. userInfo: nil
  150. )
  151. UIApplication.shared.shortcutItems = [shortcutItem]
  152. }
  153. }