const pages = require('../../mixin/pages') const { getProductCategoryList } = require('../../api/common') const { getLunboList } = require('./api/index') const app = getApp() const tmplIds = 'uc9ztDw9ZscpNkMr52XUcB7YrDBk5AzjrRtNSu8Dq-4' let leftHeight = 0 let rightHeight = 0 let query = wx.createSelectorQuery() Page({ /** * 页面的初始数据 */ data: { ...pages.data(), listUrl: '/api/user/home/product/list', searchForm: { key_words: '', category_id: '' }, lunbos: [], lunboMsg: [], headerNav: [ { icon: 'partner@2x.png', name: '供应商', path: 'business' }, { icon: 'guide@2x.png', name: '全部产品', path: 'goods' }, { icon: 'collection@2x.png', name: '收藏', path: 'collection' }, { icon: 'news.png', name: '选购指南', path: 'guide' } ], categoryList: [], leftList: [], rightList: [] }, ...pages.methods, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { this.fetchLunboList() await this.fetchProductCategoryList() if (this.data.searchForm.category_id) { this.fetchOrderList() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { const { shop_status } = app.globalData.userInfo this.getTabBar().init() if (shop_status === 1 && this.getTabBar().data.list.findIndex(item => item.pagePath === 'pages/news/news') === -1) { this.getTabBar().setData({ list: app.globalData.tabBarList }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { if (this.data.freshing) { return } this.setData({ freshing: true }) this.bindCallBack() }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.fetchOrderList() }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, bindCallBack() { this.refreshOrderList() this.fetchLunboList() }, async fetchLunboList() { try { const { status, data, msg } = await getLunboList() if (status) { const { lunbos, lunbo_msg } = data this.setData({ lunbos: lunbos, lunboMsg: lunbo_msg }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, async fetchProductCategoryList() { try { const { status, data, msg } = await getProductCategoryList() if (status && Array.isArray(data) && data.length > 0) { const temp = data.map(item => { return { ...item, text: item.category_name } }) temp.unshift({ category_img_url: '', category_name: '全部', id: 0 }) this.setData({ categoryList: temp, 'searchForm.category_id': temp.length > 0 ? temp[0].id : '' }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, handleTab(event) { const { name } = event.detail this.setData({ 'searchForm.category_id': name }, () => { this.refreshOrderList() }) }, async isLeft(list) { const { leftList, rightList } = this.data for (const item of list) { if (leftHeight < rightHeight) { leftList.push(item) } else if (leftHeight === rightHeight) { leftList.length <= rightList ? leftList.push(item) : rightList.push(item) } else { rightList.push(item) } await this.getBoxHeight(leftList, rightList) } }, getBoxHeight(leftList, rightList) { return new Promise((resolve, reject) => { this.setData({ leftList, rightList }, () => { query.select('.waterfall-left').boundingClientRect() query.select('.waterfall-right').boundingClientRect() query.exec((res) => { leftHeight = res[0].height rightHeight = res[1].height resolve() }) }) }) }, jumpLeavingAMessage() { wx.navigateTo({ url: '/pages/leavingAMessage/leavingAMessage' }) }, handleSwiperClick(e) { const isTab = app.globalData.tabBarList.map(item => item.pagePath) const { item } = e.currentTarget.dataset if (Object.prototype.toString.call(item) === '[object Object]') { const { lunbo_link_url } = item if (/^http/.test(lunbo_link_url)) { wx.navigateTo({ url: '/pages/h5/h5?url=' + lunbo_link_url }) } else if (/^\/pages/.test(lunbo_link_url)) { if (isTab.findIndex(item => lunbo_link_url.indexOf(item) > -1) > -1) { wx.switchTab({ url: lunbo_link_url }) } else { wx.navigateTo({ url: lunbo_link_url }) } } } }, jumpMessageDetail(e) { const { item } = e.currentTarget.dataset wx.navigateTo({ url: '/pages/messageDetail/messageDetail?item=' + JSON.stringify(item) }) }, handleNav(item) { if (Object.prototype.toString.call(item) === '[object Object]' && item.path) { const path = item.path if (!path) { return } if (path === 'news' || path === 'partner') { wx.switchTab({ url: `/pages/${path}/${path}` }) return } wx.navigateTo({ url: `/pages/${path}/${path}` }) } }, openMarketing(e) { const { page } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/${page}/${page}` }) }, async handleSubscribeMessage(e) { const { item } = e.currentTarget.dataset const that = this try { const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true }) if (errMsg === 'getSetting:ok') { // 用户打开了订阅消息总开关 if (subscriptionsSetting.mainSwitch) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权 if (subscriptionsSetting.itemSettings != null) { const moIdState = subscriptionsSetting.itemSettings[tmplIds] if (moIdState === 'accept') { // 接受了消息推送 that.handleNav(item) } else if (moIdState === 'reject') { // 拒绝消息推送 that.handleNav(item) } else if (moIdState === 'ban') { // 已被后台封禁 that.handleNav(item) } else { that.handleSubscribeMessageBridge(tmplIds, item) } } else { that.handleSubscribeMessageBridge(tmplIds, item) } } else { // 订阅消息未开启 that.handleSubscribeMessageBridge(tmplIds, item) } } } catch (err) {} }, handleSubscribeMessageBridge(tmplIds, item) { const that = this // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗 wx.showModal({ title: '提示', content: '请授权开通服务通知', showCancel: true, success: function (res) { if (res.confirm) { wx.requestSubscribeMessage({ tmplIds: [tmplIds], success(res) { that.handleNav(item) }, fail(err) { that.handleNav(item) } }) } } }) } })