123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- angular.module('push')
- .factory('IMChatTempData', function () {
- var iMChatTempData = {};
- /**
- * 临时存储消息数据
- * 具体消息格式:[{topic:"",msglist:[{},{}...]},{topic:"",msglist:[{},{}...]}...]
- */
- iMChatTempData.msgdata = [];
- // 主题下加入消息
- iMChatTempData.addMsgData = function (topic, msg) {
- // 1.提取临时数据已存在topic的index
- var topicindex = -1;
- angular.forEach(this.msgdata, function (value, index) {
- if (value.topic == topic) {
- topicindex = index;
- }
- });
- // 2.判断主题index是否存在
- if (topicindex != -1) {
- // 2.1主题已存在--> 消息去重添加
- var tempstr = angular.toJson(this.msgdata[topicindex].msglist);
- if (tempstr.indexOf(msg.messageid) == -1) {
- // 2.1.1 消息不存在
- if (this.msgdata[topicindex].msglist.length > 0) {
- var templist = angular.copy(this.msgdata[topicindex].msglist);
- this.msgdata[topicindex].msglist = templist.concat(msg);
- } else {
- this.msgdata[topicindex].msglist = [msg];
- }
- }
- // 2.1.2 消息存在 不处理
- } else {
- // 2.2主题不存在--> 直接添加主题、消息
- var tempobj = {
- topic: topic,
- msglist: [msg]
- };
- this.msgdata.push(tempobj);
- }
- };
- // 清空主题下消息
- iMChatTempData.clearMsgDataByTopic = function (topic) {
- // 1.提取临时数据已存在topic的index
- var topicindex = -1;
- angular.forEach(this.msgdata, function (value, index) {
- if (value.topic == topic) {
- topicindex = index;
- }
- });
- this.msgdata.splice(topicindex, 1);
- };
- // 清空所有消息
- iMChatTempData.clearMsgData = function () {
- iMChatTempData.msgdata = [];
- };
- return iMChatTempData;
- });
|