var app = getApp();
var WxParse = require('../../lib/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
Page({
data: {
goods: {},
attributeList: [],
cartGoodsCount: 0,
userHasCollect: 0,
number: 1,
checkedSpecText: '请选择规格数量',
openAttr: false,
noCollectImage: "/static/images/icon_collect.png",
hasCollectImage: "/static/images/icon_collect_checked.png",
collectBackImage: "/static/images/icon_collect.png",
id: 0,
name: '',
price: 0.00,
priceMart: 0.00,
numCurrent: 0,
unit: '',
imageList: [],
nodes: [],
isCollect: true,
selectedImg: '',
selectedPrice: 0,
selectedNumCurrent: 0,
ids: '',
commitWay: 0,
noScrollHeight: '100%',
goodsAttributeId: '0',
openCoupon: false,
couponList: [],
},
getGoodsInfo: function() {
let that = this;
util.request(api.GoodsDetail, {
id: that.data.id
}).then(function(res) {
if (res.errno === 0) {
that.setData({
goods: res.data,
selectedImg: res.data.imageList[0].imageUrl,
selectedPrice: res.data.price,
selectedNumCurrent: res.data.numCurrent,
cartGoodsCount: res.data.shopcarNum,
});
}
});
},
getGoodsIntroduction: function() {
let that = this;
util.request(api.GoodsIntroduction, {
id: that.data.id
}).then(function(res) {
if (res.errno === 0) {
that.setData({
nodes: res.data,
});
}
});
},
getAttributeList: function() {
let that = this;
util.request(api.AttributeList, {
id: that.data.id
}).then(function(res) {
if (res.errno === 0) {
let _attributeList = res.data.attributeList;
// 如果仅仅只存在一个规格,那么商品页面初始化时默认checked
if (_attributeList.length == 1) {
if (_attributeList[0].valueList.length == 1) {
_attributeList[0].valueList[0].checked = true
that.setData({
'checkedSpecText': "已选择:" + _attributeList[0].valueList[0].value,
'ids': '' + _attributeList[0].valueList[0].id
});
that.getNumAndPrice();
}
}
that.setData({
attributeList: _attributeList,
});
}
});
},
getNumAndPrice: function() {
let that = this;
util.request(api.NumAndPrice, {
id: that.data.id,
ids: that.data.ids
}).then(function(res) {
if (res.errno === 0) {
if (!util.isBlank(res.data)) {
that.setData({
selectedPrice: res.data.price,
selectedNumCurrent: res.data.numCurrent,
selectedImg: res.data.imageUrl,
goodsAttributeId: res.data.id,
});
} else {
that.setData({
selectedPrice: that.data.goods.price,
selectedNumCurrent: 0,
selectedImg: that.data.goods.imageList[0].imageUrl,
});
}
}
});
},
clickSkuValue: function(event) {
let that = this;
let specName = event.currentTarget.dataset.value;
let specValueId = event.currentTarget.dataset.valueId;
//TODO 性能可优化,加文末链接入群探讨
let _attributeList = this.data.attributeList;
for (let i = 0; i < _attributeList.length; i++) {
if (_attributeList[i].name === specName) {
for (let j = 0; j < _attributeList[i].valueList.length; j++) {
if (_attributeList[i].valueList[j].id == specValueId) {
//如果已经选中,则反选
if (_attributeList[i].valueList[j].checked) {
_attributeList[i].valueList[j].checked = false;
} else {
_attributeList[i].valueList[j].checked = true;
}
} else {
_attributeList[i].valueList[j].checked = false;
}
}
}
}
this.setData({
'attributeList': _attributeList,
});
//重新计算spec改变后的信息
this.changeSpecInfo();
this.getNumAndPrice();
//重新计算哪些值不可以点击,先不做
},
/*
*返回已选属性的list
*/
getCheckedSpecValue: function() {
let checkedValues = [];
let _attributeList = this.data.attributeList;
for (let i = 0; i < _attributeList.length; i++) {
let _checkedObj = {
//name: _attributeList[i].specification,
name: _attributeList[i].name,
valueId: 0,
valueText: ''
};
for (let j = 0; j < _attributeList[i].valueList.length; j++) {
if (_attributeList[i].valueList[j].checked) {
_checkedObj.valueId = _attributeList[i].valueList[j].id;
_checkedObj.valueText = _attributeList[i].valueList[j].value;
//checkedValues.push(_checkedObj);//放在这里,就不用过滤valueId为0的
}
}
checkedValues.push(_checkedObj);
}
return checkedValues;
},
//根据已选的值,计算其它值的状态
setSpecValueStatus: function() {
},
//判断规格是否选择完整
isCheckedAllSpec: function() {
return !this.getCheckedSpecValue().some(function(v) {
if (v.valueId == 0) {
return true;
}
});
},
getCheckedSpecKey: function() {
let checkedValue = this.getCheckedSpecValue().map(function(v) {
return v.valueId;
});
return checkedValueId;
},
getCheckedSpecText: function() {
let checkedValue = this.getCheckedSpecValue().map(function(v) {
return v.valueText;
});
return checkedValueText;
},
changeSpecInfo: function() {
let checkedNameValue = this.getCheckedSpecValue();
//设置选择的信息 (获取已选属性的文本数组?)
let checkedValue = checkedNameValue.filter(function(v) {
if (v.valueId != 0) {
return true;
} else {
return false;
}
}).map(function(v) {
return v.valueText;
});
let checkedValueId = checkedNameValue.filter(function(v) {
if (v.valueId != 0) {
return true;
} else {
return false;
}
}).map(function(v) {
return v.valueId;
});
if (checkedValue.length > 0) {
this.setData({
'checkedSpecText': checkedValue.join(' '),
'ids': checkedValueId.join(',')
});
} else {
this.setData({
'checkedSpecText': '请选择规格数量'
});
}
},
onLoad: function(options) {
// 页面初始化 options为页面跳转所带来的参数
if (options.id) {
this.setData({
id: parseInt(options.id)
});
this.getGoodsInfo();
this.getGoodsIntroduction();
this.getCouponList();
}
},
onReady: function() {
// 页面渲染完成
},
onShow: function() {
},
onHide: function() {
// 页面隐藏
},
onUnload: function() {
// 页面关闭
},
//收藏
closeAttrOrCollect: function() {
let that = this;
if (this.data.isCollect) {
this.setData({
isCollect: false,
collectBackImage: this.data.hasCollectImage
});
} else {
this.setData({
isCollect: true,
collectBackImage: this.data.noCollectImage
});
}
return;
//添加或是取消收藏
util.request(api.CollectAddOrDelete, {
typeId: 0,
valueId: this.data.id
}, "POST")
.then(function(res) {
let _res = res;
if (_res.errno == 0) {
if (_res.data.type == 'add') {
that.setData({
'collectBackImage': that.data.hasCollectImage
});
} else {
that.setData({
'collectBackImage': that.data.noCollectImage
});
}
} else {
wx.showToast({
image: '/static/images/icon_error.png',
title: _res.errmsg,
mask: true
});
}
});
//}
},
openCartPage: function() {
wx.switchTab({
url: '/pages/cart/cart'
});
},
addToCart: function(commitWay) {
var that = this;
if (this.data.openAttr == false) {
this.setData({
commitWay: 1,
});
this.openPop();
} else {
//提示选择完整规格
if (!this.isCheckedAllSpec()) {
wx.showToast({
image: '/static/images/icon_error.png',
title: '请选择完整规格'
});
return false;
}
//验证库存
if (this.data.selectedNumCurrent < this.data.number) {
wx.showToast({
image: '/static/images/icon_error.png',
title: '没有库存'
});
return false;
}
//添加到购物车
util.request(api.CartAdd, {
goodsId: this.data.goods.id,
goodsAttributeId: this.data.goodsAttributeId,
number: this.data.number,
setmealId: 0,
activeId: 0,
isSetmeal: 0,
isJoinActivte: 0
}, "POST")
.then(function(res) {
let _res = res;
if (_res.errno == 0) {
that.closePop();
that.setData({
cartGoodsCount: _res.data.shopcarNum,
});
wx.showToast({
title: '添加成功'
});
} else {
wx.showToast({
image: '/static/images/icon_error.png',
title: _res.errmsg,
mask: true
});
}
});
}
},
cutNumber: function() {
this.setData({
number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
});
},
addNumber: function() {
if (this.data.number < this.data.selectedNumCurrent) {
this.setData({
number: this.data.number + 1
});
}
},
confirmNum: function() {
if (this.data.number > this.data.selectedNumCurrent || this.data.number == 0) {
util.showErrToast("数量超出范围");
}
},
goIndex: function() {
wx.switchTab({
url: '/pages/index/index',
})
},
openPop: function() {
let _this = this;
wx.getSystemInfo({
success: function(res) {
let data = res.screenHeight + 'rpx';
_this.setData({
noScrollHeight: data
});
},
})
if (this.data.openAttr == false) {
//打开规格选择窗口
this.setData({
openAttr: true,
num: 1,
});
this.getAttributeList();
}
},
closePop: function() {
this.setData({
openAttr: false,
noScrollHeight: '100%',
});
},
btnConfirm: function() {
if (this.data.commitWay == 1) {
this.addToCart();
} else if (this.data.commitWay == 2) {
this.addFast();
}
},
onShareAppMessage: function() {
return {
title: app.globalData.mallName,
desc: this.data.goods.name,
path: '/pages/goods/goods?id=' + this.data.id,
}
},
openCoupon() {
let _this = this;
wx.getSystemInfo({
success: function(res) {
let data = res.screenHeight + 'rpx';
_this.setData({
noScrollHeight: data
});
},
})
if (this.data.openCoupon == false) {
this.setData({
openCoupon: true,
});
}
},
closeCoupon() {
this.setData({
openCoupon: false,
noScrollHeight: '100%',
});
},
getCouponList: function () {
let that = this;
util.request(api.CouponList, { sceneType: 2, goodsId: that.data.id, categoryId:0},"POST").then(function (res) {
if (res.errno === 0) {
that.setData({
couponList: res.data,
});
}
});
},
})