基于Cookie的购物车

var Cookies = {};
Cookies.set = function (name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : "");
};

Cookies.get = function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
};

Cookies.clear = function (name) {
    if (Cookies.get(name)) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
        Cookies.set(name, "", expdate);
    }
};

Cookies.getCookieVal = function (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};

var Global = {};
Global.getParam = function (param) {
    var params = Ext.urlDecode(location.search.substring(1));
    return param ? params[param] : params;
}
//购物车
var ShoppingCart = {};
//新增商品
ShoppingCart.add = function (cid, did, price) {
    var ck = Global.getParam("sid") + "_" + Global.getParam("uid") + "_sc";
    if (Cookies.get(ck) == null) {
        var exp = new Date();
        exp.setTime(exp.getTime() + 7 * 24 * 60 * 60 * 1000);
        Cookies.set(ck, JSON.stringify({ TotalCount: 0, TotalMoney: 0 }), exp);
    }
    var cart = eval('(' + Cookies.get(ck) + ')');
    //类别
    if (cart[cid] == undefined) {
        cart[cid] = { num: 0 };
    }
    //商品
    if (cart[did] == undefined) {
        cart[did] = { cid: cid, num: 0 };
    }
    //计算
    if (cart[did].num < 9) {
        cart[did].num = cart[did].num + 1;
        //类别(方便查找)
        cart[cid].num = cart[cid].num + 1;
        //总数量,总金额
        cart["TotalCount"] = cart["TotalCount"] + 1;
        cart["TotalMoney"] = cart["TotalMoney"] + price;
    }

    console.log(cart);
    Cookies.set(ck, JSON.stringify(cart));
};
//移除商品
ShoppingCart.remove = function (cid, did, price) {
    var ck = Global.getParam("sid") + "_" + Global.getParam("uid") + "_sc";
    var cart = eval('(' + Cookies.get(ck) + ')');
    if (cart[did] != undefined && cart[did].num > 0) {
        cart[did].num = cart[did].num - 1;
        cart["TotalCount"] = cart["TotalCount"] - 1;
        cart["TotalMoney"] = cart["TotalMoney"] - price;
        if (cart[cid] != undefined) {
            cart[cid].num = cart[cid].num - 1;
        }
    }
    Cookies.set(ck, JSON.stringify(cart));
};
//清空购物车
ShoppingCart.clear = function () {
    var ck = Global.getParam("sid") + "_" + Global.getParam("uid") + "_sc";
    Cookies.clear(ck);
};
//获取某商品的数量
ShoppingCart.getGoodNum = function (did) {
    var ck = Global.getParam("sid") + "_" + Global.getParam("uid") + "_sc";
    if (Cookies.get(ck) == null)
        return 0;
    var cart = eval('(' + Cookies.get(ck) + ')');
    if (cart[did] == undefined)
        return 0;
    else
        return cart[did].num;
};
//显示购物车的总数量和价格
ShoppingCart.displayTotal = function () {
    var ck = Global.getParam("sid") + "_" + Global.getParam("uid") + "_sc";
    if (Cookies.get(ck) == null)
        return "数量:0,金额:¥0";
    var cart = eval('(' + Cookies.get(ck) + ')');
    return "数量:" + cart["TotalCount"] + ",金额:¥" + cart["TotalMoney"];
};

 

posted @ 2014-01-18 15:22  jackchain  阅读(433)  评论(0编辑  收藏  举报