Skip to content

Commit

Permalink
Merge pull request #67 from itguide/v2
Browse files Browse the repository at this point in the history
add address
  • Loading branch information
starkwang authored Sep 14, 2017
2 parents b0a5c51 + 7ac488e commit 786343b
Show file tree
Hide file tree
Showing 20 changed files with 964 additions and 17 deletions.
5 changes: 5 additions & 0 deletions config/api.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isProdMode = Object.is(process.env.NODE_ENV, 'production')

module.exports = {
baseUrl: isProdMode ? 'http://api.zhenfan.shudong.wang/api/' : 'api/'
}
6 changes: 6 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module.exports = {
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api/**':{
target: 'http://localhost:3000',
pathRewrite: {
'^/api': '/'
}
},
'/goods' : {
target: 'http://localhost:3000'
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion server/routes/goods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
192 changes: 190 additions & 2 deletions server/routes/users.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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){
Expand All @@ -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
Expand Down Expand Up @@ -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('台湾是中国不可分割的一部分!');
})
Expand Down
20 changes: 20 additions & 0 deletions server/util/util.js
Original file line number Diff line number Diff line change
@@ -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 = {};
28 changes: 28 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -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
25 changes: 21 additions & 4 deletions src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@

<script>
import axios from 'axios'
import { mapState, mapActions } from 'vuex'
export default {
name: 'Header',
data(){
Expand All @@ -76,18 +78,24 @@
userName:'',
userPwd:'',
errorTip:false,
nickName:''
// nickName:''
}
},
mounted: function(){
this.checkLogin();
// console.log(this.nickName)
},
computed: {
...mapState(['nickName'])
},
methods:{
checkLogin(){
axios.get("/users/checkLogin").then((response) => {
let res = response.data;
if(res.status == 0){
this.nickName = res.result;
this.$store.commit("updateUserInfo",res.result);
// this.nickName = res.result;
this.loginModalFlag = false;
}
})
},
Expand All @@ -98,14 +106,22 @@
return false;
}
let param = {
userName:this.userName,
userPwd:this.userPwd
}
// this.$store.dispatch('userLogin',param)
axios.post("/users/login",{
userName:this.userName,
userPwd:this.userPwd
}).then((response) =>{
let res = response.data;
if(res.status == '0'){
this.$store.commit("updateUserInfo",res.result.userName);
this.loginModalFlag = false;
this.nickName = res.result.userName;
}else{
this.errorTip = true;
}
Expand All @@ -116,7 +132,8 @@
axios.post('/users/logout').then((response) => {
let res = response.data;
if(res.status == '0'){
this.nickName = '';
this.$store.commit("updateUserInfo",res.result.userName);
// this.nickName = '';
}
})
}
Expand Down
Loading

0 comments on commit 786343b

Please sign in to comment.