步骤 2 : 模仿和排错 步骤 3 : 购物车页面操作 步骤 4 : 调整订单数量 步骤 5 : 删除订单项 步骤 6 : 提交到结算页面
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
var url = "forechangeOrderItem?pid="+pid+"&num="+num;
axios.get(url).then(function(response){
if(0!=response.data.code){
location.href="login";
}
});
@GetMapping("forechangeOrderItem")
public Object changeOrderItem( HttpSession session, int pid, int num) {
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
List<OrderItem> ois = orderItemService.listByUser(user);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==pid){
oi.setNumber(num);
orderItemService.update(oi);
break;
}
}
return Result.success();
}
package com.how2java.tmall.web;
import com.how2java.tmall.comparator.*;
import com.how2java.tmall.pojo.*;
import com.how2java.tmall.service.*;
import com.how2java.tmall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;
import javax.servlet.http.HttpSession;
import java.util.*;
@RestController
public class ForeRESTController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
ProductImageService productImageService;
@Autowired
PropertyValueService propertyValueService;
@Autowired
OrderItemService orderItemService;
@Autowired
ReviewService reviewService;
@GetMapping("/forehome")
public Object home() {
List<Category> cs= categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
categoryService.removeCategoryFromProduct(cs);
return cs;
}
@PostMapping("/foreregister")
public Object register(@RequestBody User user) {
String name = user.getName();
String password = user.getPassword();
name = HtmlUtils.htmlEscape(name);
user.setName(name);
boolean exist = userService.isExist(name);
if(exist){
String message ="用户名已经被使用,不能使用";
return Result.fail(message);
}
user.setPassword(password);
userService.add(user);
return Result.success();
}
@PostMapping("/forelogin")
public Object login(@RequestBody User userParam, HttpSession session) {
String name = userParam.getName();
name = HtmlUtils.htmlEscape(name);
User user =userService.get(name,userParam.getPassword());
if(null==user){
String message ="账号密码错误";
return Result.fail(message);
}
else{
session.setAttribute("user", user);
return Result.success();
}
}
@GetMapping("/foreproduct/{pid}")
public Object product(@PathVariable("pid") int pid) {
Product product = productService.get(pid);
List<ProductImage> productSingleImages = productImageService.listSingleProductImages(product);
List<ProductImage> productDetailImages = productImageService.listDetailProductImages(product);
product.setProductSingleImages(productSingleImages);
product.setProductDetailImages(productDetailImages);
List<PropertyValue> pvs = propertyValueService.list(product);
List<Review> reviews = reviewService.list(product);
productService.setSaleAndReviewNumber(product);
productImageService.setFirstProdutImage(product);
Map<String,Object> map= new HashMap<>();
map.put("product", product);
map.put("pvs", pvs);
map.put("reviews", reviews);
return Result.success(map);
}
@GetMapping("forecheckLogin")
public Object checkLogin( HttpSession session) {
User user =(User) session.getAttribute("user");
if(null!=user)
return Result.success();
return Result.fail("未登录");
}
@GetMapping("forecategory/{cid}")
public Object category(@PathVariable int cid,String sort) {
Category c = categoryService.get(cid);
productService.fill(c);
productService.setSaleAndReviewNumber(c.getProducts());
categoryService.removeCategoryFromProduct(c);
if(null!=sort){
switch(sort){
case "review":
Collections.sort(c.getProducts(),new ProductReviewComparator());
break;
case "date" :
Collections.sort(c.getProducts(),new ProductDateComparator());
break;
case "saleCount" :
Collections.sort(c.getProducts(),new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(),new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(),new ProductAllComparator());
break;
}
}
return c;
}
@PostMapping("foresearch")
public Object search( String keyword){
if(null==keyword)
keyword = "";
List<Product> ps= productService.search(keyword,0,20);
productImageService.setFirstProdutImages(ps);
productService.setSaleAndReviewNumber(ps);
return ps;
}
@GetMapping("forebuyone")
public Object buyone(int pid, int num, HttpSession session) {
return buyoneAndAddCart(pid,num,session);
}
private int buyoneAndAddCart(int pid, int num, HttpSession session) {
Product product = productService.get(pid);
int oiid = 0;
User user =(User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==product.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setProduct(product);
oi.setNumber(num);
orderItemService.add(oi);
oiid = oi.getId();
}
return oiid;
}
@GetMapping("forebuy")
public Object buy(String[] oiid,HttpSession session){
List<OrderItem> orderItems = new ArrayList<>();
float total = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi= orderItemService.get(id);
total +=oi.getProduct().getPromotePrice()*oi.getNumber();
orderItems.add(oi);
}
productImageService.setFirstProdutImagesOnOrderItems(orderItems);
session.setAttribute("ois", orderItems);
Map<String,Object> map = new HashMap<>();
map.put("orderItems", orderItems);
map.put("total", total);
return Result.success(map);
}
@GetMapping("foreaddCart")
public Object addCart(int pid, int num, HttpSession session) {
buyoneAndAddCart(pid,num,session);
return Result.success();
}
@GetMapping("forecart")
public Object cart(HttpSession session) {
User user =(User) session.getAttribute("user");
List<OrderItem> ois = orderItemService.listByUser(user);
productImageService.setFirstProdutImagesOnOrderItems(ois);
return ois;
}
@GetMapping("forechangeOrderItem")
public Object changeOrderItem( HttpSession session, int pid, int num) {
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
List<OrderItem> ois = orderItemService.listByUser(user);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==pid){
oi.setNumber(num);
orderItemService.update(oi);
break;
}
}
return Result.success();
}
@GetMapping("foredeleteOrderItem")
public Object deleteOrderItem(HttpSession session,int oiid){
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
orderItemService.delete(oiid);
return Result.success();
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
var url = "foredeleteOrderItem?oiid="+deleteOrderItemid;
axios.get(url).then(function(response){
if(0==response.data.code){
$("tr.cartProductItemTR[oiid="+deleteOrderItemid+"]").hide();
}
else{
location.href="login";
}
});
@GetMapping("foredeleteOrderItem")
public Object deleteOrderItem(HttpSession session,int oiid){
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
orderItemService.delete(oiid);
return Result.success();
}
package com.how2java.tmall.web;
import com.how2java.tmall.comparator.*;
import com.how2java.tmall.pojo.*;
import com.how2java.tmall.service.*;
import com.how2java.tmall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;
import javax.servlet.http.HttpSession;
import java.util.*;
@RestController
public class ForeRESTController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
ProductImageService productImageService;
@Autowired
PropertyValueService propertyValueService;
@Autowired
OrderItemService orderItemService;
@Autowired
ReviewService reviewService;
@GetMapping("/forehome")
public Object home() {
List<Category> cs= categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
categoryService.removeCategoryFromProduct(cs);
return cs;
}
@PostMapping("/foreregister")
public Object register(@RequestBody User user) {
String name = user.getName();
String password = user.getPassword();
name = HtmlUtils.htmlEscape(name);
user.setName(name);
boolean exist = userService.isExist(name);
if(exist){
String message ="用户名已经被使用,不能使用";
return Result.fail(message);
}
user.setPassword(password);
userService.add(user);
return Result.success();
}
@PostMapping("/forelogin")
public Object login(@RequestBody User userParam, HttpSession session) {
String name = userParam.getName();
name = HtmlUtils.htmlEscape(name);
User user =userService.get(name,userParam.getPassword());
if(null==user){
String message ="账号密码错误";
return Result.fail(message);
}
else{
session.setAttribute("user", user);
return Result.success();
}
}
@GetMapping("/foreproduct/{pid}")
public Object product(@PathVariable("pid") int pid) {
Product product = productService.get(pid);
List<ProductImage> productSingleImages = productImageService.listSingleProductImages(product);
List<ProductImage> productDetailImages = productImageService.listDetailProductImages(product);
product.setProductSingleImages(productSingleImages);
product.setProductDetailImages(productDetailImages);
List<PropertyValue> pvs = propertyValueService.list(product);
List<Review> reviews = reviewService.list(product);
productService.setSaleAndReviewNumber(product);
productImageService.setFirstProdutImage(product);
Map<String,Object> map= new HashMap<>();
map.put("product", product);
map.put("pvs", pvs);
map.put("reviews", reviews);
return Result.success(map);
}
@GetMapping("forecheckLogin")
public Object checkLogin( HttpSession session) {
User user =(User) session.getAttribute("user");
if(null!=user)
return Result.success();
return Result.fail("未登录");
}
@GetMapping("forecategory/{cid}")
public Object category(@PathVariable int cid,String sort) {
Category c = categoryService.get(cid);
productService.fill(c);
productService.setSaleAndReviewNumber(c.getProducts());
categoryService.removeCategoryFromProduct(c);
if(null!=sort){
switch(sort){
case "review":
Collections.sort(c.getProducts(),new ProductReviewComparator());
break;
case "date" :
Collections.sort(c.getProducts(),new ProductDateComparator());
break;
case "saleCount" :
Collections.sort(c.getProducts(),new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(),new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(),new ProductAllComparator());
break;
}
}
return c;
}
@PostMapping("foresearch")
public Object search( String keyword){
if(null==keyword)
keyword = "";
List<Product> ps= productService.search(keyword,0,20);
productImageService.setFirstProdutImages(ps);
productService.setSaleAndReviewNumber(ps);
return ps;
}
@GetMapping("forebuyone")
public Object buyone(int pid, int num, HttpSession session) {
return buyoneAndAddCart(pid,num,session);
}
private int buyoneAndAddCart(int pid, int num, HttpSession session) {
Product product = productService.get(pid);
int oiid = 0;
User user =(User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==product.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setProduct(product);
oi.setNumber(num);
orderItemService.add(oi);
oiid = oi.getId();
}
return oiid;
}
@GetMapping("forebuy")
public Object buy(String[] oiid,HttpSession session){
List<OrderItem> orderItems = new ArrayList<>();
float total = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi= orderItemService.get(id);
total +=oi.getProduct().getPromotePrice()*oi.getNumber();
orderItems.add(oi);
}
productImageService.setFirstProdutImagesOnOrderItems(orderItems);
session.setAttribute("ois", orderItems);
Map<String,Object> map = new HashMap<>();
map.put("orderItems", orderItems);
map.put("total", total);
return Result.success(map);
}
@GetMapping("foreaddCart")
public Object addCart(int pid, int num, HttpSession session) {
buyoneAndAddCart(pid,num,session);
return Result.success();
}
@GetMapping("forecart")
public Object cart(HttpSession session) {
User user =(User) session.getAttribute("user");
List<OrderItem> ois = orderItemService.listByUser(user);
productImageService.setFirstProdutImagesOnOrderItems(ois);
return ois;
}
@GetMapping("forechangeOrderItem")
public Object changeOrderItem( HttpSession session, int pid, int num) {
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
List<OrderItem> ois = orderItemService.listByUser(user);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==pid){
oi.setNumber(num);
orderItemService.update(oi);
break;
}
}
return Result.success();
}
@GetMapping("foredeleteOrderItem")
public Object deleteOrderItem(HttpSession session,int oiid){
User user =(User) session.getAttribute("user");
if(null==user)
return Result.fail("未登录");
orderItemService.delete(oiid);
return Result.success();
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2021-11-23
感觉有三处地方需要优化一下,分别是删除之后需要重新加载购物车页面,增减订单数量和删除订单需要判断oiid是否是当前用户的订单
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2020-11-02
F12修改了a标签oiid,就可以删除其他用户的购物车单项
2020-04-09
站长好,有两个疑问
2019-12-28
项目快抄完了,虽说很多不能完全消化但也收获不少.
2019-12-25
站长好,问一个问题
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 8 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|