一、在Model層中創建購物車只的每一項類ShopItem.cs
public class ShopItem
{
private int _proid;//商品id
public int Proid
{
get { return _proid; }
set { _proid = value; }
}
private decimal _price;//商品單價
public decimal Price
{
get { return _price; }
set { _price = value; }
}
private int _quantity;//商品數量
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
}
二、在Model層中創建購物車類ShopCatr.cs
public class ShopCatr
{
private Hashtable _sc = new Hashtable();
/// <summary>向購物車中添加商品
///
/// </summary>
/// <param name="proid">商品的id</param>
/// <param name="item">購物車</param>
public void Add(int proid, ShopItem item)
{
if (_sc[proid] == null)
{
_sc.Add(proid, item);
}
else
{
//已存在該商品
ShopItem si = _sc[proid] as ShopItem;
si.Quantity += 1;
_sc[proid] = si;
}
}
/// <summary>刪除購物車中的商品
///
/// </summary>
/// <param name="proid">商品的id</param>
public void Del(int proid)
{
if (_sc[proid] != null)
{
_sc.Remove(proid);
}
}
/// <summary>修改購物車中商品的數量
///
/// </summary>
/// <param name="proid">商品ID</param>
/// <param name="quantity">商品數量</param>
public void Mod(int proid, int quantity)
{
if (_sc[proid] != null)
{
if (quantity > 0)
{
ShopItem si = _sc[proid] as ShopItem;
si.Quantity = quantity;
_sc[proid] = si;
}
}
}
/// <summary>獲取購物車中商品的種類數
///
/// </summary>
/// <returns></returns>
public int GetItemCount()
{
return _sc.Count;
}
/// <summary>獲取購物車中商品的總數量
///
/// </summary>
/// <returns></returns>
public int GetItemTotalCount()
{
int total = 0;
foreach (ShopItem item in _sc.Values)
{
total += item.Quantity;
}
return total;
}
/// <summary>獲取購物車中項的內容用于綁定數據控件
///
/// </summary>
/// <returns></returns>
public ICollection GetItemList()
{
return _sc.Values;
}
/// <summary>獲取購物車的總價
///
/// </summary>
/// <returns></returns>
public decimal GetTotalPrice()
{
decimal total = 0;
foreach (ShopItem item in _sc.Values)
{
total += item.Price * item.Quantity;
}
return total;
}
}
加入收藏
protected void lbtnFav_Click(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
Utility.Tool.Alert("請先登錄", this.Page);
return;
}
string proid = (sender as LinkButton).CommandArgument; //該商品ID
DAL.FavoriteDAL fdao = new DAL.FavoriteDAL();
int y = fdao.CalcCount("username='" + User.Identity.Name + "' and proid=" + proid);
if (y != 0)
{
Utility.Tool.Alert("該商品已收藏過了!", this.Page);
return;
}
int x = fdao.CalcCount("username='" + User.Identity.Name + "'");
if (x == 10)
{
Utility.Tool.Alert("你已經添加了十種商品", this.Page);
return;
}
fdao.Add(new Model.Favorite()
{
proid = int.Parse(proid),
username = User.Identity.Name
});
Utility.Tool.Alert("收藏成功", this.Page);
}
三、加入購物車
if (!User.Identity.IsAuthenticated)
{
Utility.Tool.Alert("請先登錄再購買", this.Page);
return;
}
string proid = Request.QueryString["id"];
Model.Product pro = new DAL.ProductDAL().GetModel(int.Parse(proid));
Model.User user = new DAL.UserDAL().GetModel(User.Identity.Name);
if (pro != null && user != null)
{
if (user.isuse.Length != 0)
{
Utility.Tool.Alert("請先激活帳號再購買", this.Page);
return;
}
decimal price = 0;
if (user.type == "vip")
{
price = pro.vipprice;
}
else
{
price = pro.memberprice;
}
if (Session["shopcart"] == null)
{
Session["shopcart"] = new Model.ShopCatr();
}
Model.ShopCatr sc = Session["shopcart"] as Model.ShopCatr;
sc.Add(int.Parse(proid), new Model.ShopItem()
{
Quantity = 1,
Price = price,
Proid = int.Parse(proid)
});
Utility.Tool.ExecJs("if(confirm('商品添加成功是否跳轉到購物車頁面?')){location.href='shopcart.aspx'}else{location.href='" + Request.Url.ToString() + "'}", this.Page);
前臺顯示
if (Session["shopcart"] != null)
{
Model.ShopCatr sc = Session["shopcart"] as Model.ShopCatr;
if (sc.GetItemCount() > 0)
{
hlGWC.Text = "<a href='/shopcart.aspx'>購物車【<span style='font-weight:bold;color:red'>" + sc.GetItemCount() + "</span>】</a>";
}
}
//收藏頁面中的商品批量加入購物車
foreach (RepeaterItem item in rep.Items)
{
//在rep中加上復選框前臺:
<asp:CheckBox ID="chk" ToolTip='<%#Eval("proid") %>' runat="server" />
CheckBox chk = item.FindControl("chk") as CheckBox;
if (chk.Checked)
{
string proid = chk.ToolTip;
Model.Product pro = new DAL.ProductDAL().GetModel(int.Parse(proid));
其它和上面一樣
四、激活用戶
1、在用戶數據表中加上isuse字段
2、注冊時string isuse=Guid.NewGuid().Tosring();
3、在UserDAL.cs中加上通過isuse獲取實體類的方法
在彈出成功之前加上:
//發送激活郵件
string url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/jh_user.aspx?str=" + isuse;
Utility.Tool.SendMail("請激活您在購物網注冊的帳戶", "點擊以下鏈接即可激活:<a href='" + url + "' target='_blank'>" + url + "</a>", email, "181796286@qq.com", "181796286@qq.com", "123456", "smtp.qq.com");
HttpCookie cook;
string roles = "user"; //用戶角色
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, name, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles);
cook = new HttpCookie("mycook");
cook.Value = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
Response.Redirect("user/index.aspx");
創建jh_user.aspx文件
string str = Request.QueryString["str"];
if (!string.IsNullOrEmpty(str))
{
Model.User model = new DAL.UserDAO().GetModelByIsuse(str); //GetModelByIsuse根據isuse生成Model
if (model!=null)
{
model.isuse = "";
new DAL.UserDAO().Update(model);
litmes.Text = "您的賬戶已激活,可以進行購物了。";
//用戶登錄
HttpCookie cook;
string roles = "user"; // 用戶角色
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, model.username, DateTime.Now, DateTime.Now.AddMinutes(30), true, roles);
cook = new HttpCookie("mycook");
cook.Value = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
}
else
{
litmes.Text = "沒有這個賬戶";
}
五、在user/index.aspx中加上激活用戶
Page_Load中加上
Model.User u = new DAL.UserDAO().GetModel(User.Identity.Name);
if (u != null)
{
if (u.isuse.Length != 0)
{
//未激活
pnlJH.Visible = true;
}
else
{
//已激活
pnlJH.Visible = false;
}
}
單擊激活
Model.User u = new DAL.UserDAO().GetModel(User.Identity.Name);
if (u != null)
{
string isuse = u.isuse;
string email = u.email;
//發送激活郵件
string url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/jh_user.aspx?str=" + isuse;
Utility.Tool.SendMail("請激活您在購物網的賬戶", "點擊以下鏈接即可激活賬戶: <a href='" + url + "' target='_blank'>" + url + "</a>", email, "181796286@qq.net", " 181796286@qq.net ", "123456", "smtp.qq.com");
Utility.Tool.Alert("激活郵件已經發送成功。",this.Page);
}
//前臺搜索商品
//搜索
protected void btnSo_Click(object sender, EventArgs e)
{
string key = txtkey.Text.Trim();
if (key.Length == 0)
{
Utility.Tool.Alert("請輸入關鍵字", this.Page);
return;
}
key = Server.UrlEncode(key); //編碼
string url = "prolist.aspx?key=" + key;
Response.Redirect(url);
}
//獲取條件 Prolist.aspx頁面
private string GetCond()
{
string cond = "isdel=0";
string key = Request.QueryString["key"];
if (!string.IsNullOrEmpty(key))
{
key = Server.UrlDecode(key); //轉碼
key = Utility.Tool.GetSafeSQL(key); //過濾字符
cond += " and proname like '%" + key + "%' ";
}
return cond;
}