O2IM.swift 3.1 KB

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