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

 

热门文章推荐

请稍候...

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

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