1、訂單列表渲染前端
新建OrderConfirm.vue訂單確認頁面,添加路由vue
src/router/index.js添加路由node
-
import OrderConfirm from '@/views/OrderConfirm' // 訂單確認頁面
-
-
export default new Router({
-
routes: [
-
{
-
path: '/orderConfirm', // 訂單確認頁面路由
-
name: 'OrderConfirm',
-
component: OrderConfirm
-
}
-
]
-
})
src/views/OrderConfirm.vue
要獲取訂單列表,不須要再從新寫接口,只須要使用查詢購物車列表的接口,提取出購物車列表。在渲染時頁面時,判斷選中的商品才顯示v-if="item.checked=='1'"。
對於一些價格數字不要忘記格式化,使用過濾器對價格進行格式化。ios
-
<!--列表渲染-->
-
<ul class="cart-item-list">
-
<li v-for="item in cartList" v-if="item.checked=='1'">
-
<!-- 商品圖片和商品名稱 -->
-
<div class="cart-tab-1">
-
<div class="cart-item-pic">
-
<img :src="'/static/'+item.productImage" :alt="item.productName">
-
</div>
-
<div class="cart-item-title">
-
<div class="item-name">{{item.productName}}</div>
-
</div>
-
</div>
-
<!-- 商品單價 -->
-
<div class="cart-tab-2">
-
<div class="item-price">{{item.salePrice | currency('$')}}</div>
-
</div>
-
<!-- 商品數量 -->
-
<div class="cart-tab-3">
-
<div class="item-quantity">
-
<div class="select-self">
-
<div class="select-self-area">
-
<span class="select-ipt">×{{item.productNum}}</span>
-
</div>
-
</div>
-
<div class="item-stock item-stock-no">In Stock</div>
-
</div>
-
</div>
-
<!-- 商品總金額 -->
-
<div class="cart-tab-4">
-
<div class="item-price-total">{{(item.salePrice*item.productNum) | currency('$')}}</div>
-
</div>
-
</li>
-
</ul>
-
-
-
<!-- Price count -->
-
<div class="price-count-wrap">
-
<div class="price-count">
-
<ul>
-
<li>
-
<span>Item subtotal:</span> <!-- 訂單總金額 -->
-
<span>{{subTotal | currency('$')}}</span>
-
</li>
-
<li>
-
<span>Shipping:</span> <!-- 配送費 -->
-
<span>{{shipping | currency('$')}}</span>
-
</li>
-
<li>
-
<span>Discount:</span> <!-- 折扣 -->
-
<span>{{discount | currency('$')}}</span>
-
</li>
-
<li>
-
<span>Tax:</span> <!-- 稅費 -->
-
<span>{{tax | currency('$')}}</span>
-
</li>
-
<li class="order-total-price">
-
<span>Order total:</span> <!-- 用戶支付總金額 -->
-
<span>{{orderTotal | currency('$')}}</span>
-
</li>
-
</ul>
-
</div>
-
</div>
-
-
import axios from 'axios'
-
import {currency} from '@/util/currency.js' // 對價格格式化的通用方法
-
-
export default {
-
data(){
-
return {
-
shipping: 100, // 配送費
-
discount:200, // 折扣
-
tax:400, // 扣稅
-
-
subTotal:0, // 訂單總金額(是購物車選中商品的總金額)
-
-
orderTotal:0, // 總金額+配送費-折扣+稅費 = orderTotal用戶須要支付的金額,默認爲0
-
-
cartList:[] // 購物車列表
-
}
-
},
-
mounted(){
-
this.init();
-
},
-
filters:{ // 定義局部過濾器
-
currency:currency // currency.js傳過來的本就是函數
-
},
-
methods:{
-
init(){
-
axios.get('/users/cartList').then((response)=>{ // 訂單確認列表不須要再寫接口,直接用購物車列表的接口,渲染頁面時選取選中的商品做爲訂單確認的商品
-
let res= response.data;
-
this.cartList = res.result;
-
-
this.cartList.forEach((item)=>{ // 遍歷購物車商品,獲取選中商品的總金額
-
if(item.checked == '1'){
-
this.subTotal += item.salePrice*item.productNum;
-
}
-
})
-
-
this.orderTotal = this.subTotal+this.shipping-this.discount+this.tax; // 獲取用戶最終支付的金額,(總金額+配送費-折扣+稅費)
-
})
-
}
-
}
-
}
2、建立訂單功能實現git
(1)點擊支付跳轉到支付頁面,支付是由第三方作的集成的支付,沒辦法模擬複雜的支付場景,如今只作點擊支付建立訂單和跳轉到成功頁面,支付功能省略掉。
(2)生成的訂單裏面有訂單生成的時間,首先須要有對時間格式化的函數方法。github
時間格式化server/util/util.js mongodb
-
/**
-
* Created by jacksoft on 17/4/26.
-
* 直接對Date對象原型添加方法
-
*/
-
Date.prototype.Format = function (fmt) {
-
var o = {
-
"M+": this.getMonth() + 1, //月份
-
"d+": this.getDate(), //日
-
"h+": this.getHours(), //小時
-
"m+": this.getMinutes(), //分
-
"s+": this.getSeconds(), //秒
-
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
-
"S": this.getMilliseconds() //毫秒
-
};
-
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
-
for (var k in o)
-
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
-
return fmt;
-
}
-
-
module.exports = {};
訂單生成接口 server/routes/users.js
訂單生成是將訂單信息(包含訂單Id/訂單總金額/地址信息/下單的商品信息/訂單狀態/訂單建立時間等)存入數據庫。數據庫
-
require('./../util/util'); // 引入格式化函數
-
-
// 建立訂單功能
-
router.post('/payMent', function(req,res,next){
-
// 前端傳參:訂單的地址id;訂單最終的總金額
-
var userId = req.cookies.userId,
-
addressId = req.body.addressId,
-
orderTotal = req.body.orderTotal;
-
User.findOne({userId:userId}, function(err,doc){
-
if(err){
-
res.json({
-
status:'1',
-
msg:err.message,
-
result:''
-
})
-
}else{
-
var address = '',goodsList = [];
-
// 獲取當前用戶的地址信息
-
doc.addressList.forEach((item)=>{
-
if(addressId == item.addressId){
-
address = item;
-
}
-
})
-
// 獲取當前用戶的購物車的購買商品
-
doc.cartList.filter((item)=>{
-
if(item.checked == '1'){
-
goodsList.push(item);
-
}
-
})
-
-
//建立訂單Id
-
var platform = '622'; // 平臺系統架構碼
-
var r1 = Math.floor(Math.random()*10);
-
var r2 = Math.floor(Math.random()*10);
-
-
var sysDate = new Date().Format('yyyyMMddhhmmss'); // 系統時間:年月日時分秒
-
var orderId = platform+r1+sysDate+r2; // 21位
-
-
// 訂單建立時間
-
var createDate = new Date().Format('yyyy-MM-dd hh:mm:ss');
-
-
// 生成訂單
-
var order = {
-
orderId:orderId, // 訂單id
-
orderTotal:orderTotal, // 訂單總金額(直接拿前端傳過來的參數)
-
addressInfo:address, // 地址信息
-
goodsList:goodsList, // 購買的商品信息
-
orderStatus:'1', // 訂單狀態,1成功
-
createDate:createDate // 訂單建立時間
-
}
-
-
// 訂單信息存儲到數據庫
-
doc.orderList.push(order);
-
-
doc.save(function (err1,doc1) {
-
if(err1){
-
res.json({
-
status:"1",
-
msg:err.message,
-
result:''
-
});
-
}else{
-
// 返回訂單的id和訂單的總金額給前端,下一個頁面要用到
-
res.json({
-
status:"0",
-
msg:'',
-
result:{
-
orderId:order.orderId,
-
orderTotal:order.orderTotal
-
}
-
});
-
}
-
});
-
}
-
})
-
})
src/views/OrderConfirm.vue
傳參要傳訂單的地址id和訂單的總金額
訂單的地址id從訂單頁面的路由獲取參數id值 http://localhost:8080/#/orderConfirm?addressId=100001 的100001,this.$route.query.addressId
成功時跳轉到訂單成功頁面this.$router.push({path:'/orderSuccess?orderId='+res.result.orderId}) json
-
// 點擊支付按鈕
-
payMent(){ // 點擊支付
-
-
// 從路由那裏獲取到訂單地址的id
-
// http://localhost:8080/#/orderConfirm?addressId=100001
-
var addressId = this.$route.query.addressId;
-
-
axios.post('/users/payMent',{
-
addressId:addressId,
-
orderTotal:this.orderTotal
-
}).then((response)=>{
-
let res = response.data;
-
if(res.status == '0'){
-
console.log('order created success');
-
-
// 路由跳轉到訂單成功頁面
-
this.$router.push({
-
path:'/orderSuccess?orderId='+res.result.orderId
-
})
-
}
-
})
-
}
點擊支付按鈕,請求成功,返回生成的訂單Id(orderId)和訂單總金額(orderTotal)。訂單Id=6221201801252245492,622=平臺架構碼/1=隨機數r1/20180125224549=年月日時分秒/2=隨機數r2
axios
跳轉到訂單成功頁面
訂單生成存儲到了數據庫
3、訂單成功頁面
新建訂單成功頁面組件src/views/orderSuccess.vue,添加頁面路由
src/router/index.js
-
import OrderSuccess from '@/views/OrderSuccess' // 訂單成功頁面
-
-
export default new Router({
-
routes: [
-
{
-
path: '/orderSuccess', // 訂單成功頁面
-
name: 'OrderSuccess',
-
component: OrderSuccess
-
}
-
]
-
})
後端接口server/routes/users.js,根據前端傳的訂單Id查詢訂單信息
-
//根據訂單Id查詢訂單信息
-
router.get("/orderDetail", function (req,res,next) {
-
var userId = req.cookies.userId,
-
orderId = req.param("orderId"); // 前端傳過來的訂單id
-
User.findOne({userId:userId}, function (err,userInfo) {
-
if(err){
-
res.json({
-
status:'1',
-
msg:err.message,
-
result:''
-
});
-
}else{
-
var orderList = userInfo.orderList; // orderList訂單列表
-
if(orderList.length>0){ // 說明有訂單
-
var orderTotal = 0;
-
// 遍歷訂單列表,根據訂單id獲得該訂單總金額orderTotal
-
orderList.forEach((item)=>{
-
if(item.orderId == orderId){
-
orderTotal = item.orderTotal;
-
}
-
});
-
if(orderTotal>0){
-
res.json({
-
status:'0',
-
msg:'',
-
result:{
-
orderId:orderId,
-
orderTotal:orderTotal
-
}
-
})
-
}else{
-
res.json({
-
status:'120002',
-
msg:'無此訂單',
-
result:''
-
});
-
}
-
}else{
-
res.json({
-
status:'120001',
-
msg:'當前用戶未建立訂單',
-
result:''
-
});
-
}
-
}
-
})
-
});
前端頁面初始化時get請求,傳參訂單id,返回訂單的總金額渲染頁面。訂單的id從路由獲取,http://localhost:8080/#/orderSuccess?orderId=6221201801252245492 中的orderId:this.$route.query.orderId
-
<p>
-
<span>Order ID:{{orderId}}</span>
-
<span>Order total:{{orderTotal|currency('$')}}</span>
-
</p>
-
export default {
-
data(){
-
return {
-
orderId:'', // 訂單id
-
orderTotal:0 // 訂單總金額
-
}
-
},
-
mounted(){
-
// 從路由那裏獲取到訂單id
-
// http://localhost:8080/#/orderSuccess?orderId=6221201801252245492
-
var orderId = this.$route.query.orderId;
-
console.log("orderId:"+orderId);
-
-
if(!orderId){
-
return;
-
}
-
axios.get("/users/orderDetail",{
-
params:{
-
orderId:orderId
-
}
-
}).then((response)=>{
-
let res = response.data;
-
if(res.status == '0'){
-
this.orderId = orderId;
-
this.orderTotal = res.result.orderTotal;
-
}
-
})
-
},
-
filters:{ // 定義局部過濾器
-
currency:currency // currency.js傳過來的本就是函數
-
}
-
}