Blame view

src/main/webapp/js/global.localStorage.js 3.13 KB
caiyongsong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * 20180304 yxt 添加本地缓存机制
 */

/*
 * 定义一个缓存类
 * modelctls URL
 * param 		参数
 * result 		结果
 * */
function CacehKey(url,data,type,result)
{
	this.url = url;
	this.data = data;
	this.type = type;
	
	this.KeyString = function()
	{
		var keyString = JSON.stringify(this.url) + ";" +  JSON.stringify(this.data) + ";" +  JSON.stringify(this.type) ;
		return keyString;
	}
}
//系统级请求本地缓存
function GlobalLocalCache()
{
	this.flag = true;
	//添加缓存
	this.AddCache = function(url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            return ;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		var valueString = JSON.stringify(result);
    		if(this.flag){
    			keyString = encodeURI(keyString);
    			valueString = encodeURI(valueString);
    		}
            window.localStorage.setItem(keyString,valueString);
        }
	}
	
	//获取缓存
	this.GetCacheResult = function (url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            result = null;
            return null;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		if(this.flag){
    			keyString = encodeURI(keyString);
    		}
            var value = window.localStorage.getItem(keyString);
            if(this.flag){
            	value = decodeURI(value);
            }
            result = JSON.parse(value);
            return result;            
        }
	}
	
	//单个清除缓存
	this.removeLocalCache = function(url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            return ;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		if(this.flag){
    			keyString = encodeURI(keyString);
    		}
            window.localStorage.removeItem(keyString);
        }
	}
	
	//清理缓存
	this.clearLocalCache = function ()
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage"); 
        }else{
        	
        	window.localStorage.clear();     
        }
	}
	
	//检查缓存版本
	this.checkCacheVersion=function()
	{
		var pdata = {name : "后台配置版本"};
		var localCache = this;
		var curVersion = this.GetCacheResult(CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
				pdata,"");
		$.ajax({
			type : "POST",
			url : CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
			headers : {
				"token" : $.cookie('ftoken')
			},
			dataType : "json",
			async : true,
			data : pdata,
			success : function(data) {
				var curVersionStr = JSON.stringify(curVersion);
				var dataStr = JSON.stringify(data);
				
				if(dataStr != curVersionStr)
				{
					localCache.clearLocalCache();
					localCache.AddCache(CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
							pdata,"",data);
				}
			},
			error : function(data, textStatus, errorThrown) {
				
			}
		});
	}
}
var globalLocalCache = null;

$(function(){
	globalLocalCache = new GlobalLocalCache();
});