diff --git a/config/api.config.js b/config/api.config.js new file mode 100644 index 0000000..860218b --- /dev/null +++ b/config/api.config.js @@ -0,0 +1,5 @@ +const isProdMode = Object.is(process.env.NODE_ENV, 'production') + +module.exports = { + baseUrl: isProdMode ? 'http://api.zhenfan.shudong.wang/api/' : 'api/' +} diff --git a/config/index.js b/config/index.js index 3a2706a..f7f8b39 100644 --- a/config/index.js +++ b/config/index.js @@ -28,6 +28,12 @@ module.exports = { assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { + '/api/**':{ + target: 'http://localhost:3000', + pathRewrite: { + '^/api': '/' + } + }, '/goods' : { target: 'http://localhost:3000' }, diff --git a/package.json b/package.json index 5fceafa..94e1062 100644 --- a/package.json +++ b/package.json @@ -71,10 +71,12 @@ "sinon": "^2.1.0", "sinon-chai": "^2.8.0", "url-loader": "^0.5.8", + "vue-axios": "^2.0.2", "vue-lazyload": "^1.0.6", "vue-loader": "^12.1.0", "vue-style-loader": "^3.0.1", "vue-template-compiler": "^2.3.3", + "vuex": "^2.4.0", "webpack": "^2.6.1", "webpack-bundle-analyzer": "^2.2.1", "webpack-dev-middleware": "^1.10.0", diff --git a/server/models/user.js b/server/models/user.js index d708b03..20d60eb 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -16,7 +16,16 @@ var userSchema = new Schema({ "checked": String } ], - "addressList":Array + "addressList":[ + { + "addressId": String, + "userName": String, + "streetName": String, + "postCode": Number, + "tel": Number, + "isDefault": Boolean + } + ] }) module.exports = mongoose.model("user",userSchema); \ No newline at end of file diff --git a/server/routes/goods.js b/server/routes/goods.js index df3381f..c16d29b 100644 --- a/server/routes/goods.js +++ b/server/routes/goods.js @@ -4,7 +4,7 @@ var mongoose = require('mongoose'); var Goods = require('../models/goods'); // 连接数据库 127.0.0.1 -mongoose.connect('mongodb://120.27.245.209:27019/shop'); +mongoose.connect('mongodb://47.93.231.75:27017/shop'); // 当数据库连接成功的时候触发 mongoose.connection.on('connected',function(){ diff --git a/server/routes/users.js b/server/routes/users.js index efeca92..dedee2e 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -1,7 +1,7 @@ var express = require('express'); var router = express.Router(); var User = require('../models/user'); - +require('./../util/util') /* GET users listing. */ router.get('/', function(req, res, next) { res.send('respond with a resource'); @@ -16,6 +16,8 @@ router.post('/login',function(req,res,next){ userName:req.body.userName, userPwd:req.body.userPwd } + + console.log(param); // 把用户名,去数据库查询,看看是否存在 User.findOne(param,function(err,doc){ if(err){ @@ -24,7 +26,7 @@ router.post('/login',function(req,res,next){ msg:'用户名或密码错误' }) }else{ - + // console.log(doc); res.cookie('userId',doc.userId,{ path:'/', maxAge:1000*60*60 @@ -172,6 +174,192 @@ router.post('/editCheckAll',function(req,res,next){ } }) }) + +// 查询用户地址接口 +//查询用户地址接口 +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; + console.log(item.addressId) + console.log(userId) + }else{ + item.isDefault = false; + } + }); + console.log(addressList); + doc.save(function (err1,doc1) { + if(err1){ + res.json({ + status:'1', + msg:err.message, + result:'' + }); + }else{ + res.json({ + status:'0', + msg:'', + result:doc1 + }); + } + }) + } + }); + } +}); + + +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'; + var r1 = Math.floor(Math.random()*10); + 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:'' + }); + } + } + }) +}); + router.get('*',function(req,res,next){ res.send('台湾是中国不可分割的一部分!'); }) diff --git a/server/util/util.js b/server/util/util.js new file mode 100644 index 0000000..4bff368 --- /dev/null +++ b/server/util/util.js @@ -0,0 +1,20 @@ +/** + * Created by jacksoft on 17/4/26. + */ +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 = {}; diff --git a/src/api/index.js b/src/api/index.js new file mode 100644 index 0000000..53d3af8 --- /dev/null +++ b/src/api/index.js @@ -0,0 +1,28 @@ +import Vue from 'vue' +import axios from 'axios' +import apiConfig from '../../config/api.config' +// import { getCookie,signOut,isLogin } from '../utils/authService' +const service = axios.create({ + baseURL: apiConfig.baseUrl +}) + +// 拦截器 +// service.interceptors.request.use(config => { +// if (isLogin()) { +// let token = getCookie('token').replace(/(^\")|(\"$)/g, ''); +// config.headers.Authorization = `Bearer ${token}`; +// return config; +// } +// return config +// }, error => { +// return Promise.reject(error) +// }) + +// service.interceptors.response.use(response => { +// return response +// }, error => { +// return Promise.reject(error) +// }) + +Vue.prototype.$http = axios +export default service diff --git a/src/components/Header.vue b/src/components/Header.vue index 8f41b9e..40b5483 100644 --- a/src/components/Header.vue +++ b/src/components/Header.vue @@ -68,6 +68,8 @@ diff --git a/src/view/Cart.vue b/src/view/Cart.vue index fb5aaa6..07ca4a6 100644 --- a/src/view/Cart.vue +++ b/src/view/Cart.vue @@ -120,7 +120,7 @@ 总价格: {{totalPrice}}
@@ -244,6 +244,13 @@ }, checkeds(){ this.checked = 1; + }, + checkOut(){ + if(this.checkedCount>0){ + this.$router.push({ + path:"/address" + }); + } } } } diff --git a/src/view/GoodsList.vue b/src/view/GoodsList.vue index 4d5ad36..87b3caa 100644 --- a/src/view/GoodsList.vue +++ b/src/view/GoodsList.vue @@ -26,7 +26,7 @@+ Order ID:{{orderId}} + Order total:{{orderTotal|currency('$')}} +
+