后端代码 5.3.1 服务实现层 修改 pinyougou-order-service 的 OrderServiceImpl.java [AppleScript] 纯文本查看 复制代码 @Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private TbOrderItemMapper orderItemMapper;
@Autowired
private IdWorker idWorker;
/**
* 增 加
*/ @Override
public void add(TbOrder order) {
//得到购物车数据
List<Cart> cartList = (List<Cart>)
redisTemplate.boundHashOps("cartList").get( order.getUserId() );
for(Cart cart:cartList){
long orderId = idWorker.nextId();
System.out.println("sellerId:"+cart.getSellerId());
TbOrder tborder=new TbOrder();//新创建订单对象
tborder.setOrderId(orderId);//订单 ID
tborder.setUserId(order.getUserId());//用户名
tborder.setPaymentType(order.getPaymentType());//支付类型
tborder.setStatus("1");//状态:未付款 tborder.setCreateTime(new Date());//订单创建日期 tborder.setUpdateTime(new Date());//订单更新日期
tborder.setReceiverAreaName(order.getReceiverAreaName());//地址
tborder.setReceiverMobile(order.getReceiverMobile());//手机号
tborder.setReceiver(order.getReceiver());//收货人
tbOrder.setSourceType(order.getSourceType());//订单来源
tborder.setSellerId(cart.getSellerId());//商家 ID
//循环购物车明细
double money=0;
for(TbOrderItem orderItem :cart.getOrderItemList()){
orderItem.setId(idWorker.nextId()); orderItem.setOrderId( orderId );//订单 ID
orderItem.setSellerId(cart.getSellerId());
money+=orderItem.getTotalFee().doubleValue();//金额累加 orderItemMapper.insert(orderItem);
}
tborder.setPayment(new BigDecimal(money)); orderMapper.insert(tborder);
}
redisTemplate.boundHashOps("cartList").delete(order.getUserId());
} 控制层 修改 pinyougou-cart-web 的 OrderController.java [AppleScript] 纯文本查看 复制代码 /**
*增加
*@param order
*@return
*/ @RequestMapping("/add")
public Result add(@RequestBody TbOrder order){
//获取当前登录人账号String username =
SecurityContextHolder.getContext().getAuthentication().getName();
order.setUserId(username);
order.setSourceType("2");//订单来源 PC
try {
orderService.add(order);
return new Result(true, "增加成功");
} catch (Exception e) { e.printStackTrace();
return new Result(false, "增加失败");
}
} 5.4 前端代码 5.4.1 服务层 修改 pinyougou-cart-web 的 cartService.js [AppleScript] 纯文本查看 复制代码 //保存订单
this.submitOrder=function(order){
return $http.post('order/add.do',order);
} 5.4.1 控制层 修改 cartController.js [AppleScript] 纯文本查看 复制代码 //保存订单
$scope.submitOrder=function(){
$scope.order.receiverAreaName=$scope.address.address;//地址
$scope.order.receiverMobile=$scope.address.mobile;//手机
$scope.order.receiver=$scope.address.contact;//联系人 cartService.submitOrder( $scope.order ).success(
function(response){ if(response.success){
//页面跳转
if($scope.order.paymentType=='1'){//如果是微信支付,跳转到支付页面
location.href="pay.html";
}else{//如果货到付款,跳转到提示页面location.href="paysuccess.html";
}
}else{
alert(response.message); //也可以跳转到提示页面
}
}
);
} 5.4.1 页面 修改 getOrderInfo.html [AppleScript] 纯文本查看 复制代码 < a class="sui-btn btn-danger btn-xlarge" ng-click="submitOrder()" >提交订单</ a> 将静态原型中的 pay.html paysuccess.html payfail.html 拷贝到 pinyougou-cart-web 中
|