559 lines
18 KiB
JavaScript
559 lines
18 KiB
JavaScript
var app = getApp();
|
||
|
||
Component({
|
||
// 目前组件,只接受时间戳,其他的没有做
|
||
// 现在的时间,和结束时间,都必须从后台获取,因为客户端时间不一致
|
||
properties: {
|
||
goodsInfo: {
|
||
type: Object,
|
||
},
|
||
cartList: {
|
||
type: Object,
|
||
},
|
||
},
|
||
data: {
|
||
language: "",
|
||
|
||
// 规格 -------
|
||
optionsId: 0, //选择后的 规格ID
|
||
cartid: 0, //选择后的 购物车ID
|
||
goodsCount: 1,
|
||
optionsMaxCount: 1,
|
||
popupSpecs1: false,
|
||
popupSpecs2: false, //无规格选择商品数量
|
||
popThumb: "",
|
||
popStock: 0,
|
||
popPrice: 0,
|
||
specsManage: [], //选择池 用于排序
|
||
goodsDescription: "",
|
||
popupModeshow: true, //动态模糊框
|
||
// 规格end
|
||
|
||
skuImages: [],
|
||
showSkuImages: false,
|
||
activeSkuIndex: 0,
|
||
activeSkuIndex_1: "", //用于关闭时保存点击预览前的序号,在预览关闭时重新设置预览起始位置
|
||
activeSkuIndex_2: "",
|
||
},
|
||
lifetimes: {
|
||
attached: function () {
|
||
// 在组件实例进入页面节点树时执行
|
||
let language = wx.getStorageSync("langIndex");
|
||
this.setData({
|
||
language: language.en,
|
||
specsManage: [],
|
||
optionsId: 0,
|
||
popupSpecs1: true,
|
||
});
|
||
this.initPopView();
|
||
},
|
||
detached: function () {
|
||
// 在组件实例被从页面节点树移除时执行
|
||
},
|
||
},
|
||
methods: {
|
||
//提交确认
|
||
submitAction() {
|
||
if (this.data.goodsInfo.has_option == 1 && !this.data.optionsId) {
|
||
wx.showToast({
|
||
icon: "none",
|
||
title: "规格id不可为空,请选择规格!",
|
||
duration: 1500,
|
||
});
|
||
return;
|
||
}
|
||
//点击关闭 clicktype为1:确认按钮 ,为0:关闭弹窗操作
|
||
this.triggerEvent("closeSpecsPopup", {
|
||
optionsId: this.data.optionsId,
|
||
cartid: this.data.cartid,
|
||
goodsId: this.data.goodsInfo.id,
|
||
goodsCount: this.data.goodsCount,
|
||
clicktype: 1,
|
||
});
|
||
},
|
||
//关闭popupSpecs
|
||
popupSpecsbtn() {
|
||
this.setData({
|
||
popupSpecs1: false,
|
||
popupModeshow: true,
|
||
});
|
||
this.triggerEvent("closeSpecsPopup", { clicktype: 0 });
|
||
},
|
||
//初始化弹窗view
|
||
initPopView() {
|
||
if (this.data.goodsInfo.has_option == 1) {
|
||
this.setData({
|
||
popPrice:
|
||
this.data.goodsInfo.min_price + "-" + this.data.goodsInfo.max_price, //设置默认价格
|
||
popThumb: this.data.goodsInfo.thumb, //设置默认图片
|
||
popStock: this.data.goodsInfo.stock, //设置默认库存
|
||
goodsDescription: "选择",
|
||
});
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) {
|
||
this.setData({
|
||
goodsDescription:
|
||
this.data.goodsDescription +
|
||
" " +
|
||
this.data.goodsInfo.has_many_specs[i].title,
|
||
});
|
||
}
|
||
let _optionArr = [];
|
||
this.data.goodsInfo.has_many_options.forEach((val, index) => {
|
||
let _thumb = val.thumb != "" ? val.thumb : this.data.goodsInfo.thumb;
|
||
_optionArr.push(_thumb);
|
||
});
|
||
this.setData({
|
||
skuImages: _optionArr,
|
||
});
|
||
if (!this.data.optionsId) {
|
||
// 默认选择第一个
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) {
|
||
this.data.goodsInfo.has_many_specs[
|
||
i
|
||
].description = this.data.goodsInfo.has_many_specs[
|
||
i
|
||
].specitem[0].id;
|
||
|
||
this.selectSpecs(
|
||
this.data.goodsInfo.has_many_specs[i].specitem[0],
|
||
"first",
|
||
i
|
||
);
|
||
}
|
||
}
|
||
} else {
|
||
console.log("单规格", this.data.goodsInfo);
|
||
let __optionArr2 = [];
|
||
__optionArr2.push(this.data.goodsInfo.thumb);
|
||
this.setData({
|
||
popThumb: this.data.goodsInfo.thumb, //设置默认图片
|
||
popStock: this.data.goodsInfo.stock, //设置默认库存
|
||
popPrice: this.data.goodsInfo.price, //设置默认价格
|
||
goodsDescription: "",
|
||
optionsMaxCount: this.data.goodsInfo.stock,
|
||
skuImages: __optionArr2,
|
||
});
|
||
|
||
let { cartList, goodsInfo } = this.data;
|
||
if (cartList && cartList.length > 0) {
|
||
for (let i = 0; i < cartList.length; i++) {
|
||
if (cartList[i].goods_id === goodsInfo.id) {
|
||
this.setData({
|
||
goodsCount: cartList[i].total,
|
||
cartid: cartList[i].id,
|
||
});
|
||
break;
|
||
} else {
|
||
this.data.cartid = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
// 输入数量
|
||
inputGoodsNum: function (e) {
|
||
var num = parseInt(e.detail.value);
|
||
var lastgoodsCount = this.data.goodsCount;
|
||
if (e.detail.keyCode == 8) {
|
||
if (app._isTextEmpty(e.detail.value)) {
|
||
this.setData({
|
||
goodsCount: "",
|
||
});
|
||
return;
|
||
}
|
||
}
|
||
if (num) {
|
||
this.setData({
|
||
goodsCount: num,
|
||
});
|
||
} else {
|
||
this.setData({
|
||
goodsCount: lastgoodsCount,
|
||
});
|
||
}
|
||
},
|
||
//减少
|
||
reduceGoods() {
|
||
let lastgoodsCount = this.data.goodsCount;
|
||
if (this.data.goodsCount == 1 || this.data.goodsCount == 0) {
|
||
return;
|
||
}
|
||
this.setData({
|
||
goodsCount: lastgoodsCount - 1,
|
||
});
|
||
},
|
||
//增加
|
||
addGoods() {
|
||
let lastgoodsCount = this.data.goodsCount;
|
||
let specsManage = this.data.specsManage;
|
||
let optionsMaxCount = this.data.optionsMaxCount;
|
||
console.log("增加");
|
||
if (
|
||
specsManage.length == this.data.goodsInfo.has_many_specs.length &&
|
||
this.data.goodsCount == optionsMaxCount
|
||
) {
|
||
console.log("数量超出范围");
|
||
wx.showToast({
|
||
icon: "none",
|
||
title: "数量超出范围",
|
||
duration: 1500,
|
||
});
|
||
console.log("max=" + this.data.goodsCount);
|
||
return;
|
||
}
|
||
this.setData({
|
||
goodsCount: lastgoodsCount + 1,
|
||
});
|
||
},
|
||
//界面选择规格触发事件
|
||
selectSpecs(e, str, i) {
|
||
let data = {};
|
||
let specsIdex = 0;
|
||
if (str == "first") {
|
||
specsIdex = i;
|
||
data = e;
|
||
} else if (str == "sku_swiper") {
|
||
data = e;
|
||
} else {
|
||
specsIdex = e.currentTarget.dataset.specsidex;
|
||
data = e.currentTarget.dataset.specitem;
|
||
}
|
||
console.log(data);
|
||
if (data.c) {
|
||
return false;
|
||
}
|
||
this.setData({
|
||
["goodsInfo.has_many_specs[" + specsIdex + "].description"]: data.id,
|
||
});
|
||
// let specid = data.specid; //对其他数据筛选 不筛选同级
|
||
//处理选择池
|
||
this.manageSpecs(data);
|
||
//处理规格组合选择状态
|
||
this.setGoodsSpecs(data);
|
||
//设置选择规格后的 价格、照片、库存
|
||
this.setGoodsSpecsChangeInfo();
|
||
//判断当前购买总量与库存的关系
|
||
this.getMaxCount();
|
||
},
|
||
//处理选择池
|
||
manageSpecs(data) {
|
||
let specsObject = new Object();
|
||
let specsManage = this.data.specsManage;
|
||
specsObject.id = data.id;
|
||
specsObject.specid = data.specid;
|
||
specsObject.title = data.title;
|
||
|
||
if (specsManage.length > 0) {
|
||
for (let i = 0; i < specsManage.length; i++) {
|
||
if (specsManage[i].specid == specsObject.specid) {
|
||
specsManage.splice(i, 1);
|
||
}
|
||
}
|
||
specsManage.push(specsObject);
|
||
} else {
|
||
specsManage.push(specsObject);
|
||
}
|
||
|
||
//排序
|
||
if (specsManage.length == this.data.goodsInfo.has_many_specs.length) {
|
||
var newSpecsManage = [];
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) {
|
||
for (let j = 0; j < specsManage.length; j++) {
|
||
if (
|
||
this.data.goodsInfo.has_many_specs[i].id == specsManage[j].specid
|
||
) {
|
||
newSpecsManage.push(specsManage[j]);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
specsManage = newSpecsManage;
|
||
}
|
||
this.setData({
|
||
specsManage: specsManage,
|
||
});
|
||
this.setGoodsDescription();
|
||
},
|
||
//处理goodsDescription 数据
|
||
setGoodsDescription() {
|
||
let description = "";
|
||
let specsManage = this.data.specsManage;
|
||
//相等代表全选了 体现语句
|
||
if (specsManage.length == this.data.goodsInfo.has_many_specs.length) {
|
||
description = "已选择 ";
|
||
for (let i = 0; i < specsManage.length; i++) {
|
||
description += specsManage[i].title + " ";
|
||
}
|
||
this.setData({
|
||
goodsDescription: description,
|
||
});
|
||
} else {
|
||
description = "请选择 ";
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) {
|
||
for (let j = 0; j < specsManage.length; j++) {
|
||
if (
|
||
this.data.goodsInfo.has_many_specs[i].id != specsManage[j].specid
|
||
) {
|
||
description += this.data.goodsInfo.has_many_specs[i].title + " ";
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
this.setData({
|
||
goodsDescription: description,
|
||
});
|
||
}
|
||
},
|
||
//处理规格组合选择状态 过滤数据
|
||
setGoodsSpecs(specs) {
|
||
// let goodsInfo = this.data.goodsInfo;
|
||
// for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) {
|
||
// if (specs.specid != this.data.goodsInfo.has_many_specs[i].id) {
|
||
// // let specitem = this.setGoodsSpecsStatus(
|
||
// // this.data.goodsInfo.has_many_specs[i].specitem,
|
||
// // specs.id
|
||
// // );
|
||
// // this.setData({
|
||
// // ["goodsInfo.has_many_specs[" + i + "].specitem"]: specitem
|
||
// // })
|
||
// }
|
||
// }
|
||
},
|
||
//处理规格组合选择状态 处理状态 specitem 组合数组(未选中的) id当前选中的ID 根据ID 组合算是否有当前组合
|
||
setGoodsSpecsStatus(specitem, id) {
|
||
let options = []; //数据池
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_options.length; i++) {
|
||
let _specs = this.data.goodsInfo.has_many_options[i].specs.split("_");
|
||
//console.log(_specs);
|
||
//判断是否包含
|
||
for (let j = 0; j < _specs.length; j++) {
|
||
if (_specs[j] == id) {
|
||
options.push(this.data.goodsInfo.has_many_options[i]);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
//关键处理方式 后期要优化 效率低
|
||
for (let m = 0; m < options.length; m++) {
|
||
let _specs = options[m].specs.split("_");
|
||
for (let y = 0; y < _specs.length; y++) {
|
||
if (_specs[y] != id && options[m].stock == 0) {
|
||
for (let n = 0; n < specitem.length; n++) {
|
||
if (_specs[y] == specitem[n].id) {
|
||
specitem[n].c = true;
|
||
}
|
||
}
|
||
} else if (_specs[y] != id && options[m].stock > 0) {
|
||
for (let n = 0; n < specitem.length; n++) {
|
||
if (_specs[y] == specitem[n].id) {
|
||
specitem[n].c = false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return specitem;
|
||
},
|
||
//设置选择规格后的 价格、照片、库存
|
||
setGoodsSpecsChangeInfo() {
|
||
let specsManage = this.data.specsManage;
|
||
//根据ID 排序 specsManage.sort();
|
||
specsManage.sort(function (a, b) {
|
||
return a.id - b.id;
|
||
});
|
||
if (specsManage.length == this.data.goodsInfo.has_many_specs.length) {
|
||
let goodsSpecs = "";
|
||
for (let j = 0; j < specsManage.length; j++) {
|
||
goodsSpecs += specsManage[j].id + "_";
|
||
}
|
||
goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_"
|
||
for (let i = 0; i < this.data.goodsInfo.has_many_options.length; i++) {
|
||
if (
|
||
goodsSpecs ==
|
||
this.setGoodsSpecsBySort(
|
||
this.data.goodsInfo.has_many_options[i].specs
|
||
)
|
||
) {
|
||
this.setData({
|
||
activeSkuIndex: i,
|
||
popPrice: this.data.goodsInfo.has_many_options[i].product_price, //设置价格
|
||
popThumb: app._isTextEmpty(
|
||
this.data.goodsInfo.has_many_options[i].thumb
|
||
)
|
||
? this.data.goodsInfo.thumb
|
||
: this.data.goodsInfo.has_many_options[i].thumb, //设置图片
|
||
popStock: this.data.goodsInfo.has_many_options[i].stock, //设置库存
|
||
optionsId: this.data.goodsInfo.has_many_options[i].id, //设置规格ID,用于生成订单
|
||
});
|
||
this.data.optionsMaxCount = this.data.goodsInfo.has_many_options[
|
||
i
|
||
].stock; //库存最大数 用于切换更改买家购买数量
|
||
if (this.data.optionsMaxCount > 0) {
|
||
this.setData({
|
||
goodsCount: 1,
|
||
});
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
this.setData({
|
||
specsManage: specsManage,
|
||
});
|
||
},
|
||
//处理商品goodsSpecs 并排序 新方法
|
||
setGoodsSpecsBySort(specs) {
|
||
let _specs = specs.split("_"); //先变成数组
|
||
//_specs.sort();//排序
|
||
_specs.sort(function (a, b) {
|
||
return a - b;
|
||
});
|
||
// 在组装回去
|
||
let goodsSpecs = "";
|
||
for (let j = 0; j < _specs.length; j++) {
|
||
goodsSpecs += _specs[j] + "_";
|
||
}
|
||
goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_"
|
||
return goodsSpecs;
|
||
},
|
||
//判断当前购买总量与库存的关系
|
||
getMaxCount() {
|
||
let specsManage = this.data.specsManage;
|
||
let optionsMaxCount = this.data.optionsMaxCount;
|
||
console.log(specsManage, optionsMaxCount, this.data.optionsId);
|
||
|
||
if (specsManage.length == this.data.goodsInfo.has_many_specs.length) {
|
||
if (optionsMaxCount == 0) {
|
||
//库存不足
|
||
this.setData({
|
||
goodsCount: 0,
|
||
});
|
||
}
|
||
if (this.data.goodsCount > optionsMaxCount) {
|
||
this.setData({
|
||
goodsCount: optionsMaxCount,
|
||
});
|
||
}
|
||
}
|
||
let { cartList, goodsInfo, optionsId } = this.data;
|
||
|
||
console.log("666", cartList);
|
||
if (cartList && cartList.length > 0) {
|
||
for (let i = 0; i < cartList.length; i++) {
|
||
if (
|
||
cartList[i].goods_id === goodsInfo.id &&
|
||
cartList[i].option_id === optionsId
|
||
) {
|
||
console.log(cartList[i]);
|
||
this.setData({
|
||
goodsCount: cartList[i].total,
|
||
cartid: cartList[i].id,
|
||
});
|
||
break;
|
||
} else {
|
||
this.data.cartid = 0;
|
||
}
|
||
}
|
||
}
|
||
},
|
||
//============ 处理规格图片点击放大 start============
|
||
showSkuImages_Methob() {
|
||
this.setData({
|
||
popupSpecs1: false,
|
||
popupSpecs2: false,
|
||
popupModeshow: true,
|
||
showSkuImages: true,
|
||
});
|
||
if (this.data.goodsInfo.has_option != 1) {
|
||
return;
|
||
}
|
||
this.setData({
|
||
activeSkuIndex_1: this.data.goodsInfo.has_many_options[
|
||
this.data.activeSkuIndex
|
||
].specs,
|
||
});
|
||
},
|
||
onChangeSkuImagesIndex(e) {
|
||
let _index = e.detail.current;
|
||
this.setData({
|
||
activeSkuIndex: _index,
|
||
});
|
||
if (this.data.goodsInfo.has_option != 1) {
|
||
return;
|
||
}
|
||
this.setData({
|
||
activeSkuIndex_2: this.data.goodsInfo.has_many_options[_index].specs,
|
||
});
|
||
},
|
||
onCloseSkuImages() {
|
||
if (this.data.goodsInfo.has_option != 1) {
|
||
this.setData({
|
||
popupSpecs2: true,
|
||
popupModeshow: false,
|
||
showSkuImages: false,
|
||
});
|
||
return;
|
||
}
|
||
let that = this;
|
||
let _lastOption = that.data.activeSkuIndex_1.split("_");
|
||
let goodsInfo = that.data.goodsInfo;
|
||
let _activeSkuIndex = that.data.activeSkuIndex;
|
||
|
||
if (
|
||
that.data.activeSkuIndex_1 !=
|
||
that.data.goodsInfo.has_many_options[_activeSkuIndex].specs
|
||
) {
|
||
let _activeOption = that.data.activeSkuIndex_2.split("_"); //如果没有滑动直接取值是取不到报错的
|
||
for (let i = 0; i < _lastOption.length; i++) {
|
||
if (_lastOption[i] != _activeOption[i]) {
|
||
let _specs = Number(_activeOption[i]);
|
||
for (
|
||
let ind = 0;
|
||
ind < that.data.goodsInfo.has_many_specs.length;
|
||
ind++
|
||
) {
|
||
for (
|
||
let index = 0;
|
||
index < that.data.goodsInfo.has_many_specs[ind].specitem.length;
|
||
index++
|
||
) {
|
||
if (
|
||
that.data.goodsInfo.has_many_specs[ind].specitem[index].id ==
|
||
_specs
|
||
) {
|
||
goodsInfo.has_many_specs[ind].description =
|
||
that.data.goodsInfo.has_many_specs[ind].specitem[index].id;
|
||
// that.selectSpecs(that.data.goodsInfo.has_many_specs[ind].specitem[index],"sku_swiper")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
console.log(that.data.activeSkuIndex_1, "that.data.activeSkuIndex_1");
|
||
|
||
this.setData({
|
||
goodsInfo: goodsInfo,
|
||
showSkuImages: false,
|
||
popupSpecs1: true,
|
||
popupModeshow: false,
|
||
|
||
popPrice: this.data.goodsInfo.has_many_options[_activeSkuIndex]
|
||
.product_price, //设置价格
|
||
popThumb: app._isTextEmpty(
|
||
this.data.goodsInfo.has_many_options[_activeSkuIndex].thumb
|
||
)
|
||
? this.data.goodsInfo.thumb
|
||
: this.data.goodsInfo.has_many_options[_activeSkuIndex].thumb, //设置图片
|
||
popStock: this.data.goodsInfo.has_many_options[_activeSkuIndex].stock, //设置库存
|
||
optionsId: this.data.goodsInfo.has_many_options[_activeSkuIndex].id, //设置规格ID,用于生成订单
|
||
});
|
||
},
|
||
//============ 处理规格图片点击放大 end============
|
||
},
|
||
});
|