yuminge-app/yun-min-program-plugin-master/packageG/pages/buy/cart_v2/cart_v2.js

990 lines
26 KiB
JavaScript

// pages/buy/cart_v2/cart_v2.js
var app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
language: '',
showcarrfootBol: false,
showcarrfoot_ipxBol: false,
firstshow: false,
loginshow: false,
cartList_none: false,
integral: "积分",
selectBol: true,
cartDelete: false, //切换编辑状态
eachCheckList: [], //每个店铺选中的商品购物车id
eachCartsList: [], //每个店铺全部的商品
allCartsList: [], //每个店铺全部的商品购物车id
all_carts_id: [], //全部购物车id
eachCheckShop: [], // 每个店铺是否全选的状态
cart_ids: [], //选中的购物车id
cartList: [], //购物车数据
failureCartList: [], //失效商品数据
total_goods_price: 0, //商品总价是优惠前的价格
total_amount: 0, //合计总价
total_discount_amount: 0, //优惠
discount_amount_items: [], //选中购物车优惠项
// goodData: {}, //单个商品数据=>规格弹窗
// goods_option_id: '', //单个商品规格id
// popupSpecs: false, //规格弹窗显示
// showCouponList: false, //优惠券弹窗显示
showCouponDetail: false, //优惠明细弹窗显示
isShowList: false, //购物车商品显示
checkAll: false, //全选
rentIndex: -1, // 判断购物车是否有租赁商品
//配送方式
showChoose: false,
radioChoose: '',
dispatch_types: [],
chooseType: '', // 判断是否选中了租赁商品
recommendInfo:[],
recommendPage:1,
recommendLast_page:1,
recommendCurrent_page:1,
showMore: false,
sharin_is_open: false, // 购物车分享
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let yz_basic_info = wx.getStorageSync('yz_basic_info');
this.setData({
vip_show:yz_basic_info.globalParameter.vip_show?yz_basic_info.globalParameter.vip_show:false
});
console.log(this.data.vip_show)
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
let language = wx.getStorageSync('langIndex');
this.setData({
'language': language.en
});
wx.setNavigationBarTitle({
title: this.data.language.cart["购物车"]
});
let value = wx.getStorageSync("integral");
if (value) {
this.setData({
integral: value,
});
}
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
if (this.firstlogin()) {
// do nothing
} else {
this._datainit();
this._getData();
}
// 页面设置插件--商城接口
this.designRecommend();
},
designRecommend(){
let urlStr = app.getNetAddresss("cart.list.recommend-goods");
app._getNetWork({
url: urlStr,
data:{
page:this.data.recommendPage
},
success: (resdata) => {
let res = resdata.data;
if (res.result === 1) {
this.setData({
recommendInfo:res.data.data,
recommendCurrent_page:res.data.current_page,
recommendLast_page:res.data.last_page
});
}else{
this.setData({
recommendInfo:false,
recommendPage:1,
recommendLast_page:1,
recommendCurrent_page:1
});
}
}
});
},
designRecommendMore(){
let urlStr = app.getNetAddresss("cart.list.recommend-goods");
app._getNetWork({
url: urlStr,
data:{
page:this.data.recommendPage
},
success: (resdata) => {
let res = resdata.data;
if (res.result === 1) {
this.setData({
recommendInfo:this.data.recommendInfo.concat(res.data.data),
recommendCurrent_page:res.data.current_page,
recommendLast_page:res.data.last_page
});
}
}
});
},
goRecommend(e){
let id = e.currentTarget.dataset.id;
wx.navigateTo({
url: '/packageA/detail_v2/detail_v2?id='+id,
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if(this.data.recommendCurrent_page >= this.data.recommendLast_page){
console.log('没有更多了');
return;
}else{
this.setData({
recommendPage:this.data.recommendPage + 1
});
this.designRecommendMore();
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {},
onShareTimeline:function(){
return {
query:'?id=1'
};
},
showcarrfoot_ipx(e) {
let bol = e.detail;
this.setData({
showcarrfoot_ipxBol: bol,
});
},
showcarrfoot(e) {
let bol = e.detail;
this.setData({
showcarrfootBol: bol,
});
},
//初始化数据
_datainit() {
this.setData({
selectBol: true,
cartDelete: false, //切换编辑状态
eachCheckList: [], //每个店铺选中的商品购物车id
eachCartsList: [], //每个店铺全部的商品
allCartsList: [], //每个店铺全部的商品购物车id
all_carts_id: [], //全部购物车id
eachCheckShop: [], // 每个店铺是否全选的状态
cart_ids: [], //选中的购物车id
cartList: [], //购物车数据
failureCartList: [], //失效商品数据
total_goods_price: 0, //商品总价是优惠前的价格
total_amount: 0, //合计总价
total_discount_amount: 0, //优惠
discount_amount_items: [], //选中购物车优惠项
showCouponDetail: false, //优惠明细弹窗显示
isShowList: false, //购物车商品显示
checkAll: false, //全选
rentIndex: -1, // 判断购物车是否有租赁商品
//配送方式
showChoose: false,
radioChoose: '',
dispatch_types: [],
chooseType: '', // 判断是否选中了租赁商品
});
},
//获取失效购物车数据
getFailureCart() {
let urlStr = app.getNetAddresss("cart.list.failure-cart");
app._getNetWork({
url: urlStr,
success: (resdata) => {
let res = resdata.data;
if (res.result === 1) {
this.setData({
failureCartList: res.data || [],
});
}
},
fail: function (res) {
console.log(res);
},
});
},
//清空失效购物车数据
clearFailureCart() {
wx.showModal({
title: "提示",
content: "确定清空失效商品吗?",
success:(res)=> {
if (res.confirm) {
//确认清空
let urlStr = app.getNetAddresss("cart.list.del-failure-cart");
app._postNetWork({
url: urlStr,
success: (resdata) => {
let res = resdata.data;
if (res.result === 1) {
this.setData({
failureCartList: [],
});
app.tips("清空成功");
}
},
fail: function (res) {
console.log(res);
},
});
} else if (res.cancel) {
console.log("用户点击取消");
}
},
});
},
//获取购物车数据
_getData(flag) {
let json = {
cart_ids: this.data.cart_ids.join(',')
};
if(flag === 'delete') {
// 获取购物车数量
json.basic_info = 1;
}
let urlStr = app.getNetAddresss("cart.list.index");
app._postNetWork({
url: urlStr,
data: json,
success: (resdata) => {
let res = resdata.data;
if (res.result === 1) {
this.setData({
sharin_is_open: res.data.sharin_is_open,
total_discount_amount: res.data.total_discount_amount,
discount_amount_items: res.data.discount_amount_items,
total_amount: res.data.total_amount,
total_goods_price: res.data.total_goods_price,
cartList: res.data.list
});
if (!flag) {
this.getFailureCart();
this._allCheckListInit(this.data.cartList);
} else if (flag === 'delete') {
this._allCheckListInit(this.data.cartList,flag);
}
// flag === 'count' do nothing
this.setData({
firstshow: true,
cartList_none: res.data.list.length <= 0 ? false : true
});
} else {
app.tips(res.msg);
// this.setData({
// isShowList: false,
// });
}
},
fail: function (res) {
console.log(res);
},
});
},
//初始化全选数据
_allCheckListInit(data,flag) {
this.setData({
cart_ids: [],
eachCartsList: [],
eachCheckList: [],
eachCheckShop: [],
});
if(flag == "delete" && data.length<=0){
this.setData({
cartDelete:false
})
}
if (data.length) {
let list = [];
let all_carts_id = [];
for (let i = 0; i < data.length; i++) {
this.data.eachCartsList[i] = data[i].carts;
this.data.eachCheckList.push([]);
this.data.eachCheckShop.push({
check: false,
code: data[i].code,
is_alone: data[i].is_alone
});
if (data[i].code === 'lease-toy') {
this.data.rentIndex = i;
}
list[i] = data[i].carts.map((item) => {
all_carts_id.push(item.cart_id);
return item.cart_id.toString();
});
}
//取得购物车全部数据
this.setData({
eachCartsList: this.data.eachCartsList,
eachCheckList: this.data.eachCheckList,
eachCheckShop: this.data.eachCheckShop,
rentIndex: this.data.rentIndex,
allCartsList: list,
all_carts_id: all_carts_id,
isShowList: true,
});
// console.log(this.data.eachCartsList, this.data.eachCheckList,this.data.eachCheckShop,this.data.allCartsList, this.data.all_carts_id);
}
// data的length为0也显示购物车内容 #66497
this.setData({
isShowList: true,
});
},
selectBolfun(num) {
// 判断点击是否点击全选 '1', 防止多次触发this.eachCheckList[c_index]变化allSelectHandle
num === '1' ? this.data.selectBol = true : this.data.selectBol = false;
},
//全选
allSelect(event) {
let bol = event.detail;
this.setData({
checkAll: bol,
});
this.selectBolfun('1');
if (this.data.selectBol) {
if (this.data.checkAll) {
if (this.data.allCartsList.length === 1) {
// 判断只有租赁的情况
this.setData({
chooseType: 'rent',
eachCheckList: JSON.parse(JSON.stringify(this.data.allCartsList)),
[`eachCheckShop[0].check`]: true
});
} else if (this.data.cartDelete) {
// 删除操作的时候
let allCartsList = JSON.parse(JSON.stringify(this.data.allCartsList));
for (let i = 0; i < allCartsList.length; i++) {
this.data.eachCheckShop[i].check = true;
}
this.setData({
eachCheckList: JSON.parse(JSON.stringify(this.data.allCartsList)),
eachCheckShop: this.data.eachCheckShop
});
} else {
// 租赁和平台混合
this.data.chooseType = 'buy';
this.data.eachCheckList = [];
let allCartsList = JSON.parse(JSON.stringify(this.data.allCartsList));
for (let i = 0; i < allCartsList.length; i++) {
if (this.data.eachCheckShop[i].is_alone) {
this.data.eachCheckShop[i].check = false;
this.data.eachCheckList.push([]);
} else {
this.data.eachCheckShop[i].check = true;
this.data.eachCheckList.push(this.data.allCartsList[i]);
}
}
this.setData({
chooseType: 'buy',
eachCheckList: this.data.eachCheckList,
eachCheckShop: this.data.eachCheckShop
});
}
} else {
this.data.chooseType = '';
this.data.eachCheckList = [];
for (let i = 0; i < this.data.allCartsList.length; i++) {
this.data.eachCheckList.push([]);
this.data.eachCheckShop[i].check = false;
}
this.setData({
chooseType: '',
eachCheckList: this.data.eachCheckList,
eachCheckShop: this.data.eachCheckShop
});
}
this._countCartView();
}
},
//选择单个店铺商品购买监听 this.eachCheckList[c_index]变化会触发
allSelectHandle(event) {
let value = event.detail;
let c_index = event.currentTarget.dataset.c_index;
this.selectBolfun('0');
if (!this.data.selectBol) {
this.data.eachCheckShop[c_index].check = value.length === this.data.eachCartsList[c_index].length;
this.data.eachCheckList[c_index] = value;
this.setData({
eachCheckShop: this.data.eachCheckShop,
eachCheckList: this.data.eachCheckList
});
this._countCartView();
}
},
// 点击整个店铺购买 通过触发this.eachCheckShop[c_index]请求
shopChange(event) {
let val = event.detail;
let c_index = event.currentTarget.dataset.c_index;
if (this.data.cartList[c_index].disable) {
return;
}
this.selectBolfun('0');
if (!this.data.selectBol) {
if (val) {
this.data.eachCheckShop[c_index].check = true;
// 先清空再赋值
this.data.eachCheckList[c_index].splice(0, this.data.eachCheckList[c_index].length);
this.data.eachCartsList[c_index].map((item) => {
this.data.eachCheckList[c_index].push(item.cart_id.toString());
});
} else {
this.data.eachCheckShop[c_index].check = false;
this.data.eachCheckList[c_index].splice(0, this.data.eachCheckList[c_index].length);
}
this.setData({
eachCheckShop: this.data.eachCheckShop,
eachCheckList: this.data.eachCheckList,
});
this._countCartView();
}
},
//删除界面操作
onCartDelete() {
this.data.eachCheckList = [];
for (let i = 0; i < this.data.eachCartsList.length; i++) {
this.data.eachCheckList.push([]);
this.data.cartList[i].disable = false;
this.data.eachCheckShop[i].check = false;
}
this.selectBolfun('1');
this.setData({
cartDelete: !this.data.cartDelete,
chooseType: '',
checkAll: false,
eachCheckList: this.data.eachCheckList,
cart_ids: [],
total_amount: 0,
total_goods_price: 0,
total_discount_amount: 0,
cartList: this.data.cartList,
eachCheckShop: this.data.eachCheckShop,
});
},
//删除商品
deleteGoods() {
if (this.data.cart_ids.length == 0) {
wx.showToast({
icon: 'none',
title: '请选择删除商品',
duration: 1500
});
return;
}
let urlStr = app.getNetAddresss("cart.list.destroy");
urlStr += "&ids=" + this.data.cart_ids.join(',');
app._getNetWork({
url: urlStr,
success: (resdata) => {
let res = resdata.data;
if (res.result == 1) {
this._getData('delete');
} else {
wx.showToast({
icon: 'none',
title: res.msg,
duration: 1500
});
}
},
fail: (res) => {
console.log(res);
}
});
},
// 检查配送方式是否冲突
checkGoods(event) {
// let str = event.currentTarget.dataset.str;
if (this.data.cart_ids.length == 0) {
wx.showToast({
icon: 'none',
title: '请选择结算商品',
duration: 1500
});
return;
}
if (this.data.cart_ids.length > 50) {
wx.showToast({
icon: 'none',
title: '最多一次性只能下单50个商品',
duration: 1500
});
return;
}
let urlStr = app.getNetAddresss("memberCart.checkout");
urlStr += "&cart_ids=" + this.data.cart_ids.join(",");
app._getNetWork({
url: urlStr,
success: (resdata) => {
var res = resdata.data;
if (res.result == 1) {
if (res.data.need_choose == 1) {
this.setData({
dispatch_types: res.data.dispatch_types,
showChoose: true,
});
} else {
this.submitGoods();
}
} else {
wx.showToast({
icon: "none",
title: res.msg,
duration: 1500,
});
}
},
fail: function (res) {
console.log(res);
},
});
},
toBuy() {
this.submitGoods();
},
//提交订单
submitGoods() {
let _cart_ids = [];
if (this.data.showChoose && !this.data.radioChoose.toString()) {
wx.showToast({
icon: "none",
title: "请先选择配送方式",
duration: 1500,
});
return;
}
if (this.data.showChoose) {
// 配送方式有冲突
for (let i = 0; i < this.data.dispatch_types[this.data.radioChoose].member_carts.length; i++) {
_cart_ids.push(
this.data.dispatch_types[this.data.radioChoose].member_carts[i].id
);
}
} else {
_cart_ids = this.data.cart_ids;
}
if (this.data.chooseType === 'rent') {
wx.navigateTo({
url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=rentCartBuy&cart_ids=' + JSON.stringify(_cart_ids)
});
} else if (this.data.chooseType === 'buy') {
wx.navigateTo({
url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=' + JSON.stringify(_cart_ids)
});
}
},
//跳转商品详情
toGoodsInfo(e) {
let goods_option_id = e.currentTarget.dataset.option_id;
let goodsid = e.currentTarget.dataset.goodsid;
if(goods_option_id){
wx.navigateTo({
url: "/packageA/detail_v2/detail_v2?id=" + goodsid + "&coid=" + goods_option_id,
});
}else{
wx.navigateTo({
url: "/packageA/detail_v2/detail_v2?id=" + goodsid,
});
}
},
// 供应商
toShop(e) {
let merchant_id = e.currentTarget.dataset.item.merchant_id;
if (merchant_id > 0) {
wx.navigateTo({
url: "/packageA/member/supplier/SupplierShop/SupplierShop?id=" + merchant_id,
});
}
},
// 凑单
toSearch(e) {
let merchant_id = e.currentTarget.dataset.item.merchant_id;
if (merchant_id > 0) {
wx.navigateTo({
url: "/packageA/member/supplier/SupplierShop/SupplierShop?id=" + merchant_id,
});
} else {
wx.navigateTo({
url: "/packageB/member/category/search_v2/search_v2?plugin_id=1",
});
}
},
//改变数量
_changeCount(_num, cart_id, index, c_index) {
if (_num < 0) {
wx.showToast({
icon: "none",
title: "商品数量不能为负数",
duration: 1500,
});
this.setData({
[`cartList[${c_index}].carts[${index}].total`]: 1
});
return;
}
let urlStr = app.getNetAddresss("cart.list.updateNumV2");
urlStr += "&id=" + cart_id;
urlStr += "&num=" + _num;
app._getNetWork({
url: urlStr,
success: (resdata) => {
let res = resdata.data;
if (res.result == 1) {
this._countCartView();
} else {
wx.showToast({
icon: "none",
title: res.msg,
duration: 1500,
});
this.setData({
[`cartList[${c_index}].carts[${index}].total`]: 1,
});
}
},
fail: function (res) {
console.log(res);
},
});
},
numberLeft(e) {
let index = e.currentTarget.dataset.index;
let c_index = e.currentTarget.dataset.c_index;
if (this.data.cartList[c_index].carts[index].total <= 1) {
wx.showToast({
icon: "none",
title: "商品数量不能为负数或零",
duration: 1500,
});
return;
}
this.setData({
[`cartList[${c_index}].carts[${index}].total`]: Number(this.data.cartList[c_index].carts[index].total) - 1,
});
this._changeCount(
this.data.cartList[c_index].carts[index].total,
this.data.cartList[c_index].carts[index].cart_id,
index,
c_index
);
},
numberRight(e) {
let index = e.currentTarget.dataset.index;
let c_index = e.currentTarget.dataset.c_index;
this.setData({
[`cartList[${c_index}].carts[${index}].total`]: Number(this.data.cartList[c_index].carts[index].total) + 1,
});
// if (this.data.cartList[c_index].carts[index].total >= 51) {
// wx.showToast({
// icon: 'none',
// title: '该商品每周最多可购买50件',
// duration: 1500
// })
// this.setData({
// [`cartList[${c_index}].carts[${index}].total`]: 1
// })
// return;
// }
this._changeCount(
this.data.cartList[c_index].carts[index].total,
this.data.cartList[c_index].carts[index].cart_id,
index,
c_index
);
},
//输入框失去焦点改变数量
blurCount(e) {
let goodtotal = e.currentTarget.dataset.goodtotal;
let cart_id = e.currentTarget.dataset.cart_id;
let index = e.currentTarget.dataset.index;
let c_index = e.currentTarget.dataset.c_index;
this._changeCount(
goodtotal,
cart_id,
index,
c_index
);
},
//商品输入框输入绑定
goodinpbtn(e) {
let index = e.currentTarget.dataset.index;
let c_index = e.currentTarget.dataset.c_index;
let goodval = e.detail.value;
this.setData({
[`cartList[${c_index}].carts[${index}].total`]: goodval,
});
},
//计算总价格
_countCartView() {
let cart_ids = [];
for (let i = 0; i < this.data.eachCheckList.length; i++) {
this.data.eachCheckList[i].map((item) => {
cart_ids.push(item);
});
}
this.setData({
cart_ids: cart_ids,
});
this.checkRent();
this.checkChooseAll();
if (!this.data.cartDelete) {
this._getData('count');
}
},
// 判断是否全选
checkChooseAll() {
if (this.data.cartDelete) {
if (this.data.all_carts_id.length === this.data.cart_ids.length) {
//判断全选
this.data.checkAll = true;
} else {
this.data.checkAll = false;
}
} else {
let alone_num = 0;
if (this.data.allCartsList.length > 1) {
// 计算单独下单的商品数量 再判断是否全选
for (let i = 0; i < this.data.allCartsList.length; i++) {
if (this.data.eachCheckShop[i].is_alone) {
alone_num = alone_num + this.data.allCartsList[i].length;
}
}
}
if ((this.data.all_carts_id.length - alone_num) === this.data.cart_ids.length) {
//判断全选
this.data.checkAll = true;
} else {
this.data.checkAll = false;
}
}
this.setData({
checkAll: this.data.checkAll
});
},
// 判断是否选中了租赁商品
checkRent() {
if (this.data.cart_ids.length > 0) {
if (this.data.rentIndex > -1 && this.data.eachCheckList[this.data.rentIndex].length > 0) {
this.data.chooseType = 'rent';
} else {
this.data.chooseType = 'buy';
}
} else {
// 全部清空
this.data.chooseType = '';
}
this.setData({
chooseType: this.data.chooseType
});
},
closeChoose() {
this.setData({
showChoose: false,
});
},
typeChange(event) {
this.setData({
radioChoose: Number(event.detail),
});
},
openCoupon() {
this.setData({
showCouponDetail: true
});
},
closeCoupon() {
this.setData({
showCouponDetail: false
});
},
//跳转个人中心
gomember() {
wx.reLaunch({
url: '/packageG/member_v2/member_v2'
});
},
//跳转到首页
gohome() {
wx.reLaunch({
url: '/packageG/index/index'
});
},
gotologin() {
app.seyzredirect();
wx.navigateTo({
url: '/pages/login/login'
});
},
firstlogin() {
let firstlogin = false;
try {
var value = wx.getStorageSync('wx_token');
if (value) {
// Do something with return value
this.setData({
loginshow: false
});
firstlogin = false;
} else {
this.setData({
loginshow: true
});
firstlogin = true;
}
} catch (e) {
// Do something when catch error
}
return firstlogin;
},
toShowMore() {
this.setData({
showMore: !this.data.showMore
});
},
shareCartGood() {
wx.navigateTo({
url: '/packageF/cart_share/cart_share',
});
}
// showCouponList() {
// this.setData({
// showCouponList: true
// });
// },
// closeCouponList() {
// this.setData({
// showCouponList: false
// });
// },
// openSpecs(e) {
// let goods_id = e.currentTarget.dataset.good.goods_id;
// this.getGood(goods_id);
// },
// close_yz_specs_popup(e) {
// if(e.detail.clicktype == 1) {
// //点击确认按钮
// this.setData({
// popupSpecs: false,
// optionsId: e.detail.optionsId,
// goodsCount: e.detail.goodsCount
// });
// }else {
// this.setData({
// popupSpecs: false,
// });
// }
// },
// //获取数据
// getGood(goods_id) {
// let that = this;
// let urlStr = app.getNetAddresss("goods.goods.get-goods-page");
// app._postNetWork({
// url: urlStr,
// showToastIn: false,
// data: {
// id: goods_id,
// },
// success: function(resdata) {
// let res = resdata.data;
// if (res.result == 1) {
// that.setData({
// popupSpecs: true,
// goodsInfo: res.data.get_goods
// });
// }
// },
// fail: function(res) {
// console.log(res.msg);
// }
// });
// },
});