mobileoa-common-modules
Version:
移动办公平台前端公共功能模块
109 lines (87 loc) • 2.64 kB
JavaScript
;
var angular = require('angular'),
_ = require('jsUtil');
require('../modules');
/**
* 信息发布Detail页面 缓存服务
*/
var module = angular.module('notice.services');
module.factory('NoticeCache', function() {
var service = {
updateNotices: updateNotices,
getNotices: getNotices,
clearNotices: clearNotices,
getNotice: getNotice, //用于测试
getNoticeIds: getNoticeIds, //用于测试
updateUserIdsHistory: updateUserIdsHistory,
getUserIds: getUserIds,
clearUserIds: clearUserIds
};
function clearUserIds() {
var key = 'notice_userIds';
localStorage.removeItem(key);
}
function updateUserIdsHistory(userId) {
var userIdsCache = getUserIds() || [];
if(userIdsCache.indexOf(userId) == -1) {
userIdsCache.push(userId);
var jsonUserids, key = 'notice_userIds';
jsonUserids = JSON.stringify(userIdsCache);
localStorage.setItem(key, jsonUserids);
}
}
function getUserIds() {
var key = 'notice_userIds';
return JSON.parse(localStorage.getItem(key));
}
function updateNotices(userId, notices) {
var jsonNotice, key, ids = [];
_.each(notices, function(notice) {
updateNotice(userId, notice.id, notice);
ids.push(notice.id);
});
updateNoticeIds(userId, ids);
}
function clearNotices() {
var userIds = service.getUserIds() || [];
var userId;
for(var i = 0, len = userIds.length; i < len; i++) {
userId = userIds[i];
clearNotice(userId);
}
}
function clearNotice(userId) {
var ids = getNoticeIds(userId);
_.each(ids, function(id) {
localStorage.removeItem('Notice:' + id + '_' + userId);
});
localStorage.removeItem('NoticeIdListst:' + userId);
}
function getNotices(userId) {
var ids = getNoticeIds(userId),
notices = [];
_.each(ids, function(id) {
notices.push(getNotice(userId, id));
});
return notices;
}
function updateNotice(userId, id, notice) {
var jsonNotice = JSON.stringify(notice),
key = 'Notice:' + id + '_' + userId;
localStorage.setItem(key, jsonNotice);
}
function getNotice(userId, noticeId) {
var key = 'Notice:' + noticeId + '_' + userId;
return JSON.parse(localStorage.getItem(key));
}
function updateNoticeIds(userId, noticeIds) {
var jsonNoticeIds = JSON.stringify(noticeIds),
key = 'NoticeIdListst:' + userId;
localStorage.setItem(key, jsonNoticeIds);
}
function getNoticeIds(userId) {
var key = 'NoticeIdListst:' + userId;
return JSON.parse(localStorage.getItem(key));
}
return service;
});