// packageE/orderullPackage/orderullPackage.js var app = getApp(); Page({ /** * 页面的初始数据 */ data: { cartsNum: 0, //购物车数量 cartList: [], categoryList: [], //顶部分类导航 activeCat: null, goodList: [], sortOption: [{ value: "id", name: "综合排序" }, { value: "price", name: "价格最低" }, { value: "discount", name: "优惠力度" } ], activeSort: "id", activity_gid: 0, activity_index: 0, showgp: false, popupSpecs: false, goodinfo: {}, freight: null, page: 1, //分页数,当前页数 isLoadMore: true, //判断是否要加载更多的标志 total_page: 0, //总页数 orderCart: [], //记录下单页传递的购物车id showCarList: false, // 购物车配送方式冲突 dispatch_types: [], showChoose: false, radioChoose: "" }, options: { multipleSlots: true // 在组件定义时的选项中启用多slot支持 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options.cart_id) { let cartStr = JSON.parse(options.cart_id); //注意!! [].split(',')会返回[""],此时再.map[Number]会返回[0],要处理 if (cartStr[0] === "") { cartStr = []; } this.data.orderCart = cartStr.map(Number); } this.getCategory(); }, getCategory() { //获取分类列表 let urlStr = app.getNetAddresss("goods.postage-included-category.index"); app._postNetWork({ url: urlStr, data: {}, success: (resdata) => { let res = resdata.data; if (res.result == 1) { this.setData({ categoryList: res.data }); if (res.data && res.data.length > 0) { this.setData({ activeCat: res.data[0].id }); this.changeSort(); } } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, changeCategory(e) { let _id = e.currentTarget.dataset.id; this.setData({ activeCat: _id //记录当前选择的分类 }); this.changeSort(); }, changeSort(e) { let _sort = null; if (e && e.currentTarget.dataset && e.currentTarget.dataset.value) { _sort = e.currentTarget.dataset.value; this.setData({ activeSort: _sort //记录当前选择的排序方式 }); } this.setData({ goodList: [] }); let _json = { category_id: this.data.activeCat, sort: _sort || "id" }; let urlStr = app.getNetAddresss("goods.postage-included-category.goods"); app._postNetWork({ url: urlStr, data: _json, success: (resdata) => { let res = resdata.data; if (res.result == 1) { this.setData({ goodList: res.data.data, //商品列表 isLoadMore: true, total_page: res.data.last_page, page: 1 }); this.getCartList(); } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, getCartList() { //获取购物车列表,该列表继承商城购物车,只返回通过接口postageIncludedCategory.member-cart.store添加的购物车数据 let urlStr = app.getNetAddresss("postageIncludedCategory.member-cart.index"); app._postNetWork({ url: urlStr, data: {}, success: (resdata) => { let res = resdata.data; if (res.result == 1) { this.setData({ cartList: res.data }); this.setGoodTotal(); this.getFreight(); } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, setGoodTotal() { this.data.cartsNum = 0; // 计算设置当前商品列表中是否有已加入购物车的数据,有则显示购物车数量 for (let index = 0; index < this.data.cartList.length; index++) { this.data.cartsNum += this.data.cartList[index].total; for (let j = 0; j < this.data.goodList.length; j++) { if (index == 0 && this.data.goodList[j].total) { this.data.goodList[j].total = 0; //初始化置0 } if (this.data.cartList[index].goods_id == this.data.goodList[j].id) { let _total = 0; if (this.data.cartList[index].option_id == 0) { // 无规格 _total = this.data.cartList[index].total; //设置商品的购物车数量 } else { // 有规格 _total = this.data.goodList[j].total || 0; _total += this.data.cartList[index].total; } this.data.goodList[j].total = _total; //设置商品的购物车数量 this.data.goodList[j].card_id = this.data.cartList[index].id; //设置商品的购物车id } } } this.setData({ cartsNum: this.data.cartsNum, goodList: this.data.goodList }); }, getFreight() { // 计算运费, let _cart_ids = []; this.data.cartList.forEach(item => { _cart_ids.push(item.id); }); let _cartArrConcat = _cart_ids.concat(this.data.orderCart); let _cartArr = new Set(_cartArrConcat); _cartArr = [..._cartArr]; let urlStr = app.getNetAddresss("postageIncludedCategory.cart-list.index"); app._postNetWork({ url: urlStr, data: { cart_ids: _cartArr.join(",") }, success: (resdata) => { let res = resdata.data; if (res.result == 1) { // postage_included_msg为空时,说明符合包邮条件了 this.setData({ freight: res.data.postage_included_msg && res.data.postage_included_msg != "" ? Number(res.data.postage_included_msg) : 0 }); } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, _getMoreData() { let that = this; let urlStr = app.getNetAddresss("goods.postage-included-category.goods"); that.data.isLoadMore = false; // 防止多次请求分页数据 if (this.data.page >= this.data.total_page) { return; } else { this.data.page = this.data.page + 1; app._postNetWork({ url: urlStr, showToastIn: false, data: { page: this.data.page, category_id: this.data.activeCat, sort: this.data.activeSort }, success: function (resdata) { let res = resdata.data; if (res.result == 1) { that.setData({ isLoadMore: true, goodList: that.data.goodList.concat(res.data.data) }); } else { that.setData({ page: that.data.page - 1, isLoadMore: false }); return; } }, fail: function (res) { console.log(res.msg); } }); } }, addCart(e) { console.log(e.detail); let _data = e.detail; this.setData({ activity_index: _data.index }); if (_data.isSpecs) { this.showGoodDetail(_data, true); //规格商品,显示规格弹窗 return; } this.addMemberCart({ id: _data.id, total: "1", index: _data.index }); }, addMemberCart(_data) { //添加到购物车,当前的加入购物车数据会影响到商城购物车的数据;如果商城购物车有,则修改数量;没有则加入购物车 // 但当前页面只显示在当前操作的商品的购物车数据, if (!_data.id) return; let _json = { goods_id: _data.id, total: _data.total || 1, option_id: _data.option_id || 0 }; let urlStr = app.getNetAddresss("postageIncludedCategory.member-cart.store"); app._postNetWork({ url: urlStr, data: _json, success: (resdata) => { let res = resdata.data; wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); if (res.result == 1) { this.setData({ popupSpecs: false, showgp: false, ['goodList[' + _data.index + '].total']: _data.total }); } this.getCartList(); }, fail: (res) => { console.log(res); }, }); }, showGoodDetail(e, _spec) { let _data = e.detail ? e.detail : e; if (this.data.activity_gid == _data.id) { let _showPopup = !_spec ? 'showgp' : 'popupSpecs'; this.setData({ [_showPopup]: true }); return; } this.setData({ activity_gid: _data.id }); let urlStr = app.getNetAddresss("goods.goods.get-goods-page"); app._postNetWork({ url: urlStr, data: { id: Number(_data.id) }, success: (resdata) => { let res = resdata.data; if (res.result == 1) { let name = !_spec ? "showgp" : 'popupSpecs'; this.setData({ [name]: true, goodinfo: res.data.get_goods, }); } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, close_yz_specs_popup(e) { this.setData({ popupSpecs: false, }); if (e.detail.clicktype == 1) { //点击确认按钮 if (e.detail.cartid) { // 有购物车记录 this.updateCart(e.detail.cartid, e.detail.goodsCount); } else { // 新加入购物车 this.addMemberCart({ id: this.data.activity_gid, total: e.detail.goodsCount, option_id: e.detail.optionsId||0, index: this.data.activity_index }); } } }, openCartListPopup() { this.setData({ showCarList: true }); }, close_yz_cartList_popup() { this.setData({ showCarList: false }); }, changeCart(_data) { // 更新购物车数量 let e = _data.detail ? _data.detail : _data; if (!e.cartId) { return; } if (Number(e.num) == 0) { //删除购物车 this.destroyCart(e.cartId); return; } let urlStr = app.getNetAddresss("cart.list.updateNumV2"); app._postNetWork({ url: urlStr, data: { id: e.cartId, num: e.num }, success: (resdata) => { let res = resdata.data; wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); if (res.result == 1) { this.setData({ popupSpecs: false, showgp: false }); if (e.index) { this.setData({ ['goodList[' + e.index + '].total']: e.num }); } this.getCartList(); } }, fail: (res) => { console.log(res); }, }); }, destroyCart(_id) { let urlStr = app.getNetAddresss("cart.list.destroy"); app._postNetWork({ url: urlStr, data: { ids: _id }, success: (resdata) => { let res = resdata.data; wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); this.getCartList(); }, fail: (res) => { console.log(res); }, }); }, // 购物车列表组件里的步进器 ============ start ================== changeCarList(e) { let _total = e.detail.name == "plus" ? e.detail.total + 1 : e.detail.name == "minus" ? e.detail.total - 1 : e.detail.total; this.changeCart({ cartId: e.detail.id, num: _total }); }, // 购物车列表组件里的步进器 ============ end ================== typeChange(event) { this.setData({ radioChoose: Number(event.detail), }); }, // 检查购物车商品列表是否有配送方式冲突 checkGoods() { let _cart_ids = []; this.data.cartList.forEach(item => { _cart_ids.push(item.id); }); let _cartArrConcat = _cart_ids.concat(this.orderCart); let _cartArr = new Set(_cartArrConcat); _cartArr = [..._cartArr]; let urlStr = app.getNetAddresss("memberCart.checkout"); urlStr += "&cart_ids=" + _cartArr.join(","); app._getNetWork({ url: urlStr, success: (resdata) => { let 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.toOrder(); } } else { wx.showToast({ icon: "none", title: res.msg, duration: 1000, }); } }, fail: (res) => { console.log(res); }, }); }, toOrder() { this.initData(); //当前是否是从下单页跳转过来的 let _cart_ids = []; let _cartArr = []; if (this.data.showChoose && !this.data.radioChoose) { wx.showToast({ icon: "none", title: '请先选择配送方式', duration: 1000, }); return; } if (this.data.showChoose) { // 配送方式有冲突 for (let i = 0; i < this.data.dispatch_types[this.data.radioChoose].member_carts.length; i++) { _cartArr.push(this.data.dispatch_types[this.data.radioChoose].member_carts[i].id); } } else { this.data.cartList.forEach(item => { _cart_ids.push(item.id); }); let _cartArrConcat = _cart_ids.concat(this.data.orderCart); _cartArr = new Set(_cartArrConcat); _cartArr = [..._cartArr]; //去重 } setTimeout(() => { //跳转下单页 wx.navigateTo({ url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=' + JSON.stringify(_cartArr) }); }, 80); }, initData() { this.setData({ showgp: false, popupSpecs: false, showCarList: false, showChoose: false }); }, showgoodspecs(){ this.setData({ popupSpecs: true }); }, closeChoose(){ this.setData({ showChoose: false }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { if (this.data.isLoadMore) { this._getMoreData(); } else { console.log('没有更多数据'); } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } });