Merge pull request #54 from gadfly3173/develop

refactor: 更改购物车的userid判断,以便用户在未登录时访问购物车页面不会被弹出
This commit is contained in:
有来技术 2021-08-16 20:12:30 +08:00 committed by GitHub
commit 5b43212f4f
2 changed files with 46 additions and 14 deletions

View File

@ -12,6 +12,7 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -32,16 +33,20 @@ public class CartController {
@ApiOperation(value = "查询购物车") @ApiOperation(value = "查询购物车")
@GetMapping @GetMapping
@ApiOperationSupport(order = 1) @ApiOperationSupport(order = 1)
public Result getCart() { public <T> Result<T> getCart() {
Long memberId = JwtUtils.getUserId(); try {
List<CartItemDTO> result = cartService.listCartItemByMemberId(memberId); Long memberId = JwtUtils.getUserId();
return Result.success(result); List<CartItemDTO> result = cartService.listCartItemByMemberId(memberId);
return Result.success((T) result);
} catch (Exception e) {
return Result.success((T) Collections.EMPTY_LIST);
}
} }
@ApiOperation(value = "删除购物车") @ApiOperation(value = "删除购物车")
@DeleteMapping @DeleteMapping
@ApiOperationSupport(order = 2) @ApiOperationSupport(order = 2)
public Result deleteCart() { public <T> Result<T> deleteCart() {
boolean result = cartService.deleteCart(); boolean result = cartService.deleteCart();
return Result.judge(result); return Result.judge(result);
} }
@ -50,7 +55,7 @@ public class CartController {
@ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long") @ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long")
@PostMapping @PostMapping
@ApiOperationSupport(order = 3) @ApiOperationSupport(order = 3)
public Result addCartItem(@RequestParam Long skuId) { public <T> Result<T> addCartItem(@RequestParam Long skuId) {
cartService.addCartItem(skuId); cartService.addCartItem(skuId);
return Result.success(); return Result.success();
} }
@ -58,7 +63,7 @@ public class CartController {
@ApiOperation(value = "更新购物车商品") @ApiOperation(value = "更新购物车商品")
@PutMapping("/skuId/{skuId}") @PutMapping("/skuId/{skuId}")
@ApiOperationSupport(order = 4) @ApiOperationSupport(order = 4)
public Result updateCartItem(@PathVariable Long skuId,@RequestBody CartItemDTO cartItem) { public <T> Result<T> updateCartItem(@PathVariable Long skuId, @RequestBody CartItemDTO cartItem) {
cartItem.setSkuId(skuId); cartItem.setSkuId(skuId);
boolean result = cartService.updateCartItem(cartItem); boolean result = cartService.updateCartItem(cartItem);
return Result.judge(result); return Result.judge(result);
@ -68,7 +73,7 @@ public class CartController {
@ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long") @ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long")
@DeleteMapping("/skuId/{skuId}") @DeleteMapping("/skuId/{skuId}")
@ApiOperationSupport(order = 5) @ApiOperationSupport(order = 5)
public Result removeCartItem(@PathVariable Long skuId) { public <T> Result<T> removeCartItem(@PathVariable Long skuId) {
boolean result = cartService.removeCartItem(skuId); boolean result = cartService.removeCartItem(skuId);
return Result.judge(result); return Result.judge(result);
} }
@ -77,7 +82,7 @@ public class CartController {
@ApiImplicitParam(name = "checked", value = "全选/全不选", required = true, paramType = "param", dataType = "Boolean") @ApiImplicitParam(name = "checked", value = "全选/全不选", required = true, paramType = "param", dataType = "Boolean")
@PatchMapping("/_check") @PatchMapping("/_check")
@ApiOperationSupport(order = 6) @ApiOperationSupport(order = 6)
public Result check(boolean checked) { public <T> Result<T> check(boolean checked) {
boolean result = cartService.checkAll(checked); boolean result = cartService.checkAll(checked);
return Result.judge(result); return Result.judge(result);
} }

View File

@ -1,6 +1,8 @@
package com.youlai.mall.oms.service.impl; package com.youlai.mall.oms.service.impl;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.exception.BizException;
import com.youlai.common.web.util.JwtUtils; import com.youlai.common.web.util.JwtUtils;
import com.youlai.mall.oms.constant.OmsConstants; import com.youlai.mall.oms.constant.OmsConstants;
import com.youlai.mall.oms.pojo.dto.CartItemDTO; import com.youlai.mall.oms.pojo.dto.CartItemDTO;
@ -57,7 +59,12 @@ public class CartServiceImpl implements ICartService {
*/ */
@Override @Override
public boolean addCartItem(Long skuId) { public boolean addCartItem(Long skuId) {
Long memberId = JwtUtils.getUserId(); Long memberId;
try {
memberId = JwtUtils.getUserId();
} catch (Exception e) {
throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId); BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
String hKey = skuId + ""; String hKey = skuId + "";
@ -98,7 +105,12 @@ public class CartServiceImpl implements ICartService {
*/ */
@Override @Override
public boolean updateCartItem(CartItemDTO cartItem) { public boolean updateCartItem(CartItemDTO cartItem) {
Long memberId = JwtUtils.getUserId(); Long memberId;
try {
memberId = JwtUtils.getUserId();
} catch (Exception e) {
throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId); BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
String hKey = cartItem.getSkuId() + ""; String hKey = cartItem.getSkuId() + "";
if (cartHashOperations.get(hKey) != null) { if (cartHashOperations.get(hKey) != null) {
@ -119,7 +131,12 @@ public class CartServiceImpl implements ICartService {
*/ */
@Override @Override
public boolean removeCartItem(Long skuId) { public boolean removeCartItem(Long skuId) {
Long memberId = JwtUtils.getUserId(); Long memberId;
try {
memberId = JwtUtils.getUserId();
} catch (Exception e) {
throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId); BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
String hKey = skuId + ""; String hKey = skuId + "";
cartHashOperations.delete(hKey); cartHashOperations.delete(hKey);
@ -132,7 +149,12 @@ public class CartServiceImpl implements ICartService {
*/ */
@Override @Override
public boolean checkAll(boolean checked) { public boolean checkAll(boolean checked) {
Long memberId = JwtUtils.getUserId(); Long memberId;
try {
memberId = JwtUtils.getUserId();
} catch (Exception e) {
throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId); BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
for (Object value : cartHashOperations.values()) { for (Object value : cartHashOperations.values()) {
CartItemDTO cartItem = (CartItemDTO) value; CartItemDTO cartItem = (CartItemDTO) value;
@ -150,7 +172,12 @@ public class CartServiceImpl implements ICartService {
*/ */
@Override @Override
public boolean removeCheckedItem() { public boolean removeCheckedItem() {
Long memberId = JwtUtils.getUserId(); Long memberId;
try {
memberId = JwtUtils.getUserId();
} catch (Exception e) {
throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId); BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
for (Object value : cartHashOperations.values()) { for (Object value : cartHashOperations.values()) {
CartItemDTO cartItem = (CartItemDTO) value; CartItemDTO cartItem = (CartItemDTO) value;