You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

486 lines
12 KiB

// pages/store/store.js
var app = getApp();
var WXAPI = require('../../utils/server.js');
Page({
/**
* 页面的初始数据
*/
data: {
animation: {}, // 动画
animation_mask: {}, // 动画
showModalStatus: false, // modal是否需要渲染
currentIndex: 0,
navTab: [{'type':'推荐','status':'','num':0},
{'type':'普通商品','status':'payment','num':0},
{'type':'预售商品','status':'send','num':0},
{'type':'商品分类','status':'receipt','num':0}],
currentStatus: '',
listArray: [],
page: 1,
shop: {},
shop_name: '',
shop_logo: '',
goods_list: [],
shop_info: {},
type: 1,
shopId: '',
showModalStatus: false,
animationData: '',
buynum: 1,
attrList: [],
sizeid: '',
itemData: {}
},
switchTab: function(e) {
var dataset = e.currentTarget.dataset
this.setData({
currentIndex: dataset.idx,
currentStatus: dataset.item.status,
type: dataset.idx==2?4:dataset.idx==3?3:dataset.idx+1
});
this.reloadData(this.data.shopId);
},
// 数据加载
reloadData: function(shopId) {
var that = this;
WXAPI.storeHome({
accessId: app.globalData.accessId,
storeId: 239,
storeType: 1,
shop_id: shopId,
shop_list_id: '',
type: that.data.type,
latitude: 31.772752,
longitude: 119.946973,
}).then(res => {
if (res.code == 200) {
var data = res.data;
that.setData({
shop: data,
shop_name: data.shop_name,
shop_logo: data.shop_logo,
shop_info: data.shop_list[0],
goods_list: data.list,
})
}
});
},
navToStore() {
var info = this.data.shop_info
wx.navigateTo({
url: '/pages/store/myStore?shopId='+info.id,
})
},
navToList: function (e) {
// console.log(e)
var that = this
var info = e.currentTarget.dataset.info;
wx.navigateTo({
url: '/pages/listdetail/listdetail?id='+info.cid+'&title='+info.pname,
})
},
navToDetail: function(e) {
var info = e.currentTarget.dataset.info;
wx.navigateTo({
url: '/pages/product/detail?productId='+info.id,
})
},
showAttribute: function(e) {
var productId = e.currentTarget.dataset.id;
var that = this;
that.loadProductDetail(productId);
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
});
this.animation = animation
animation.translateY(300).step();
this.setData({
// paytype: type,
animationData: animation.export()
})
if (e.currentTarget.dataset.status == 1) {
this.setData({
showModalStatus: true
});
}
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation
})
if (e.currentTarget.dataset.status == 0) {
this.setData({
showModalStatus: false
});
}
}.bind(this), 200);
},
// 商品详情数据获取
loadProductDetail: function (productId) {
var that = this;
WXAPI.productIndex({
accessId: app.globalData.accessId,
storeId: 239,
storeType: 1,
pro_id: productId,
vipSource: 0
}).then(res => {
if (res.code == 200) {
var data = res.data;
var pro = data.pro;
that.setData({
// itemData: pro,
attrList: data.attrList,
sizeid: data.attrList[0].attr[0].attrId,
skuBeanList: data.skuBeanList
});
//默认选中
that.one();
}
});
},
//首次进去选中
one: function () {
var attrListIn = this.data.attrList;
var skuBeanListIn = this.data.skuBeanList;
var select_list = skuBeanListIn[0];
for (var i = 0; i < attrListIn.length; i++) {
for (var j = 0; j < attrListIn[i].attr.length; j++) {
for (var b = 0; b < select_list.attributes.length; b++) {
if (select_list.attributes[b].attributeId == attrListIn[i].attr[j].attributeId && select_list.attributes[b].attributeValId == attrListIn[i].attr[j].id) {
attrListIn[i].attr[j].select = true;
}
}
}
}
var itemData = this.data.itemData;
itemData.photo_x = select_list.imgurl;
itemData.price_yh = select_list.price;
itemData.num = select_list.count;
var sizeid = select_list.cid;
// 重新赋值
this.setData({
attrList: attrListIn,
skuBeanList: skuBeanListIn,
itemData: itemData,
sizeid: sizeid,
value: select_list.name
})
this.onData();
},
// 弹窗
setModalStatus: function (e) {
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
});
//定义点击的类型
var type = e.target.dataset.type ? e.target.dataset.type : false;
//控制两种不同显示方式
if (type) {
this.setData({
xefl: false,
})
} else {
this.setData({
xefl: true,
})
type = this.data.paytype;
}
this.animation = animation
animation.translateY(300).step();
this.setData({
paytype: type,
animationData: animation.export()
})
if (e.currentTarget.dataset.status == 1) {
this.setData({
showModalStatus: true
});
}
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation
})
if (e.currentTarget.dataset.status == 0) {
this.setData({
showModalStatus: false
});
}
}.bind(this), 200)
},
// 加减
changeNum: function (e) {
var that = this;
var num = that.data.itemData.num;
if (e.target.dataset.alphaBeta == 0) {
if (this.data.buynum <= 1) {
wx.showToast({
title: '不能再少了',
icon: 'none',
duration: 1000
})
} else {
this.setData({
buynum: this.data.buynum - 1
})
};
} else {
if (that.data.buynum < num) {
this.setData({
buynum: this.data.buynum + 1
})
} else {
wx.showToast({
title: '不能再多了',
icon: 'none',
duration: 1000
})
}
};
},
/**
* 规格属性点击事件
*/
onChangeShowState: function (event) {
var that = this;
var listItem = this.data.attrList;
var items = listItem[event.currentTarget.dataset.idx];
var item = items.attr[event.currentTarget.dataset.index];
// if (!item.enable) {
// return;
// }
var select = !item.select;
for (var i = 0; i < items.attr.length; i++) {
items.attr[i].select = false;
}
item.select = select;
// 获取点击属性列表
var canGetInfo = [];
for (var skuIndex = 0; skuIndex < listItem.length; skuIndex++) {
for (var skuIndexIn = 0; skuIndexIn < listItem[skuIndex].attr.length; skuIndexIn++) {
if (listItem[skuIndex].attr[skuIndexIn].enable && listItem[skuIndex].attr[skuIndexIn].select) {
canGetInfo.push(listItem[skuIndex].attr[skuIndexIn]);
}
}
}
var canGetInfoLog = "";
var skuBeanList = this.data.skuBeanList;
var haveSkuBean = [];
// 对应库存清单扫描
for (var skuBeanIndex = 0; skuBeanIndex < skuBeanList.length; skuBeanIndex++) {
var iListCount = 0;
for (var skuBeanIndexIn = 0; skuBeanIndexIn < skuBeanList[skuBeanIndex].attributes.length; skuBeanIndexIn++) {
if (canGetInfo.length == skuBeanList[skuBeanIndex].attributes.length) {
if (skuBeanList[skuBeanIndex].attributes[skuBeanIndexIn].attributeValId == canGetInfo[skuBeanIndexIn].id) {
iListCount++;
}
} else {
canGetInfoLog = "库存清单不存在此属性" + " ";
}
}
if (iListCount == skuBeanList[skuBeanIndex].attributes.length) {
haveSkuBean.push(skuBeanList[skuBeanIndex]);
}
}
for (var iox = 0; iox < canGetInfo.length; iox++) {
canGetInfoLog += canGetInfo[iox].attributeValue + " ";
}
if (haveSkuBean.length != 0) {
//选中设置
var itemData = that.data.itemData;
itemData.photo_x = haveSkuBean[0].imgurl;
itemData.price_yh = haveSkuBean[0].price;
itemData.num = haveSkuBean[0].count;
var choujiangid = that.data.choujiangid;
var sizeid = haveSkuBean[0].cid;
that.setData({
itemData: itemData,
sizeid: sizeid,
choujiangid: choujiangid,
value: canGetInfoLog
});
} else {
that.setData({
sizeid: '',
value: ''
});
}
// 重新赋值
this.setData({
attrList: listItem,
infoText: canGetInfoLog,
})
// 重新sku运算
this.onData();
},
/**
* Sku核心算法
* 根据所有出当前类别之外的选择 判断按钮的enable false or true
*/
onData: function () {
var attrListIn = this.data.attrList;
for (var i = 0; i < attrListIn.length; i++) {
var attrListBig = attrListIn[i];
//当前类别之外的选择列表
var attrsOtherSelect = [];
for (var j = 0; j < attrListIn.length; j++) {
var attrListSmall = attrListIn[j];
if (attrListSmall.id != attrListBig.id) {
for (var k = 0; k < attrListSmall.attr.length; k++) {
var attrListSmallAttr = attrListSmall.attr[k];
if (attrListSmallAttr.enable && attrListSmallAttr.select) {
attrsOtherSelect.push(attrListSmallAttr);
}
}
}
}
var enableIds = [];
var skuBeanListIn = this.data.skuBeanList;
for (var z = 0; z < skuBeanListIn.length; z++) {
var ism = true;
var skuBean = skuBeanListIn[z];
for (var j = 0; j < attrsOtherSelect.length; j++) {
var enable = false;
for (var k = 0; k < skuBean.attributes.length; k++) {
var goodAttrBean = skuBean.attributes[k];
if (attrsOtherSelect[j].attributeId == goodAttrBean.attributeId
&& attrsOtherSelect[j].id == goodAttrBean.attributeValId) {
enable = true;
break;
}
}
ism = enable && ism;
}
if (ism) {
for (var o = 0; o < skuBean.attributes.length; o++) {
var goodAttrBean = skuBean.attributes[o];
if (attrListBig.id == goodAttrBean.attributeId) {
enableIds.push(goodAttrBean.attributeValId);
}
}
}
}
var integers = enableIds;
for (var s = 0; s < attrListBig.attr.length; s++) {
var attrItem = attrListBig.attr[s];
attrItem.enable = integers.indexOf(attrItem.id) != -1;
}
}
// 重新赋值
this.setData({
attrList: attrListIn,
skuBeanList: skuBeanListIn
})
},
//加入购物车
addShopCart: function (e) {
// console.log(e.currentTarget.dataset)
var info = e.currentTarget.dataset.info;
//添加到购物车
var that = this;
WXAPI.productAddCart({
accessId: app.globalData.accessId,
storeId: 239,
storeType: 1,
pro_id: info.pro_id,
attribute_id: that.data.sizeid,
num: that.data.buynum,
type: 'addcart',
}).then(res => {
if (res.code == 200) {
//设置购物车刷新
app.d.purchase = 1;
wx.showToast({
title: '已添加到购物车',
icon: 'success',
duration: 2000
});
that.setData({
showModalStatus: false
});
} else {
wx.showToast({
icon: 'loading',
title: res.message,
duration: 2000
});
}
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.setData({
shopId: options.shopId
});
this.reloadData(options.shopId);
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})