using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using System.Web.Script.Services;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
private readonly List<Product> _products = new List<Product>
{
new Product {
Id = 1,
Name = "专业减震跑步鞋",
Category = "运动鞋",
Price = 599,
ImageUrl = "image/专业减震跑步鞋.jpg",
Brand = "耐克",
Description = "专业级减震科技,适合长距离跑步"
},
new Product {
Id = 2,
Name = "专业篮球鞋",
Category = "运动鞋",
Price = 699,
ImageUrl = "image/专业篮球鞋.jpg",
Brand = "阿迪达斯",
Description = "高帮支撑设计,保护脚踝"
},
new Product { Id = 3, Name = "多功能运动背包", Category = "运动装备", Price = 299, ImageUrl = "image/多功能运动背包.jpg", Brand = "彪马" },
new Product { Id = 4, Name = "专业运动智能手表", Category = "运动配件", Price = 799, ImageUrl = "image/专业运动智能手表.jpg", Brand = "Apple" },
new Product { Id = 5, Name = "速干透气运动T恤", Category = "运动服饰", Price = 199, ImageUrl = "image/速干透气运动T恤.jpg", Brand = "耐克" },
new Product { Id = 6, Name = "弹性运动短裤", Category = "运动服饰", Price = 159, ImageUrl = "image/弹性运动短裤.jpg", Brand = "阿迪达斯" }
};
// 用户实体类
public class User
{
}
// 商品实体类
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public string ImageUrl { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
}
public class CartItem
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public string ImageUrl { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 记录用户浏览记录(示例)
if (Session["UserID"] != null)
{
int userId = Convert.ToInt32(Session["UserID"]);
LogUserBrowse(userId, 1); // 假设浏览了商品ID=1
}
}
}
private List<Product> GetRecommendedProducts()
{
List<Product> products = new List<Product>();
if (Session["UserID"] == null) return products;
int userId = Convert.ToInt32(Session["UserID"]);
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; // 读取配置
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = @"SELECT p.*, c.CategoryName, b.BrandName
FROM tb_Product p
JOIN tb_Category c ON p.CategoryID = c.CategoryID
JOIN tb_Brand b ON p.BrandID = b.BrandID
WHERE p.CategoryID IN (
SELECT DISTINCT CategoryID
FROM tb_Product
WHERE ProductID IN (
SELECT ProductID FROM tb_UserBrowse WHERE UserID=@UserID
)
) OR p.BrandID IN (
SELECT DISTINCT BrandID
FROM tb_Product
WHERE ProductID IN (
SELECT ProductID FROM tb_UserBrowse WHERE UserID=@UserID
)
)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@UserID", userId);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// 填充 Product 对象...
}
}
}
}
return products;
}
private void LogUserBrowse(int userId, int productId)
{
}
}
}
根据已有的前后端代码更新改善后端代码,给出完整的后端代码
最新发布