CartController.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.fdkankan.ucenter.controller;
  2. import com.fdkankan.common.util.JwtUtil;
  3. import com.fdkankan.ucenter.common.BaseController;
  4. import com.fdkankan.ucenter.common.Result;
  5. import com.fdkankan.ucenter.service.ICartService;
  6. import com.fdkankan.ucenter.vo.request.CartParam;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.*;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. /**
  13. * 购物车
  14. */
  15. @RestController
  16. @RequestMapping("/ucenter/user/cart")
  17. public class CartController extends BaseController {
  18. @Autowired
  19. ICartService cartService;
  20. /**
  21. * 添加购物车
  22. */
  23. @PostMapping("/add")
  24. public Result add(@RequestBody CartParam param){
  25. param.setUserName(JwtUtil.getUsername(getToken()));
  26. cartService.add(param);
  27. return Result.success();
  28. }
  29. /**
  30. * 购物车商品减或移除
  31. */
  32. @PostMapping(value = "/remove")
  33. public Result remove(@RequestBody CartParam param){
  34. param.setUserName(JwtUtil.getUsername(getToken()));
  35. cartService.delete(param);
  36. return Result.success();
  37. }
  38. /**
  39. * 查找会员的购物车记录
  40. *
  41. */
  42. @PostMapping(value = "/list")
  43. public Result list() throws Exception {
  44. String username = JwtUtil.getUsername(getToken());
  45. return Result.success(cartService.listByUser(username));
  46. }
  47. /**
  48. * 删除购物车
  49. *
  50. */
  51. @PostMapping(value = "/delete")
  52. public Result delete(@RequestBody CartParam param) throws Exception {
  53. if (StringUtils.isEmpty(param.getIds())){
  54. return Result.failure("ids为空");
  55. }
  56. String[] split = param.getIds().split(",");
  57. List<String> ids = Arrays.asList(split);
  58. cartService.deleteIds(ids);
  59. return Result.success();
  60. }
  61. }