本地选择商品
2023-09-04 本文已影响0人
糖醋里脊120625
//存放右侧分类的高度累加数组
import { centToYuan,yuanToCent } from '../../unit/priceUtil'
import { goodsGroup } from '../../http/goodsApi'
const app = getApp()
// https://www.bookstack.cn/read/vant-3.4.7-zh/8bd11acebe45f23c.md
Page({
/**
* 页面的初始数据
*/
data: {
goodsList:[],
ProductList:[],
HasSelectedList:[],
curIndex: 0,
pageData:1,
pageSize:999,
hasLoadData:true,
itemCateData:{},
mainActiveIndex: 0,
basicInfo:{},
SeledGoods:[],
GoodsNum:"",
showPopup:false,
heightArr: [],
distance: 0,
active: 0,
selectId: "item0",
},
// 选择左侧标签锚点定位
activeNav(e) {
var index = e.currentTarget.dataset.index
this.setData({
active: index,
selectId: "item" + index
})
},
//计算右侧每个锚点的高度
selectHeight() {
var list = []
var height = 0;
const query = wx.createSelectorQuery();
query.selectAll('.subtitle').boundingClientRect()
query.exec((res) => {
res[0].forEach((item) => {
height += item.height;
list.push(height)
})
console.log(list)
this.data.heightArr = list
})
},
//监听scroll-view的滚动事件
watchScroll(e) {
let scrollTop = e.detail.scrollTop; //获取距离顶部的距离
let active = this.data.active;
if (scrollTop >= this.data.distance) {
if (active + 1 < this.data.heightArr.length && scrollTop >= this.data.heightArr[active]) {
this.setData({
active: active + 1
})
}
} else {
if (active - 1 >= 0 && scrollTop < this.data.heightArr[active - 1]) {
this.setData({
active: active - 1
})
}
}
this.data.distance = scrollTop;
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log("add带过来的数据")
console.log(options)
if(JSON.stringify(options) == "{}"){
}else{
let optionData=JSON.parse(decodeURIComponent(options.detInfo));
console.log(optionData)
this.setData({
basicInfo:optionData.basicInfo,
HasSelectedList:optionData.SeledGoods,
})
}
this.getGoodsData()
// this.getProListHeight();
wx.setNavigationBarTitle({
title: '选择商品',
})
this.selectHeight();
},
switchCategory(e) {
var that =this;
var index = e.currentTarget.dataset.index;
var childData = e.currentTarget.dataset.bean.children;
console.log(this.data.HasSelectedList)
function FindItem(itemID,moreList){
let findData = moreList.find(x => x.piId == itemID.piId);
return findData
}
var changeGoods = childData.map((each)=>{
if(FindItem(each,that.data.HasSelectedList)){
return{
...each,
SelNum:FindItem(each,that.data.HasSelectedList).SelNum,
showAddOrSub:true
}
}else{
return{
...each
}
}
})
this.setData({
ProductList:changeGoods,
curIndex: e.currentTarget.dataset.index?e.currentTarget.dataset.index:0,
toView: 'order' + e.currentTarget.dataset.bean.productTypeId,
active: index,
selectId: "item" + index,
})
},
TotalAllSelecteFun(){
this.setData({
GoodsNum:this.data.HasSelectedList.length
})
},
onCancelCategory(){
this.setData({
showPopup:false
})
},
onClickIcon(){
this.setData({
showPopup:!this.data.showPopup
})
},
SubmitFun(){
// var selData=[]
// this.data.goodsList.map((item) => {
// item.children.map((each)=>{
// if(each.SelNum>0){
// selData.push(each)
// }
// })
// });
if(this.data.HasSelectedList.length==0){
wx.showToast({
icon: 'none',
title: "请选择商品",
})
return
}
var pages = getCurrentPages(); // 获取页面栈
var prevPage = pages[pages.length - 2]; // 上一个页面
console.log(prevPage.__route__)
if (prevPage.__route__ == 'pages/addOrder/addOrder') {
prevPage.haveSeledGoods(this.data.HasSelectedList);
wx.navigateBack({
delta: 1
})
}
},
searchClick(){
const newData = JSON.stringify(
{
hasIncludedData:this.data.HasSelectedList
}
);
wx.navigateTo({ url: `/pages/GoodsSearch/GoodsSearch?detInfo=`+ encodeURIComponent(newData) });
},
getGoodsData(){
let that =this;
let postData={
keyword:"",
agreementId:"",
pageIndex: 1,
pageSize: 9999
}
goodsGroup(postData).then((res) => {
let resultData =res.data.list;
let changeGoods = []
resultData.forEach((item, index) => {
let has = changeGoods.findIndex(o => o.productTypeId === item.productTypeId)
if(has == -1) {
changeGoods.push({
productTypeId: item.productTypeId,
productTypeName:item.productTypeName,
children: [item]
})
} else {
changeGoods[has].children.push(item)
}
})
function FindItem(itemID,moreList){
let findData = moreList.find(x => x.piId == itemID.piId);
return findData
}
let newGoods= changeGoods.map((item) => {
let childFloor = item.children.map((each)=>{
return {
...each,
latestSaleUnitPrice:centToYuan(each.latestSaleUnitPrice),
showAddOrSub:false,
SelNum:0,
}
})
return {
...item,
children: childFloor,
};
});
// aaaa
console.log(newGoods)
console.log("选中的返回")
console.log(that.data.HasSelectedList)
function FindItem(itemID,moreList){
let findData = moreList.find(x => x.piId == itemID.piId);
return findData
}
var oneChild = newGoods[0].children.map((each)=>{
if(FindItem(each,that.data.HasSelectedList)){
return{
...each,
SelNum:FindItem(each,that.data.HasSelectedList).SelNum,
showAddOrSub:true
}
}else{
return{
...each
}
}
})
this.setData({
goodsList:newGoods,
ProductList:oneChild
})
this.TotalAllSelecteFun()
})
},
// qqq
ChangeSelectedItemNum(event){
let that =this;
setTimeout(function () {
that.changedSelected(event)
}, 600);
},
changedSelected(event){
let that =this;
let childIndex =event.currentTarget.dataset.itemIndex;
console.log(childIndex)
this.data.HasSelectedList[childIndex].SelNum =event.detail;
this.data.ProductList = this.data.ProductList.map((item,index) => {
if(item.piId==this.data.HasSelectedList[childIndex].piId){
if(event.detail==0){
return{
...item,
showAddOrSub:false,
SelNum:0
}
}else{
return{
...item,
SelNum:this.data.HasSelectedList[childIndex].SelNum
}
}
}else{
return{
...item,
}
}
});
if(event.detail==0){
this.data.HasSelectedList.splice(childIndex, 1);
console.log("删除")
}
// 需要时候给加上
that.setData({
HasSelectedList:this.data.HasSelectedList,
ProductList:this.data.ProductList
});
this.TotalAllSelecteFun()
},
ChangeItemNum(event){
var that =this;
setTimeout(function () {
let childIndex =event.currentTarget.dataset.itemIndex;
that.data.ProductList[childIndex].SelNum =event.detail;
if(event.detail==0){
that.data.ProductList[childIndex].showAddOrSub =false;
}
that.DynamicCacheArray(that.data.ProductList[childIndex])
}, 600);
},
addCart(event){
let childIndex =event.currentTarget.dataset.itemIndex;
this.data.ProductList[childIndex].showAddOrSub =true;
this.data.ProductList[childIndex].SelNum =1;
this.setData({
ProductList:this.data.ProductList
});
this.DynamicCacheArray(this.data.ProductList[childIndex])
},
DynamicCacheArray(itemData){
let that =this;
var findData = this.data.HasSelectedList.find(x => x.piId == itemData.piId);
if(findData){
this.data.HasSelectedList = this.data.HasSelectedList.map((item) => {
if(item.piId==itemData.piId){
return{
...item,
SelNum:itemData.SelNum
}
}else{
return{
...item,
}
}
});
}else{
this.data.HasSelectedList.push(itemData)
}
if(itemData.SelNum==0){
var indexNum = this.data.HasSelectedList.findIndex((tableData) => tableData.piId == itemData.piId);
this.data.HasSelectedList.splice(indexNum, 1);
console.log("删除")
}
console.log("最后")
console.log(this.data.HasSelectedList)
that.setData({
HasSelectedList:that.data.HasSelectedList,
});
// setTimeout(function () {
// that.setData({
// ProductList:that.data.ProductList,
// });
// }, 100);
this.TotalAllSelecteFun()
},
// sss
haveSearchGoods(usefulData){
var that =this;
console.log(usefulData)
console.log(this.data.HasSelectedList)
var selData=[]
this.data.HasSelectedList.map((item) => {
if(item.SelNum>0){
selData.push(item)
}
});
let OldGoods= selData;
let NewGoods= usefulData;
NewGoods.forEach((item)=>{
let index = OldGoods.findIndex(e=>
e.piId == item.piId
)
if(index>-1){
OldGoods[index]=item
}else{
OldGoods.push(item)
}
})
console.log(OldGoods)
this.setData({
// SeledGoods:OldGoods,
HasSelectedList:OldGoods
});
setTimeout(function () {
that.getGoodsData()
}, 10);
return
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})