后端 + 前端:提交购物车订单

This commit is contained in:
YunaiV 2019-04-14 01:28:41 +08:00
parent b2abc625d1
commit 38b2613add
11 changed files with 146 additions and 38 deletions

View File

@ -21,7 +21,7 @@ export function confirmReceiving(orderId) {
}); });
} }
export function getConfirmCreateOrder(skuId, quantity) { export function getOrderConfirmCreateOrder(skuId, quantity) {
return request({ return request({
url: '/order-api/users/order/confirm_create_order', url: '/order-api/users/order/confirm_create_order',
method: 'get', method: 'get',
@ -45,6 +45,18 @@ export function createOrder(params) {
}); });
} }
export function createOrderFromCart(userAddressId,
remark) {
return request({
url: '/order-api/users/order/create_order_from_cart',
method: 'post',
params: {
userAddressId,
remark,
}
});
}
// Cart // Cart
export function addCart(skuId, quantity) { export function addCart(skuId, quantity) {
@ -87,6 +99,17 @@ export function updateCartSelected(skuIds, selected) {
}); });
} }
export function getCartConfirmCreateOrder(skuId, quantity) {
return request({
url: '/order-api/users/cart/confirm_create_order',
method: 'get',
params: {
skuId,
quantity,
}
});
}
// 物流信息 // 物流信息
export function getLogisticsInfo(params) { export function getLogisticsInfo(params) {

View File

@ -157,7 +157,7 @@ export default {
}) })
}, },
onSubmit() { onSubmit() {
this.$router.push('/order') this.$router.push('/order?from=cart')
}, },
convertProduct(item) { convertProduct(item) {
// debugger; // debugger;

View File

@ -68,7 +68,12 @@
<script> <script>
import {createOrder, getConfirmCreateOrder} from '../../api/order'; import {
createOrder,
getOrderConfirmCreateOrder,
getCartConfirmCreateOrder,
createOrderFromCart
} from '../../api/order';
import {GetDefaultAddress} from '../../api/user'; import {GetDefaultAddress} from '../../api/user';
import orderStore from '../../store/order' import orderStore from '../../store/order'
@ -96,15 +101,15 @@
}, },
methods: { methods: {
onSubmit() { onSubmit() {
const { skuId, quantity } = this.$route.query;
const userAddressId = this.addressData.id; const userAddressId = this.addressData.id;
const remark = ''; const remark = '';
if (this.from === 'direct_order') {
const { skuId, quantity } = this.$route.query;
const orderItems = [{ const orderItems = [{
skuId, skuId,
quantity, quantity,
}]; }];
createOrder({ createOrder({
orderItems, orderItems,
userAddressId, userAddressId,
@ -119,7 +124,20 @@
} }
}); });
} }
}) });
} else if (this.from === 'cart') {
createOrderFromCart(userAddressId, remark).then(result => {
if (result) {
const { orderNo } = result;
this.$router.push({ //
path:`/order/success`, //
query:{ //pushquery使
...result,
}
});
}
});
}
}, },
convertProduct(item) { convertProduct(item) {
// debugger; // debugger;
@ -143,13 +161,18 @@
this.addressData = this.$store.state.addressData; this.addressData = this.$store.state.addressData;
// //
// debugger;
if (this.from === 'direct_order') { if (this.from === 'direct_order') {
getConfirmCreateOrder(this.skuId, this.quantity).then(data => { getOrderConfirmCreateOrder(this.skuId, this.quantity).then(data => {
this.itemGroups = data.itemGroups;
this.fee = data.fee;
})
} else if (this.from === 'cart') {
getCartConfirmCreateOrder().then(data => {
this.itemGroups = data.itemGroups; this.itemGroups = data.itemGroups;
this.fee = data.fee; this.fee = data.fee;
}) })
} }
}, },
created() { created() {
// //
@ -160,6 +183,9 @@
} }
}) })
// //
if (this.$route.query.from === 'cart') {
this.from = this.$route.query.from;
}
}, },
store: orderStore, store: orderStore,
}; };

View File

@ -1,11 +1,15 @@
package cn.iocoder.mall.order.application.controller.users; package cn.iocoder.mall.order.application.controller.users;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.order.api.CartService; import cn.iocoder.mall.order.api.CartService;
import cn.iocoder.mall.order.api.OrderService; import cn.iocoder.mall.order.api.OrderService;
import cn.iocoder.mall.order.api.bo.CalcOrderPriceBO; import cn.iocoder.mall.order.api.bo.CalcOrderPriceBO;
import cn.iocoder.mall.order.api.bo.CartItemBO;
import cn.iocoder.mall.order.api.bo.OrderCreateBO; import cn.iocoder.mall.order.api.bo.OrderCreateBO;
import cn.iocoder.mall.order.api.bo.OrderPageBO; import cn.iocoder.mall.order.api.bo.OrderPageBO;
import cn.iocoder.mall.order.api.constant.OrderErrorCodeEnum;
import cn.iocoder.mall.order.api.dto.CalcOrderPriceDTO; import cn.iocoder.mall.order.api.dto.CalcOrderPriceDTO;
import cn.iocoder.mall.order.api.dto.OrderCreateDTO; import cn.iocoder.mall.order.api.dto.OrderCreateDTO;
import cn.iocoder.mall.order.api.dto.OrderQueryDTO; import cn.iocoder.mall.order.api.dto.OrderQueryDTO;
@ -19,7 +23,10 @@ import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* 订单API(users) * 订单API(users)
@ -52,6 +59,31 @@ public class UsersOrderController {
return orderService.createOrder(orderCreateDTO); return orderService.createOrder(orderCreateDTO);
} }
@PostMapping("create_order_from_cart")
public CommonResult<OrderCreateBO> createOrderFromCart(@RequestParam("userAddressId") Integer userAddressId,
@RequestParam(value = "remark", required = false) String remark,
HttpServletRequest request) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
// 获得购物车中选中的商品
List<CartItemBO> cartItems = cartService.list(userId, true).getData();
if (cartItems.isEmpty()) {
return ServiceExceptionUtil.error(OrderErrorCodeEnum.ORDER_CREATE_CART_IS_EMPTY.getCode());
}
// 创建 OrderCreateDTO 对象
OrderCreateDTO orderCreateDTO = OrderConvertAPP.INSTANCE.createOrderCreateDTO(userId, userAddressId,
remark, HttpUtil.getIp(request),
cartItems);
// 创建订单
CommonResult<OrderCreateBO> createResult= orderService.createOrder(orderCreateDTO);
if (createResult.isError()) {
return CommonResult.error(createResult);
}
// 清空购物车 // TODO 芋艿需要标记删除的原因即结果为创建为某个订单
cartService.deleteList(userId, cartItems.stream().map(CartItemBO::getSkuId).collect(Collectors.toList()));
// 返回结果
return createResult;
}
@GetMapping("confirm_create_order") @GetMapping("confirm_create_order")
public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam("skuId") Integer skuId, public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity) { @RequestParam("quantity") Integer quantity) {

View File

@ -1,9 +1,7 @@
package cn.iocoder.mall.order.application.convert; package cn.iocoder.mall.order.application.convert;
import cn.iocoder.mall.order.api.dto.OrderCreateDTO; import cn.iocoder.mall.order.api.bo.CartItemBO;
import cn.iocoder.mall.order.api.dto.OrderItemUpdateDTO; import cn.iocoder.mall.order.api.dto.*;
import cn.iocoder.mall.order.api.dto.OrderLogisticsUpdateDTO;
import cn.iocoder.mall.order.api.dto.OrderQueryDTO;
import cn.iocoder.mall.order.application.po.admin.OrderItemUpdatePO; import cn.iocoder.mall.order.application.po.admin.OrderItemUpdatePO;
import cn.iocoder.mall.order.application.po.admin.OrderLogisticsPO; import cn.iocoder.mall.order.application.po.admin.OrderLogisticsPO;
import cn.iocoder.mall.order.application.po.admin.OrderPageQueryPO; import cn.iocoder.mall.order.application.po.admin.OrderPageQueryPO;
@ -12,6 +10,8 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import java.util.List;
/** /**
* application 订单 convert * application 订单 convert
* *
@ -36,4 +36,19 @@ public interface OrderConvertAPP {
@Mappings({}) @Mappings({})
OrderCreateDTO convert(OrderCreatePO orderCreatePO); OrderCreateDTO convert(OrderCreatePO orderCreatePO);
@Mappings({})
List<OrderCreateItemDTO> convert(List<CartItemBO> cartItems);
default OrderCreateDTO createOrderCreateDTO(Integer userId, Integer userAddressId,
String remark, String ip,
List<CartItemBO> cartItems) {
return new OrderCreateDTO()
.setUserId(userId)
.setUserAddressId(userAddressId)
.setRemark(remark)
.setIp(ip)
.setOrderItems(this.convert(cartItems));
}
} }

View File

@ -53,7 +53,7 @@ public interface CartService {
* *
* @return 是否成功 * @return 是否成功
*/ */
CommonResult<Boolean> delete(Integer userId, List<Integer> skuIds); CommonResult<Boolean> deleteList(Integer userId, List<Integer> skuIds);
/** /**
* 清空购物车 * 清空购物车

View File

@ -6,7 +6,7 @@ public enum CartItemStatusEnum {
ENABLE(1, "正常"), ENABLE(1, "正常"),
DELETE_BY_MANUAL(2, "主动删除"), DELETE_BY_MANUAL(2, "主动删除"),
DELETE_BY_(3, "下单删除"), DELETE_BY_ORDER(3, "下单删除"),
; ;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CartItemStatusEnum::getValue).toArray(); public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CartItemStatusEnum::getValue).toArray();

View File

@ -24,6 +24,7 @@ public enum OrderErrorCodeEnum {
ORDER_GET_PAY_FAIL(1008000010, "调用pay失败!"), ORDER_GET_PAY_FAIL(1008000010, "调用pay失败!"),
ORDER_NOT_USER_ORDER(1008000011, "不是该用户的订单!"), ORDER_NOT_USER_ORDER(1008000011, "不是该用户的订单!"),
ORDER_UNABLE_CONFIRM_ORDER(1008000012, "状态不对不能确认订单!"), ORDER_UNABLE_CONFIRM_ORDER(1008000012, "状态不对不能确认订单!"),
ORDER_CREATE_CART_IS_EMPTY(1008000013, "购物车无选中的商品,无法创建订单"),
// order item // order item
ORDER_ITEM_ONLY_ONE(1008000200, "订单Item只有一个!"), ORDER_ITEM_ONLY_ONE(1008000200, "订单Item只有一个!"),

View File

@ -38,8 +38,9 @@ public interface CartMapper {
int updateQuantity(@Param("id") Integer id, int updateQuantity(@Param("id") Integer id,
@Param("quantityIncr") Integer quantityIncr); @Param("quantityIncr") Integer quantityIncr);
int updateListSelected(@Param("userId") Integer userId, int updateListByUserIdAndSkuId(@Param("userId") Integer userId,
@Param("skuIds") Collection<Integer> skuIds, @Param("skuIds") Collection<Integer> skuIds,
@Param("selected") Boolean selected); @Param("selected") Boolean selected,
@Param("status") Integer status);
} }

View File

@ -116,14 +116,17 @@ public class CartServiceImpl implements CartService {
@Override @Override
public CommonResult<Boolean> updateSelected(Integer userId, Collection<Integer> skuIds, Boolean selected) { public CommonResult<Boolean> updateSelected(Integer userId, Collection<Integer> skuIds, Boolean selected) {
// 更新 CartItemDO // 更新 CartItemDO
cartMapper.updateListSelected(userId, skuIds, selected); cartMapper.updateListByUserIdAndSkuId(userId, skuIds, selected, null);
// 返回成功 // 返回成功
return CommonResult.success(true); return CommonResult.success(true);
} }
@Override @Override
public CommonResult<Boolean> delete(Integer userId, List<Integer> skuIds) { public CommonResult<Boolean> deleteList(Integer userId, List<Integer> skuIds) {
return null; // 更新 CartItemDO
cartMapper.updateListByUserIdAndSkuId(userId, skuIds, null, CartItemStatusEnum.DELETE_BY_MANUAL.getValue());
// 返回成功
return CommonResult.success(true);
} }
@Override @Override

View File

@ -108,9 +108,16 @@
WHERE id = #{id} WHERE id = #{id}
</update> </update>
<update id="updateListSelected"> <update id="updateListByUserIdAndSkuId">
UPDATE cart_item UPDATE cart_item
SET selected = #{selected} <set>
<if test="selected != null">
selected = #{selected},
</if>
<if test="status != null">
status = #{status},
</if>
</set>
WHERE user_id = #{userId} WHERE user_id = #{userId}
AND sku_id IN AND sku_id IN
<foreach item="skuId" collection="skuIds" separator="," open="(" close=")" index=""> <foreach item="skuId" collection="skuIds" separator="," open="(" close=")" index="">