123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- 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 = null
- 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: [],
- shop_status: 0
- },
- ...pages.methods,
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad(options) {
- app.fetchUserDataCallback = () => {
- this.setShopStatus()
- this.setTabBarList()
- }
- this.fetchLunboList()
- await this.fetchProductCategoryList()
- if (this.data.searchForm.category_id) {
- this.fetchOrderList()
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- this.setShopStatus()
- this.setTabBarList()
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- 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: 'https://lanman-shining.oss-cn-hangzhou.aliyuncs.com/images/user/bashi635cd28a12481.png',
- 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
- query = wx.createSelectorQuery().in(this)
- 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.setStorageSync(app.globalData.messageDetailStorageKey, JSON.stringify(item))
- wx.navigateTo({
- url: '/pages/messageDetail/messageDetail'
- })
- },
- 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)
- }
- })
- }
- }
- })
- },
- setShopStatus() {
- const { shop_status } = app.globalData.userInfo
- this.setData({
- shop_status: shop_status === 1 ? shop_status : 0
- })
- },
- setTabBarList() {
- 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 })
- }
- }
- })
|