You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

817 lines
23 KiB

// pages/list/store/index.js
// import commonApi from "../../../utils/https/common"
// import util from "../../../utils/util"
import commonApi from "../../utils/https/common"
import util from "../../utils/util"
let app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
height: 90,
padHeight:0,
right:0,
list:[], //获取的产品列表
total:1, //总数
keywords:"", //搜索关键字
navIndex:0, //一级分类选中下标
navList:[
{
title:'列表样式',
icon:'https://static.ticket.sz-trip.com/uploads/20240715/1cb81757944dd8523f3786195969266c.png',
seldIcon:'https://static.ticket.sz-trip.com/uploads/20240710/62bb38d5f88754e57447a26b14176aa2.png'
},
{
title:'宫格样式',
icon:'https://static.ticket.sz-trip.com/uploads/20240717/eebe3e2345a8adb2668bc34cb28cee74.png',
seldIcon:'https://static.ticket.sz-trip.com/uploads/20240717/b3e9136ecb05bedcd9d3f59ee4399b62.png'
}
],
flag:true, //切换样式,true按分类,false按店铺展示
typeList:[], //左侧的二级分类
typeIndex:0, //二级分类选中下标
id:'1',
cartCount:0,
skuFlag:null, //规格弹窗
info: null, //规格信息
skuIndex: 0, //默认选中第一个规格
producNum: 1,
showPopup:false, //购物车的弹窗
gwcList:[], //gwc里的产品数据
allSelectedBtn:false, //全选按钮
seachFlag:false, //搜索模式
totalPrice:0, //获取购物车总价
name:'非遗专卖',
nowAddListId:[],
user_type: null,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
app.globalData.nowAddListId = []
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
})
if (options.idIndex) {
this.setData({
typeIndex:options.idIndex
})
}
// this.getCount()
// this.getType()
},
// 清空
delAll() {
let ids = []
this.data.gwcList.forEach(item => {
ids.push(item.id)
})
commonApi.user_post("cart_within/del_sku", {
id: ids+'',
type:'2'
}).then(res => {
if (res.code == 1) {
wx.showToast({
title: '清空成功',
icon: "success"
})
this.setData({
allSelectedBtn:false,
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:'1'
}).then(res => {
if (res.code == 1) {
// this.getCount()
commonApi.user_post('cart_within/get_list', {
type:'2'
}).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
})
})
}
})
},
// 添加产品到购物车
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:"2",
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
_nowAddListId.push(sku_id)
this.setData({
nowAddListId: _nowAddListId
})
commonApi.user_post('cart_within/get_list', {
type:'2'
}).then(res => {
let cartCount = 0,totalPrice = 0,gwcList=this.data.gwcList
res.data.forEach(val=> {
cartCount+=val.num
console.log(this.data.skuFlag);
// debugger
if (!this.data.skuFlag && !this.data.showPopup) {
// debugger
if (this.data.nowAddListId.includes(val.sku.id)) {
val.selected = 1
}
}else {
// debugger
gwcList.forEach(el=> {
if (el.id == val.id) {
val.selected = el.selected
}
})
}
// if (this.data.showPopup) {
// totalPrice += Number(val.num*val.sku.price)
// 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
}
})
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;
}
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;
// 是否从购物车下单
app.globalData.isCartBuy = true;
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,
})
}
})
},
// 全选
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
})
},
// 单选一个
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
})
},
// 左侧购物车打开弹窗
openPopup() {
// this.getCount()
let gwcList = this.data.gwcList
let status = false
if (gwcList.length>0) {
status = gwcList.every(el => el.selected === 1)
}
this.setData({
showPopup:!this.data.showPopup,
allSelectedBtn:status?true:false,
})
},
// 获取商户标签列表
getSupplierTag() {
let list = this.data.list;
if(list.length>=this.data.total) return;
commonApi.user_post('supplier/get_supplier_by_title_tag_id', {
tag_id:5, //标签的ID
offset:list.length, //起始查询
limit:10, //查询数量
title: this.data.seachFlag ? this.data.keywords : '' //搜索产品名称
}).then(res => {
let arr = []
if (this.data.seachFlag) {
res.data.list.map(obj => {
arr = [...arr, ...obj.product]
})
}
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:this.data.seachFlag?arr:list.concat(res.data.list),
total:res.data.total
})
})
},
// 获取左侧二级分类
getType() {
commonApi._post("product/tag_list",{
// pid:645, //父标签的ID
pid: 5,
}).then(res=>{
this.setData({
typeList:res.data
})
this.getList()
})
},
// 获取历史购物车详情
getCount() {
commonApi.user_post('cart_within/get_list', {
type:'2'
}).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
})
}
if (app.globalData.nowAddListId.length>0) {
arr.map(item=> {
app.globalData.nowAddListId.forEach(val=> {
if (val == item.sku_id) {
item.selected = 1
}
})
})
// debugger
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
}
})
}
this.setData({
cartCount: cartCount, //1
gwcList:res.data,
allSelectedBtn:false,
totalPrice:app.globalData.nowAddListId.length>0?totalPrice:0
})
})
},
// 重置
onReload() {
this.setData({
list:[],
total:1,
navIndex:0,
typeIndex:this.data.typeIndex,
flag:true,
seachFlag:false
})
this.getType()
this.getCount()
},
// 搜索
search:function(e){
if (e.detail.keywords == '') {
this.setData({
seachFlag:false,
keywords:'',
list:[],
total:1
})
// this.onReload()
}else {
this.setData({
seachFlag:true,
keywords:e.detail.keywords,
list:[],
total:1
})
// this.getList();
}
if (!this.data.navIndex == 0) {
this.getSupplierTag()
} else {
this.getList()
}
},
// 去产品详情页
goodsDetail(e){
wx.navigateTo({
url: '/pages/info/postProductInfo/index?id='+e.currentTarget.dataset.goods.id,
})
},
// 去店铺
goShop(e){
let id = e.currentTarget.dataset.id;
util.pagePoint({
event: 'legacy_shop',
param:{
id:id
},
type:'supplier'
},1)
wx.navigateTo({
url: '/pages/list/store/index?id='+id,
})
},
gotoProduct:function(e){
let item = e.currentTarget.dataset.item;
util.gotoDetail(item)
},
// 切换样式
changeStyle() {
this.setData({
navIndex:this.data.navIndex == 0? 1: 0,
flag:!this.data.flag,
total:1,
list:[],
typeIndex:0
})
if (this.data.flag) {
this.getType()
}else {
this.getSupplierTag()
}
},
// 切换一级分类
changeNav:function(e){
let index = e.currentTarget.dataset.index,navList=this.data.navList;
this.setData({
navIndex:index,
list:[],
total:1
})
if (this.data.navIndex == 0) {
this.getType()
} else {
this.getSupplierTag()
}
},
// 切换左侧二级分类
changeType(e) {
let index = e.currentTarget.dataset.index,typeList=this.data.typeList;
this.setData({
typeIndex:index,
type:typeList[index].id,
list:[],
total:1
})
this.getList()
},
// 获取产品列表
getList:function(){
let list = this.data.list;
if(list.length>=this.data.total) return;
if (!this.data.typeList[this.data.typeIndex]) {
this.setData({typeIndex: 0})
}
commonApi._post("product/get_product_by_tag",{
tag_id:this.data.seachFlag? '5':this.data.typeList[this.data.typeIndex].id, //标签的ID
offset:list.length, //起始查询
limit: 10, //查询数量
title:this.data.seachFlag ? 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),
total:res.data.total
})
console.log(this.data.list);
})
// commonApi._post("supplier/get_supplier_by_tag_id",{
// tag_id:this.data.flag ? this.data.navList[this.data.navIndex].type :this.data.navList2[this.data.navIndex].type,
// limit:10,
// offset:list.length,
// sort:"weight",
// title: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),
// total:res.data.total
// })
// })
},
// 添加产品弹窗 1.首先获取到该产品下的规格
addBuyCart(e) {
this.setData({
user_type: null
})
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"
user_type: res.data.sku[0].use_type>1?null: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
})
},
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
})
}
},
goNewSearch() {
wx.navigateTo({
// url: "/subPackages/postSearch/index?type_id=2",
url: '/subPackages/postSearch/index?type_id=2&ids='+this.data.nowAddListId,
})
},
// lower() {
// if (this.data.flag) {
// this.getList()
// } else {
// this.getSupplierTag()
// }
// },
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.getHeight()
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
if (app.globalData.nowAddListId.length>0) {
this.getCount()
}else {
this.onReload()
}
this.getHeight()
// this.setData({
// list:[],
// total:1,
// navIndex:0,
// typeIndex:0,
// flag:true
// })
// this.getList()
app.globalData.isCartBuy = false;
app.globalData.listName = null
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if (this.data.flag) {
this.getList()
} else {
this.getSupplierTag()
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
onShareTimeline: function() {
return {
title: '君到苏州-文创特产',
query: '',
imageUrl: 'https://static.ticket.sz-trip.com/xcxImages/index/icon5New.png'
}
}
})