card-mini/packagecard/shop/catalog/column/column.js

475 lines
12 KiB
JavaScript

// pages/category/index.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
CustomBar: app.globalData.CustomBar,
productList: [],
isRefreshing: false,
currentPage: 1,
totalSize: 0,
shopId: '',
isAddPage: false,
isCatalogPage: false,
activeNames: ['0'],
baseImageUrl: app.loginUrl + app.baseImgUrl,
currentCatalogId: '',
curIndex: 0,
active: 1,
curGoodsList: [],
contentHeight: app.globalData.windowHeight - app.globalData.CustomBar,
isLoadMore: false,
hasMore: true,
isShowCart: false,
shopCart: [],
baseImg: app.restAjax.baseUrl.tradeUrl + app.restAjax.baseUrl.baseImgUrl,
},
/**
* 切换类目
*/
switchRightTap(e) {
let curindex = parseInt(e.currentTarget.dataset.curindex);
this.data.productList[curindex].goodsList = []
this.setData({
currentPage: 1,
hasMore: true,
isLoadMore: false,
curIndex: curindex
})
let item = this.data.productList[curindex]
this.getGoodsListByCId(item.categoryId)
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
shopId: options.shopId,
shopCart: app.globalData.shopCart
})
this.getShopCatalogList(this.data.shopId)
this.selectComponent('#sCart').refreshCart()
},
//获取店铺所有商品类目
getShopCatalogList(id) {
let _self = this
wx.showLoading({
title: '加载中...',
})
app.restAjax.get(app.restAjax.path(app.apis.getShopCatalogList, [app.baseUrls.tradeUrl, id]), {}, {
headers: {
token: app.globalData.token
}
}, (code, data) => {
wx.hideLoading({})
_self.setData({
isRefreshing: false
})
if (code == 200) {
_self.buildDatas(data)
}
}, (code, error) => {
wx.hideLoading({})
if (error.msg) {
app.dialog.msg(error.msg)
} else {
wx.showToast({
title: '网络错误',
icon: 'error'
})
}
})
},
doAdd(e) {
let key = e.currentTarget.dataset.key
if ('chooseCatalog' == key) {
wx.navigateTo({
url: './choosecatalog?shopId=' + this.data.shopId,
})
} else {
if (e.currentTarget.dataset.item) {
wx.navigateTo({
url: './addproduct?shopId=' + this.data.shopId + '&cataLogId=' + e.currentTarget.dataset.item.categoryId,
})
} else {
wx.showToast({
title: '请先添加类目',
icon: 'error'
})
}
}
},
manageGoods(e) {
wx.navigateTo({
url: '../../product/goodsdetail?shopId=' + e.currentTarget.dataset.item.shopId + '&goodsId=' + e.currentTarget.dataset.item.goodsId
})
},
/**
* 获取商品列表
*/
getGoodsList(shopId) {
wx.showLoading({
title: '加载中...',
})
app.restAjax.get(app.restAjax.path(app.apis.getShopGoodsList, [app.baseUrls.tradeUrl, shopId]), {}, {
headers: {
token: app.globalData.token
}
}, (code, data) => {
wx.hideLoading({})
this.setData({
isRefreshing: false
})
if (code == 200) {
this.buildDatas(data)
}
}, (code, error) => {
wx.hideLoading({})
if (error.msg) {
app.dialog.msg(error.msg)
} else {
wx.showToast({
title: '网络错误',
icon: 'error'
})
}
})
},
//构建数据
buildDatas(data) {
let _self = this
if (data.length > 0) {
data.forEach(el => {
el.goodsList = []
_self.data.productList.push(el)
})
_self.setData({
curIndex: 0,
productList: _self.data.productList
})
_self.getGoodsListByCId(_self.data.productList[0].categoryId)
}
},
//去除重复的
unique(rows) {
let temp = rows.concat()
let tempList = []
for (var i = 0; i < temp.length; i++) {
for (var j = i + 1; j < temp.length; j++) {
if (temp[i].categoryId == temp[j].categoryId) {
temp.splice(j, 1);
j--
}
}
}
temp.forEach(el => {
tempList.push({
name: el.categoryName,
id: el.categoryId,
goods: []
})
})
return tempList
},
//刷新页面
dorefreshList() {
this.setData({
currentPage: 1,
isLoadMore: false,
hasMore: true,
productList: []
})
this.getShopCatalogList(this.data.shopId)
},
onShow() {
if (this.data.isAddPage) {
this.setData({
isAddPage: false
})
this.dorefreshList()
}
this.selectComponent('#sCart').refreshCart()
},
//根据类目ID获取商品
getGoodsListByCId(id) {
let _self = this
wx.showLoading({
title: '加载中...',
})
app.restAjax.get(app.restAjax.path(app.apis.getGoodsByCatalogAndShop, [app.baseUrls.tradeUrl, id]), {
page: _self.data.currentPage,
shopId: _self.data.shopId
}, {
headers: {
token: app.globalData.token
}
}, (code, data) => {
wx.hideLoading({})
_self.setData({
isLoadMore: false
})
if (code == 200) {
if (data.rows.length > 0) {
data.rows.forEach(it => {
it.buyNum = 0
})
data.rows.forEach(it => {
//判断购物车是否存在该商品
if (app.globalData.shopCart.length > 0) {
app.globalData.shopCart.forEach(i => {
if (i.goodsId == it.goodsId) {
it.buyNum = i.buyNum
}
})
}
})
_self.data.productList.forEach(el => {
if (el.categoryId == id) {
el.goodsList = el.goodsList.concat(data.rows)
}
})
_self.setData({
productList: _self.data.productList
})
} else {
_self.setData({
hasMore: false
})
}
}
}, (code, error) => {
wx.hideLoading({})
_self.setData({
isLoadMore: false
})
if (error.msg) {
app.dialog.msg(error.msg)
} else {
wx.showToast({
title: '网络错误',
icon: 'error'
})
}
})
},
onChange(e) {
let _self = this
let item = e.currentTarget.dataset.item
this.setData({
activeNames: e.detail,
currentCatalogId: item.categoryId
})
console.log(e)
if (e.detail.length > 1) {
//展开
if (item.goodsList || item.goodsList.length <= 0) {
_self.getGoodsListByCId(item.categoryId)
}
}
},
doSearchGoods() {
wx.navigateTo({
url: './searchproduct?shopId=' + this.data.shopId,
})
},
//加载更多
doLoadMore(e) {
let _self = this
if (_self.data.hasMore) {
if (_self.data.isLoadMore) {
return
}
let item = _self.data.productList[_self.data.curIndex]
_self.setData({
isLoadMore: true,
currentPage: ++_self.data.currentPage
})
_self.getGoodsListByCId(item.categoryId)
} else {
wx.showToast({
title: '暂无更多数据',
icon: 'none',
duration: 500
})
}
},
doDelCategory(e) {
let _self = this
wx.showLoading({
title: '删除中...',
})
app.restAjax.delete(app.restAjax.path(app.apis.doDelShopCategoryById, [app.baseUrls.tradeUrl, e.currentTarget.dataset.item.categoryId]), {}, {
headers: {
token: app.globalData.token
}
}, (code, data) => {
wx.hideLoading({})
if (code == 200) {
_self.setData({
productList: []
})
wx.showToast({
title: '删除成功',
icon: 'success'
})
_self.getShopCatalogList(_self.data.shopId)
}
}, (code, error) => {
wx.hideLoading({})
if (error.msg) {
app.dialog.msg(error.msg)
} else {
wx.showToast({
title: '网络错误',
icon: 'error'
})
}
})
},
showConfirmDialog(e) {
let _self = this
wx.showModal({
title: '提示',
content: '确定要删除该类目吗?(删除后该类目下所有商品将删除!)',
success(res) {
if (res.confirm) {
_self.doDelCategory(e)
} else {
}
}
})
},
//列表添加购物车
addToCart(e) {
let _self = this
let goods = e.currentTarget.dataset.item
_self.data.productList[_self.data.curIndex].goodsList.forEach(it => {
if (goods.goodsId == it.goodsId) {
it.buyNum += 1
if (it.buyNum > goods.goodsTotal) {
it.buyNum = goods.goodsTotal
wx.showToast({
title: '超出库存',
icon: 'error'
})
}
}
})
this.selectComponent('#sCart').listAddToCart(goods)
_self.setData({
productList: _self.data.productList,
shopCart: app.globalData.shopCart
})
},
removeToCart(e) {
let goods = e.currentTarget.dataset.item
let _self = this
_self.data.productList[_self.data.curIndex].goodsList.forEach(it => {
if (goods.goodsId == it.goodsId) {
it.buyNum -= 1
}
})
this.selectComponent('#sCart').listRemoveCart(goods)
_self.setData({
productList: _self.data.productList,
shopCart: app.globalData.shopCart
})
},
//显示购物车弹框
doShowCart() {
var _self = this
if (this.data.shopCart.length > 0) {
//判断当前购物车是否存在该商店的商品
var isExist = false
this.data.shopCart.forEach(it => {
if (it.shopId == _self.data.shopId) {
isExist = true
}
})
if (isExist) {
if (this.data.isShowCart) {
this.setData({
isShowCart: false
})
} else {
this.setData({
isShowCart: true
})
}
} else {
wx.showToast({
title: '购物车空空如也',
icon: 'error'
})
}
} else {
wx.showToast({
title: '购物车空空如也',
icon: 'error'
})
}
},
//隐藏购物车弹框
onHideCart() {
this.setData({
isShowCart: false
})
},
//删除购物车中的商品
delGoods(e) {
let _self = this
var index = e.currentTarget.dataset.index
var goods = e.currentTarget.dataset.goods
app.globalData.shopCart.splice(index, 1)
//删除购物车中数据
_self.selectComponent('#sCart').refreshCart()
//将列表中的数据购买数量重置
_self.data.productList[_self.data.curIndex].goodsList.forEach(it => {
if (it.goodsId == goods.goodsId) {
it.buyNum = 0
}
})
_self.setData({
productList: _self.data.productList,
shopCart: app.globalData.shopCart
})
if (_self.data.shopCart.length <= 0) {
_self.setData({
isShowCart: false
})
}
},
//购物车弹框删除或添加商品
onChange(e) {
let buyNum = e.detail
let goods = e.currentTarget.dataset.goods
let _self = this
//遍历列表
_self.data.productList[_self.data.curIndex].goodsList.forEach(it => {
if (it.goodsId == goods.goodsId) {
it.buyNum = buyNum
}
})
//遍历购物车
_self.data.shopCart.forEach(it => {
if (it.goodsId == goods.goodsId) {
it.buyNum = buyNum
}
})
//刷新视图
_self.setData({
productList: _self.data.productList,
shopCart: _self.data.shopCart
})
//刷新购物车
_self.selectComponent('#sCart').refreshCart()
},
onHide() {
this.setData({
isShowCart: false
})
}
})