KeyboardApi.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KeyboardApi.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/28.
  6. //
  7. import Foundation
  8. import UIKit
  9. import AdSupport
  10. import AppTrackingTransparency
  11. enum NetworkEnvironment {
  12. case local
  13. case dev
  14. case prod
  15. }
  16. struct KeyboardApi {
  17. static let env: NetworkEnvironment = .prod
  18. static let localUrl = "http://192.168.10.113:8880"
  19. static let devUrl = "http://42.193.245.11"
  20. static let prodUrl = "https://project-api.atmob.com"
  21. static func getBaseUrl() -> String {
  22. switch KeyboardApi.env {
  23. case .local:
  24. return KeyboardApi.localUrl
  25. case .dev:
  26. return KeyboardApi.devUrl
  27. case .prod:
  28. return KeyboardApi.prodUrl
  29. }
  30. }
  31. static var params: [String:Any] = [
  32. "channelName":"appstore",
  33. "idfa":"",
  34. "idfv":"",
  35. "packageName":"com.qihuan.zhuiaijianpan",
  36. "appPlatform":2,
  37. "appVersionName":"1.0.0",
  38. "appVersionCode":100,
  39. "authToken": ""
  40. ]
  41. static func updateAppVersionName(appVersionName: String) {
  42. params["appVersionName"] = appVersionName
  43. }
  44. static func updateAppVersionCode(appVersionCode: Int) {
  45. params["appVersionCode"] = appVersionCode
  46. }
  47. static func updateIDFV(idfv: String) {
  48. params["idfv"] = idfv
  49. }
  50. static func updateIDFA(idfa: String) {
  51. params["idfa"] = idfa
  52. }
  53. static func updateAuthToken(authToken: String) {
  54. params["authToken"] = authToken
  55. }
  56. // 初始化基本参数
  57. static func initParams() {
  58. // 获取版本号
  59. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  60. debugPrint("App Version: \(appVersion)")
  61. KeyboardApi.updateAppVersionName(appVersionName: appVersion)
  62. let array = appVersion.components(separatedBy: ".")
  63. var appVersionCode = ""
  64. for i in array {
  65. appVersionCode.append(i)
  66. }
  67. if appVersionCode.count < 3 {
  68. appVersionCode.append("0")
  69. }
  70. KeyboardApi.updateAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  71. } else {
  72. debugPrint("Unable to retrieve app version")
  73. }
  74. // 获取 设置uuid 并更新
  75. if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  76. KeyboardApi.updateIDFV(idfv: uuid)
  77. debugPrint("IDFV ID: \(uuid)")
  78. } else {
  79. debugPrint("获取 IDFV ID 参数失败")
  80. }
  81. // 获取idfa
  82. initIDFA()
  83. // 获取token
  84. if let authToken = KeyboardSharedDataManager.shared.getToken() {
  85. KeyboardApi.updateAuthToken(authToken: authToken)
  86. }
  87. }
  88. // 初始化idfa
  89. static func initIDFA() {
  90. DispatchQueue.global().asyncAfter(deadline: .now() + 0.1){
  91. if #available(iOS 14.0, *) {
  92. // 用户请求授权获得IDFA权限
  93. ATTrackingManager.requestTrackingAuthorization { status in
  94. if status == .authorized {
  95. debugPrint("授权成功")
  96. // 用户已授权
  97. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  98. debugPrint("IDFA: \(idfa.uuidString)")
  99. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  100. } else {
  101. debugPrint("无法获取idfa")
  102. }
  103. }
  104. } else {
  105. // 检查用户是否授权应用使用广告标识符
  106. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  107. // 获取 IDFA
  108. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  109. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  110. debugPrint("IDFA: \(idfa)")
  111. } else {
  112. debugPrint("用户拒绝广告追踪")
  113. }
  114. }
  115. }
  116. }
  117. }
  118. extension KeyboardApi {
  119. // 获取键盘人设列表
  120. static let character_list = "/project/keyboard/v1/character/list"
  121. // 获取键盘列表
  122. static let keyboard_list = "/project/keyboard/v1/keyboard/list"
  123. // 选择键盘
  124. static let keyboard_choose = "/project/keyboard/v1/keyboard/choose"
  125. // 获取开场白列表
  126. static let prologue_list = "/project/keyboard/v1/keyboard/prologue/list"
  127. // 获取用户信息
  128. static let user_info = "/project/keyboard/v1/user/info"
  129. }
  130. extension KeyboardApi {
  131. // 超会回
  132. static let chat_super_reply = "/project/keyboard/v1/chat/superReply"
  133. // 超会说
  134. static let chat_super_speak = "/project/keyboard/v1/chat/superSpeak"
  135. // 开场白
  136. static let chat_super_prologue = "/project/keyboard/v1/chat/prologue"
  137. }