QSLTools.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. func dealwithScheme(_ scheme: String) {
  23. // 将字符串转换为 URL 对象
  24. guard let url = URL(string: scheme) else {
  25. print("无效的 URL scheme")
  26. return
  27. }
  28. // 检查 scheme 是否为 location
  29. guard url.scheme == "location" else {
  30. print("不支持的 scheme 类型")
  31. return
  32. }
  33. if #available(iOS 16.0, *) {
  34. let host = url.host()
  35. let pathComponents = url.lastPathComponent
  36. let query = url.query()
  37. jumpToPath(path: host ?? "", pathName: pathComponents, param: query ?? "")
  38. } else {
  39. let currentPath = scheme.replacingOccurrences(of: "location://", with: "")
  40. let pathComponents = url.lastPathComponent
  41. let queryList = currentPath.components(separatedBy: "?")
  42. if(queryList.count > 1){
  43. let host = currentPath.replacingOccurrences(of: pathComponents, with: "").replacingOccurrences(of: "/", with: "")
  44. let query = currentPath.replacingOccurrences(of: host, with: "")
  45. jumpToPath(path: host, pathName: pathComponents.replacingOccurrences(of: "/", with: ""), param: query)
  46. }else{
  47. let host = currentPath.replacingOccurrences(of: pathComponents, with: "").replacingOccurrences(of: "/", with: "")
  48. jumpToPath(path: host, pathName: pathComponents.replacingOccurrences(of: "/", with: ""), param: "")
  49. }
  50. }
  51. }
  52. func jumpToPath(path:String, pathName:String, param:String = ""){
  53. if path == "dialog" {
  54. let dialogType = pathName
  55. switch dialogType {
  56. case "member_trial":
  57. QSLJumpManager.shared.pushToVipTrail(type: .notiPush)
  58. break;
  59. default:
  60. print("未知的对话框类型: \(dialogType)")
  61. }
  62. }else if path == "page" {
  63. if(pathName == "QSLVipController"){
  64. QSLJumpManager.shared.pushToVip(type: .shortcut)
  65. return
  66. }
  67. let controllerName = pathName
  68. navigateToViewController(controllerName)
  69. }else if path == "shortcut" {
  70. if(pathName == "com.manbu.shouji.member_or_trial"){
  71. checkMemeberOrTrial()
  72. return
  73. }
  74. }else{
  75. print("未知的路径类型")
  76. }
  77. }
  78. func checkMemeberOrTrial(){
  79. QSLNetwork().request(.wakeUpPressCheck(dict: [String: Any]())) { response in
  80. let model: QSLWakeupModel = response.mapObject(QSLWakeupModel.self, modelKey: "data")
  81. var scheme = "location://page/QSLVipController"
  82. if(model.functionType == "trial"){
  83. scheme = "location://page/QSLVipTrialVC"
  84. }
  85. if(QSLTools.shared.isOpen){
  86. //处理urlScheme
  87. QSLTools.shared.urlScheme = ""
  88. QSLTools.shared.dealwithScheme(scheme)
  89. return
  90. }
  91. QSLTools.shared.urlScheme = scheme
  92. } fail: { code, msg in
  93. }
  94. }
  95. func navigateToViewController(_ controllerName: String) {
  96. // 获取命名空间(Swift 类名通常是 "YourAppName.ViewController")
  97. let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String ?? ""
  98. let fullClassName = "\(namespace).\(controllerName)"
  99. // 通过类名创建控制器实例
  100. guard let controllerClass = NSClassFromString(fullClassName) as? UIViewController.Type else {
  101. print("找不到控制器类: \(controllerName)")
  102. return
  103. }
  104. let viewController = controllerClass.init()
  105. self.rootViewController()?.pushVC(vc: viewController)
  106. }
  107. func requestShortcutItem(){
  108. //判断登录过没有
  109. if QSLBaseManager.shared.isLogin() {
  110. var isAttr = false
  111. if((UserDefaults.standard.value(forKey: "QSAttribution")) != nil){
  112. if let attr = UserDefaults.standard.value(forKey: "QSAttribution") as? Bool {
  113. isAttr = attr
  114. }
  115. }
  116. QSLNetwork().request(.wakeUpPressInfo(dict: ["attribution":isAttr])) { response in
  117. let model: QSLWakeupModel = response.mapObject(QSLWakeupModel.self, modelKey: "data")
  118. var iconName = "mine_func_gift"
  119. if(model.functionType == "feedback"){
  120. iconName = "mine_func_advice"
  121. }
  122. self.setupShortcutItem(typeName: model.functionType, title: model.title, subTitle: model.subTitle, icon: iconName)
  123. } fail: { code, msg in
  124. }
  125. }
  126. }
  127. func setupShortcutItem(typeName:String, title: String, subTitle:String, icon:String ){
  128. let shortcutItem = UIApplicationShortcutItem(
  129. type: "com.manbu.shouji." + typeName,
  130. localizedTitle: title,
  131. localizedSubtitle: subTitle,
  132. icon: UIApplicationShortcutIcon(templateImageName: icon),
  133. userInfo: nil
  134. )
  135. UIApplication.shared.shortcutItems = [shortcutItem]
  136. }
  137. }