IMChatTempData.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. angular.module('push')
  2. .factory('IMChatTempData', function () {
  3. var iMChatTempData = {};
  4. /**
  5. * 临时存储消息数据
  6. * 具体消息格式:[{topic:"",msglist:[{},{}...]},{topic:"",msglist:[{},{}...]}...]
  7. */
  8. iMChatTempData.msgdata = [];
  9. // 主题下加入消息
  10. iMChatTempData.addMsgData = function (topic, msg) {
  11. // 1.提取临时数据已存在topic的index
  12. var topicindex = -1;
  13. angular.forEach(this.msgdata, function (value, index) {
  14. if (value.topic == topic) {
  15. topicindex = index;
  16. }
  17. });
  18. // 2.判断主题index是否存在
  19. if (topicindex != -1) {
  20. // 2.1主题已存在--> 消息去重添加
  21. var tempstr = angular.toJson(this.msgdata[topicindex].msglist);
  22. if (tempstr.indexOf(msg.messageid) == -1) {
  23. // 2.1.1 消息不存在
  24. if (this.msgdata[topicindex].msglist.length > 0) {
  25. var templist = angular.copy(this.msgdata[topicindex].msglist);
  26. this.msgdata[topicindex].msglist = templist.concat(msg);
  27. } else {
  28. this.msgdata[topicindex].msglist = [msg];
  29. }
  30. }
  31. // 2.1.2 消息存在 不处理
  32. } else {
  33. // 2.2主题不存在--> 直接添加主题、消息
  34. var tempobj = {
  35. topic: topic,
  36. msglist: [msg]
  37. };
  38. this.msgdata.push(tempobj);
  39. }
  40. };
  41. // 清空主题下消息
  42. iMChatTempData.clearMsgDataByTopic = function (topic) {
  43. // 1.提取临时数据已存在topic的index
  44. var topicindex = -1;
  45. angular.forEach(this.msgdata, function (value, index) {
  46. if (value.topic == topic) {
  47. topicindex = index;
  48. }
  49. });
  50. this.msgdata.splice(topicindex, 1);
  51. };
  52. // 清空所有消息
  53. iMChatTempData.clearMsgData = function () {
  54. iMChatTempData.msgdata = [];
  55. };
  56. return iMChatTempData;
  57. });