node实现购物车功能 快来打我* 2022-04-12 07:47 285阅读 0赞 node服务端 配置转发代理 config/index.js // see http://vuejs-templates.github.io/webpack for documentation. var path = require('path') module.exports = { build: { env: require('./prod.env'), index: path.resolve(__dirname, '../dist/index.html'), assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: 'baidu.com', productionSourceMap: true, // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report }, dev: { env: require('./dev.env'), port: 8989, autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/goods':{ target:'http://localhost:3000' }, '/goods/*':{ target:'http://localhost:3000' }, '/users/*':{ // **表示匹配这个路由下面所有的路由接口 target:'http://localhost:3000' } }, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false } } server/bin/www #!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('server:server'); var http = require('http'); /** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } return false; } /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } server/models/user.js var mongoose = require('mongoose') var userSchema = mongoose.Schema({ "userId":String, // 用户Id "userName":String, // 用户名 "userPwd":String, // 用户密码 "orderList":Array, // 订单列表 "cartList":[ // 购物车列表 { "productId": String, // 商品Id "productName": String, // 商品名称 "salePrice":String, // 商品价格 "productImage":String, // 图片地址 "checked":String, // 是否选中 "productNum":String // 商品数量 } ], "addressList":Array // 用户地址列表 }); // 通过module.exports进行输出,这样才能加载到 三个参数分别是 模型名,userSchema名,管理数据库集合名 module.exports = mongoose.model("User",userSchema,"users"); server/models/goods.js var mongoose = require('mongoose') var Schema = mongoose.Schema; var produtSchema = new Schema({ "productId":String, "productName":String, "salePrice":Number, "checked":String, "productNum": Number, "checked":String, "prodcutImage": String }); module.exports = mongoose.model('Good',produtSchema); server/routes/user.js var express = require('express'); var router = express.Router(); 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' }); } }) } } }); }); module.exports = router; \-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》-》 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 VueLazyLoad from 'vue-lazyload' // 引入图片懒加载插件 import infiniteScroll from 'vue-infinite-scroll' import {currency} from './util/currency' // 引入金额过滤器 Vue.config.productionTip = false Vue.use(infiniteScroll) // 使用并配置loading图片 Vue.use(VueLazyLoad,{ loading:"/static/loading-svg/loading-bars.svg" }); Vue.filter("currency",currency); // 定义全局金额过滤器 /* eslint-disable no-new */ new Vue({ el: '#app', router, template: '<App/>', components: { App } }); 金额数据格式化工具 util/currency.js const digitsRE = /(\d{3})(?=\d)/g //金额,货币符号,小数点位数 export function currency (value, currency, decimals) { value = parseFloat(value) if (!isFinite(value) || (!value && value !== 0)) return '' currency = currency != null ? currency : '$' decimals = decimals != null ? decimals : 2 var stringified = Math.abs(value).toFixed(decimals) var _int = decimals ? stringified.slice(0, -1 - decimals) : stringified var i = _int.length % 3 var head = i > 0 ? (_int.slice(0, i) + (_int.length > 3 ? ',' : '')) : '' var _float = decimals ? stringified.slice(-1 - decimals) : '' var sign = value < 0 ? '-' : '' return sign + currency + head + _int.slice(i).replace(digitsRE, '$1,') + _float } 购物车 views/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.productId)"> <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" >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:[], productId:'', 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(productId){ this.productId = productId; this.modalConfirm = true; }, delCart(){ axios.post("/users/cartDel",{ // 删除购物车 productId:this.productId }).then((response)=>{ let res = response.data; if(res.status == '0'){ this.modalConfirm = false; 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; if(res.status=="0"){ this.$store.commit("updateCartCount",flag=="add"?1:-1); } }) } } } </script>
还没有评论,来说两句吧...