以下ZPVisitor类的定义格式在实际企业项目中经常被使用:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
  
  //获取随机数
 function getRand(min,max){
       return   Math.round(Math.random() * (max-min)) + min;
   }

  console.log(getRand(3,9));

   var ZPVisitor = {
         init : function(){
           var url = ZPConstant.AJAX_SERVER + "&a=hello" + "&sid=" + Math.random();
           $.getJSON(url,function(result){ // #TODO 跨域请求?
 if (result.done) {
               var logininfo = result.data.name + "&nbsp;您好!";
               if (!result.data.login) {
                 logininfo += "&nbsp;<a href=\"" + ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=login\">登录</a>/<a href=\"" +
                   ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=regist\">注册</a>";
               } else {
                 logininfo += "&nbsp;<a href=\"" + ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=logout\">注销</a>";
               }
               $("#login-info").html(logininfo);

             } else {
               popAlert(result.msg);
             }
           });                                                                                         //结束getJSON
 },                                                                                                         //结束init方法

         // 收藏商品
 collect : function(pid) {
           var pid = parseInt(pid);
           if (isNaN(pid)) return false;
           var url = ZPConstant.AJAX_SERVER + "&a=collect&pid=" + pid + "&sid=" + Math.random();
           $.getJSON(url,function(result){
             if(result.done){
               popAlert(result.msg);
             } else {
               popAlert(result.msg);
             }
           });//结束getJSON方法

 },// 结束collect方法

         // 浏览历史
 vHistory :{
           get:function(callback){
             var items_str = getCookie(ZPConstant.VIEW_HISTORY_CKNAME);
             if (items_str == null) return;
             var items = items_str.split(",").reverse().join(","),
               url = ZPConstant.AJAX_SERVER + "&a=viewHistory" + "&items="+ items + "&sid=" + Math.random();

             // TODO 向服务端请求数据
 $.getJSON(url,function(response){
               if(response.done){
                 callback(response.data,items);
               } else {
                 return false;
               }
             });//结束getJSON方法
 },//结束get方法

 add:function(pid){
             var pid = parseInt(pid);
             if (isNaN(pid)) return;
             var cookieStr = getCookie(ZPConstant.VIEW_HISTORY_CKNAME);
             if (cookieStr) {
               var pids = cookieStr.split(","),_tempPids = [];
               for (var i=0;i<pids.length;i++){
                 if (pids[i] != pid) { // 如果已经存在,则移除之前写入的数据
 _tempPids.push(parseInt(pids[i]));
                 }
               }
               _tempPids.push(pid);// 将新数据放到数组最后
 setCookie(ZPConstant.VIEW_HISTORY_CKNAME, _tempPids.reverse().slice(0,5).reverse().join(","));
             } else {
               setCookie(ZPConstant.VIEW_HISTORY_CKNAME, pid);
             }
           },// 结束add方法

 clear:function(){
             setCookie(ZPConstant.VIEW_HISTORY_CKNAME, '', -100);
           }
         },//结束vHistory


         // 购物车已删除的商品记录
 dropHistory: function(spid){
           var cookieStr = getCookie(ZPConstant.CARTDEL_CKNAME);
           if (cookieStr){
             var idsDel = cookieStr.split(",");
             for (var i=0;i<idsDel.length;i++){
               idsDel[i] = parseInt(idsDel[i]);
             }
             if ($.inArray(spid,idsDel) == -1){
               idsDel.push(spid);
               setCookie(ZPConstant.CARTDEL_CKNAME, idsDel.join(","));
             }
           } else {
             setCookie(ZPConstant.CARTDEL_CKNAME, spid);
           }
         }//结束dropHistory
 }//结束ZPVisitor


</script>

</body>
</html>


你可能感兴趣的:(以下ZPVisitor类的定义格式在实际企业项目中经常被使用:)