// subPackages/xx/index.js import commonApi from "../../utils/https/common" import util from "../../utils/util" let app = getApp() Page({ /** * 页面的初始数据 */ data: { height: 90, padHeight:0, right:0, type_id:'', //用来区别文创特产、非遗 tag_id:'', list:[], keywords:'', skuFlag:null, //规格弹窗 info: null, //规格信息 skuIndex: 0, //默认选中第一个规格 producNum: 1, cartCount:0, showPopup:false, //购物车的弹窗 gwcList:[], //gwc里的产品数据 allSelectedBtn:false, //全选按钮 totalPrice:0, //获取购物车总价--打开购物车后勾选的产品价格 nowAddListId:[], type:'', //购物车分类1文创,2非遗 show:false, user_type: null, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options); let id = '',type='' if (options.type_id == '1') { id = '20,19' type='1' } else { id = '5' // id = '645' type='2' } this.setData({ tag_id:id, type, nowAddListId:options.ids.split(',') }) // this.getList() this.getCount() }, // 去产品详情页 goodsDetail(e){ wx.navigateTo({ url: '/pages/info/postProductInfo/index?id='+e.currentTarget.dataset.goods.id, }) }, // 添加产品到购物车 addCart: function (e) { if (![0,1].includes(this.data.user_type)) { wx.showToast({ title: "请选择配送方式", icon:"none" }) return } let item = e.currentTarget.dataset.item app.globalData.postProduct = [] app.globalData.list = [] commonApi.user_post("/product/checkStock", { sku_id: this.data.showPopup ? item.sku.id : this.data.info.sku[this.data.skuIndex].id, }).then(res => { if (res && res.code != 1) { return; } else { commonApi.user_post("cart_within/add_sku", { sku_id: this.data.showPopup ? item.sku.id :this.data.info.sku[this.data.skuIndex].id, num: this.data.producNum, type:this.data.type, delivery_method: this.data.user_type }).then(res => { if (res.code == 1) { let sku_id = this.data.showPopup ? item.sku.id :this.data.info.sku[this.data.skuIndex].id let _nowAddListId = this.data.nowAddListId // debugger _nowAddListId.push(sku_id) this.setData({ nowAddListId: _nowAddListId }) commonApi.user_post('cart_within/get_list', { type:this.data.type }).then(res => { // debugger let cartCount = 0,totalPrice = 0,gwcList=this.data.gwcList res.data.forEach(val=> { cartCount+=val.num console.log(this.data.skuFlag); if (!this.data.skuFlag && !this.data.showPopup) { console.log(this.data.nowAddListId) if (this.data.nowAddListId.some(s => +s === +val.sku_id) ) { val.selected = 1 } }else { gwcList.forEach(el=> { if (el.id == val.id) { val.selected = el.selected } }) } }) res.data.forEach(element=> { if (element.selected == 1) { totalPrice += Number(element.num*element.sku.price) } if (element.delivery_method === null) { element.delivery_method = element.sku.sku_model.use_type == 1?1:0 } }) console.log(res.data); let status = res.data.every(el => el.selected === 1) this.setData({ cartCount: cartCount, totalPrice, producNum:1, gwcList:res.data, allSelectedBtn:status?true:false, }) }) // 加动效 this.setData({ skuFlag: null, aniSkuIndex: this.data.skuIndex, cartImgInfo: null }) setTimeout(() => { this.setData({ aniSkuIndex: -1 }) // wx.showModal({ // title: "提示", // content: "去购物车结算?", // success: function (res) { // if (res.confirm) { // wx.navigateTo({ // url: '/pages/user/cartlist/list', // }) // } // } // }) }, 650) } }) } }) }, // 去结算 order: function () { commonApi.user_post('wx/get_user_keep', { jumpurl: '/pages/user/cartlist/list', title: '购物车', type: 'mini' }).then(res => { if (res.data.subscribe == 0) { this.setData({ wxqrcode: res.data.qrcode, showQrCode: true }) } else { app.globalData.couponInfo = null; util.pagePoint({ event: 'cart_order' }, 1) let list = this.data.gwcList, price = 0, product = [], product1 = [] list.map(item => { if (item.selected == 1) { // item.product.type="post"; if (item.product.type == 'post') { product.push({ product: item.product, sku: {...item.sku, use_type: item.delivery_method||0}, productNum: item.num }) } else { product1.push(item) app.globalData.list = product1 } if (item.product.type.includes('post')) { this.data.productType = 'post' } } }) if (product.length == 0 && product1.length == 0) { // wx.showToast({ // title: '请先选择产品', // icon: 'none' // }) return; } if (product.length > 0 && product1.length > 0) { this.setData({ flag: 'mix', isCar: 'multiple' }) } if (product1.length > 1) { this.setData({ isCar: 'multiple' }) } else { this.setData({ isCar: 'single' }) } let repeatSku = product.find(x=>product.filter(v=>v.sku.id == x.sku.id).length>1) if(repeatSku) { let str = `${repeatSku.product.title}-${repeatSku.sku.sku_name}邮寄和自提只能选择一种` wx.showModal({ content: str, showCancel: false, complete: (res) => {} }) return } let useType = product[0].sku.use_type let flag = this.data.flag, isCar = "single"; if (product.some(x=>x.sku.use_type!=useType)) { flag = "mix" isCar = "multiple" } this.setData({ flag: flag, isCar: isCar }) console.log(product1); app.globalData.postProduct = product; if (app.globalData.list) { app.globalData.product = app.globalData.list[app.globalData.index] } wx.navigateTo({ url: '/pages/order/postOrder/index?from=cart&flag=' + this.data.flag, }) } }) }, // 单选一个 selectedIt(e) { let item = e.currentTarget.dataset.item,gwcList= this.data.gwcList,totalPrice=0,nowAddListId=[] gwcList.forEach(el=> { if (el.id == item.id) { el.selected == 1 ? el.selected =0 : el.selected = 1 } }) gwcList.forEach(el=> { if (el.selected == 1) { totalPrice += Number(el.num*el.sku.price) } }) gwcList.forEach(item=> { if (item.selected == 1) { nowAddListId.push(item.sku_id) } }) let status = gwcList.every(el => el.selected === 1) this.setData({ gwcList, allSelectedBtn:status?true:false, totalPrice, nowAddListId }) }, // 清空 delAll() { let ids = [] this.data.gwcList.forEach(item => { ids.push(item.id) }) commonApi.user_post("cart_within/del_sku", { id: ids+'', type:this.data.type }).then(res => { if (res.code == 1) { wx.showToast({ title: '清空成功', icon: "success" }) this.setData({ totalPrice:0 }) this.getCount() } }) }, // 减少一个 del(e) { let item = e.currentTarget.dataset.item let gwcList = this.data.gwcList let valueNum = e.currentTarget.dataset.value commonApi.user_post("cart_within/update_sku", { num:item.num + valueNum, id: item.id, type:this.data.type }).then(res => { if (res.code == 1) { // this.getCount() commonApi.user_post('cart_within/get_list', { type:this.data.type }).then(res => { let arr = res.data,totalPrice = 0,cartCount=0 if (res.data.length>0) { res.data.forEach(item=> { // totalPrice += item.num*item.sku.price cartCount += item.num gwcList.forEach(el=> { if (el.id == item.id) { item.selected = el.selected } }) }) res.data.forEach(item=> { if (item.selected ==1) { totalPrice += item.num*item.sku.price } if (item.delivery_method === null) { item.delivery_method = item.sku.sku_model.use_type == 1?1:0 } }) } let status = res.data.every(el => el.selected === 1) this.setData({ cartCount: cartCount, //1 gwcList:res.data, allSelectedBtn:status?true:false, totalPrice }) }) } }) }, // 全选 allSelected() { let gwcList = this.data.gwcList let totalPrice = 0 let nowAddListId = [] if (this.data.allSelectedBtn) { gwcList.forEach(item=> { item.selected =0 }) nowAddListId = [] } else { gwcList.forEach(item=> { item.selected =1 }) gwcList.forEach(item=> { totalPrice += Number(item.num*item.sku.price) }) gwcList.forEach(item=> { nowAddListId.push(item.sku_id) }) } this.setData({ gwcList, allSelectedBtn:!this.data.allSelectedBtn, totalPrice, nowAddListId }) }, // 左侧购物车打开弹窗 openPopup() { // this.getCount() let gwcList = this.data.gwcList let status = gwcList.every(el => el.selected === 1) this.setData({ showPopup:!this.data.showPopup, allSelectedBtn:status?true:false, }) }, // 获取历史购物车详情 getCount() { commonApi.user_post('cart_within/get_list', { type:this.data.type }).then(res => { let arr = res.data,totalPrice = 0,cartCount=0 if (arr.length>0) { arr.forEach(item=> { // totalPrice += item.num*item.sku.price cartCount += item.num }) } arr.forEach(item=> { if (this.data.nowAddListId.some(s => +s === +item.sku_id) ) { item.selected = 1 } }) arr.forEach(item=> { if (item.selected == 1) { totalPrice += item.num*item.sku.price } if (item.delivery_method === null) { item.delivery_method = item.sku.sku_model.use_type == 1?1:0 } }) // console.log(arr); // debugger this.setData({ cartCount: cartCount, //1 gwcList:arr, allSelectedBtn:false, totalPrice }) }) }, search() { this.setData({ list:[], show:false }) this.getList() }, searchInput(e) { this.setData({ keywords:e.detail.value }) }, getList() { let list= this.data.list; if (this.data.keywords == '' || this.data.keywords.indexOf(' ')!==-1) { return } if(list.length>=this.data.total) return; commonApi._post("product/get_product_by_tag",{ tag_id:this.data.tag_id, //标签的ID offset:this.data.list.length, //起始查询 limit: 10, //查询数量 title:this.data.keywords.length>0 ? this.data.keywords : '', //搜索产品名称 }).then(res=>{ res.data.list.map(item=>{ item.display_tags = item.display_tags?item.display_tags.split(","):[]; item.display_tags = item.display_tags.splice(0,2); }) this.setData({ list:list.concat(res.data.list), show:true }) console.log(this.data.list); }) }, getHeight() { let systemInfo = wx.getSystemInfoSync(),rect = wx.getMenuButtonBoundingClientRect(); let height = (rect.top - systemInfo.statusBarHeight) * 2 + rect.height; this.setData({ height:height, padHeight:systemInfo.statusBarHeight, right:(systemInfo.screenWidth - rect.right) + rect.width }) console.log(systemInfo,rect) }, back: function () { if(this.properties.clickid){ wx.uma.trackEvent(this.properties.clickid); } const pages = getCurrentPages(); // wx.showTabBar({}) console.log(this.properties.isIndex) if (pages.length<=1){ wx.switchTab({ url: app.globalData.menuRoute }) } else { wx.navigateBack({ delta: 1 }) } }, // 添加产品弹窗 1.首先获取到该产品下的规格 addBuyCart(e) { let item = e.currentTarget.dataset.item console.log(item); commonApi._post("product/get_product_detail", { id: item.id }).then(res => { if (item.skuid) { let sku = res.data.sku.find(skuItem => item.id == skuItem.skuid); res.data.sku = [sku]; } res.data.flag = res.data.sku.find(item => item.flag == 'on') ? res.data.flag : 0 this.setData({ info: res.data, // skuFlag: "cart" skuIndex: 0, user_type: res.data.sku[0].use_type>1?1:res.data.sku[0].use_type }) this.showCart() this.BroswerRecord() }) }, // 添加产品弹窗 2.渲染规格,未登录去登录 showCart: function () { commonApi.user_post('wx/get_user_keep', { jumpurl: '/pages/info/postProductInfo/index?id=' + this.data.id, title: this.data.info.title, type: 'mini' }).then(res => { if (res.data.subscribe == 0) { this.setData({ wxqrcode: res.data.qrcode, showQrCode: true }) } else { if (this.data.info.sku.length == 0) { wx.showToast({ title: '该产品未设置规格,不能加购', icon: 'none' }) return; } this.setData({ skuFlag: "cart" }) let that = this wx.createSelectorQuery().select('#skuImg').boundingClientRect(function (res) { console.log(res) that.setData({ cartImgInfo: 'top:' + res.top + 'px;left:' + res.left + 'px;' }) }).exec() } this.getCount() }) }, // 规格弹窗切换其他规格 selectSku: function (e) { let index = e.currentTarget.dataset.index; this.setData({ skuIndex: index, user_type: this.data.info.sku[index].use_type>1?null:this.data.info.sku[index].use_type, }) }, changeSelectSkuPost: function(e) { let value = e.currentTarget.dataset.value; this.setData({ user_type: value, }) }, // 关闭弹窗 hideSku: function () { this.setData({ skuFlag: null, cartImgInfo: null }) }, // 减少数量 minus: function () { if (this.data.producNum == 1) return; this.setData({ producNum: this.data.producNum - 1 }) }, // 加数量 add: function () { this.setData({ producNum: this.data.producNum + 1 }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { this.getHeight() }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ show:false }) this.getHeight() this.getCount() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { let ids = this.data.nowAddListId ids = ids.map(item => Number(item)); app.globalData.nowAddListId = ids // debugger }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.getList() }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })