IMChatMessageViewCell.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //
  2. // IMChatMessageViewCell.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/8.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. protocol IMChatMessageDelegate {
  11. func playAudio(info: IMMessageBodyInfo, id: String?)
  12. func openImageOrFileMessage(info: IMMessageBodyInfo)
  13. func openLocatinMap(info: IMMessageBodyInfo)
  14. func openApplication(storyboard: String)
  15. func openWork(workId: String)
  16. }
  17. class IMChatMessageViewCell: UITableViewCell {
  18. @IBOutlet weak var avatarImage: UIImageView!
  19. @IBOutlet weak var titleLabel: UILabel!
  20. @IBOutlet weak var timeLabel: UILabel!
  21. @IBOutlet weak var messageBackgroundView: UIView!
  22. @IBOutlet weak var messageBackgroundWidth: NSLayoutConstraint!
  23. @IBOutlet weak var messageBackgroundHeight: NSLayoutConstraint!
  24. private lazy var audioView: IMAudioView = {
  25. let view = Bundle.main.loadNibNamed("IMAudioView", owner: self, options: nil)?.first as! IMAudioView
  26. view.frame = CGRect(x: 0, y: 0, width: IMAudioView.IMAudioView_width, height: IMAudioView.IMAudioView_height)
  27. return view
  28. }()
  29. //位置消息 主体view
  30. private lazy var locationView: IMLocationView = {
  31. let view = Bundle.main.loadNibNamed("IMLocationView", owner: self, options: nil)?.first as! IMLocationView
  32. view.frame = CGRect(x: 0, y: 0, width: IMLocationView.IMLocationViewWidth, height: IMLocationView.IMLocationViewHeight)
  33. return view
  34. }()
  35. //文件消息
  36. private lazy var fileView: IMFileView = {
  37. let view = Bundle.main.loadNibNamed("IMFileView", owner: self, options: nil)?.first as! IMFileView
  38. view.frame = CGRect(x: 0, y: 0, width: IMFileView.IMFileView_width, height: IMFileView.IMFileView_height)
  39. return view
  40. }()
  41. var delegate: IMChatMessageDelegate?
  42. //是否正在播放音频 音频消息使用
  43. private var isPlayingAudio = false
  44. override func awakeFromNib() {
  45. super.awakeFromNib()
  46. }
  47. override func setSelected(_ selected: Bool, animated: Bool) {
  48. super.setSelected(selected, animated: animated)
  49. }
  50. //普通通知消息
  51. func setInstantContent(item: InstantMessage) {
  52. if let time = item.createTime {
  53. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  54. self.timeLabel.text = date.friendlyTime()
  55. }
  56. self.messageBackgroundView.removeSubviews()
  57. if let msg = item.title {
  58. let msgLabel = textMsgRender(msg: msg)
  59. setColorAndClickEvent(item: item, label: msgLabel)
  60. }
  61. if let type = item.type {
  62. if type.starts(with: "task_") {
  63. self.avatarImage.image = UIImage(named: "icon_daiban")
  64. self.titleLabel.text = "待办消息"
  65. } else if type.starts(with: "taskCompleted_") {
  66. self.avatarImage.image = UIImage(named: "icon_taskcompleted")
  67. self.titleLabel.text = "已办消息"
  68. } else if type.starts(with: "read_") {
  69. self.avatarImage.image = UIImage(named: "icon_read")
  70. self.titleLabel.text = "待阅消息"
  71. } else if type.starts(with: "readCompleted_") {
  72. self.avatarImage.image = UIImage(named: "icon_readcompleted")
  73. self.titleLabel.text = "已阅消息"
  74. } else if type.starts(with: "review_") || type.starts(with: "work_") || type.starts(with: "process_") {
  75. self.avatarImage.image = UIImage(named: "icon_daiban")
  76. self.titleLabel.text = "工作消息"
  77. } else if type.starts(with: "meeting_") {
  78. self.avatarImage.image = UIImage(named: "icon_meeting")
  79. self.titleLabel.text = "会议消息"
  80. } else if type.starts(with: "attachment_") {
  81. self.avatarImage.image = UIImage(named: "icon_yunpan")
  82. self.titleLabel.text = "云盘消息"
  83. } else if type.starts(with: "calendar_") {
  84. self.avatarImage.image = UIImage(named: "icon_calendar")
  85. self.titleLabel.text = "日历消息"
  86. } else if type.starts(with: "cms_") {
  87. self.avatarImage.image = UIImage(named: "icon_cms")
  88. self.titleLabel.text = "信息中心消息"
  89. } else if type.starts(with: "bbs_") {
  90. self.avatarImage.image = UIImage(named: "icon_bbs")
  91. self.titleLabel.text = "论坛消息"
  92. } else if type.starts(with: "mind_") {
  93. self.avatarImage.image = UIImage(named: "icon_mindMap")
  94. self.titleLabel.text = "脑图消息"
  95. } else {
  96. self.avatarImage.image = UIImage(named: "icon_email")
  97. self.titleLabel.text = "其他消息"
  98. }
  99. }
  100. }
  101. private func setcc(label:UILabel, clickEvent: ((UITapGestureRecognizer)->Void)?) {
  102. if let textString = label.text {
  103. let attributedString = NSMutableAttributedString(string: textString)
  104. attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
  105. attributedString.addAttribute(.foregroundColor, value: base_blue_color, range: NSRange(location: 0, length: attributedString.length))
  106. label.attributedText = attributedString
  107. label.addTapGesture(action: clickEvent)
  108. }
  109. }
  110. private func setColorAndClickEvent(item: InstantMessage, label:UILabel) {
  111. func parseWorkId(body: String) -> String? {
  112. if let jsonData = String(body).data(using: .utf8) {
  113. let dicArr = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
  114. if let work = dicArr["work"] as? String {
  115. return work
  116. }
  117. if let workCompleted = dicArr["workCompleted"] as? String {
  118. return workCompleted
  119. }
  120. }
  121. return nil
  122. }
  123. if let type = item.type {
  124. if type.starts(with: "task_") {
  125. if !type.contains("_delete") {
  126. guard let body = item.body else {
  127. return
  128. }
  129. guard let workId = parseWorkId(body: body) else {
  130. return
  131. }
  132. setcc(label: label) { tap in
  133. //打开工作 ?
  134. self.delegate?.openWork(workId: workId)
  135. }
  136. }
  137. } else if type.starts(with: "taskCompleted_") {
  138. if !type.contains("_delete") {
  139. guard let body = item.body else {
  140. return
  141. }
  142. guard let workId = parseWorkId(body: body) else {
  143. return
  144. }
  145. setcc(label: label) { tap in
  146. //打开已办
  147. self.delegate?.openWork(workId: workId)
  148. }
  149. }
  150. } else if type.starts(with: "read_") {
  151. if !type.contains("_delete") {
  152. guard let body = item.body else {
  153. return
  154. }
  155. guard let workId = parseWorkId(body: body) else {
  156. return
  157. }
  158. setcc(label: label) { tap in
  159. //打开待阅
  160. self.delegate?.openWork(workId: workId)
  161. }
  162. }
  163. } else if type.starts(with: "readCompleted_") {
  164. if !type.contains("_delete") {
  165. guard let body = item.body else {
  166. return
  167. }
  168. guard let workId = parseWorkId(body: body) else {
  169. return
  170. }
  171. setcc(label: label) { tap in
  172. //打开已阅
  173. self.delegate?.openWork(workId: workId)
  174. }
  175. }
  176. } else if type.starts(with: "review_") || type.starts(with: "work_") || type.starts(with: "process_") {
  177. if !type.contains("_delete") {
  178. guard let body = item.body else {
  179. return
  180. }
  181. guard let workId = parseWorkId(body: body) else {
  182. return
  183. }
  184. setcc(label: label) { tap in
  185. //打开 其他工作
  186. self.delegate?.openWork(workId: workId)
  187. }
  188. }
  189. } else if type.starts(with: "meeting_") {
  190. setcc(label: label) { tap in
  191. //打开会议模块
  192. self.delegate?.openApplication(storyboard: "meeting")
  193. }
  194. } else if type.starts(with: "attachment_") {
  195. setcc(label: label) { tap in
  196. //打开云盘
  197. self.delegate?.openApplication(storyboard: "CloudFile")
  198. }
  199. } else if type.starts(with: "calendar_") {
  200. setcc(label: label) { tap in
  201. //打开日历
  202. self.delegate?.openApplication(storyboard: "calendar")
  203. }
  204. } else if type.starts(with: "cms_") {
  205. setcc(label: label) { tap in
  206. //打开cms
  207. self.delegate?.openApplication(storyboard: "information")
  208. }
  209. } else if type.starts(with: "bbs_") {
  210. setcc(label: label) { tap in
  211. //打开论坛
  212. self.delegate?.openApplication(storyboard: "bbs")
  213. }
  214. } else if type.starts(with: "mind_") {
  215. setcc(label: label) { tap in
  216. //打开脑图
  217. self.delegate?.openApplication(storyboard: "mind")
  218. }
  219. } else {
  220. }
  221. }
  222. }
  223. //聊天消息
  224. func setContent(item: IMMessageInfo, isPlayingAudio: Bool) {
  225. self.isPlayingAudio = isPlayingAudio
  226. //time
  227. if let time = item.createTime {
  228. let date = time.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
  229. self.timeLabel.text = date.friendlyTime()
  230. }
  231. //name avatart
  232. if let person = item.createPerson {
  233. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##": person as AnyObject], generateTime: false)
  234. if let u = URL(string: urlstr!) {
  235. self.avatarImage.hnk_setImageFromURL(u)
  236. } else {
  237. self.avatarImage.image = UIImage(named: "icon_men")
  238. }
  239. //姓名
  240. self.titleLabel.text = person.split("@").first ?? ""
  241. } else {
  242. self.avatarImage.image = UIImage(named: "icon_men")
  243. self.titleLabel.text = ""
  244. }
  245. self.messageBackgroundView.removeSubviews()
  246. if let jsonBody = item.body, let body = parseJson(msg: jsonBody) {
  247. if body.type == o2_im_msg_type_emoji {
  248. emojiMsgRender(emoji: body.body!)
  249. } else if body.type == o2_im_msg_type_image {
  250. imageMsgRender(info: body)
  251. } else if o2_im_msg_type_audio == body.type {
  252. audioMsgRender(info: body, id: item.id)
  253. } else if o2_im_msg_type_location == body.type {
  254. locationMsgRender(info: body)
  255. } else if o2_im_msg_type_file == body.type {
  256. fileMsgRender(info: body)
  257. } else {
  258. _ = textMsgRender(msg: body.body!)
  259. }
  260. }
  261. }
  262. //文件消息
  263. private func fileMsgRender(info: IMMessageBodyInfo) {
  264. self.messageBackgroundWidth.constant = IMFileView.IMFileView_width + 20
  265. self.messageBackgroundHeight.constant = IMFileView.IMFileView_height + 20
  266. self.fileView.translatesAutoresizingMaskIntoConstraints = false
  267. self.messageBackgroundView.addSubview(self.fileView)
  268. if let fileId = info.fileId {
  269. self.fileView.setFile(name: info.fileName ?? fileId, fileExt: info.fileExtension)
  270. }else if let filePath = info.fileTempPath {
  271. let ext = filePath.pathExtension
  272. let fileName = filePath.pathFileName
  273. self.fileView.setFile(name: fileName, fileExt: ext)
  274. }
  275. self.fileView.addTapGesture { (tap) in
  276. self.delegate?.openImageOrFileMessage(info: info)
  277. }
  278. //点击事件
  279. self.constraintWithContent(contentView: self.fileView)
  280. }
  281. //位置消息
  282. private func locationMsgRender(info: IMMessageBodyInfo) {
  283. self.messageBackgroundWidth.constant = IMLocationView.IMLocationViewWidth + 20
  284. self.messageBackgroundHeight.constant = IMLocationView.IMLocationViewHeight + 20
  285. self.locationView.translatesAutoresizingMaskIntoConstraints = false
  286. self.messageBackgroundView.addSubview(self.locationView)
  287. self.locationView.setLocationAddress(address: info.address ?? "")
  288. //点击打开地址
  289. self.locationView.addTapGesture { (tap) in
  290. //open map view
  291. self.delegate?.openLocatinMap(info: info)
  292. }
  293. self.constraintWithContent(contentView: self.locationView)
  294. }
  295. //音频消息
  296. private func audioMsgRender(info: IMMessageBodyInfo, id: String?) {
  297. let width = IMAudioView.IMAudioView_width + 20
  298. let height = IMAudioView.IMAudioView_height + 20
  299. self.messageBackgroundWidth.constant = width
  300. self.messageBackgroundHeight.constant = height
  301. //背景图片
  302. let bgImg = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
  303. let insets = UIEdgeInsets(top: 28, left: 10, bottom: 5, right: 5); // 上、左、下、右
  304. var bubble = UIImage(named: "chat_bubble_incomming")
  305. bubble = bubble?.resizableImage(withCapInsets: insets, resizingMode: .stretch)
  306. bgImg.image = bubble
  307. self.messageBackgroundView.addSubview(bgImg)
  308. self.audioView.translatesAutoresizingMaskIntoConstraints = false
  309. self.messageBackgroundView.addSubview(self.audioView)
  310. self.audioView.setDuration(duration: info.audioDuration ?? "0")
  311. if self.isPlayingAudio {
  312. self.audioView.playAudioGif()
  313. }else {
  314. self.audioView.stopPlayAudioGif()
  315. }
  316. self.audioView.addTapGesture { (tap) in
  317. self.delegate?.playAudio(info: info, id: id)
  318. }
  319. self.constraintWithContent(contentView: self.audioView)
  320. }
  321. private func constraintWithContent(contentView: UIView) {
  322. let top = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: contentView.superview!, attribute: .top, multiplier: 1, constant: 10)
  323. let bottom = NSLayoutConstraint(item: contentView.superview!, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1, constant: 10)
  324. let left = NSLayoutConstraint(item: contentView, attribute: .leading, relatedBy: .equal, toItem: contentView.superview!, attribute: .leading, multiplier: 1, constant: 10)
  325. let right = NSLayoutConstraint(item: contentView.superview!, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1, constant: 10)
  326. NSLayoutConstraint.activate([top, bottom, left, right])
  327. }
  328. //图片消息
  329. private func imageMsgRender(info: IMMessageBodyInfo) {
  330. let width: CGFloat = 144
  331. let height: CGFloat = 192
  332. self.messageBackgroundWidth.constant = width + 20
  333. self.messageBackgroundHeight.constant = height + 20
  334. //图片
  335. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
  336. if let fileId = info.fileId {
  337. DDLogDebug("file id :\(fileId)")
  338. let urlStr = AppDelegate.o2Collect.generateURLWithAppContextKey(
  339. CommunicateContext.communicateContextKey,
  340. query: CommunicateContext.imDownloadImageWithSizeQuery,
  341. parameter: ["##id##": fileId as AnyObject,
  342. "##width##": "144" as AnyObject,
  343. "##height##": "192" as AnyObject], generateTime: false)
  344. if let url = URL(string: urlStr!) {
  345. imageView.hnk_setImageFromURL(url)
  346. } else {
  347. imageView.image = UIImage(named: "chat_image")
  348. }
  349. } else if let filePath = info.fileTempPath {
  350. DDLogDebug("filePath :\(filePath)")
  351. imageView.hnk_setImageFromFile(filePath)
  352. } else {
  353. imageView.image = UIImage(named: "chat_image")
  354. }
  355. imageView.translatesAutoresizingMaskIntoConstraints = false
  356. self.messageBackgroundView.addSubview(imageView)
  357. imageView.addTapGesture { (tap) in
  358. self.delegate?.openImageOrFileMessage(info: info)
  359. }
  360. self.constraintWithContent(contentView: imageView)
  361. }
  362. private func emojiMsgRender(emoji: String) {
  363. let emojiSize = 36
  364. let width = CGFloat(emojiSize + 20)
  365. let height = CGFloat(emojiSize + 20)
  366. self.messageBackgroundWidth.constant = width
  367. self.messageBackgroundHeight.constant = height
  368. //背景图片
  369. let bgImg = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
  370. let insets = UIEdgeInsets(top: 28, left: 10, bottom: 5, right: 5); // 上、左、下、右
  371. var bubble = UIImage(named: "chat_bubble_incomming")
  372. bubble = bubble?.resizableImage(withCapInsets: insets, resizingMode: .stretch)
  373. bgImg.image = bubble
  374. self.messageBackgroundView.addSubview(bgImg)
  375. //表情图
  376. let emojiImage = UIImageView(frame: CGRect(x: 0, y: 0, width: emojiSize, height: emojiSize))
  377. let bundle = Bundle().o2EmojiBundle(anyClass: IMChatMessageViewCell.self)
  378. let path = o2ImEmojiPath(emojiBody: emoji)
  379. emojiImage.image = UIImage(named: path, in: bundle, compatibleWith: nil)
  380. emojiImage.translatesAutoresizingMaskIntoConstraints = false
  381. self.messageBackgroundView.addSubview(emojiImage)
  382. self.constraintWithContent(contentView: emojiImage)
  383. }
  384. private func textMsgRender(msg: String) -> UILabel {
  385. let size = msg.getSizeWithMaxWidth(fontSize: 16, maxWidth: messageWidth)
  386. self.messageBackgroundWidth.constant = size.width + 28
  387. self.messageBackgroundHeight.constant = size.height + 28
  388. //背景图片
  389. let bgImg = UIImageView(frame: CGRect(x: 0, y: 0, width: size.width + 28, height: size.height + 28))
  390. let insets = UIEdgeInsets(top: 28, left: 10, bottom: 5, right: 5); // 上、左、下、右
  391. var bubble = UIImage(named: "chat_bubble_incomming")
  392. bubble = bubble?.resizableImage(withCapInsets: insets, resizingMode: .stretch)
  393. bgImg.image = bubble
  394. self.messageBackgroundView.addSubview(bgImg)
  395. //文字
  396. let label = generateMessagelabel(str: msg, size: size)
  397. label.translatesAutoresizingMaskIntoConstraints = false
  398. self.messageBackgroundView.addSubview(label)
  399. self.constraintWithContent(contentView: label)
  400. return label
  401. // let top = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: label.superview!, attribute: .top, multiplier: 1, constant: 10)
  402. // let left = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: label.superview!, attribute: .leading, multiplier: 1, constant: 10)
  403. // let right = NSLayoutConstraint(item: label.superview!, attribute: .trailing, relatedBy: .equal, toItem: label, attribute: .trailing, multiplier: 1, constant: 10)
  404. // NSLayoutConstraint.activate([top, left, right])
  405. }
  406. private func generateMessagelabel(str: String, size: CGSize) -> UILabel {
  407. let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width + 8, height: size.height + 8))
  408. label.text = str
  409. label.font = UIFont.systemFont(ofSize: 16)
  410. label.numberOfLines = 0
  411. label.lineBreakMode = .byCharWrapping
  412. label.preferredMaxLayoutWidth = size.width
  413. return label
  414. }
  415. // private func calTextSize(str: String) -> CGSize {
  416. // let size = CGSize(width: messageWidth.toCGFloat, height: CGFloat(MAXFLOAT))
  417. // return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], context: nil).size
  418. // }
  419. //解析json为消息对象
  420. private func parseJson(msg: String) -> IMMessageBodyInfo? {
  421. return IMMessageBodyInfo.deserialize(from: msg)
  422. }
  423. }