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

[AS3]as3.0添加Cookie类来记录用户的临时Cookie信息

时间:2013-11-25 16:31cuplayer.com
[AS3]as3.0添加Cookie类来记录用户的临时Cookie信息

AS3]as3.0添加Cookie类来记录用户的临时Cookie信息

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

 

热门文章推荐

请稍候...

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

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