版权声明: https://blog.csdn.net/xyphf/article/details/84728205
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import VueLazyLoad from 'vue-lazyload' // 引入图片懒加载插件
import infiniteScroll from 'vue-infinite-scroll'
import {currency} from './util/currency' // 引入金额过滤器
Vue.config.productionTip = false
Vue.use(infiniteScroll);
Vue.use(Vuex);
// 使用并配置loading图片
Vue.use(VueLazyLoad,{
loading:"/static/loading-svg/loading-bars.svg"
});
Vue.filter("currency",currency); // 定义全局金额过滤器
const store = new Vuex.Store({
state: {
nickName:'',
cartCount:0
},
mutations: {
//更新用户信息
updateUserInfo(state, nickName) {
state.nickName = nickName;
},
updateCartCount(state,cartCount){
state.cartCount += cartCount;
},
initCartCount(state,cartCount){
state.cartCount = cartCount;
}
}
});
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
template: '<App/>',
components: { App }
});
NavHeader.vue 登录头文件
<template>
<header class="header">
<symbol id="icon-cart" viewBox="0 0 38 32">
<title>cart</title>
<path class="path1" d="M37.759 0h-4.133c-0.733 0.004-1.337 0.549-1.434 1.255l-0.546 4.342c-0.081 0.484-0.496 0.849-0.997 0.849-0.005 0-0.009-0-0.014-0h-27.604c-0.003 0-0.007-0-0.011-0-1.674 0-3.031 1.357-3.031 3.031 0 0.34 0.056 0.666 0.159 0.971l2.52 8.062c0.385 1.194 1.486 2.043 2.785 2.043 0.126 0 0.25-0.008 0.372-0.023l22.983 0.002c0.515 0.131 0.626 0.768 0.626 1.283 0.005 0.044 0.009 0.095 0.009 0.146 0 0.501-0.294 0.933-0.718 1.134l-22.439 0.003c-0.354 0-0.642 0.287-0.642 0.642s0.287 0.642 0.642 0.642h22.745l0.131-0.071c0.919-0.392 1.551-1.287 1.551-2.33 0-0.058-0.002-0.116-0.006-0.173 0.021-0.108 0.033-0.24 0.033-0.376 0-1.072-0.732-1.973-1.724-2.23l-23.357-0.004c-0.063 0.008-0.135 0.013-0.209 0.013-0.719 0-1.332-0.455-1.566-1.093l-2.53-8.095c-0.048-0.154-0.076-0.332-0.076-0.515 0-0.973 0.782-1.764 1.752-1.778h27.657c1.159-0.004 2.112-0.883 2.232-2.011l0.547-4.345c0.010-0.083 0.078-0.147 0.161-0.152l4.133-0c0.354 0 0.642-0.287 0.642-0.642s-0.287-0.642-0.642-0.642z"></path>
<path class="path2" d="M31.323 9.69c-0.022-0.003-0.048-0.004-0.074-0.004-0.328 0-0.598 0.248-0.633 0.567l-0.809 7.268c-0.003 0.022-0.004 0.048-0.004 0.074 0 0.328 0.248 0.598 0.567 0.633l0.074 0c0.001 0 0.003 0 0.004 0 0.327 0 0.596-0.246 0.632-0.563l0.809-7.268c0.003-0.022 0.004-0.048 0.004-0.074 0-0.328-0.248-0.598-0.567-0.633z"></path>
<path class="path3" d="M27.514 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM27.514 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
<path class="path4" d="M9.599 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM9.599 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
</symbol>
<div class="navbar">
<div class="navbar-left-container">
<a href="/">
<img class="navbar-brand-logo" src="static/logo.png"></a>
</div>
<div class="navbar-right-container" style="display: flex;">
<div class="navbar-menu-container">
<!--<a href="/" class="navbar-link">我的账户</a>-->
<span class="navbar-link" v-text="nickName" v-if="nickName"></span>
<!-- <span v-text="nickName" v-if="nickName"></span> -->
<a href="javascript:void(0)" class="navbar-link" @click="loginModalFlag=true" v-if="!nickName">Login</a>
<a href="javascript:void(0)" class="navbar-link" @click="logOut" v-if="nickName">Logout</a>
<div class="navbar-cart-container">
<span class="navbar-cart-count" v-text="cartCount" v-if="cartCount"></span>
<a class="navbar-link navbar-cart-link" href="/#/cart">
<svg class="navbar-cart-logo">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-cart"></use>
</svg>
</a>
</div>
</div>
<div class="md-modal modal-msg md-modal-transition" v-bind:class="{'md-show':loginModalFlag}">
<div class="md-modal-inner">
<div class="md-top">
<div class="md-title">Login in</div>
<button class="md-close" @click="loginModalFlag=false">Close</button>
</div>
<div class="md-content">
<div class="confirm-tips">
<div class="error-wrap">
<span class="error error-show" v-show="errorTip">用户名或者密码错误</span>
</div>
<ul>
<li class="regi_form_input">
<i class="icon IconPeople"></i>
<input type="text" tabindex="1" name="loginname" v-model="userName" class="regi_login_input regi_login_input_left" placeholder="User Name" data-type="loginname">
</li>
<li class="regi_form_input noMargin">
<i class="icon IconPwd"></i>
<input type="password" tabindex="2" name="password" v-model="userPwd" class="regi_login_input regi_login_input_left login-input-no input_text" placeholder="Password" @keyup.enter="login">
</li>
</ul>
</div>
<div class="login-wrap">
<a href="javascript:;" class="btn-login" @click="login">登 录</a>
</div>
</div>
</div>
</div>
<div class="md-overlay" v-if="loginModalFlag" @click="loginModalFlag=false"></div>
</div>
</div>
</header>
</template>
<script>
import './../assets/css/login.css'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
data(){
return{
userName:'admin', // 用户名
userPwd:'123456', // 密码
errorTip:false, // 校验
loginModalFlag:false // 是否显示遮罩层
}
},
computed:{
// 三个点对数组的解构 mapState是vuex的一个函数
...mapState(['nickName','cartCount'])
// nickName(){
// return this.$store.state.nickName;
// },
// cartCount(){
// return this.$store.state.cartCount;
// }
},
mounted() {
this.checkLogin();
},
methods: {
checkLogin(){
axios.get("/users/checkLogin").then((response)=>{
var res = response.data;
var path = this.$route.pathname;
if(res.status=="0"){
this.$store.commit("updateUserInfo",res.result);
this.getCartCount();
this.loginModalFlag = false;
}
});
},
login(){ // 登录方法
if(!this.userName || !this.userPwd){
this.errorTip = true;
return;
}
axios.post("/users/login",{
userName:this.userName,
userPwd:this.userPwd
}).then((response)=>{
let res = response.data;
if(res.status=='0'){
this.errorTip = false;
this.loginModalFlag = false;
this.$store.commit("updateCartCount",res.result.userName);
this.getCartCount();
// this.nickName = res.result.userName;
}else{
this.errorTip = true;
}
});
},
logOut(){
axios.post("/users/logout").then((response)=>{
let res = response.data;
if(res.status=="0"){
// this.nickName = '';
this.$store.commit("updateUserInfo",'');
}
})
},
getCartCount(){
axios.get("users/getCartCount").then(res=>{
var res = res.data;
this.$store.commit("initCartCount",res.result);
});
}
}
}
</script>
GoodsList.vue 商品列表闻文件
<template>
<div>
<nav-header></nav-header>
<nav-bread>
<span>Goods</span>
</nav-bread>
<div class="accessory-result-page accessory-page">
<div class="container">
<div class="filter-nav">
<span class="sortby">Sort by:</span>
<a href="javascript:void(0)" class="default cur">Default</a>
<a @click="showFilterPop" href="javascript:void(0)" class="price">
Price
<svg class="icon-arrow-short"
v-bind:class="{'sort-up':sortFlag}">
<use xlink:href="#icon-arrow-short"></use>
</svg>
</a>
<a href="javascript:void(0)" class="filterby stopPop" @click="showFilterPop">Filter by</a>
</div>
<div class="accessory-result">
<!-- filter -->
<div class="filter stopPop" id="filter">
<dl class="filter-price">
<dt>Price:</dt>
<dd><a href="javascript:void(0)" v-bind:class="{'cur':priceChecked=='all'}" @click="priceChecked='all'">All</a></dd>
<dd v-for="(price,index) in priceFilter" :key="index">
<a href="javascript:void(0)" @click="setPriceFilter(index)" v-bind:class="{'cur':priceChecked==index}">{{ price.startPrice }}-{{ price.endPrice }}</a>
</dd>
</dl>
</div>
<!-- search result accessories list -->
<div class="accessory-list-wrap">
<div class="accessory-list col-4">
<ul>
<li v-for="(item,index) in goodsList" :key="index">
<div class="pic">
<a href="#"><img v-lazy="'/static/'+item.productImage" alt=""></a>
</div>
<div class="main">
<div class="name">{{item.productName}}</div>
<div class="price">{{item.salePrice}}</div>
<div class="btn-area">
<a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入购物车</a>
</div>
</div>
</li>
</ul>
<div class="load-more" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="30">
<img src="./../assets/loading-spinning-bubbles.svg" alt="" v-show="loading">
</div>
</div>
</div>
</div>
</div>
</div>
<!-- <div class="md-overlay" v-show="overLayFlag" @click="closePop"></div> -->
<modal v-bind:mdShow="mdShow" v-on:close="closeModal">
<p slot="message">
请先登录,否则无法加入到购物车中!
</p>
<div slot="btnGroup">
<a class="btn btn--m" href="javascript:;" @click="mdShow = false">关闭</a>
</div>
</modal>
<modal v-bind:mdShow="mdShowCart" v-on:close="closeModal">
<p slot="message">
<svg class="icon-status-ok">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-status-ok"></use>
</svg>
<span>加入购物车成功!</span>
</p>
<div slot="btnGroup">
<a class="btn btn--m" href="javascript:;" @click="mdShowCart = false">继续购物</a>
<router-link class="btn btn--m" href="javascript:;" to="/cart">查看购物车</router-link>
</div>
</modal>
<nav-footer></nav-footer>
</div>
</template>
<script>
import './../assets/css/base.css'
import './../assets/css/product.css'
import './../assets/css/checkout.css'
import './../assets/css/login.css'
import NavHeader from '@/components/NavHeader.vue'
import NavFooter from '@/components/NavFooter.vue'
import NavBread from '@/components/NavBread.vue'
import Modal from './../components/Modal'
import axios from 'axios'
export default {
data () {
return {
goodsList: [],
sortFlag: true, // 排序字段
page:1, // 默认页
pageSize: 8, // 默认一页显示8条数据
busy:true,
loading:true,
mdShow:false, // 默认关闭全局模态框
mdShowCart:false,
// priceLevel:this.priceChecked, // 筛选选择价格
priceFilter:[
{
startPrice:'0.00',
endPrice:'100.00'
},
{
startPrice:'100.00',
endPrice:'500.00'
},
{
startPrice:'500.00',
endPrice:'1000.00'
},
{
startPrice:'1000.00',
endPrice:'5000.00'
}
],
priceChecked:'all'
// filterBy:false,
// overLayFlag:false
}
},
components: {
NavHeader,
NavFooter,
NavBread,
Modal
},
mounted () {
this.getGoodList();
},
methods: {
getGoodList (flag) {
var param = { // 分页及排序参数
page:this.page,
pageSize: this.pageSize,
sort: this.sortFlag?1:-1, // 升序是1 降序是-1
priceLevel: this.priceChecked
}
this.loading = true;
axios.get("/goods/list", {params:param}).then((result)=>{
let res = result.data;
this.loading = false;
if(res.status=="0"){
if(flag){ // 如果是分页需要累计
// 将数组进行串联起来 concat表示将数组连接起来
this.goodsList = this.goodsList.concat(res.result.list);
if(res.result.count == 0){
this.busy = true;
} else {
this.busy = false;
}
} else { // 普通的请求
this.goodsList = res.result.list;
this.busy = false;
}
} else {
this.goodsList = [];
}
});
},
showFilterPop() { // 排序
// this.filterBy = true;
// this.overLayFlag = true;
this.sortFlag = ! this.sortFlag; // 点击后升序降序切换
this.page = 1; // 点击升降序后默认从第一页显示
this.getGoodList(); // 重新加载一次
},
loadMore () {
this.busy = true; // 在请求成功之前禁止再滚动加载
// 鼠标滚动实在太快了,滚动一秒钟可能有上千个请求,这样对服务器压力太大 因此必须要通过setTimeout来控制
// 只有第一个请求结束以后才能请求第二个
setTimeout(() => {
this.page++; // 滚动之后要给page++
this.getGoodList(true);
}, 500);
},
setPriceFilter(index) { // 价格过滤
this.priceChecked = index;
this.page = 1; // 价格过滤后分页从第一页重新开始
this.getGoodList();
// this.closePop();
},
addCart(productId) { // 添加购物车
axios.post("/goods/addCart",{
productId:productId
}).then((res)=>{
console.log(res);
if(res.data.status==0){
this.mdShowCart = true;
this.$store.commit("updateCartCount",1);
}else{
// alert("msg:"+res.data.msg);
this.mdShow = true;
}
});
},
closeModal () {
this.mdShow = false;
}
// ,
// closePop() {
// this.filterBy = false;
// this.overLayFlag = false;
// }
}
}
</script>
Cart.vue 购物车文件
<template>
<div>
<nav-header></nav-header>
<nav-bread>
<span>My Cart</span>
</nav-bread>
<svg style="position: absolute; width: 0; height: 0; overflow: hidden;" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="icon-add" viewBox="0 0 32 32">
<title>add2</title>
<path class="path1"
d="M15 17h-13.664c-0.554 0-1.002-0.446-1.002-1 0-0.552 0.452-1 1.002-1h13.664v-13.664c0-0.554 0.446-1.002 1-1.002 0.552 0 1 0.452 1 1.002v13.664h13.664c0.554 0 1.002 0.446 1.002 1 0 0.552-0.452 1-1.002 1h-13.664v13.664c0 0.554-0.446 1.002-1 1.002-0.552 0-1-0.452-1-1.002v-13.664z"></path>
</symbol>
<symbol id="icon-ok" viewBox="0 0 32 32">
<title>ok</title>
<path class="path1"
d="M31.020 0.438c-0.512-0.363-1.135-0.507-1.757-0.406s-1.166 0.435-1.529 0.937l-17.965 24.679-5.753-5.67c-0.445-0.438-1.035-0.679-1.664-0.679s-1.219 0.241-1.664 0.679c-0.917 0.904-0.917 2.375 0 3.279l7.712 7.6c0.438 0.432 1.045 0.681 1.665 0.681l0.195-0.008c0.688-0.057 1.314-0.406 1.717-0.959l19.582-26.9c0.754-1.038 0.512-2.488-0.538-3.233z"></path>
</symbol>
<symbol id="icon-edit" viewBox="0 0 32 32">
<title>edit</title>
<path class="path1"
d="M28.287 8.51l-4.805-4.806 0.831-0.831c0.472-0.472 1.086-0.777 1.564-0.777 0.248 0 0.452 0.082 0.622 0.253l3.143 3.144c0.539 0.54 0.133 1.529-0.524 2.186l-0.831 0.831zM26.805 9.992l-1.138 1.138-4.805-4.806 1.138-1.138 4.805 4.806zM24.186 12.612l-14.758 14.762-4.805-4.806 14.758-14.762 4.805 4.806zM7.379 28.288l-4.892 1.224 1.223-4.894 3.669 3.67zM31.123 4.011l-3.143-3.144c-0.567-0.567-1.294-0.867-2.103-0.867-1.036 0-2.174 0.52-3.045 1.391l-20.429 20.436c-0.135 0.134-0.23 0.302-0.276 0.487l-2.095 8.385c-0.089 0.355 0.017 0.736 0.276 0.995 0.198 0.198 0.461 0.307 0.741 0.307 0.085 0 0.171-0.010 0.254-0.031l8.381-2.096c0.185-0.047 0.354-0.142 0.487-0.276l20.43-20.436c1.409-1.41 2.042-3.632 0.524-5.15v0z"></path>
</symbol>
<symbol id="icon-del" viewBox="0 0 32 32">
<title>delete</title>
<path class="path1"
d="M11.355 4.129v-2.065h9.29v2.065h-9.29zM6.194 29.935v-23.742h19.613v23.742h-19.613zM30.968 4.129h-8.258v-3.097c0-0.569-0.463-1.032-1.032-1.032h-11.355c-0.569 0-1.032 0.463-1.032 1.032v3.097h-8.258c-0.569 0-1.032 0.463-1.032 1.032s0.463 1.032 1.032 1.032h3.097v24.774c0 0.569 0.463 1.032 1.032 1.032h21.677c0.569 0 1.032-0.463 1.032-1.032v-24.774h3.097c0.569 0 1.032-0.463 1.032-1.032s-0.463-1.032-1.032-1.032v0z"></path>
<path class="path2"
d="M10.323 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
<path class="path3"
d="M16 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
<path class="path4"
d="M21.677 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
</symbol>
<symbol id="icon-clock" viewBox="0 0 32 32">
<title>clock</title>
<path class="path1" fill="#605f5f"
d="M16 29c-7.168 0-13-5.831-13-13s5.832-13 13-13c7.168 0 13 5.832 13 13s-5.832 13-13 13zM16 0c-8.822 0-16 7.178-16 16s7.178 16 16 16c8.822 0 16-7.178 16-16s-7.178-16-16-16z"></path>
<path class="path2" fill="#605f5f"
d="M23.958 21.837l-6.958-6.489v-6.282c0-0.827-0.673-1.5-1.5-1.5s-1.5 0.673-1.5 1.5v6.934c0 0.414 0.174 0.814 0.477 1.098l7.435 6.934c0.279 0.259 0.642 0.402 1.023 0.402 0.415 0 0.814-0.174 1.096-0.477 0.564-0.605 0.532-1.555-0.073-2.12z"></path>
</symbol>
</defs>
</svg>
<div class="container">
<div class="cart">
<div class="page-title-normal">
<h2 class="page-title-h2"><span>My Cart</span></h2>
</div>
<div class="item-list-wrap">
<div class="cart-item">
<div class="cart-item-head">
<ul>
<li>Items</li>
<li>Price</li>
<li>Quantity</li>
<li>Subtotal</li>
<li>Edit</li>
</ul>
</div>
<ul class="cart-item-list">
<li v-for="(item,index) in cartList" :key="index">
<div class="cart-tab-1">
<div class="cart-item-check">
<a href="javascipt:;" class="checkbox-btn item-check-btn" v-bind:class="{'check':item.checked=='1'}" @click="editCart('checked',item)">
<svg class="icon icon-ok">
<use xlink:href="#icon-ok"></use>
</svg>
</a>
</div>
<div class="cart-item-pic">
<img v-lazy="'/static/'+item.productImage" v-bind: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 select-self-open">
<div class="select-self-area">
<a class="input-sub" @click="editCart('minu',item)">-</a>
<span class="select-ipt">{{item.productNum}}</span>
<a class="input-add" @click="editCart('add',item)">+</a>
</div>
</div>
</div>
</div>
<div class="cart-tab-4">
<div class="item-price-total">{{(item.productNum*item.salePrice) | currency('$') }}</div>
</div>
<div class="cart-tab-5">
<div class="cart-item-opration">
<a href="javascript:;" class="item-edit-btn" @click="delCartConfirm(item)">
<svg class="icon icon-del">
<use xlink:href="#icon-del"></use>
</svg>
</a>
</div>
</div>
</li>
</ul>
</div>
</div>
<div class="cart-foot-wrap">
<div class="cart-foot-inner">
<div class="cart-foot-l">
<div class="item-all-check">
<a href="javascipt:;" @click="toggleCheckAll">
<span class="checkbox-btn item-check-btn" v-bind:class="{'check':checkAllFlag}">
<svg class="icon icon-ok"><use xlink:href="#icon-ok"/></svg>
</span>
<span>Select all</span>
</a>
</div>
</div>
<div class="cart-foot-r">
<div class="item-total">
Item total: <span class="total-price">{{totalPrice | currency('$')}}</span>
</div>
<div class="btn-wrap">
<a class="btn btn--red" v-bind:class="{'btn--dis':checkedCount==0}" @click="checkOut">Checkout</a>
</div>
</div>
</div>
</div>
</div>
</div>
<Modal :mdShow="modalConfirm" @close="closeModal">
<p slot="message">你确认要删除此条数据吗?</p>
<div slot="btnGroup">
<a class="btn btn--m" href="javascript:;" @click="delCart">确认</a>
<a class="btn btn--m btn--red" href="javascript:;" @click="modalConfirm = false">关闭</a>
</div>
</Modal>
<nav-footer></nav-footer>
</div>
</template>
<style>
.input-sub,.input-add{
min-width: 40px;
height: 100%;
border: 0;
color: #605F5F;
text-align: center;
font-size: 16px;
overflow: hidden;
display: inline-block;
background: #f0f0f0;
}
.item-quantity .select-self-area{
background:none;
border: 1px solid #f0f0f0;
}
.item-quantity .select-self-area .select-ipt{
display: inline-block;
padding:0 3px;
width: 30px;
min-width: 30px;
text-align: center;
}
</style>
<script>
import './../assets/css/checkout.css'
import NavHeader from './../components/NavHeader'
import NavFooter from './../components/NavFooter'
import NavBread from './../components/NavBread'
import Modal from './../components/Modal'
import axios from 'axios'
export default{
data(){
return{
cartList:[],
delItem:{},
modalConfirm:false
}
},
mounted(){
this.init();
},
computed:{ // 实时计算
checkAllFlag() { // 如果选中的数目和购物车列表的数目相等就是全选
return this.checkedCount == this.cartList.length;
},
checkedCount(){ // 获取选中的数目
var i = 0;
this.cartList.forEach((item)=>{
if(item.checked=='1')i++;
})
return i;
},
totalPrice(){ // 计算购物车选中的总金额
var money = 0;
this.cartList.forEach((item)=>{
if(item.checked=='1'){
// 单价成语数量
money += parseFloat(item.salePrice)*parseInt(item.productNum);
}
})
return money;
}
},
components:{
NavHeader,
NavFooter,
NavBread,
Modal
},
methods:{
init(){
axios.get("/users/cartList").then((response)=>{
let res = response.data;
this.cartList = res.result;
});
},
closeModal () {
this.modalConfirm = false;
},
delCartConfirm(item){
this.delItem = item;
this.modalConfirm = true;
},
delCart(){
axios.post("/users/cartDel",{
productId:this.delItem.productId
}).then((response)=>{
let res = response.data;
if(res.status == '0'){
this.modalConfirm = false;
this.$store.commit("updateCartCount",-this.delItem.productNum);
this.init();
}
});
},
toggleCheckAll(){ // 全选反选
var flag = !this.checkAllFlag;
this.cartList.forEach((item)=>{
item.checked = flag?'1':'0';
})
axios.post("/users/editCheckAll",{
checkAll:flag
}).then((response)=>{
let res = response.data;
if(res.status=='0'){
console.log("update suc");
}
})
},
editCart(flag,item){ // 购物车商品数量加减
if(flag=='add'){
item.productNum++;
}else if(flag=='minu'){
if(item.productNum<=1){ // 商品不得小于等于1
return;
}
item.productNum--;
}else{
item.checked = item.checked=="1"?'0':'1';
}
axios.post("/users/cartEdit",{
productId:item.productId,
productNum:item.productNum,
checked:item.checked
}).then((response)=>{
let res = response.data;
let num = 0;
if(flag=='add'){
num = 1;
}else if(flag=='minu'){
num = -1;
}
this.$store.commit("updateCartCount",num);
})
},
checkOut() { // 跳转至收货地址列表
if(this.checkedCount>0){ // 当选中商品数量小于0时不允许跳转
this.$router.push({
path:"/address"
});
}
}
}
}
</script>
node后端 users.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("/getCartCount", function (req,res,next) {
if(req.cookies && req.cookies.userId){
console.log("userId:"+req.cookies.userId);
var userId = req.cookies.userId;
User.findOne({"userId":userId}, function (err,doc) {
if(err){
res.json({
status:"0",
msg:err.message
});
}else{
let cartList = doc.cartList;
let cartCount = 0;
cartList.map(function(item){
cartCount += parseFloat(item.productNum);
});
res.json({
status:"0",
msg:"",
result:cartCount
});
}
});
}else{
res.json({
status:"0",
msg:"当前用户不存在"
});
}
});
// 查询当前用户的购物车数据
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;