// pages/class/class.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { bgImg: app.globalData.bgImg, activeOne: 0, activeTwo: 0, scrolltop: 0, goodsList: [], typeParentList: [], goodsName: '', topImg: '', }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { //上级id if (options.pId) { wx.setStorage({ data: options.pId, key: 'pId', }) } wx.getSystemInfo({ success: res => { console.log(res) this.setData({ winHeight: (res.windowHeight - (res.windowWidth / 750) * 130) - 10, }); this.setData({ rightHeight: this.data.winHeight - (res.windowWidth / 750) * 310, }); } }); //头部图片 2 this.topImg(); wx.showLoading({ title: '加载中', }) wx.request({ url: app.globalData.ip + '/wisdommining/api/goods/getGoodsTypeList', method: 'GET', header: { "content-type": "application/x-www-form-urlencoded", }, data: {}, success: res => { console.log(res) wx.hideLoading() if (res.data.code == 1) { var typeParentList = res.data.value; if (options.id) { console.log(options.id) console.log(options.pid) for (let i = 0; i < typeParentList.length; i++) { typeParentList[i].oneType = false; if (typeParentList[i].id == options.pid) { typeParentList[i].oneType = true; for (let j = 0; j < typeParentList[i].goodsTypeList.length; j++) { typeParentList[i].goodsTypeList[j].twoType = false if (typeParentList[i].goodsTypeList[j].id == options.id) { typeParentList[i].goodsTypeList[j].twoType = true this.setData({ activeOne: i, activeTwo: j, goodsList: typeParentList[i].goodsTypeList[j].goodsTypeList, }) } } } } } else { for (let i = 0; i < typeParentList.length; i++) { typeParentList[i].oneType = false; for (let j = 0; j < typeParentList[i].goodsTypeList.length; j++) { typeParentList[i].goodsTypeList[j].twoType = false } } typeParentList[0].oneType = true; typeParentList[0].goodsTypeList[0].twoType = true; this.setData({ goodsList: typeParentList[0].goodsTypeList[0].goodsTypeList, }) } this.setData({ typeParentList: typeParentList, }) } else { wx.showModal({ title: '获取数据失败!', content: res.data.message, showCancel: false, }) } } }) }, //头部图片 2 topImg(options) { console.log(options) wx.showLoading({ title: '加载中', }) wx.request({ url: app.globalData.ip + '/wisdommining/api/banner/getBanner', method: 'GET', header: { "content-type": "application/x-www-form-urlencoded", }, data: { type: 2 }, success: res => { console.log(res) wx.hideLoading() if (res.data.code == 1) { let topImg = res.data.value; this.setData({ topImg: topImg }) } else { wx.showModal({ title: '获取数据失败!', content: res.data.message, showCancel: false, }) } } }) }, //左边栏选择 one getSelOne(e) { var typeParentList = this.data.typeParentList; var index = e.currentTarget.dataset.index; typeParentList[index].oneType = !typeParentList[index].oneType; this.setData({ typeParentList:typeParentList }) }, //左边栏选择 two getSelTwo(e) { var typeParentList = this.data.typeParentList; var index = e.currentTarget.dataset.index; var dindex = e.currentTarget.dataset.dindex; for (let i = 0; i < typeParentList.length; i++) { for (let j = 0; j < typeParentList[i].goodsTypeList.length; j++) { typeParentList[i].goodsTypeList[j].twoType = false } } typeParentList[dindex].goodsTypeList[index].twoType = true; this.setData({ scrolltop: 0, goodsList: typeParentList[dindex].goodsTypeList[index].goodsTypeList, typeParentList:typeParentList }) }, //数据 dataList: function (e) { wx.showLoading({ title: '加载中', }) wx.request({ url: app.globalData.ip + '/wisdommining/api/goods/getGoodsTypeList', method: 'GET', header: { "content-type": "application/x-www-form-urlencoded", }, data: {}, success: res => { console.log(res) wx.hideLoading() if (res.data.code == 1) { var typeParentList = res.data.value; this.setData({ typeParentList: typeParentList, goodsList: typeParentList[0].goodsTypeList[0].goodsTypeList, }) console.log(this.data.typeParentList) } else { wx.showModal({ title: '获取数据失败!', content: res.data.message, showCancel: false, }) } } }) }, // 三级分类搜商品 toShopDetail(e) { let id = e.currentTarget.dataset.id; let name = e.currentTarget.dataset.name; console.log(id) wx.navigateTo({ url: '/pages/search_details/search_details?threeTypeId=' + id + '&name=' + name, }) }, //搜索 search() { wx.navigateTo({ url: '/pages/search_details/search_details?search=true', }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { wx.getStorage({ key: 'user', success: user => { app.globalData.userId = user.data.id; console.log(app.globalData.userId) wx.request({ url: app.globalData.ip + '/wisdommining/api/goods/getCart', method: 'POST', header: { "content-type": "application/x-www-form-urlencoded", }, data: { userId: app.globalData.userId }, success: res => { console.log(res) if (res.data.code == 1) { if (res.data.value.length == 0) { app.globalData.cartNum = ''; wx.hideTabBarRedDot({ index: 3 }) } else { app.globalData.cartNum = res.data.value.length.toString() //购物车数量显示 wx.setTabBarBadge({ index: 3, text: app.globalData.cartNum }) } } else { wx.showModal({ title: '提示!', content: res.data.message, showCancel: false, }) } } }) }, fail: f => { app.globalData.cartNum = ''; wx.hideTabBarRedDot({ index: 3 }) } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { app.share(); return { path: '/pages/index/index?pId=' + app.globalData.userId, title: '三品慧采小程序等你来~' } }, })