O2IM.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // O2IM.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/4.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import Promises
  10. import CocoaLumberjack
  11. //心跳消息
  12. let o2_im_ws_heartbeat = "heartbeat"
  13. let o2_im_conversation_type_single = "single"
  14. let o2_im_conversation_type_group = "group"
  15. //消息分类
  16. let o2_im_msg_type_text = "text"
  17. let o2_im_msg_type_emoji = "emoji"
  18. let o2_im_msg_type_image = "image"
  19. let o2_im_msg_type_audio = "audio"
  20. //消息body
  21. let o2_im_msg_body_image = "[图片]"
  22. let o2_im_msg_body_audio = "[语音]"
  23. let o2_im_msg_body_video = "[视频]"
  24. //表情的字符串转化为O2Emoji.bundle里面的图片路径 [01] -> im_emotion_01
  25. func o2ImEmojiPath(emojiBody: String) -> String {
  26. if emojiBody.length == 4 {
  27. let s = emojiBody.subString(from: 1, to: 3)
  28. return "im_emotion_\(s)"
  29. }
  30. return ""
  31. }
  32. class O2IMFileManager {
  33. static let shared: O2IMFileManager = {
  34. return O2IMFileManager()
  35. }()
  36. private let communicateAPI = {
  37. return OOMoyaProvider<CommunicateAPI>()
  38. }()
  39. private init() { }
  40. //根据id下载文件,并返回文件的本地url
  41. func getFileLocalUrl(fileId: String) -> Promise<URL> {
  42. return Promise { fulfill, reject in
  43. let url = self.localFilePath(fileId: fileId)
  44. if FileUtil.share.fileExist(filePath: url.path) {
  45. fulfill(url)
  46. } else {
  47. self.communicateAPI.request(.imDownloadFullFile(fileId), completion: { result in
  48. switch result {
  49. case .success(_):
  50. DDLogError("下载成功。。。。。\(fileId)")
  51. fulfill(url)
  52. break
  53. case .failure(let err):
  54. DDLogError(err.localizedDescription)
  55. reject(err)
  56. break
  57. }
  58. })
  59. }
  60. }
  61. }
  62. func localFilePath(fileId: String) -> URL {
  63. return FileUtil.share.cacheDir().appendingPathComponent("\(fileId).png")
  64. }
  65. //音频文件存储地址
  66. func getRecorderPath(type: RecordType) -> String {
  67. var recorderPath = FileUtil.share.cacheDir()
  68. recorderPath.appendPathComponent("o2im")
  69. //目录不存在就创建
  70. DDLogDebug("开始创建目录\(recorderPath.path)")
  71. FileUtil.share.createDirectory(path: recorderPath.path)
  72. let now:Date = Date()
  73. let dateFormatter = DateFormatter()
  74. dateFormatter.dateFormat = "yyyy-MM-dd-hh-mm-ss"
  75. let fileName = (type == RecordType.Caf) ? "\(dateFormatter.string(from: now))-MySound.caf" : "\(dateFormatter.string(from: now))-MySound.mp3"
  76. recorderPath.appendPathComponent(fileName)
  77. return recorderPath.path
  78. }
  79. }
  80. enum RecordType :String {
  81. case Caf = "caf"
  82. case MP3 = "mp3"
  83. }