• Mvc4.0添加商品到Cookie


    /// <summary>
    /// 添加Cookie
    /// </summary>
    /// <param name="gc">GoodsToCookie</param>
    public static int AddCookies(GoodsCookie gs)
    {
    string userID = null;
    if (HttpContext.Current.Session["hasLogin"] != null)
    {
    userID = HttpContext.Current.Session["hasLogin"].ToString();//登录
    }
    ShoppingMallContext db = new ShoppingMallContext();
    var cookies = HttpContext.Current.Request.Cookies["MyShoppingCart"]; //创建Cookie并命名
    if (cookies == null)
    {
    string strCookie = gs.skuID + "-" + gs.count + ",";
    cookies = new HttpCookie("MyShoppingCart") //对象初始化
    {
    Expires = DateTime.Today.AddDays(1),//过期时间
    Value = strCookie//Cookie赋值
    };
    
    HttpContext.Current.Response.Cookies.Add(cookies);//添加进Cookie
    
    if (userID != null)//如果用户登录了
    {
    SkuShoppingCart sc = new SkuShoppingCart();//初始化购物车类
    sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值
    sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID
    .Where(f => f.UserID == userID)
    .Select(f => f.ShoppingCartID)
    .Single();
    sc.SkuCount = gs.Count;//统计数量
    db.SkuShoppingCarts.Add(sc);//添加进数据库
    }
    return 1;
    }
    else
    {
    
    if (String.IsNullOrWhiteSpace(cookies.Value))//判断Cookies里值为不为空
    {
    string strCookie = gs.skuID + "-" + gs.count + ",";
    cookies = new HttpCookie("MyShoppingCart") //对象初始化
    {
    Expires = DateTime.Today.AddDays(1),
    Value = strCookie
    };
    HttpContext.Current.Response.Cookies.Add(cookies);//添加进Cookie
    if(userID != null)
    {
    SkuShoppingCart sc = new SkuShoppingCart();
    sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值
    sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID
    .Where(f => f.UserID == userID)
    .Select(f => f.ShoppingCartID)
    .Single();
    sc.SkuCount = gs.Count;//统计数量
    db.SkuShoppingCarts.Add(sc);//添加进数据库
    }
    return 1;
    }
    else//返之
    {
    #region -----添加到字典-----
    
    Dictionary<int, int> dict = new Dictionary<int, int>();//初始化字典
    string goods = cookies.Value;//声明一个string变量并赋值 为 Cookie
    goods = goods.Substring(0, goods.Length - 1);//指定字符串长度
    string[] skuidArray = goods.Split(',');//拆解字符串并赋值给skuidArray数组
    int count = 0;//声明一个Int变量
    foreach (var i in skuidArray)//迭代数组
    {
    if(i =="" || i == null)//如果没有值
    {
    continue;//继续
    }
    else//如果有
    {
    string[] strCont = i.Split('-');//拆解字符串
    int key = int.Parse(strCont[0]);//第一个
    int value = int.Parse(strCont[1]);//第二个
    //判断字典里有没有这个key
    if (dict.ContainsKey(key))
    {
    dict[key] += value;//如果有就叠加
    }
    else//没有
    {
    dict.Add(key, value);//添加进字典
    }
    }
    
    }
    #endregion
    
    #region ------- 字典判断 -------
    
    if (dict.ContainsKey(gs.skuID))//如果字典里有这个ID
    {
    dict[gs.skuID] += gs.count;
    count += skuidArray.Count();//就让他们的数量相加
    }
    else
    {
    dict.Add(gs.skuID, gs.count);
    count = skuidArray.Count() + 1;//如果没有,就往字典(dict)添加一个新的
    }
    
    string countStr = "";
    foreach (var v in dict)//再迭代字典拼字符串
    {
    countStr += v.Key + "-" + v.Value + ",";//键值对
    }
    cookies = new HttpCookie(countStr)
    {
    Expires = DateTime.Today.AddDays(1),
    Value = countStr
    };//拼好了添加到Cookie
    
    HttpContext.Current.Request.Cookies.Add(cookies);
    if (userID != null)//如果用户登录了
    {
    SkuShoppingCart sc = new SkuShoppingCart();//初始化购物车类
    sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值
    sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID
    .Where(f => f.UserID == userID)
    .Select(f => f.ShoppingCartID)
    .Single();
    sc.SkuCount = gs.Count;//统计数量
    db.SkuShoppingCarts.Add(sc);//添加进数据库
    }
    return count;//数量
    
    #endregion
    
    }
    }
    }
  • 相关阅读:
    noi2002银河英雄传说(并查集)
    Ural1076(km算法)
    km算法的个人理解
    函数之装饰器
    函数进阶(一)
    python全栈测试题(一)
    python基础之循环语句
    字符串方法总结
    python基础3
    python基础2
  • 原文地址:https://www.cnblogs.com/LindaLiu/p/3637816.html
Copyright © 2020-2023  润新知