接上篇订单详情页面
购物车界面: 通过购物界面, 点击生成订单按钮依据页面信息添加收货地址
订单预览界面: 订单创建成功, 显示订单预览界面, 在预览界面就可以显示添加收获地址
支付提示界面: 当点击提交订单,此时将订单数据存储到数据表
支付宝支付 : 一般的开发中使用的是沙箱做模拟支付
支付成功可以通过点击我的订单查看订单列表
订单详情界面 : 通过点击订单详情按钮打开该订单的详情信息界面
2 提交订单 2.1功能逻辑抽象商品购物车点击生成订单时: 先判断用户是否登录,如果用户没有登录,则需要先完成登录, 获取到用户对象,然后从session中取得购物车的数据根据购物车数据创建订单对象和订单list集合数据
商品购物车是面向数据库中的tb_cart表, 订单表示tb_order表, 订单详情表tb_orderdetail表. 在订单预览界面添加数据到对应的表. 建立对应的类
购物车: CartServlet类 , CartService, CartDao接口实现类
订单: OrderServlet类, OrderServce, OrderDao接口实现类
订单详情: OrderDetailServlet类, OrderDetailService, OrderDetailDao接口实现类
提示 : 还需要创建辅助的VO类
订单列表显示需要一个VO类: 其中包含订单信息和地址信息用于显示
OdersView : oid, uid, orderTime, userName, telphon, address, totalMoney, ostate
GoodOdersDeailVO: pid, pimg, pname, star, pubdate , price, ,num, money
3.代码实现 3.1前端 3.1.1修改生成添加收获地址的a链接 3.1.2 修改order.html代码 $(function () {
$("#btn_add").click(function () {
location.href = "order.do?action=generateOrder&aid=" + $("#address").val();
})
});
$(document).ready(function () {
//获取购物车的session数据显示到页面
$.get("cart.do?action=showCart", "", function (result) {
if (result.flag === true) {
var sum = 0;//计算总金额变量
var index = 1;
for (var i in result.data) {
var tbhtm = '' +
'' +
'' + index + ' ' +
'' + result.data[i].goodsName + ' ' +
'¥ ' + result.data[i].price + ' ' +
'' + result.data[i].quantity + ' ' +
'¥ ' + result.data[i].subtotal + ' ' +
' ' +
'';
index++;
sum += result.data[i].subtotal;
$("#cartBody").append(tbhtm);
}
//显示总计
$("#total").html("¥ " + sum + "");
}
});
//获取地址列表的数据显示到页面
$.get("address.do?action=showAddress", "", function (result) {
var addStr = '';
var tbhtm = '' +
'' +
'收货地址' +
';
if (result.flag === true) {
for (var i in result.data) {
tbhtm += '
+ result.data[i].name + ' ' +
result.data[i].phone + ' ' + result.data[i].detail + '';
}
} else {
addStr = '添加收货地址';
}
tbhtm += '' + addStr +
' ' +
' ';
$("#addressFoot").append(tbhtm);
});
});
3.2后端
3.2.1 AddressServlet
在AddressServlet类中根据购物车信息创建订单信息
@WebServlet("/address.do")
public class AddressServlet extends BaseServlet{
private ResultData resultData = new ResultData();
/**
* 显示收货地址列表
* @param request
* @param response
* @return
*/
public String showAddress(HttpServletRequest request , HttpServletResponse response){
HttpSession session = request.getSession();
//判断是否登录
User user = (User) request.getSession().getAttribute(Constants.LOGINUSER);
if (user==null){
resultData.setFlag(false);
return Constants.REDIRECT + "/login.html";
}else {
AddressServiceImpl addressService = new AddressServiceImpl();
List<Address> addressList = addressService.querAddressByUid(user.getId());
if (addressList!=null){
resultData.setFlag(true);
resultData.setData(addressList);
}else {
resultData.setFlag(false);
}
}
String json = JSON.toJSONString(resultData);
System.out.println(json);
//在响应中声明返回的是json格式字符串
response.setContentType("application/json;charset=utf-8");
return json;
}
}
3.2.2 AddressServiceImpl
@Override
public List<Address> querAddressByUid(int uid) {
return addressDao.queryAddressByUid(uid);
}
3.2.3 AddressDaoImpl
@Override
public List<Address> queryBySql(String sql, Object... parms) {
List<Address> address = null;
QueryRunner qrun = new QueryRunner(DBUtils.getDataSource());
try {
address = qrun.query(sql,parms,new BeanListHandler<>(Address.class));
} catch (SQLException e) {
e.printStackTrace();
}
return address;
}
/**
* 通过uid查询
* @param uid
* @return
*/
@Override
public List<Address> queryAddressByUid(int uid) {
String sql = "select * from tb_address where uid=?";
return queryBySql(sql,uid);
}
OrderServlet
@WebServlet("/order.do")
public class OrderServlet extends BaseServlet{
private ResultData resultData = new ResultData();
/**
* 生成订单页面
* @param request
* @param response
* @return
*/
public String generateOrder(HttpServletRequest request, HttpServletResponse response) {
String aidStr = request.getParameter("aid");
System.out.println(aidStr);
Order order = new Order();
boolean flag = false;
int aid = 0;
if (aidStr!=null){
try {
aid = Integer.parseInt(aidStr);
} catch (NumberFormatException e) {
e.printStackTrace();
return Constants.REDIRECT+"/error.html?msg=订单创建失败";
}
}
HttpSession session = request.getSession();
//判断是否登录
User user = (User) request.getSession().getAttribute(Constants.LOGINUSER);
if (user==null){
return Constants.REDIRECT + "/login.html";
}else {
Map<String, CartItem> cartItemMap = (Map<String, CartItem>) request.getSession().getAttribute(Constants.CART);
if (cartItemMap!=null){
//一个订单 对应多条 订单详情数据 === 购物车数据
String oid = RandomUtils.createOrderId();//创建订单oid
order.setId(oid);
List<Cart> cartList = new ArrayList<>();
List<OrderDetail> orderDetailList = new ArrayList<>();
double totalMoney = 0.0;
for (String goodId :cartItemMap.keySet()){
CartItem cartItem = cartItemMap.get(goodId);
//将订单项添加到Cart
int pid = Integer.parseInt(cartItem.getGoodId());
Cart cart = new Cart(0,pid,cartItem.getQuantity(),cartItem.getSubtotal());
cartList.add(cart);
//将订单项数据添加到OrderDetail
OrderDetail orderDetail = new OrderDetail(0,pid,cartItem.getQuantity(),cartItem.getSubtotal(),oid);
orderDetailList.add(orderDetail);
totalMoney += cartItem.getSubtotal();
}
//设置订单详情
order.setUid(user.getId());//订单编号
order.setStatus(0);//订单状态
order.setTime(RandomUtils.getNewTime());//订单时间
order.setMoney(totalMoney);//订单总金额
//
OrderServiceImpl orderService = new OrderServiceImpl();
flag = orderService.saveOrder(order,cartList,orderDetailList);
}
}
if (flag){
//清空购物车
session.removeAttribute(Constants.CART);
return Constants.REDIRECT+"/orderSuccess.html?oid="+order.getId() + "&money=" + order.getMoney();
}else {
return Constants.REDIRECT+"/error.html?msg=订单创建失败";
}
}
/**
* 显示订单信息
* @param request
* @param response
* @return
*/
public String getOrderInfo(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
Order order =(Order) session.getAttribute(Constants.ORDER);
if (order == null){
resultData.setFlag(false);
}
resultData.setData(order);
String json = JSON.toJSONString(resultData);
System.out.println(json);
//在响应中声明返回的是json格式字符串
response.setContentType("application/json;charset=utf-8");
return json;
}
}
OrderServceImpl
/**
* 手动事务控制
* @param order
* @param cartList
* @param orderDetailList
* @return
*/
@Override
public boolean saveOrder(Order order, List<Cart> cartList, List<OrderDetail> orderDetailList) {
boolean isSuccess = false;//默认状态为失败
try {
DBUtils.beginTransaction();//开启失误
} catch (Exception e) {
e.printStackTrace();
}
int n1 = orderDao.addDate(order);
if (n1>=1){
int n2 = cartDao.addCartList(cartList);
if (n2==cartList.size()){
int n3 =orderDetailDao.addOrderDetailsList(orderDetailList);
if (n3==orderDetailList.size()){
isSuccess =true;
DBUtils.commit();
}
}
}
if (isSuccess ==false){
DBUtils.rollback();
}
return isSuccess;
}
OrderDaoImpl
@Override
public int addDate(Order order) {
int n = 0;
String sql = "insert into tb_order(id,uid,money,status,time,aid) values(?,?,?,?,?,?);";
try {
n= queryRunner.update(DBUtils.getConn(),sql,order.getId(),
order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid());
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
欢迎分享,转载请注明来源:内存溢出
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
React 源码学习
上一篇
2022-06-10
JavaScript的节流讲解与实现
下一篇
2022-06-10
评论列表(0条)