KeyboardApi.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. import GravityEngineSDK
  12. enum NetworkEnvironment {
  13. case local
  14. case dev
  15. case prod
  16. }
  17. struct KeyboardApi {
  18. static let env: NetworkEnvironment = .prod
  19. static let localUrl = "http://192.168.10.113:8880"
  20. static let devUrl = "http://42.193.245.11"
  21. static let prodUrl = "https://project-api.atmob.com"
  22. static func getBaseUrl() -> String {
  23. switch KeyboardApi.env {
  24. case .local:
  25. return KeyboardApi.localUrl
  26. case .dev:
  27. return KeyboardApi.devUrl
  28. case .prod:
  29. return KeyboardApi.prodUrl
  30. }
  31. }
  32. static var params: [String:Any] = [
  33. "channelName":"appstore",
  34. "idfa":"",
  35. "idfv":"",
  36. "packageName":"com.qihuan.zhuiaijianpan",
  37. "appPlatform":2,
  38. "appVersionName":"1.0.0",
  39. "appVersionCode":100,
  40. "authToken": ""
  41. ]
  42. static func updateAppVersionName(appVersionName: String) {
  43. params["appVersionName"] = appVersionName
  44. }
  45. static func updateAppVersionCode(appVersionCode: Int) {
  46. params["appVersionCode"] = appVersionCode
  47. }
  48. static func updateIDFV(idfv: String) {
  49. params["idfv"] = idfv
  50. }
  51. static func updateIDFA(idfa: String) {
  52. params["idfa"] = idfa
  53. }
  54. static func updateAuthToken(authToken: String) {
  55. params["authToken"] = authToken
  56. }
  57. // 初始化基本参数
  58. static func initParams() {
  59. // 获取版本号
  60. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  61. debugPrint("App Version: \(appVersion)")
  62. KeyboardApi.updateAppVersionName(appVersionName: appVersion)
  63. let array = appVersion.components(separatedBy: ".")
  64. var appVersionCode = ""
  65. for i in array {
  66. appVersionCode.append(i)
  67. }
  68. if appVersionCode.count < 3 {
  69. appVersionCode.append("0")
  70. }
  71. KeyboardApi.updateAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  72. } else {
  73. debugPrint("Unable to retrieve app version")
  74. }
  75. // 获取 设置uuid 并更新
  76. // if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  77. // KeyboardApi.updateIDFV(idfv: uuid)
  78. // debugPrint("IDFV ID: \(uuid)")
  79. // } else {
  80. // debugPrint("获取 IDFV ID 参数失败")
  81. // }
  82. if let idfv = KeyboardSharedDataManager.shared.getInitIDFV() {
  83. KeyboardApi.updateIDFV(idfv: idfv)
  84. debugPrint("IDFV ID: \(idfv)")
  85. } else {
  86. debugPrint("获取 IDFV ID 参数失败")
  87. }
  88. // 获取idfa
  89. // initIDFA()
  90. if let idfa = KeyboardSharedDataManager.shared.getInitIDFA() {
  91. KeyboardApi.updateIDFA(idfa: idfa)
  92. debugPrint("IDFA ID: \(idfa)")
  93. } else {
  94. debugPrint("获取 IDFA ID 参数失败")
  95. }
  96. // 获取token
  97. if let authToken = KeyboardSharedDataManager.shared.getToken() {
  98. KeyboardApi.updateAuthToken(authToken: authToken)
  99. }
  100. initGravity()
  101. }
  102. // 初始化idfa
  103. static func initIDFA() {
  104. DispatchQueue.global().asyncAfter(deadline: .now() + 0.1){
  105. if #available(iOS 14.0, *) {
  106. // 用户请求授权获得IDFA权限
  107. ATTrackingManager.requestTrackingAuthorization { status in
  108. if status == .authorized {
  109. debugPrint("授权成功")
  110. // 用户已授权
  111. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  112. debugPrint("IDFA: \(idfa.uuidString)")
  113. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  114. } else {
  115. debugPrint("无法获取idfa")
  116. initGravity()
  117. }
  118. }
  119. } else {
  120. // 检查用户是否授权应用使用广告标识符
  121. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  122. // 获取 IDFA
  123. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  124. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  125. debugPrint("IDFA: \(idfa)")
  126. initGravity()
  127. } else {
  128. debugPrint("用户拒绝广告追踪")
  129. initGravity()
  130. }
  131. }
  132. }
  133. }
  134. static func initGravity() {
  135. let config = GEConfig();
  136. config.appid = "21705269";
  137. config.accessToken = "ulA57gz2zpioGukhyiQFkTEjfKS1aPxw";
  138. config.debugMode = GravityEngineDebugMode.on;
  139. GravityEngineSDK.start(with: config);
  140. let instance = GravityEngineSDK.sharedInstance(withAppid: config.appid);
  141. // 开启自动采集
  142. instance?.enableAutoTrack(GravityEngineAutoTrackEventType.eventTypeAll);
  143. var clientId = KeyboardSharedDataManager.shared.getInitIDFV()
  144. if let idfa = KeyboardSharedDataManager.shared.getInitIDFA() {
  145. clientId = idfa
  146. }
  147. instance?.initializeGravityEngine(withAsaEnable: true, withClientId: clientId ?? "", withCaid1: "", withCaid2: "", withSyncAttribution: true, withChannel: "Appstore", withSuccessCallback: { response in
  148. print("gravity engine initialize success, response is", response)
  149. }, withErrorCallback: { error in
  150. print("gravity engine initialize failed, and error is", error)
  151. })
  152. }
  153. }
  154. extension KeyboardApi {
  155. // 获取键盘人设列表
  156. static let character_list = "/project/keyboard/v1/character/list"
  157. // 获取键盘列表
  158. static let keyboard_list = "/project/keyboard/v1/keyboard/list"
  159. // 选择键盘
  160. static let keyboard_choose = "/project/keyboard/v1/keyboard/choose"
  161. // 获取开场白列表
  162. static let prologue_list = "/project/keyboard/v1/keyboard/prologue/list"
  163. // 获取用户信息
  164. static let user_info = "/project/keyboard/v1/user/info"
  165. }
  166. extension KeyboardApi {
  167. // 超会回
  168. static let chat_super_reply = "/project/keyboard/v1/chat/superReply"
  169. // 超会说
  170. static let chat_super_speak = "/project/keyboard/v1/chat/superSpeak"
  171. // 开场白
  172. static let chat_super_prologue = "/project/keyboard/v1/chat/prologue"
  173. }