价格过滤功能的实现

一、价格过滤功能

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. export default {

  4.     data(){

  5.         return {

  6.             loading:false // 往下滚动"加载图标"的出现效果:默认不出现

  7.         }

  8.     },

  9.     methods:{

  10.         getGoodsList(flag){

  11.             this.loading = true; // 请求前出现

  12.             axios.get("/goods",{

  13.               params:param // 传参

  14.             }).then((res)=>{

  15.                 var res = res.data;

  16.                 this.loading = false; // 请求到数据图标消失

  17.                 if(res.status == "0"){

  18.                     ......

  19.                 }

  20.             })

  21.         }

  22.     }

  23. }

二、加入购物车功能

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

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

  1. // 对应数据库用户数据在resource文件夹的dumall-users

  2. var mongoose = require('mongoose');

  3. var Schema = mongoose.Schema;

  4. // 定义一个Schema

  5. var userSchema = new Schema({

  6.    'userId':String, // 或者 'userId':{type:String}

  7.    'userName':String,

  8.    'userPwd':String,

  9.    'orderList':Array,

  10.     'cartList':[ // 购物车列表

  11.         {

  12.             "productId":String,

  13.             "productName":String,

  14.             "salePrice":Number,

  15.             "productImage":String,

  16.             "checked":String, // 是否选中

  17.             "productNum":String // 商品数量

  18.         }

  19.     ],

  20.     "addressList":Array

  21. })

  22. // 输出(导出)

  23. module.exports = mongoose.model('user',userSchema); // 定义一个user模型,可以根据这个模型调用其API方法。

  24. // 这个模型定义的是数据库dumall的users集合数据,所以这个model取名user是对应这个集合,连接数据库之后,这个模型会根据名字的复数形式"users"来查找数据集合。

  25. // 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.   User.findOne({

  9.     userId:userId // 查询条件

  10.   },function(err,userDoc){

  11.     if(err){

  12.       res.json({

  13.         status:"1",

  14.         msg:err.message

  15.       })

  16.     }else{

  17.       console.log("userDoc"+userDoc); // 用户数据

  18.       if(userDoc){

  19.         let goodsItem = '';

  20.         userDoc.cartList.forEach(function(item){ // 遍历用户购物车,判断加入购物车的商品是否已经存在

  21.           if(item.productId == productId){

  22.             goodsItem = item;

  23.             item.productNum++; // 购物车这件商品数量+1

  24.           }

  25.         })

  26.         if(goodsItem){ // 若购物车商品已存在

  27.           userDoc.save(function (err2,doc2) {

  28.             if(err2){

  29.                 res.json({

  30.                     status:"1",

  31.                     msg:err2.message

  32.                 })

  33.             }else{

  34.                 res.json({

  35.                     status:'0',

  36.                     msg:'',

  37.                     result:'suc'

  38.                 })

  39.             }

  40.           })

  41.         }else{ // 若购物车商品不存在,就添加进去

  42.           Goods.findOne({productId:productId},function(err1,doc){ // 从商品列表页Goods查询点击加入购物车的那件商品信息

  43.             if(err1){

  44.               res.json({

  45.                 status:"1",

  46.                 msg:err1.message

  47.               })

  48.             }else{

  49.               if(doc){

  50.                 doc.productNum = 1;

  51.                 doc.checked = 1;

  52.                 userDoc.cartList.push(doc); // 添加信息到用户购物车列表中

  53.                 userDoc.save(function(err2,doc2){ // 保存数据库

  54.                   if(err2){

  55.                     res.json({

  56.                       status:"1",

  57.                       msg:err2.message

  58.                     })

  59.                   }else{

  60.                     res.json({

  61.                       status:"0",

  62.                       msg:'',

  63.                       result:'suc'

  64.                     })

  65.                   }

  66.                 })

  67.               }

  68.             }

  69.           })

  70.         }

  71.       }

  72.     }

  73.   })

  74. })

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

  1. <!--传入商品id参数-->

  2. <a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入购物车</a>

  3. methods:{

  4.     addCart(productId){ // 点击加入购物车

  5.         axios.post("/goods/addCart",{ // 接口设置在server/routes/goods.js

  6.             productId:productId

  7.         }).then((res)=>{

  8.             var res = res.data;

  9.             if(res.status==0){

  10.                 alert("加入成功")

  11.             }else{

  12.                 alert("msg:"+res.msg)

  13.             }

  14.         })

  15.     }

  16. }

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

猜你喜欢

转载自blog.csdn.net/qq_41153478/article/details/83217238