项目准备用而没有用到的代码,留存备用
/* 购物车相关逻辑 */
function GetMallUserInfo() {
var mui = $.cookie("MallUserInfo");
return mui ? JSON.parse(mui) : null;
}
function SetMallUserInfo(mui) {
$.cookie('MallUserInfo', null, { path: '/Mall' });
$.cookie("MallUserInfo", JSON.stringify(mui), { expires: MallCookieExpires, path: '/Mall' });
}
function GetSCart() {
var mui = GetMallUserInfo();
return mui ? mui.S_Cart : null;
}
function GetSCartNum() {
var dt = GetSCart();
if (dt && Object.prototype.toString.call(dt) === '[object Array]' && dt.length > 0) {
return dt.length;
} else {
return 0;
}
}
function SetSCart(dt) {
var mui = GetMallUserInfo();
mui.S_Cart = dt;
SetMallUserInfo(mui);
}
function addSCart(id, num) {
if (!id) return;
num = num || 1;
var dt = GetSCart();
if (dt) {
var isExist = false;
if (Object.prototype.toString.call(dt) === '[object Array]' && dt.length > 0) {
for (var i in dt) {
if (dt[i].id == id) {
dt[i].Num = num;
isExist = true;
}
}
if (!isExist) {
clearSCart();
dt.push({ "id": id, "Num": num });
}
} else {
dt = [{ "id": id, "Num": num }];
}
} else {
dt = [{ "id": id, "Num": num }];
}
SetSCart(dt);
return dt;
}
function delSCart(id) {
if (!id) return;
var dt = GetSCart();
if (dt) {
if (Object.prototype.toString.call(dt) === '[object Array]' && dt.length > 0) {
var idx = -1;
for (var i in dt) {
idx = dt[i].id == id ? i : -1;
}
if (idx > -1) {
dt.splice(i, 1);
SetSCart(dt);
}
}
}
}
function clearSCart() {
SetSCart(null);
}
function DetailSCart() {
location.href = "/Mall/SCart";
}
/* 用户信息cookie处理 */
//写入cookies
function setCookie(name, value) {
var exp = new Date();
exp.setTime(exp.getTime() + 1800 * 1000);//1800秒,30分钟
document.cookie = name + "=" + escape(JSON.stringify(value)) + ";expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return JSON.parse(unescape(arr[2]));
else
return null;
}
//删除cookies
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}