node
server/routes/user.js
var express = require('express');
var router = express.Router();
require('./../util/util'); // 引入时间处理工具类
var User = require('./../models/user');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/test', function(req, res, next) {
res.send('test');
});
// 登录
router.post('/login', function(req, res, next) {
// 获取前端传过来的参数 post方式用req.Body形式获取参数
var param = {
userName:req.body.userName,
userPwd:req.body.userPwd
}
User.findOne(param, function(err,doc) {
if(err){
res.json({
status:"1",
msg:err.message
});
}else{
if(doc){
res.cookie("userId",doc.userId,{ // 将用户信息存入cookie
path:'/',
maxAge: 1000*60*60
});
res.cookie("userName",doc.userName, {
path:'/',
maxAge: 1000*60*60
});
// req.session.user = doc; // 将用户信息存入session
res.json({
status:'0',
msg:'',
result:{
userName:doc.userName
}
})
}
}
});
});
// 登出接口
router.post("/logout", function (req,res,next) {
res.cookie("userId", "", { // 登出将userId设置为""
path:"/",
maxAge:-1 // 设置位过期
})
res.json({
status:"0",
msg:'',
result:''
})
})
// 校验用户信息
router.get("/checkLogin", function (req,res,next) {
if(req.cookies.userId){
res.json({
status:'0',
msg:'',
result: req.cookies.userName || '' // 获取cookeie req.cookies.属性
});
}else{ // 取不到就说明当前没有登录
res.json({
status:'1',
msg:'未登录',
result:''
});
}
})
// 查询当前用户的购物车数据
router.get("/cartList", function (req,res,next) {
var userId = req.cookies.userId;
console.log(userId);
User.findOne({userId:userId}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
if(doc){
res.json({
status:'0',
msg:'',
result:doc.cartList
});
}
}
});
});
// 购物车删除
router.post("/cartDel", function (req,res,next) {
// 获取浏览器的cookie 以及用户传递过来的参数 productId
var userId = req.cookies.userId,productId = req.body.productId;
User.update({
userId:userId
},{
$pull:{
'cartList':{
'productId':productId
}
}
}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
});
});
//修改购物车商品数量
router.post("/cartEdit", function (req,res,next) {
var userId = req.cookies.userId, // 获取用户客户端的userId
productId = req.body.productId, // 获取用户传的参数商品id
productNum = req.body.productNum, // 获取用户传的参数商品id
checked = req.body.checked; // 获取用户传的参数是否选中
User.update({
"userId":userId,
"cartList.productId":productId},{
"cartList.$.productNum":productNum,
"cartList.$.checked":checked,
}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
})
});
// 商品全选不选
router.post("/editCheckAll", function (req,res,next) {
var userId = req.cookies.userId,
checkAll = req.body.checkAll?'1':'0';
User.findOne({userId:userId}, function (err,user) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
if(user){
user.cartList.forEach((item)=>{
item.checked = checkAll;
})
user.save(function (err1,doc) {
if(err1){
res.json({
status:'1',
msg:err1,message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:'suc'
});
}
})
}
}
});
});
// 用户地址模块开始-----------------------------------------------------------------------------------------------
// 查询用户地址接口
router.get("/addressList", function (req,res,next) {
var userId = req.cookies.userId;
User.findOne({userId:userId}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:doc.addressList
});
}
})
});
// 设置默认地址
router.post("/setDefault", function (req,res,next) {
var userId = req.cookies.userId,
addressId = req.body.addressId;
if(!addressId){
res.json({
status:'1003',
msg:'addressId is null',
result:''
});
}else{
User.findOne({userId:userId}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
var addressList = doc.addressList;
addressList.forEach((item)=>{
if(item.addressId == addressId){
item.isDefault = true;
}else{
item.isDefault = false;
}
});
doc.save(function (err1,doc1) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:''
});
}
})
}
});
}
});
// 删除地址接口
router.post("/delAddress", function (req,res,next) {
var userId = req.cookies.userId,addressId = req.body.addressId;
User.update({
userId:userId
},{
$pull:{ // 删除子文档元素
'addressList':{
'addressId':addressId
}
}
}, function (err,doc) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
res.json({
status:'0',
msg:'',
result:''
});
}
});
});
// 用户地址模块结束-----------------------------------------------------------------------------------------------
// 创建订单模块开始-----------------------------------------------------------------------------------------------
// 创建订单
router.post("/payMent", function (req,res,next) {
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);
}
});
var platform = '622'; //平台变化 622代表当前系统平台的码
var r1 = Math.floor(Math.random()*10); // 获取两个0-9的随机数
var r2 = Math.floor(Math.random()*10);
var sysDate = new Date().Format('yyyyMMddhhmmss'); // 生成系统时间
var createDate = new Date().Format('yyyy-MM-dd hh:mm:ss'); // 生成订单创建时间
var orderId = platform+r1+sysDate+r2; // 生成订单编号
var order = {
orderId:orderId,
orderTotal:orderTotal,
addressInfo:address,
goodsList:goodsList,
orderStatus:'1',
createDate:createDate
};
doc.orderList.push(order);
doc.save(function (err1,doc1) {
if(err1){
res.json({
status:"1",
msg:err.message,
result:''
});
}else{
res.json({
status:"0",
msg:'',
result:{
orderId:order.orderId,
orderTotal:order.orderTotal
}
});
}
});
}
})
});
//根据订单Id查询订单信息
router.get("/orderDetail", function (req,res,next) {
var userId = req.cookies.userId,orderId = req.param("orderId");
User.findOne({userId:userId}, function (err,userInfo) {
if(err){
res.json({
status:'1',
msg:err.message,
result:''
});
}else{
var orderList = userInfo.orderList;
if(orderList.length>0){
var orderTotal = 0;
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:''
});
}
}
})
});
module.exports = router;
vue 前端
<template>
<div>
<nav-header></nav-header>
<div class="container">
<div class="page-title-normal">
<h2 class="page-title-h2"><span>check out</span></h2>
</div>
<!-- 进度条 -->
<div class="check-step">
<ul>
<li class="cur"><span>Confirm</span> address</li>
<li class="cur"><span>View your</span> order</li>
<li class="cur"><span>Make</span> payment</li>
<li class="cur"><span>Order</span> confirmation</li>
</ul>
</div>
<div class="order-create">
<div class="order-create-pic"><img src="/static/ok-2.png" alt=""></div>
<div class="order-create-main">
<h3>Congratulations! <br>Your order is under processing!</h3>
<p>
<span>Order ID:{{orderId}}</span>
<span>Order total:{{orderTotal|currency('$')}}</span>
</p>
<div class="order-create-btn-wrap">
<div class="btn-l-wrap">
<router-link class="btn btn--m" to="/cart">Cart List</router-link>
</div>
<div class="btn-r-wrap">
<router-link class="btn btn--m" to="/">Goods List</router-link>
</div>
</div>
</div>
</div>
</div>
<nav-footer></nav-footer>
</div>
</template>
<script>
import NavHeader from './../components/NavHeader'
import NavFooter from './../components/NavFooter'
import NavBread from './../components/NavBread'
import {currency} from './../util/currency'
import axios from 'axios'
export default{
data(){
return{
orderId:'',
orderTotal:0
}
},
components:{
NavHeader,
NavFooter,
NavBread
},
filters:{
currency:currency
},
mounted(){
var orderId = this.$route.query.orderId;
console.log("orderId:"+orderId);
if(!orderId){
return;
}
axios.get("/users/orderDetail",{ // post请求直接传参,get请求需要使用params的形式
params:{
orderId:orderId
}
}).then((response)=>{
let res = response.data;
if(res.status=='0'){
this.orderId = orderId;
this.orderTotal = res.result.orderTotal;
}
});
}
}
</script>