文章目錄
- 0.效果展示
- 1.效果展示
- 2.后端接口
- 3.前端js
- 4.前端html
0.效果展示
1.效果展示
1)當房東點擊“客戶訂單”,js向后端接口get_user_orders()獲取數據,訂單頁面開始加載;
2)當房東確定接單時,js會向后端接口發送數據,后端接口進行修改狀態、提交數據庫相關操作;
3)當房東拒單時,js會向后端接口發送數據,后端接口進行修改狀態、獲取失敗原因、失敗回滾等相關操作;
2.后端接口
orders.py部分接口:
@api.route("/orders/<int:order_id>/status", methods=["PUT"])
@login_required
def accept_reject_order(order_id):"""接單、拒單"""user_id = g.user_id# 獲取參數req_data = request.get_json()if not req_data:return jsonify(errno=RET.PARAMERR, errmsg="參數錯誤")# action參數表明客戶端請求的是接單還是拒單的行為action = req_data.get("action")if action not in ("accept", "reject"):return jsonify(errno=RET.PARAMERR, errmsg="參數錯誤")try:# 根據訂單號查詢訂單,并且要求訂單處于等待接單狀態order = Order.query.filter(Order.id == order_id, Order.status == "WAIT_ACCEPT").first()house = order.houseexcept Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="無法獲取訂單數據")# 確保房東只能修改屬于自己房子的訂單if not order or house.user_id != user_id:return jsonify(errno=RET.REQERR, errmsg="操作無效")if action == "accept":# 接單,將訂單狀態設置為等待評論order.status = "WAIT_PAYMENT"elif action == "reject":# 拒單,要求用戶傳遞拒單原因reason = req_data.get("reason")if not reason:return jsonify(errno=RET.PARAMERR, errmsg="參數錯誤")order.status = "REJECTED"order.comment = reasontry:db.session.add(order)db.session.commit()except Exception as e:current_app.logger.error(e)db.session.rollback()return jsonify(errno=RET.DBERR, errmsg="操作失敗")return jsonify(errno=RET.OK, errmsg="OK")
3.前端js
lorder.js
//模態框居中的控制
function centerModals(){$('.modal').each(function(i){ //遍歷每一個模態框var $clone = $(this).clone().css('display', 'block').appendTo('body'); var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);top = top > 0 ? top : 0;$clone.remove();$(this).find('.modal-content').css("margin-top", top-30); //修正原先已經有的30個像素});
}function getCookie(name) {var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");return r ? r[1] : undefined;
}$(document).ready(function(){$('.modal').on('show.bs.modal', centerModals); //當模態框出現的時候$(window).on('resize', centerModals);// 查詢房東的訂單$.get("/api/v1.0/user/orders?role=landlord", function(resp){if ("0" == resp.errno) {$(".orders-list").html(template("orders-list-tmpl", {orders:resp.data.orders}));$(".order-accept").on("click", function(){var orderId = $(this).parents("li").attr("order-id");$(".modal-accept").attr("order-id", orderId);});// 接單處理$(".modal-accept").on("click", function(){var orderId = $(this).attr("order-id");$.ajax({url:"/api/v1.0/orders/"+orderId+"/status",type:"PUT",data:'{"action":"accept"}',contentType:"application/json",dataType:"json",headers:{"X-CSRFTOKEN":getCookie("csrf_token"),},success:function (resp) {if ("4101" == resp.errno) {location.href = "/login.html";} else if ("0" == resp.errno) {$(".orders-list>li[order-id="+ orderId +"]>div.order-content>div.order-text>ul li:eq(4)>span").html("已接單");$("ul.orders-list>li[order-id="+ orderId +"]>div.order-title>div.order-operate").hide();$("#accept-modal").modal("hide");}}})});$(".order-reject").on("click", function(){var orderId = $(this).parents("li").attr("order-id");$(".modal-reject").attr("order-id", orderId);});// 處理拒單$(".modal-reject").on("click", function(){var orderId = $(this).attr("order-id");var reject_reason = $("#reject-reason").val();if (!reject_reason) return;var data = {action: "reject",reason:reject_reason};$.ajax({url:"/api/v1.0/orders/"+orderId+"/status",type:"PUT",data:JSON.stringify(data),contentType:"application/json",headers: {"X-CSRFTOKEN":getCookie("csrf_token")},dataType:"json",success:function (resp) {if ("4101" == resp.errno) {location.href = "/login.html";} else if ("0" == resp.errno) {$(".orders-list>li[order-id="+ orderId +"]>div.order-content>div.order-text>ul li:eq(4)>span").html("已拒單");$("ul.orders-list>li[order-id="+ orderId +"]>div.order-title>div.order-operate").hide();$("#reject-modal").modal("hide");}}});})}});
});
4.前端html
lorder.html
<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><title>愛家-我的訂單</title><link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"><link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"><link href="/static/css/reset.css" rel="stylesheet"><link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet"><link href="/static/css/ihome/main.css" rel="stylesheet"><link href="/static/css/ihome/orders.css" rel="stylesheet">
</head>
<body><div class="container"><div class="top-bar"><div class="nav-bar"><h3 class="page-title">客戶訂單</h3><a class="nav-btn fl" href="/my.html"><span><i class="fa fa-angle-left fa-2x"></i></span></a></div></div><div class="orders-con"><ul class="orders-list"></ul><script id="orders-list-tmpl" type="text/html">{{if orders}}{{each orders as order}}<li order-id={{order.order_id}}><div class="order-title"><h3>訂單編號:{{order.order_id}}</h3>{{ if "WAIT_ACCEPT" == order.status }}<div class="fr order-operate"><button type="button" class="btn btn-success order-accept" data-toggle="modal" data-target="#accept-modal">接單</button><button type="button" class="btn btn-danger order-reject" data-toggle="modal" data-target="#reject-modal">拒單</button></div>{{/if}}</div><div class="order-content"><img src="{{order.img_url}}"><div class="order-text"><h3>{{order.title}}</h3><ul><li>創建時間:{{order.ctime}}</li><li>入住日期:{{order.start_date}}</li><li>離開日期:{{order.end_date}}</li><li>合計金額:¥{{(order.amount/100.0).toFixed(0)}}(共{{order.days}}晚)</li><li>訂單狀態:<span>{{if "WAIT_ACCEPT" == order.status}}待接單{{else if "WAIT_COMMENT" == order.status}}待評價{{else if "COMPLETE" == order.status}}已完成{{else if "REJECTED" == order.status}}已拒單{{/if}}</span></li>{{if "COMPLETE" == order.status}}<li>我的評價: {{order.comment}}</li>{{else if "REJECTED" == order.status}}<li>拒單原因: {{order.comment}}</li>{{/if}}</ul></div></div></li>{{/each}}{{else}}暫時沒有訂單。{{/if}}</script><div class="modal fade" id="accept-modal" tabindex="-1" role="dialog" aria-labelledby="accept-label"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span></button><h4 class="modal-title">操作提示</h4></div><div class="modal-body"><p>您確定接此訂單嗎?</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-primary modal-accept">確定接單</button></div></div></div></div><div class="modal fade" id="reject-modal" tabindex="-1" role="dialog" aria-labelledby="reject-label"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span></button><h4 class="modal-title">請輸入拒接單原因</h4></div><div class="modal-body"><textarea class="form-control" rows="3" id="reject-reason" placeholder="此處必須填寫原因"></textarea></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-primary modal-reject">確定</button></div></div></div></div></div><div class="footer"><p><span><i class="fa fa-copyright"></i></span>愛家租房 享受家的溫馨</p></div> </div><script src="/static/js/jquery.min.js"></script><script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script><script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script><script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script><script src="/static/js/template.js"></script><script src="/static/js/ihome/lorders.js"></script>
</body>
</html>