// pages/posterList/posterList.js
|
// import Notify from '../../src/vant-weapp/notify/notify';
|
import utils from "../../utils/util";
|
import Dialog from "../../src/vant-weapp/dialog/dialog";
|
const app = getApp();
|
let goodsListArr = [];
|
Page({
|
/**
|
* 页面的初始数据
|
*/
|
data: {
|
activeKey: 0,
|
activeTabIndex: 0,
|
windowHeight: 600,
|
selectedTag: "全部",
|
isShowMyPoster: false,
|
isHiddenTypePoster: false,
|
scrollTop: 0,
|
isFirstShow: true,
|
isFirstShowLayer:true,
|
currentPage:0 //当前页码默认值
|
},
|
|
onClickNavBarLeft() {
|
wx.reLaunch({
|
url: "../user/user",
|
});
|
},
|
|
/**
|
* 生命周期函数--监听页面加载
|
*/
|
onLoad: async function (options) {
|
// console.log(wx.$http)
|
console.log("。。。首页onload开始了。。。");
|
if (!app.globalData.openID || !app.globalData.FromUserId) {
|
app.globalData.indexCallback = async (res) => {
|
// wx.setNavigationBarTitle({
|
// title: app.globalData.shopName || "",
|
// });
|
this.setData({
|
navTitle: app.globalData.shopName || "首页",
|
avatarUrl: app.globalData.avatarUrl,
|
});
|
await this.setLeftMenuData();
|
await this.setGoodsData(this.data.menuList[0].MatGroup);
|
await this.setCartList();
|
};
|
} else {
|
await this.setLeftMenuData();
|
await this.setGoodsData(this.data.menuList[0].MatGroup);
|
await this.setCartList();
|
}
|
},
|
|
async setGoodsData(currentId) {
|
let [err, res] = await this.getGoodsList(currentId);
|
if (err) {
|
console.log("出错了。。。", err);
|
return;
|
}
|
let {currentPage} = this.data;
|
currentPage++;
|
// if(currentId==null){
|
// currentPage++;
|
// }
|
goodsListArr=goodsListArr.concat(res.list);
|
this.setData({
|
goodsList: goodsListArr,
|
TotalPages: res.list[0]&&res.list[0].TotalPages,
|
currentPage,
|
isLoading:false
|
});
|
console.log('设置产品列表setGoodsData。。。');
|
console.table({
|
'this.data.currentPage':this.data.currentPage,
|
'this.data.TotalPages': this.data.TotalPages,
|
})
|
console.log("商品List与页数", res.list);
|
},
|
|
async getGoodsList(currentId) {
|
console.log('获取产品列表getGoodsList。。。');
|
console.table({
|
'传参currentId':currentId,
|
'this.data.currentId': this.data.currentId,
|
'this.data.currentPage': this.data.currentPage
|
})
|
this.setData({
|
isLoading:true,
|
})
|
// wx.showLoading({
|
// title: '正在加载',
|
// })
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url:
|
"/shopping/getMatCodeList.do?fromdata=2&isShowSubMatGroup=false&path=" +
|
(currentId||this.data.currentId),
|
data:{
|
limit: 10,
|
page: this.data.currentPage+1||1
|
},
|
// isShowLoading:true,
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
async setLeftMenuData() {
|
let [err, res] = await this.getLeftMenu();
|
if (err) {
|
console.log("出错了。。。", err);
|
return;
|
}
|
this.setData({
|
menuList: res.list,
|
currentId:res.list[0]&&res.list[0].MatGroup
|
});
|
console.log("左侧菜单List,currentId", res.list,this.data.currentId);
|
},
|
async getLeftMenu() {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/getMatGroupInHomePage.do?isShowMatGroup=true",
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
// 左侧菜单点击
|
async goCurrentMenu(event) {
|
console.log(event);
|
// let tagList = this.getTagList(event.detail);
|
goodsListArr = [];
|
this.setData({
|
activeKey: event.detail,
|
currentId: event.currentTarget.dataset.id,
|
currentPage:0,
|
scrollTop:0
|
// activeMatGroup:event.currentTarget.dataset.id
|
});
|
|
|
await this.setGoodsData(event.currentTarget.dataset.id);
|
this.handleGoodsListInCart();
|
},
|
|
scrollLower: utils.throttle(async function(e){
|
console.log("%c准备滚动加载。。。","color:red");
|
console.table({
|
currentPage:this.data.currentPage,
|
TotalPages:this.data.TotalPages,
|
currentId:this.data.currentId
|
});
|
|
if (this.data.currentPage<this.data.TotalPages&&!this.data.isLoading) {
|
await this.setGoodsData();
|
this.handleGoodsListInCart();
|
}
|
},300),
|
|
openHideLayer(){
|
if (this.data.isFirstShowLayer) {
|
wx.showLoading({
|
mask:true
|
})
|
setTimeout(()=> {
|
this.setData({
|
isFirstShowLayer: false
|
});
|
wx.hideLoading()
|
}, 200)
|
}
|
this.setData({
|
showHideLayer: !this.data.showHideLayer,
|
// isFirstShowLayer: false
|
});
|
console.log(this.data.showHideLayer);
|
},
|
// openHideLayer:utils.throttle(function(){
|
// this.setData({
|
// showHideLayer: !this.data.showHideLayer,
|
// });
|
// console.log(this.data.showHideLayer);
|
// },200),
|
|
maskTap(event) {
|
this.setData({
|
showHideLayer: event.detail.isShow,
|
});
|
},
|
async getCartList() {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/cart.do?m=getCartList",
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
updateCartList(res){
|
console.log("待处理的购物车数据...", res);
|
// this.setData({
|
// cartList: [],
|
// });
|
this.setData({
|
cartList: res.list,
|
selectTotalAmount: res.SelectTotalAmount,
|
selectItems: res.SelectItems,
|
cartItems: res.CartItems,
|
});
|
try {
|
wx.setTabBarBadge({
|
index: app.globalData.cartIndex,
|
text: res.list.length.toString()
|
})
|
} catch (error) {
|
console.log(error);
|
|
}
|
this.handleGoodsListInCart();
|
},
|
|
async setCartList() {
|
let [err, res] = await this.getCartList();
|
if (err) {
|
console.log(err);
|
return;
|
}
|
this.updateCartList(res);
|
},
|
|
handleGoodsListInCart() {
|
// let goodsList = this.data.goodsList;
|
// let cartList = this.data.cartList;
|
let goodsList = [];
|
// let cartList = [];
|
goodsList = this.data.goodsList.map((goodsItem,index) => {
|
delete goodsItem.CartId;
|
goodsItem.selectedQuantity = 0;
|
// this.setData({
|
// [`goodsList[${index}].CartId`]:'',
|
// [`goodsList[${index}].selectedQuantity`]:0,
|
// });
|
this.data.cartList.forEach((cartItem) => {
|
if (goodsItem.MatCode === cartItem.MatCode) {
|
goodsItem.CartId = cartItem.CartId;
|
goodsItem.selectedQuantity = cartItem.Quantity;
|
// console.log('待更新的数据',goodsItem);
|
// this.setData({
|
// [`goodsList[${index}].CartId`]:cartItem.CartId,
|
// [`goodsList[${index}].selectedQuantity`]:cartItem.Quantity,
|
// });
|
}
|
});
|
return goodsItem;
|
});
|
// console.log("处理后。。", this.data.goodsList);
|
// this.setData({
|
// goodsList: goodsList,
|
// });
|
this.setData({
|
goodsList: goodsList,
|
});
|
console.log("处理后的商品列表。。。", this.data.goodsList);
|
},
|
|
updateGoodsList(){
|
let matCode = this.data.matCode;
|
let goodsList = this.data.goodsList;
|
let cartId=0;
|
let selectselectedQuantity = 0;
|
this.data.cartList.forEach((cartItem) => {
|
if (matCode === cartItem.MatCode) {
|
cartId = cartItem.CartId;
|
selectedQuantity = cartItem.Quantity;
|
}
|
});
|
goodsList.forEach((goodsItem,index)=>{
|
if (matCode === goodsItem.MatCode) {
|
this.setData({
|
[goodsList[index].CartId]:cartId,
|
[goodsList[index].selectedQuantity]:selectselectedQuantity,
|
})
|
}
|
})
|
},
|
|
async addCartItem(event) {
|
const matcode = event.currentTarget.dataset.matcode;
|
const mininum = event.currentTarget.dataset.mininum;
|
|
let [err, res] = await this.addCartItemRequest(matcode,mininum);
|
if (err) {
|
return;
|
}
|
// this.setData({
|
// matCode:matcode
|
// })
|
// console.log(res);
|
// this.setCartList();
|
this.updateCartList(res);
|
// this.updateGoodsList();
|
},
|
async addCartItemRequest(matcode,mininum) {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/cart.do",
|
data: {
|
m: "add",
|
matcode,
|
quantity: mininum||1,
|
},
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
async checkedCartItems(cartId) {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/cart.do",
|
data: {
|
m: "checkbox",
|
checkbox: cartId,
|
},
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
onDeleteCartItem(event) {
|
const { position, instance } = event.detail;
|
const cartId = event.currentTarget.dataset.id;
|
switch (position) {
|
// case "left":
|
// case "cell":
|
// instance.close();
|
// break;
|
case "right":
|
wx.showModal({
|
title: "提示",
|
content: "您确定要从购物车中删除该商品吗?",
|
success: async (res) => {
|
if (res.confirm) {
|
console.log("用户点击确定");
|
let [err, res] = await this.deleteCartItem(cartId);
|
if (err) {
|
return;
|
}
|
console.log(res);
|
// await this.setCartList();
|
this.updateCartList(res);
|
instance.close();
|
} else if (res.cancel) {
|
console.log("用户点击取消");
|
}
|
},
|
});
|
break;
|
}
|
},
|
clearCart() {
|
const cartList = this.data.cartList;
|
wx.showModal({
|
title: "提示",
|
content: "您确定要清空购物车吗?",
|
success: async (res) => {
|
if (res.confirm) {
|
console.log("用户点击确定");
|
let [err, res] = await this.deleteCartItem(getAllCartId());
|
if (err) {
|
return;
|
}
|
console.log(res);
|
await this.setCartList();
|
} else if (res.cancel) {
|
console.log("用户点击取消");
|
}
|
},
|
});
|
|
function getAllCartId() {
|
let cartIdArr = [];
|
cartList.forEach((item) => {
|
cartIdArr.push(item.CartId);
|
});
|
return cartIdArr.join(";");
|
}
|
},
|
async deleteCartItem(cartId) {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/cart.do",
|
data: {
|
m: "del",
|
key: cartId,
|
},
|
title:'正在加载'
|
})
|
);
|
return [err, res];
|
},
|
|
async onCartItemMinus(event) {
|
console.log(event);
|
const cartId = event.currentTarget.dataset.id;
|
let quantity = event.currentTarget.dataset.quantity;
|
let mininum = event.currentTarget.dataset.mininum;
|
quantity=quantity-mininum;
|
if (quantity <= 0) {
|
let [err, res] = await this.deleteCartItem(cartId);
|
if (err) {
|
return;
|
}
|
console.log(res);
|
// await this.setCartList();
|
this.updateCartList(res);
|
} else {
|
await this.sendCartItemChange(cartId, quantity);
|
}
|
},
|
async onCartItemPlus(event) {
|
console.log(event);
|
const cartId = event.currentTarget.dataset.id;
|
let quantity = event.currentTarget.dataset.quantity;
|
let mininum = event.currentTarget.dataset.mininum;
|
quantity=quantity+mininum;
|
if (cartId) {
|
await this.sendCartItemChange(cartId, quantity);
|
} else {
|
await this.addCartItem(event);
|
}
|
},
|
// onCartItemPlus: utils.debounce(async function(event){
|
// console.log(event);
|
// const cartId = event.currentTarget.dataset.id;
|
// let quantity = event.currentTarget.dataset.quantity;
|
// quantity++;
|
// if (cartId) {
|
// await this.sendCartItemChange(cartId, quantity);
|
// } else {
|
// await this.addCartItem(event);
|
// }
|
// }),
|
onCartItemInputFocus(event){
|
|
// const cartId = event.currentTarget.dataset.id;
|
const quantity = Number(event.detail.value)||0;
|
this.setData({
|
currentEditQuantity:quantity
|
})
|
console.log(this.data.currentEditQuantity);
|
},
|
async onCartItemInputBlur(event) {
|
console.log(event);
|
const cartId = event.currentTarget.dataset.id;
|
const index = event.currentTarget.dataset.index;
|
let quantity = event.detail.value;
|
if (this.data.currentEditQuantity===quantity) {
|
return;
|
}
|
// if (quantity<=1) {
|
// this.setData({
|
// [`goodsList[${index}].selectedQuantity`]:this.data.currentEditQuantity
|
// })
|
// console.log(this.data.currentEditQuantity);
|
// return;
|
// }
|
// await this.sendCartItemChange(cartId, quantity);
|
if (quantity <= 0 || !Number(quantity)) {
|
console.log('!Number(quantity):',!Number(quantity));
|
let [err, res] = await this.deleteCartItem(cartId);
|
if (err) {
|
return;
|
}
|
// console.log(res);
|
// await this.setCartList();
|
this.updateCartList(res);
|
} else {
|
await this.sendCartItemChange(cartId, quantity);
|
}
|
},
|
onCartItemInputChange(e){
|
console.log(e);
|
},
|
async onCartItemInputBlur2(event) {
|
console.log(event);
|
const cartId = event.currentTarget.dataset.id;
|
const index = event.currentTarget.dataset.index;
|
let quantity = event.detail.value;
|
if (this.data.currentEditQuantity===quantity) {
|
return;
|
}
|
if (quantity<=1 || !Number(quantity)) {
|
console.log('!Number(quantity):',!Number(quantity));
|
quantity = 1;
|
this.setData({
|
[`goodsList[${index}].Quantity`]:1
|
})
|
console.log(this.data.currentEditQuantity,this.data.goodsList[index].Quantity);
|
// return;
|
}
|
await this.sendCartItemChange(cartId, quantity);
|
},
|
async submitCart(e) {
|
// console.log(e);
|
// if (e.detail.isAuthorize) {
|
//
|
// }
|
const cartList = this.data.cartList;
|
|
let cartIdList = cartList.map((item)=>{
|
return item.CartId
|
})
|
let allCartId = cartIdList.join(';');
|
console.log(allCartId);
|
|
// let [err,res] = await this.checkedCartItems(allCartId);
|
// if (err) {
|
// return;
|
// }
|
// console.log(res);
|
|
wx.navigateTo({
|
url: `../order/order?selectedCartIds=${allCartId}`,
|
});
|
},
|
// async onCartItemChange(event) {
|
// console.log(event);
|
// const cartId = event.currentTarget.dataset.id;
|
// const quantity = event.currentTarget.dataset.quantity;
|
// let [err, res] = await this.sendCartItemChange(cartId, quantity);
|
// if (err) {
|
// return;
|
// }
|
// console.log(res);
|
// this.setCartList();
|
// },
|
async sendCartItemChange(cartId, quantity) {
|
let [err, res] = await utils.to(
|
wx.$http.request({
|
url: "/shopping/cart.do",
|
data: {
|
m: "edit",
|
[`quantity_${cartId}`]: quantity,
|
json:true
|
},
|
title:'正在加载'
|
})
|
);
|
if (err) {
|
return;
|
}
|
// console.log(res);
|
// await this.setCartList();
|
this.updateCartList(res)
|
},
|
/**
|
* 生命周期函数--监听页面初次渲染完成
|
*/
|
onReady: function () {
|
var res = wx.getSystemInfoSync();
|
this.setData({
|
// 可使用窗口高度-状态栏的高度
|
windowHeight: res.windowHeight - 105,
|
});
|
console.log(this.data.windowHeight);
|
},
|
|
/**
|
* 生命周期函数--监听页面显示
|
*/
|
onShow: function () {
|
console.log("用户信息", app.globalData.userInfo);
|
this.setData({
|
userInfo: app.globalData.userInfo,
|
});
|
if (!this.data.isFirstShow) {
|
this.setCartList();
|
}
|
},
|
|
/**
|
* 生命周期函数--监听页面隐藏
|
*/
|
onHide: function () {
|
this.setData({
|
isFirstShow: false
|
})
|
},
|
|
/**
|
* 生命周期函数--监听页面卸载
|
*/
|
onUnload: function () {},
|
|
/**
|
* 用户点击右上角分享
|
*/
|
// onShareAppMessage: function () {
|
|
// }
|
});
|