IMViewModel.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // IMViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/4.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. import CocoaLumberjack
  10. import Moya
  11. class IMViewModel: NSObject {
  12. override init() {
  13. super.init()
  14. }
  15. private let communicateAPI = OOMoyaProvider<CommunicateAPI>()
  16. private let workAPI = OOMoyaProvider<OOWorkAPI>()
  17. }
  18. extension IMViewModel {
  19. func loadImConfig() -> Promise<IMConfig> {
  20. return Promise { fulfill, reject in
  21. self.communicateAPI.request(.getImConfig, completion: {result in
  22. let response = OOResult<BaseModelClass<IMConfig>>(result)
  23. if response.isResultSuccess() {
  24. if let config = response.model?.data {
  25. O2UserDefaults.shared.imConfig = config
  26. fulfill(config)
  27. } else {
  28. DDLogError("IM配置文件为空!!")
  29. let imConfig = IMConfig()
  30. imConfig.enableClearMsg = false
  31. imConfig.enableRevokeMsg = false
  32. O2UserDefaults.shared.imConfig = imConfig
  33. fulfill(imConfig)
  34. }
  35. } else {
  36. DDLogError(response.error?.localizedDescription ?? "IM配置文件查询出错!")
  37. let imConfig = IMConfig()
  38. imConfig.enableClearMsg = false
  39. imConfig.enableRevokeMsg = false
  40. O2UserDefaults.shared.imConfig = imConfig
  41. fulfill(imConfig)
  42. }
  43. })
  44. }
  45. }
  46. //创建会话 @param type: single group
  47. func createConversation(type: String, users: [String]) -> Promise<IMConversationInfo> {
  48. let conversation = IMConversationInfo()
  49. conversation.type = type
  50. conversation.personList = users
  51. return Promise { fulfill, reject in
  52. self.communicateAPI.request(.createConversation(conversation), completion: { result in
  53. let response = OOResult<BaseModelClass<IMConversationInfo>>(result)
  54. if response.isResultSuccess() {
  55. if let info = response.model?.data {
  56. fulfill(info)
  57. } else {
  58. reject(OOAppError.apiEmptyResultError)
  59. }
  60. } else {
  61. reject(response.error!)
  62. }
  63. })
  64. }
  65. }
  66. //修改标题
  67. func updateConversationTitle(id: String, title: String) -> Promise<IMConversationInfo> {
  68. return Promise { fulfill, reject in
  69. self.communicateAPI.request(.updateConversationTitle(id, title), completion: { result in
  70. let response = OOResult<BaseModelClass<IMConversationInfo>>(result)
  71. if response.isResultSuccess() {
  72. if let info = response.model?.data {
  73. fulfill(info)
  74. } else {
  75. reject(OOAppError.apiEmptyResultError)
  76. }
  77. } else {
  78. reject(response.error!)
  79. }
  80. })
  81. }
  82. }
  83. //修改成员列表
  84. func updateConversationPeople(id: String, users: [String]) -> Promise<IMConversationInfo> {
  85. return Promise { fulfill, reject in
  86. self.communicateAPI.request(.updateConversationPeople(id, users), completion: { result in
  87. let response = OOResult<BaseModelClass<IMConversationInfo>>(result)
  88. if response.isResultSuccess() {
  89. if let info = response.model?.data {
  90. fulfill(info)
  91. } else {
  92. reject(OOAppError.apiEmptyResultError)
  93. }
  94. } else {
  95. reject(response.error!)
  96. }
  97. })
  98. }
  99. }
  100. //阅读会话
  101. func readConversation(conversationId: String?) {
  102. guard let id = conversationId else {
  103. DDLogError("阅读会话失败, 传入id为空")
  104. return
  105. }
  106. self.communicateAPI.request(.readConversation(id), completion: {result in
  107. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  108. if response.isResultSuccess() {
  109. DDLogDebug("阅读当前会话成功!")
  110. }else {
  111. DDLogError("阅读会话失败!")
  112. }
  113. })
  114. }
  115. //发送消息
  116. func sendMsg(msg: IMMessageInfo) -> Promise<Bool> {
  117. return Promise { fulfill, reject in
  118. self.communicateAPI.request(.sendMsg(msg), completion: { result in
  119. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  120. if response.isResultSuccess() {
  121. if let _ = response.model?.data {
  122. fulfill(true)
  123. } else {
  124. reject(OOAppError.apiEmptyResultError)
  125. }
  126. } else {
  127. reject(response.error!)
  128. }
  129. })
  130. }
  131. }
  132. //上传文件
  133. func uploadFile(conversationId: String, type: String, fileName: String, file: Data) -> Promise<IMUploadBackModel> {
  134. return Promise { fulfill, reject in
  135. self.communicateAPI.request(.imUploadFile(conversationId, type, fileName, file), completion: { (result) in
  136. let response = OOResult<BaseModelClass<IMUploadBackModel>>(result)
  137. if response.isResultSuccess() {
  138. if let back = response.model?.data {
  139. fulfill(back)
  140. }else {
  141. reject(OOAppError.apiEmptyResultError)
  142. }
  143. }else {
  144. reject(response.error!)
  145. }
  146. })
  147. }
  148. }
  149. //查询会话列表
  150. func myConversationList() -> Promise<[IMConversationInfo]> {
  151. return Promise { fulfill, reject in
  152. self.communicateAPI.request(.myConversationList, completion: { result in
  153. let response = OOResult<BaseModelClass<[IMConversationInfo]>>(result)
  154. if response.isResultSuccess() {
  155. if let list = response.model?.data {
  156. let rList = list.sorted { (f, s) -> Bool in
  157. let ft = f.lastMessage?.createTime ?? ""
  158. let st = s.lastMessage?.createTime ?? ""
  159. if ft == "" {
  160. return true
  161. }
  162. if st == "" {
  163. return false
  164. }
  165. return ft.toDate(formatter: "yyyy-MM-dd HH:mm:ss") > st.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  166. }
  167. fulfill(rList)
  168. } else {
  169. reject(OOAppError.apiEmptyResultError)
  170. }
  171. } else {
  172. reject(response.error!)
  173. }
  174. })
  175. }
  176. }
  177. //查询消息列表
  178. func myMsgPageList(page: Int, conversationId: String) -> Promise<[IMMessageInfo]> {
  179. return Promise { fulfill, reject in
  180. self.communicateAPI.request(.msgListByPaging(page, 15, conversationId), completion: { result in
  181. let response = OOResult<BaseModelClass<[IMMessageInfo]>>(result)
  182. if response.isResultSuccess() {
  183. if let list = response.model?.data {
  184. //列表翻转
  185. let rList = list.sorted { (f, s) -> Bool in
  186. if let ft = f.createTime, let st = s.createTime {
  187. return ft.toDate(formatter: "yyyy-MM-dd HH:mm:ss") < st.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  188. }
  189. return true
  190. }
  191. fulfill(rList)
  192. } else {
  193. reject(OOAppError.apiEmptyResultError)
  194. }
  195. } else {
  196. reject(response.error!)
  197. }
  198. })
  199. }
  200. }
  201. func getInstantMsgList() -> Promise<[InstantMessage]> {
  202. return Promise { fulfill, reject in
  203. self.communicateAPI.request(.instantMessageList(50), completion: { result in
  204. let response = OOResult<BaseModelClass<[InstantMessage]>>(result)
  205. if response.isResultSuccess() {
  206. if let list = response.model?.data {
  207. //列表翻转
  208. let rList = list.sorted { (f, s) -> Bool in
  209. if let ft = f.createTime, let st = s.createTime {
  210. return ft.toDate(formatter: "yyyy-MM-dd HH:mm:ss") < st.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  211. }
  212. return true
  213. }
  214. fulfill(rList)
  215. } else {
  216. reject(OOAppError.apiEmptyResultError)
  217. }
  218. } else {
  219. reject(response.error!)
  220. }
  221. })
  222. }
  223. }
  224. ///判断是否工作已经完成
  225. func isWorkCompleted(work: String) -> Promise<Bool> {
  226. return Promise { fulfill, reject in
  227. self.workAPI.request(.getWork(work), completion: {result in
  228. let response = OOResult<BaseModelClass<WorkInfoResData>>(result)
  229. if response.isResultSuccess() {
  230. fulfill(false)
  231. }else {
  232. fulfill(true)
  233. }
  234. })
  235. }
  236. }
  237. // 清空聊天记录
  238. func clearAllChatMsg(conversationId: String) -> Promise<Bool> {
  239. return Promise {fulfill, reject in
  240. self.communicateAPI.request(.clearAllChatMsg(conversationId), completion: {result in
  241. let response = OOResult<BaseModelClass<OOCommonValueBoolModel>>(result)
  242. if response.isResultSuccess() {
  243. fulfill(true)
  244. }else {
  245. fulfill(false)
  246. }
  247. })
  248. }
  249. }
  250. // 撤回消息
  251. func revokeChatMsg(msgId: String) -> Promise<Bool> {
  252. return Promise {fulfill, reject in
  253. self.communicateAPI.request(.revokeMsg(msgId), completion: {result in
  254. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  255. if response.isResultSuccess() {
  256. fulfill(true)
  257. }else {
  258. fulfill(false)
  259. }
  260. })
  261. }
  262. }
  263. }