O2IM.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. let o2_im_msg_type_location = "location"
  21. //消息body
  22. let o2_im_msg_body_image = "[图片]"
  23. let o2_im_msg_body_audio = "[语音]"
  24. let o2_im_msg_body_video = "[视频]"
  25. let o2_im_msg_body_location = "[位置]"
  26. //表情的字符串转化为O2Emoji.bundle里面的图片路径 [01] -> im_emotion_01
  27. func o2ImEmojiPath(emojiBody: String) -> String {
  28. if emojiBody.length == 4 {
  29. let s = emojiBody.subString(from: 1, to: 3)
  30. return "im_emotion_\(s)"
  31. }
  32. return ""
  33. }
  34. class O2IMFileManager {
  35. static let shared: O2IMFileManager = {
  36. return O2IMFileManager()
  37. }()
  38. private let communicateAPI = {
  39. return OOMoyaProvider<CommunicateAPI>()
  40. }()
  41. private init() { }
  42. //根据id下载文件,并返回文件的本地url
  43. func getFileLocalUrl(fileId: String) -> Promise<URL> {
  44. return Promise { fulfill, reject in
  45. let url = self.localFilePath(fileId: fileId)
  46. if FileUtil.share.fileExist(filePath: url.path) {
  47. fulfill(url)
  48. } else {
  49. self.communicateAPI.request(.imDownloadFullFile(fileId), completion: { result in
  50. switch result {
  51. case .success(_):
  52. DDLogError("下载成功。。。。。\(fileId)")
  53. fulfill(url)
  54. break
  55. case .failure(let err):
  56. DDLogError(err.localizedDescription)
  57. reject(err)
  58. break
  59. }
  60. })
  61. }
  62. }
  63. }
  64. func localFilePath(fileId: String) -> URL {
  65. return FileUtil.share.cacheDir().appendingPathComponent("\(fileId).png")
  66. }
  67. //音频文件存储地址
  68. func getRecorderPath(type: RecordType) -> String {
  69. var recorderPath = FileUtil.share.cacheDir()
  70. recorderPath.appendPathComponent("o2im")
  71. //目录不存在就创建
  72. DDLogDebug("开始创建目录\(recorderPath.path)")
  73. FileUtil.share.createDirectory(path: recorderPath.path)
  74. let now:Date = Date()
  75. let dateFormatter = DateFormatter()
  76. dateFormatter.dateFormat = "yyyy-MM-dd-hh-mm-ss"
  77. let fileName = (type == RecordType.Caf) ? "\(dateFormatter.string(from: now))-MySound.caf" : "\(dateFormatter.string(from: now))-MySound.mp3"
  78. recorderPath.appendPathComponent(fileName)
  79. return recorderPath.path
  80. }
  81. }
  82. enum RecordType :String {
  83. case Caf = "caf"
  84. case MP3 = "mp3"
  85. }