·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]klstudio写的一个as3的Cookie类源代码

时间:2015-01-26 10:11酷播
[AS3]klstudio写的一个as3的Cookie类源代码

[AS3]klstudio写的一个as3的Cookie类源代码

  1. 代码 
  2.  
  3. package com.klstudio.util 
  4. { 
  5.     import flash.net.SharedObject; 
  6.     public class Cookie 
  7.     { 
  8.         private var _time:uint; 
  9.         private var _name:String; 
  10.         private var _so:SharedObject; 
  11.  
  12.         public function Cookie(name:String = "klstudio", timeOut:uint = 3600) 
  13.         { 
  14.             _name = name; 
  15.             _time = timeOut; 
  16.             _so = SharedObject.getLocal(name, "/"); 
  17.         } 
  18.         //清楚超时内容;    
  19.         public function clearTimeOut():void 
  20.         { 
  21.             var obj:* = _so.data.cookie; 
  22.             if (obj == undefined) 
  23.             { 
  24.                 return; 
  25.             } 
  26.             for (var key in obj) 
  27.             { 
  28.                 if (obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)) 
  29.                 { 
  30.                     delete obj[key]; 
  31.                 } 
  32.             } 
  33.             _so.data.cookie = obj; 
  34.             _so.flush(); 
  35.         } 
  36.  
  37.         private function isTimeOut(time:uint):Boolean 
  38.         { 
  39.             var today:Date = new Date(); 
  40.             return time + _time * 1000 < today.getTime(); 
  41.         } 
  42.  
  43.         //获取超时值;    
  44.         public function getTimeOut():uint 
  45.         { 
  46.             return _time; 
  47.         } 
  48.  
  49.         //获取名称;    
  50.         public function getName():String 
  51.         { 
  52.             return _name; 
  53.         } 
  54.  
  55.         //清除Cookie所有值;    
  56.         public function clear():void 
  57.         { 
  58.             _so.clear(); 
  59.         } 
  60.  
  61.         //添加Cookie值    
  62.         public function put(key:String, value:*):void 
  63.         { 
  64.             var today:Date = new Date(); 
  65.             key = "key_" + key; 
  66.             value.time = today.getTime(); 
  67.             if (_so.data.cookie == undefined) 
  68.             { 
  69.                 var obj:Object = {}; 
  70.                 obj[key] = value; 
  71.                 _so.data.cookie = obj; 
  72.             } 
  73.             else 
  74.             { 
  75.                 _so.data.cookie[key] = value; 
  76.             } 
  77.             _so.flush(); 
  78.         } 
  79.         //删除Cookie值;    
  80.         public function remove(key:String):void 
  81.         { 
  82.             if (contains(key)) 
  83.             { 
  84.                 delete _so.data.cookie["key_" + key]; 
  85.                 _so.flush(); 
  86.             } 
  87.         } 
  88.         //获取Cookie值;    
  89.         public function get(key:String):Object 
  90.         { 
  91.             return contains(key) ? _so.data.cookie["key_" + key] : null; 
  92.         } 
  93.         //Cookie值是否存在;    
  94.         public function contains(key:String):Boolean 
  95.         { 
  96.             key = "key_" + key; 
  97.             return _so.data.cookie != undefined && _so.data.cookie[key] != undefined; 
  98.         } 
  99.     } 
  100. } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器