O2JPushManager.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // O2JPushManager.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/11/11.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Moya
  10. import Promises
  11. import CocoaLumberjack
  12. typealias PushCallback = () -> Void
  13. class O2JPushManager {
  14. static let shared: O2JPushManager = {
  15. return O2JPushManager()
  16. }()
  17. private init() {
  18. }
  19. private let o2JPushAPI = {
  20. return OOMoyaProvider<JPushAPI>()
  21. }()
  22. private func pushConfig(completedBlock:@escaping PushCallback) {
  23. DDLogDebug("请求推送配置,目前启用的通道是极光还是华为")
  24. if let _ = O2AuthSDK.shared.o2APIServer(context: .x_jpush_assemble_control) {
  25. self.o2JPushAPI.request(.pushConfig) { result in
  26. let response = OOResult<BaseModelClass<JPushConfig>>(result)
  27. if response.isResultSuccess() {
  28. let type = response.model?.data?.pushType ?? "jpush"
  29. O2AuthSDK.shared.setPushType(type: type)
  30. }else {
  31. O2AuthSDK.shared.setPushType(type: "jpush")
  32. DDLogError(response.error?.localizedDescription ?? "绑定设备到服务器失败!")
  33. }
  34. completedBlock()
  35. }
  36. }else {
  37. DDLogError("绑定,没有获取到极光推送消息模块,服务器版本不支持!!!!!")
  38. O2AuthSDK.shared.setPushType(type: "jpush")
  39. completedBlock()
  40. }
  41. }
  42. //直连版本 连接o2oa服务器绑定设备号到个人属性中
  43. func o2JPushBind() {
  44. DDLogDebug("绑定设备号")
  45. if let _ = O2AuthSDK.shared.o2APIServer(context: .x_jpush_assemble_control) {
  46. pushConfig {
  47. let pushType = O2AuthSDK.shared.getPushType()
  48. let device = JPushDevice()
  49. // apns deviceToken 华为推送通道的时候用
  50. if pushType == "jpush" {
  51. let deviceName = O2AuthSDK.shared.getDeviceToken() // jpush 的deviceToken
  52. device.deviceName = deviceName
  53. } else {
  54. let deviceToken = O2AuthSDK.shared.getApnsToken()
  55. device.deviceName = deviceToken
  56. }
  57. device.deviceType = "ios"
  58. device.pushType = pushType
  59. self.o2JPushAPI.request(.bindDevice(device)) { (result) in
  60. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  61. if response.isResultSuccess() {
  62. let value = response.model?.data
  63. DDLogInfo("绑定设备到服务器,结果:\(value?.value ?? false)")
  64. }else {
  65. DDLogError(response.error?.localizedDescription ?? "绑定设备到服务器失败!")
  66. }
  67. }
  68. }
  69. }else {
  70. DDLogError("绑定,没有获取到极光推送消息模块,服务器版本不支持!!!!!")
  71. }
  72. }
  73. func O2JPushUnBind() {
  74. DDLogDebug("解除绑定设备号")
  75. if let _ = O2AuthSDK.shared.o2APIServer(context: .x_jpush_assemble_control) {
  76. let deviceName = O2AuthSDK.shared.getDeviceToken()
  77. let pushType = O2AuthSDK.shared.getPushType()
  78. self.o2JPushAPI.request(.unBindDeviceNew(deviceName, pushType)) { (result) in
  79. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  80. if response.isResultSuccess() {
  81. let value = response.model?.data
  82. DDLogInfo("解绑设备号 ,结果:\(value?.value ?? false)")
  83. } else {
  84. DDLogDebug("老的解绑方式。。。。。")
  85. self.o2JPushAPI.request(.unBindDevice(deviceName)) { (result) in
  86. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  87. if response.isResultSuccess() {
  88. let value = response.model?.data
  89. DDLogInfo("解绑设备号 ,结果:\(value?.value ?? false)")
  90. }else {
  91. DDLogError(response.error?.localizedDescription ?? "解绑设备号 失败!")
  92. }
  93. }
  94. }
  95. }
  96. }else {
  97. DDLogError("解绑,没有获取到极光推送消息模块,服务器版本不支持!!!!!")
  98. }
  99. }
  100. }