| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 | // pages/category/category.jsvar http = require("../../utils/http.js");var util = require('../../utils/util.js');var config = require("../../utils/config.js");import eventBus from '../../utils/eventBus';Page({  /**   * 页面的初始数据   */  data: {    showTabbar:true,    showLoading:false,    rightView:'',    paramsObj:{      sort:0,      orderBy:0    },    type:'bottom',    toViewId:'test1',    topHeight:300,    sort:0,    imgUrl:'',    testShopId:0,//109    totalInfo:{"totalMoney":0,"finalMoney":0,"subtractMoney":0.0,"count":0},    shopId:0,    show:false,    showCar:false,    selIndex: 0,    selCategory:0,    subIndex:0,    thridIndex:-1,    subCategoryId:0,    categoryList: [],    categoryImg: '',    subCategoryList: [],    thridCategoryList:[],    pageTopHeight: wx.getWindowInfo().statusBarHeight,    hotList: [],    topCurrentIndex: 0,    productList:[],    carData:[],//购物车数据  },  /**   * 生命周期函数--监听页面加载   */  onLoad: function (options) {        this.getRecommended()    // this.get_neighborShop()    // this.get_categoryInfo()  },  scroll(){  },  scrollTop(){    console.log('top',);    if(this.data.subIndex==0||this.data.subCategoryList.length==0||this.data.productList.length == 0) return    this.setData({      type:'top'    })    let data = {currentTarget:{      dataset:{        index:this.data.subIndex-1      }    }}    this.onleftMenuTab(data)    // this.setData({    //   topHeight:0    // })  },  scrollBottom(){    // this.data.productList 新增判断  避免为空自动滚动    if(this.data.subIndex==(this.data.subCategoryList.length-1)||this.data.subCategoryList.length==0||this.data.productList.length == 0) return    this.setData({      type:'bottom'    })    let data = {currentTarget:{      dataset:{        index:this.data.subIndex+1      }    }}    this.onleftMenuTab(data)    // this.setData({    //   topHeight:0    // })  },  handleSale(){    this.setData({      sort:1,      paramsObj:{sort:1,orderBy:0}    })    this.getData(this.data.subCategoryList[this.data.subIndex])  },  handlePrice(){    // 没点击价格    if(this.data.sort !=2&&this.data.sort!=3){      this.setData({        sort:2,        paramsObj:{sort:2,orderBy:0}      })    }else{     let sort = this.data.sort ==2?3:2     this.setData({      sort,      paramsObj:{sort:2,orderBy:this.data.sort ==2?0:1}    })    }    this.getData(this.data.subCategoryList[this.data.subIndex])  },  resetData(){    this.setData({      subIndex:0,      thridIndex:-1,      subCategoryId:0,    })  },  // 点击购物车内加减按钮  handleNum(e){    let {num,item} = e.currentTarget.dataset    console.log(num,item);    let data = {      "basketId": item.basketId||0,      "count": num,      "distributionCardNo": item.distributionCardNo||'',      "prodId": item.prodId,      "shopId": item.shopId,      "skuId": item.skuId||item.skuList[0].skuId,      platform:1    }    this.changeCar(data)  },  handleAll(){    this.setData({      show:true    })  },  onClose(){    this.setData({      show:false    })  },  openCar(){    console.log(111111);    this.setData({      showCar:true    })  },  onCarClose(){    this.setData({      showCar:false    })  },  /**   * 获取距离最近店铺id   * @param {*} e    */   get_neighborShop: function () {    var params = {      url: "/shop/neighborShop",      method: "GET",      data: {        lat: wx.getStorageSync('LATITUDE'),        lon: wx.getStorageSync('LONGITUDE'),        platform:1      },      callBack: (res) => {        // this.get_categoryInfo(res)    console.log(99999999,res);        this.setData({          shopId:res.shopId        })          this.get_categoryInfo(res.shopId)          this.getCarData(this.data.shopId)      }    };    http.request(params);  },  get_categoryInfo: function (shopid) {    var ths = this;    let that = this    //加载分类列表    var params = {      // url: "/category/categoryInfo",      url: "/category/listCategoryForUser/"+(this.data.testShopId||shopid),      method: "GET",      callBack: function (res) {        ths.setData({          categoryList: res,          subCategoryList: res[ths.data.selIndex].children        });        let categoryId =wx.getStorageSync('categoryId')        if(categoryId){          that.data.categoryList.map((item,index)=>{            if(item.categoryId == categoryId){                that.setData({                  selCategory:categoryId,                  selIndex:index,                  subCategoryList:res[index].children                })              }          })          wx.removeStorageSync('categoryId')        }        // eventBus.on('categoryId', (data) => {        //   eventBus.clear()        //   that.data.categoryList.map((item,index)=>{        //     if(item.categoryId == data){        //         that.setData({        //           selCategory:data,        //           selIndex:index,        //           subCategoryList:res[index].children        //         })        //       }        //   })        // });        ths.getData(ths.data.subCategoryList[0])         }    };    http.request(params);  },  getData(data){      this.setData({        thridCategoryList:[],        showLoading:true      })      // 二级分类有商品      if(data.productBeBound == 1){        this.getProdList(data.categoryId)      }else{//二级分类有三级分类        this.getAllProList(data.categoryId)        this.setData({          thridCategoryList:data.children,          subCategoryId:data.categoryId        })      }  }, /**   * 选择定位后更新列表   */  go_update() {    this.get_neighborShop()  },  /**   * 生命周期函数--监听页面初次渲染完成   */  onReady: function () {  },  /**   * 生命周期函数--监听页面显示   */  onShow: function () {    console.log('show');    this.resetData()    // if (getApp().globalData.categoryId) {    //   let categoryId = getApp().globalData.categoryId    //   let index = getApp().globalData.index    //   let pic = getApp().globalData.pic    //   this.setData({    //     categoryImg: pic,    //     selIndex: index    //   });    // }else{    // this.get_neighborShop()    // }    this.get_neighborShop()  },  /**   * 生命周期函数--监听页面隐藏   */  onHide: function () {},  /**   * 生命周期函数--监听页面卸载   */  onUnload: function () {  },  /**   * 页面相关事件处理函数--监听用户下拉动作   */  onPullDownRefresh: function () {  },  /**   * 页面上拉触底事件的处理函数   */  onReachBottom: function () {  },  /**   * 用户点击右上角分享   */  onShareAppMessage: function () {  },  /**   * 分类点击事件,获取子分类   */  onMenuTab: function (e) {    var id = e.currentTarget.dataset.id;    var index = e.currentTarget.dataset.index;    this.setData({      categoryImg: this.data.categoryList[index].pic,      subCategoryList:this.data.categoryList[index].children,      selIndex: index,      selCategory:id,      productList:[],      subIndex:0,      paramsObj:{sort:0,orderBy:0},      sort:0    });    // this.getProdList(this.data.subCategoryList[0].categoryId)    this.getData(this.data.subCategoryList[0])    getApp().globalData.categoryId = ''    getApp().globalData.index = ''    getApp().globalData.pic = ''  },  /**   * 推荐词,type:1关键词,2热门搜索词,3推荐搜索词   */  getRecommended: function () {    var params = {      url: "/keyword/list",      method: "GET",      data: {        type: 2      },      callBack: (res) => {        let reslut = res.filter(item => item.status !== 2) //过滤下线词        let timeReslut = reslut.filter(e => util.dateToTimestamp(e.effectiveTime) < new Date().getTime()) //过滤未生效词        this.setData({          hotList: timeReslut        })      }    };    http.request(params);  },  /**   * 推荐词搜索   */  topSwiperChange: function (e) {    this.setData({      topCurrentIndex: e.detail.current // 获取当前滚动到的swiper-item的索引并更新到data中    })  },  topHotSearch: function () {    const topname = this.data.hotList[this.data.topCurrentIndex].name    wx.navigateTo({      url: `/pages/search-prod-show/search-prod-show?prodName=${topname}&shopId=${this.data.shopId}`,    })  },  // 跳转搜索页  toSearchPage: function () {    wx.navigateTo({      url: `/pages/search-page/search-page?shopId=${this.data.shopId}`,    })  },  /**   * 跳转到定位页面   */  toLocationPage: function () {    wx.navigateTo({      url: '/pages/locationAdd/locationAdd',    })  },  // 点击二级分类  onleftMenuTab(e){    const {      index,      id    } = e.currentTarget.dataset    if(id){      this.setData({        type:'click'      })      console.log('click');    }    this.setData({      subIndex:index,      paramsObj:{sort:0,orderBy:0},      sort:0    })    // this.getProdList(this.data.subCategoryList[index].categoryId)    this.getData(this.data.subCategoryList[index])  },  // 根据分类id获取商品数据  getProdList(categoryId) {    var params = {      url: "/search/searchProdPage",      method: "GET",      data: {        categoryId,        current: 1,        size: 999999,        orderBy:this.data.paramsObj.orderBy,        sort:this.data.paramsObj.sort,        isAllProdType: true,        platform:1        // lat: wx.getStorageSync('LATITUDE'),        // lon: wx.getStorageSync('LONGITUDE'),        // distance: wx.getStorageSync('DISTANCE') || 0      },      callBack: (res) => {        setTimeout(() => {          this.setData({            showLoading:false          })        }, 500);      console.log(res);      let productList = res.records      let img = ''      productList.map(e => {        img = e.pic.split(',')        e.pic = img[0]      })        this.setData({          productList,          topHeight:15        })        if(productList.length == 0){          if(this.data.type == 'top'){            this.scrollTop()          }else{            this.scrollBottom()          }        }        this.addParmasProduct()      }    };    http.request(params);  },  // 根据分类id获取所有三级商品数据  getAllProList(categoryId) {    var params = {      url: "/prod/listProdByCategoryIdAndShopId",      method: "post",      data: {        orderBy:this.data.paramsObj.orderBy,        sort:this.data.paramsObj.sort,        categoryId,        current: 1,        size: 999999,        shopId:this.data.testShopId||this.data.shopId      },      callBack: (res) => {        setTimeout(() => {          this.setData({            showLoading:false          })        }, 500);      let productList = res.records      let img = ''      productList.map(e => {        img = e.pic.split(',')        e.pic = img[0]      })        this.setData({          productList,          topHeight:15        })        if(productList.length == 0){          if(this.data.type == 'top'){            this.scrollTop()          }else if(this.data.type == 'bottom'){            this.scrollBottom()          }        }        this.addParmasProduct()      }    };    http.request(params);  },  // 点击三级分类  handleThrid(e){    let {      index,      categoryid    } = e.currentTarget.dataset    console.log(33333333333,e.currentTarget.dataset);    this.setData({      thridIndex:index    })    if(index == -1){      categoryid = this.data.subCategoryId    }    this.getAllProList(categoryid)  },  // 除了产品列表数据 给产品列表添加显示和数量  同步购物车数据  addParmasProduct(){    if(this.data.productList.length >0){      let productList = JSON.parse(JSON.stringify( this.data.productList))      productList.map(i=>{        i.show = false        this.data.carData.map(item=>{          if(item.prodId == i.prodId){            i.prodCount = item.prodCount            i.show = true          }        })      })      this.setData({        productList      })    }  },  // 获取购物车数据  getCarData() {    var params = {      url: "/p/shopCart/info/1",      method: "post",      data: [{        "basketId": 0,        "discountId": 0,        "shopId":this.data.testShopId||this.data.shopId      }],      callBack: (res) => {        if(res.length){          let carData = res[0].shopCartItemDiscounts[0].shopCartItems          let img = ''          carData.map(e => {            img = e.pic.split(',')            e.pic = img[0]          })          this.setData({            carData          },()=>{                       this.getCarTotal()          })                }else{        this.setData({          carData:[]        },()=>{          this.getCarTotal()        })      }      // 给产品列表添加显示和数量  同步购物车数据      this.addParmasProduct()      }    };    http.request(params);  },  // 获取购物车数量和总价  getCarTotal() {    let basketIds = this.data.carData.map(item=>{      return  item.basketId           })    var params = {      url: "/p/shopCart/totalPay?t="+new Date().getTime(),      method: "post",      data: basketIds,      callBack: (res) => {       this.setData({         totalInfo:res       })      }    };    http.request(params);  },  // 添加修改购物车数量  changeCar(data) {    var params = {      url: "/p/shopCart/changeItem?t="+new Date().getTime(),      method: "post",      data,      callBack: (res) => {        this.getCarData()      }    };    http.request(params);  },   /**   * 跳转到商品详情页   */  toProdPage: function (e) {    var prodid = e.currentTarget.dataset.prodid;    console.log(22222222,e);    if (prodid) {      wx.navigateTo({        url: '/pages/prod/prod?prodid=' + prodid,      })    }  },  handleBuy(){    if(this.data.carData.length == 0){      return     }    let basketIds = this.data.carData.map(item=>{      return item.basketId    })    wx.setStorageSync("basketIds", JSON.stringify(basketIds));    wx.navigateTo({      url: '/pages/submit-order/submit-order?orderEntry=0',    })  }})
 |