一、价格过滤功能

GoodsList.vue

  1. >>点击价格区间时发送请求
  2. methods:{
  3.     getGoodsList(flag){
  4.         var param = {
  5.             // 请求时传点击的价格区间数据给后台
  6.             priceLevel:this.priceChecked // 点击的价格区间
  7.         }
  8.         ......
  9.     },
  10.     setPriceFilter(index){ // 点击价格
  11.         this.priceChecked = index;
  12.         this.closePop();
  13.         this.getGoodsList(); // 发送请求
  14.     },
  15. }

server/routes/goods.js 
获取传过来的参数,对价格参数进行处理,查询价格区间内的数据库数据

  1. // 二级路由
  2. /* GET goods page. */
  3. router.get('/', function(req, res, next) {
  4.    // express获取请求参数
  5.    let priceLevel = req.param("priceLevel"); // 传过来的价格区间
  6.    var priceGt = '',priceLte = '';
  7.    let params = {};
  8.    if(priceLevel != 'all'){ // 价格区间过滤功能
  9.       switch (priceLevel){
  10.          case '0':priceGt=0;priceLte =100;break;
  11.          case '1':priceGt=100;priceLte =500;break;
  12.          case '2':priceGt=500;priceLte =1000;break;
  13.          case '3':priceGt=1000;priceLte =5000;break;
  14.       }
  15.       params = {
  16.          salePrice:{
  17.             $gt:priceGt,
  18.             $lte:priceLte
  19.          }
  20.       }
  21.    }
  22.    ......
  23. });

往下滚动分页加载图标效果

  1. <!-- 加载中... -->
  2. <img v-show="loading" src="/static/loading-svg/loading-spinning-bubbles.svg" alt="">
  3.  
  4. export default {
  5.     data(){
  6.         return {
  7.             loading:false
    // 往下滚动"加载图标"的出现效果:默认不出现
  8.         }
  9.     },
  10.     methods:{
  11.         getGoodsList(flag){
  12.             this.loading = true; // 请求前出现
  13.             axios.get("/goods",{
  14.               params:param // 传参
  15.             }).then((res)=>{
  16.                 var res = res.data;
  17.                 this.loading = false; // 请求到数据图标消失
  18.                 "){
  19.                     ......
  20.                 }
  21.             })
  22.         }
  23.     }
  24. }

二、加入购物车功能

dumall数据库建立users集合导入resource文件夹的dumall-users 

与商品列表接口一样,先建立用户数据的模型 
server/models/users.js

  1. // 对应数据库用户数据在resource文件夹的dumall-users
  2. var mongoose = require('mongoose');
  3. var Schema = mongoose.Schema;
  4.  
  5. // 定义一个Schema
  6. var userSchema = new Schema({
  7.    'userId':String, // 或者 'userId':{type:String}
  8.    'userName':String,
  9.    'userPwd':String,
  10.    'orderList':Array,
  11.     'cartList':[ // 购物车列表
  12.         {
  13.             "productId":String,
  14.             "productName":String,
  15.             "salePrice":Number,
  16.             "productImage":String,
  17.             "checked":String, // 是否选中
  18.             "productNum":String // 商品数量
  19.         }
  20.     ],
  21.     "addressList":Array
  22. })
  23.  
  24. // 输出(导出)
  25. module.exports = mongoose.model('user',userSchema); // 定义一个user模型,可以根据这个模型调用其API方法。
  26. // 这个模型定义的是数据库dumall的users集合数据,所以这个model取名user是对应这个集合,连接数据库之后,这个模型会根据名字的复数形式"users"来查找数据集合。
  27. // module.exports = mongoose.model('user',userSchema,'users'); 也可以后面注明链接的是数据库的goods集合

数据库链接(之前商品列表页已连接),查询操作用户数据,建立接口,实现加入购物车功能 server/routes/goods.js

  1. // 加入到购物车
  2. // 是二级路由,一级路由在app.js
  3. router.post("/addCart",function(req, res, next){
  4.   var userId = '100000077',
  5.     productId = req.body.productId; // post请求拿到res参数:req.body
  6.   var User = require('../models/users.js'); // 引入user模型
  7.  
  8.   // 查询第一条:拿到用户信息
  9.   User.findOne({
  10.     userId:userId // 查询条件
  11.   },function(err,userDoc){
  12.     if(err){
  13.       res.json({
  14.         status:"",
  15.         msg:err.message
  16.       })
  17.     }else{
  18.       console.log("userDoc"+userDoc); // 用户数据
  19.       if(userDoc){
  20.         let goodsItem = '';
  21.         userDoc.cartList.forEach(function(item){ // 遍历用户购物车,判断加入购物车的商品是否已经存在
  22.           if(item.productId == productId){
  23.             goodsItem = item;
  24.             item.productNum++; // 购物车这件商品数量+1
  25.           }
  26.         })
  27.         if(goodsItem){ // 若购物车商品已存在
  28.           userDoc.save(function (err2,doc2) {
  29.             if(err2){
  30.                 res.json({
  31.                     status:"",
  32.                     msg:err2.message
  33.                 })
  34.             }else{
  35.                 res.json({
  36.                     status:'0',
  37.                     msg:'',
  38.                     result:'suc'
  39.                 })
  40.             }
  41.           })
  42.         }else{ // 若购物车商品不存在,就添加进去
  43.           Goods.findOne({productId:productId},function(err1,doc){ // 从商品列表页Goods查询点击加入购物车的那件商品信息
  44.             if(err1){
  45.               res.json({
  46.                 status:"",
  47.                 msg:err1.message
  48.               })
  49.             }else{
  50.               if(doc){
  51.                 doc.productNum = 1;
  52.                 doc.checked = 1;
  53.                 userDoc.cartList.push(doc); // 添加信息到用户购物车列表中
  54.                 userDoc.save(function(err2,doc2){ // 保存数据库
  55.                   if(err2){
  56.                     res.json({
  57.                       status:"",
  58.                       msg:err2.message
  59.                     })
  60.                   }else{
  61.                     res.json({
  62.                       status:"",
  63.                       msg:'',
  64.                       result:'suc'
  65.                     })
  66.                   }
  67.                 })
  68.               }
  69.             }
  70.           })
  71.         }
  72.       }
  73.     }
  74.   })
  75. })

页面实现加入购物车请求实现 GoodsList.vue

  1. <!--传入商品id参数-->
  2. <a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入购物车</a>
  3.  
  4. methods:{
  5.     addCart(productId){ // 点击加入购物车
  6.         axios.post("/goods/addCart",{ // 接口设置在server/routes/goods.js
  7.             productId:productId
  8.         }).then((res)=>{
  9.             var res = res.data;
  10.             if(res.status==0){
  11.                 alert("加入成功")
  12.             }else{
  13.                 alert("msg:"+res.msg)
  14.             }
  15.         })
  16.     }
  17. }

运行项目,点击购物车,请求成功,数据库购物车列表变化

  • © 2018 GitHub, Inc.

Vue nodejs商城项目-商品列表价格过滤和加入购物车功能的更多相关文章

  1. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  2. Vue nodejs商城项目-商品的分页、排序、筛选

    .分页 ,要查第3页的数据,则跳过2*8条数据,然后返回8条数据. 在实现滚动加载时,页面刚一加载完成并不需要请求所有数据,当用户下拉到页面底部时,再去请求数据并拼接到商品数据列表中. 通过vue-i ...

  3. Vue nodejs商城项目-项目概述

    项目概况 用vue2.0 +node.js +MongonDB 做了一个商城系统 技术选型 Vue2.0+node.js+express+MongoDB+axios+vuex 构建工具 Webpack ...

  4. Vue nodejs商城项目-登录模块

    一.登录功能 后端server/routes/users.js var User = require('./../models/users.js');   // 二级路由 // 登录接口 router ...

  5. 13 Flutter仿京东商城项目 商品列表筛选以及上拉分页加载更多

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  6. 12 Flutter仿京东商城项目 商品列表页面请求数据、封装Loading Widget、上拉分页加载更多

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  7. 11 Flutter仿京东商城项目 商品列表页面二级筛选导航布局

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  8. 商城项目:商品列表ajax加载,ajax加入购物车--五张表的联合查询

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductLists.a ...

  9. Vue nodejs商城项目- 前后端数据传递

    .利用Mongoose查询MongoDB 通过mongoose插件可以简捷地从mondodb中获取数据,首先安装mongoose: cnpm install mongoose --save   使用m ...

随机推荐

  1. PHP SECURITY CALENDAR 2017 学习总结-更新中

    这篇文章主要以审计代码为主来分析每道题目中所存在的漏洞点,记录一下自己的学习: 1.Day 1 - Wish List class Challenge { const UPLOAD_DIRECTORY ...

  2. 【linux相识相知】sed命令

    在之前的博客中我们介绍了文本三剑客中grep,本次博客就另外一名剑客——sed做出详细的描述,sed真的是一款强大的工具.下面让我们来一起看一下吧! 概述和工作机制 SED的英文全称为Stream E ...

  3. Design Pattern理解碎片

    开发封闭原则(Open-Closed Principle OCP)Software entities(classes,modules,functions etc) should open for ex ...

  4. reload()与reload(true)

    如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已 改变.如果文档已改变,reload() 会再次下载该文档.如果文 ...

  5. DJango小总结一

    views.py                        def func(request):                # 包含所有的请求数据                ...     ...

  6. dubbo-admin网页管理控制台

    由于近段时间在看dubbo,网上找到的这个war包发布到tomcat报错,故从git(https://github.com/apache/incubator-dubbo-ops)上重新下载编译了版本 ...

  7. c# 字体库跨域解决

    网上大部分的资料说的都是在apache和ng服务器的情况下解决方案,但基本的思路都是添加响应头 场景: 页面引用css文件: <link href="http://www.tuohua ...

  8. 通过学生-课程关系表,熟悉hive语句

    通过学生-课程关系表,熟悉hive语句 1.在hive中创建以下三个表. create table  student(Sno int,Sname string,Sex string,Sage int, ...

  9. 原生js实现星星闪烁的效果

    星星闪烁的原理其实很简单: html代码: <body style="background:#000"> <div id="stars_box" ...

  10. Stage5--Python GUI编程TKinter

    Python图形库简要介绍 python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是Python的标准 ...