一、價格過濾功能
GoodsList.vue
-
>>點擊價格區間時發送請求
-
methods:{
-
getGoodsList(flag){
-
var param = {
-
// 請求時傳點擊的價格區間數據給后台
-
priceLevel:this.priceChecked // 點擊的價格區間
-
}
-
......
-
},
-
setPriceFilter(index){ // 點擊價格
-
this.priceChecked = index;
-
this.closePop();
-
this.getGoodsList(); // 發送請求
-
},
-
}
server/routes/goods.js
獲取傳過來的參數,對價格參數進行處理,查詢價格區間內的數據庫數據
-
// 二級路由
-
/* GET goods page. */
-
router.get('/', function(req, res, next) {
-
// express獲取請求參數
-
let priceLevel = req.param("priceLevel"); // 傳過來的價格區間
-
var priceGt = '',priceLte = '';
-
let params = {};
-
if(priceLevel != 'all'){ // 價格區間過濾功能
-
switch (priceLevel){
-
case '0':priceGt=0;priceLte =100;break;
-
case '1':priceGt=100;priceLte =500;break;
-
case '2':priceGt=500;priceLte =1000;break;
-
case '3':priceGt=1000;priceLte =5000;break;
-
}
-
params = {
-
salePrice:{
-
$gt:priceGt,
-
$lte:priceLte
-
}
-
}
-
}
-
......
-
});
往下滾動分頁加載圖標效果
-
<!-- 加載中... -->
-
<img v-show="loading" src="/static/loading-svg/loading-spinning-bubbles.svg" alt="">
-
-
export default {
-
data(){
-
return {
-
loading:false // 往下滾動"加載圖標"的出現效果:默認不出現
-
}
-
},
-
methods:{
-
getGoodsList(flag){
-
this.loading = true; // 請求前出現
-
axios.get("/goods",{
-
params:param // 傳參
-
}).then((res)=>{
-
var res = res.data;
-
this.loading = false; // 請求到數據圖標消失
-
if(res.status == "0"){
-
......
-
}
-
})
-
}
-
}
-
}
二、加入購物車功能
dumall數據庫建立users集合導入resource文件夾的dumall-users
與商品列表接口一樣,先建立用戶數據的模型
server/models/users.js
-
// 對應數據庫用戶數據在resource文件夾的dumall-users
-
var mongoose = require('mongoose');
-
var Schema = mongoose.Schema;
-
-
// 定義一個Schema
-
var userSchema = new Schema({
-
'userId':String, // 或者 'userId':{type:String}
-
'userName':String,
-
'userPwd':String,
-
'orderList':Array,
-
'cartList':[ // 購物車列表
-
{
-
"productId":String,
-
"productName":String,
-
"salePrice":Number,
-
"productImage":String,
-
"checked":String, // 是否選中
-
"productNum":String // 商品數量
-
}
-
],
-
"addressList":Array
-
})
-
-
// 輸出(導出)
-
module.exports = mongoose.model('user',userSchema); // 定義一個user模型,可以根據這個模型調用其API方法。
-
// 這個模型定義的是數據庫dumall的users集合數據,所以這個model取名user是對應這個集合,連接數據庫之后,這個模型會根據名字的復數形式"users"來查找數據集合。
-
// module.exports = mongoose.model('user',userSchema,'users'); 也可以后面注明鏈接的是數據庫的goods集合
數據庫鏈接(之前商品列表頁已連接),查詢操作用戶數據,建立接口,實現加入購物車功能 server/routes/goods.js
-
// 加入到購物車
-
// 是二級路由,一級路由在app.js
-
router.post("/addCart",function(req, res, next){
-
var userId = '100000077',
-
productId = req.body.productId; // post請求拿到res參數:req.body
-
var User = require('../models/users.js'); // 引入user模型
-
-
// 查詢第一條:拿到用戶信息
-
User.findOne({
-
userId:userId // 查詢條件
-
},function(err,userDoc){
-
if(err){
-
res.json({
-
status:"1",
-
msg:err.message
-
})
-
}else{
-
console.log("userDoc"+userDoc); // 用戶數據
-
if(userDoc){
-
let goodsItem = '';
-
userDoc.cartList.forEach(function(item){ // 遍歷用戶購物車,判斷加入購物車的商品是否已經存在
-
if(item.productId == productId){
-
goodsItem = item;
-
item.productNum++; // 購物車這件商品數量+1
-
}
-
})
-
if(goodsItem){ // 若購物車商品已存在
-
userDoc.save(function (err2,doc2) {
-
if(err2){
-
res.json({
-
status:"1",
-
msg:err2.message
-
})
-
}else{
-
res.json({
-
status:'0',
-
msg:'',
-
result:'suc'
-
})
-
}
-
})
-
}else{ // 若購物車商品不存在,就添加進去
-
Goods.findOne({productId:productId},function(err1,doc){ // 從商品列表頁Goods查詢點擊加入購物車的那件商品信息
-
if(err1){
-
res.json({
-
status:"1",
-
msg:err1.message
-
})
-
}else{
-
if(doc){
-
doc.productNum = 1;
-
doc.checked = 1;
-
userDoc.cartList.push(doc); // 添加信息到用戶購物車列表中
-
userDoc.save(function(err2,doc2){ // 保存數據庫
-
if(err2){
-
res.json({
-
status:"1",
-
msg:err2.message
-
})
-
}else{
-
res.json({
-
status:"0",
-
msg:'',
-
result:'suc'
-
})
-
}
-
})
-
}
-
}
-
})
-
}
-
}
-
}
-
})
-
})
頁面實現加入購物車請求實現 GoodsList.vue
-
<!--傳入商品id參數-->
-
<a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入購物車</a>
-
-
methods:{
-
addCart(productId){ // 點擊加入購物車
-
axios.post("/goods/addCart",{ // 接口設置在server/routes/goods.js
-
productId:productId
-
}).then((res)=>{
-
var res = res.data;
-
if(res.status==0){
-
alert("加入成功")
-
}else{
-
alert("msg:"+res.msg)
-
}
-
})
-
}
-
}
運行項目,點擊購物車,請求成功,數據庫購物車列表變化
- © 2018 GitHub, Inc.



