// pages/mall/shops/index.js const request = require('../../../utils/request'); //导入模块 const util = require('../../../utils/util'); const event = require('../../../utils/event.js') const app = getApp() Page({ /** * 页面的初始数据 */ data: { firstShow: false, height: app.globalData.fragmentHeight - 180, CustomBar: app.globalData.CustomBar, scroll: true, top: 0, pageIndex: 0, pageList: [], tabs: [], loading: true, finished: false, requesting: false, cheapList: [], taskList: [], form: { productTypeId: '', pageSize: 10, pageNum: 1 }, token: null }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.checkLogin() this.setData({ height: app.globalData.fragmentHeight - 200, CustomBar: app.globalData.CustomBar, token: app.globalData.token }) event.on('EventMessage', this, this.onEvent) }, // 事件通知 onEvent: function (message) { console.log('shops>>index>>onEvent', message) if(message.what == 1 && util.isEmpty(this.data.token)){ this.setData({ token: app.globalData.token }) } this.fetchTabList() this.fetchCheapList() // this.fetchTaskList() }, onReady: function(){ wx.getSystemInfo({ success: e => { app.globalData.StatusBar = e.statusBarHeight; let custom = wx.getMenuButtonBoundingClientRect(); app.globalData.Custom = custom; app.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight; let windowHeight = e.windowHeight * (750 / e.windowWidth); let statusBarHeight = app.globalData.CustomBar * (750 / e.windowWidth); app.globalData.fragmentHeight = windowHeight - statusBarHeight this.setData({ height: app.globalData.fragmentHeight - 200, CustomBar: app.globalData.CustomBar }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onShow: function(){ if(this.data.firstShow){ this.onRefreshList() } if (!this.data.firstShow) { // this.data.firstShow = true } }, // 获取特价列表 fetchTabList: function () { // /product/type/list产品分类列表 request.get('/bxe-mall/product/type/list').then(result => { //成功回调 var tabList = ['全部'] var size = 0 for (let index = 0; index < result.data.length; index++) { tabList.push(result.data[index].name) size += result.data[index].name.length } this.data.pageList = result.data this.setData({ tabs: tabList, scroll: size > 14 }) }) }, // Tab 点击切换,并刷新列表; onTabChange: function (e) { if(this.data.pageIndex == Number(e.detail.index)){ return } this.data.pageIndex = Number(e.detail.index) if(this.data.pageIndex == 0){ this.data.form.productTypeId = '' } else { this.data.form.productTypeId = this.data.pageList[this.data.pageIndex - 1].id } this.onRefreshList() }, // 下拉刷新... onRefreshList: function(){ this.setData({ cheapList: [], taskList: [], ['form.pageNum']: 1, loading: true, finished: false }) this.fetchCheapList() // this.fetchTaskList() }, // 获取特价列表 fetchCheapList: function () { if (this.data.requesting || this.data.finished) { return } if(this.data.loading){ this.data.requesting = true } else { this.setData({ requesting: true }) } request.get('/bxe-mall/product/indexList', this.data.form).then(result => { //成功回调 if(this.data.form.pageNum == 1){ this.setData({ requesting: false, loading: false, finished: true, top: 0, cheapList: result.data }) } else { this.setData({ requesting: false, loading: false, finished: true, cheapList: result.data }) } }).catch(err => { //异常回调 this.setData({ requesting: false, loading: false, finished: true }) }) }, // 获取团购列表 fetchTaskList: function () { if (this.data.requesting || this.data.finished) { return } if(this.data.loading){ this.data.requesting = true } else { this.setData({ requesting: true }) } // /product/cheapList特价产品列表 request.get('/bxe-mall/product/groupList', this.data.form).then(result => { //成功回调 this.setData({ requesting: false, loading: false }) if (result.data.records.length) { var respList = result.data.records let nowList = `taskList[${this.data.taskList.length}]` var num = this.data.form.pageNum var finished = this.data.form.pageNum >= result.data.pages this.setData({ [nowList]: respList, total: result.data.total, ['form.pageNum']: (num + 1), finished }) } else { this.setData({ finished: true }) } }).catch(err => { //异常回调 this.setData({ requesting: false, loading: false }) util.showToast(err) }) }, // 进入搜索页 searchList: function (e) { wx.navigateTo({ url: '/pages/mall/search-list/index' }) }, // 查看团购Item lookGroupItem: function (e) { var item = this.data.taskList[e.currentTarget.dataset.page][e.currentTarget.dataset.index] wx.navigateTo({ url: '/pages/mall/order-info/index?id=' + item.id }) }, // 查看特价Item lookCheapItem: function (e) { var item = this.data.cheapList[e.currentTarget.dataset.index] wx.navigateTo({ url: '/pages/mall/order-info/index?id=' + item.id }) }, // 进行登录,同时获取到openId,为下一步的微信登录做准备, checkLogin: function () { wx.login({ success: res => { wx.request({ url: app.httpUrl + '/user-centre/wx/bxe/session', data: { code: res.code }, method: 'POST', success: function (result) { if (result.data.code != 0) { event.emit('EventMessage', { what: 1, desc: 'Login' }) return } app.globalData.openId = result.data.data.openid // /commonLogin/login通用登录 wx.request({ url: app.httpUrl + '/user-centre/commonLogin/login', data: { openid: app.globalData.openId }, method: 'POST', success: function (result) { if (result.data.code == 0 && result.data.data.userToken) { app.globalData.token = result.data.data.userToken } }, complete: function () { event.emit('EventMessage', { what: 1, desc: 'Login' }) } }) }, fail: function () { event.emit('EventMessage', { what: 1, desc: 'Login' }) } }) }, fail: function () { event.emit('EventMessage', { what: 1, desc: 'Login' }) } }) }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { event.remove('EventMessage', this) } })