index.js 8.02 KB
import {
  env
} from '~/api/env/index'
import mapApi from '~/utils/mapapi/mapapi'
const app = getApp()
Page({

  data: {
    foodMenuList: [],
    funMenuList: [],
    carMenuList: [],
    commodityList: [],
    pageParam: {
      appearance: 1,
      pageSize: 0,
      pageCount: 10
    },
    loadingMore: false,
    loadOver: false,
    commodityFinish: false
  },

  onLoad() {
    this.getAllMerchantList()
    let freecard = wx.p.storage.get('bindInfo')
    if (freecard) {
      let bingInfo = JSON.parse(decodeURIComponent(freecard))
      const sid = bingInfo.data.sessionId
      wx.p.cache.storageApi.setSessionId(sid)
      this.handleFreeCard(bingInfo)
    }
    this.shareTitle = "省钱专区"
  },

  navbarReady(e) {
    let {
      height
    } = e.detail
    this.setData({
      user_box_style: `padding-top:${height + 15}px`
    })
  },

  onShow() {
    let authComponent = this.selectComponent('#authorize')
    if (authComponent) authComponent.check()
    const FIRST = wx.p.storage.get('first_enter_miniprograme')
    this.setData({
      showStore:!FIRST
    })
  },

  goMorePage() {
    wx.p.router.go('/packageSaveMoney/pages/allEquity/allEquity')
  },

  /**授权成功 */
  authSuccess() {
    this.getLocation()
  },

  /**授权失败 */
  authFail() {
    wx.p.storage.set('unauthorized', true)
    this.setData({
      unauthorized: true
    })
  },

  // 定位
  async getLocation() {
    try {
      const res = await this.getAddressInfo()
      this.toGetCommodity(res.location.lat, res.location.lng, 1)
      this.setData({
        currentAddress: res,
        unauthorized: false
      })
      wx.p.storage.set('unauthorized', false)
    } catch (error) {
      console.log(error)
    }
  },

  // 获取附近优惠数据
  toGetCommodity(lat, lng, firstLoad) {
    let para = {
      lat,
      lng,
      ...app.globalData.goodsPageParam
    }
    if (app.globalData.goodsLoaded || (firstLoad && app.globalData.goodsPageParam?.length)) {
      this.setData({
        commodityList: app.globalData.goodsCommodityList,
      })
      return
    }
    para.pageOffset = para.pageSize
    wx.p.api.equityApi.getLasteCommodity(para).then(res => {
      if (!res?.data?.length) {
        app.globalData.goodsLoaded = true
        this.setData({
          commodityFinish: true
        })
        return
      }
      app.globalData.goodsPageParam.pageSize = res.pageOffset
      let tempArr = []
      res.data.forEach(item => {
        tempArr.push(item)
      })
      app.globalData.goodsCommodityList = app.globalData.goodsCommodityList.concat(tempArr)
      app.globalData.goodsLoaded = false
      this.setData({
        commodityList: app.globalData.goodsCommodityList,
      })
    })
  },

  // 重新收起获取定位
  relocation() {
    this.setData({
      showAuth: true
    })
  },
  onPullDownRefresh() {
    this.getShopPackage()
    this.toCheckMember()
    app.globalData.goodsCommodityList = []
    app.globalData.goodsPageParam.pageSize = 0
    app.globalData.goodsLoaded = false
    let {
      currentAddress
    } = this.data
    if (!currentAddress) return
    const {
      lat,
      lng
    } = currentAddress.location
    this.toGetCommodity(lat, lng, false, 1)
  },
  onReachBottom() {
    if (this.data.unauthorized) {
      return
    }
    if (!this.data.commodityList.length) {
      return
    }
    let {
      currentAddress
    } = this.data
    if (!currentAddress) return
    const {
      lat,
      lng
    } = currentAddress.location
    this.toGetCommodity(lat, lng)
  },
  getAllMerchantList() {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let para = {
      merchantId
    }
    wx.p.api.equityApi.getAllEquity(para).then(res => {
      const carList = res.carServiceList
      const equityList = res.goodsServiceList
      let foodMenuList = [],
        funMenuList = [],
        carMenuList = []
      const needShowEquity = ['星巴克', '瑞幸咖啡', '奈雪の茶', '麦当劳', '肯德基']
      const needShowEquity2 = ['猫眼电影', '爱奇艺', '优酷', '网易云音乐', '喜马拉雅']
      const needShowEquity3 = ['高德打车', '曹操出行']
      if (env == 'production' || env == 'develop') {
        let all = []
        equityList.forEach(el => {
          if (el.equityGoodsGroupDescDtos) {
            el.equityGoodsGroupDescDtos.forEach(item => {
              all.push(item)
            })
          }
        })
        all.forEach(item => {
          if (needShowEquity.includes(item.equityName)) {
            if (item.equityName == '瑞幸咖啡') {
              item.sign = '9.9元起'
            } else if (item.equityName == '麦当劳') {
              item.sign = '新品'
            }
            foodMenuList.push({
              name: item.equityName,
              icon: item.equityIcon,
              ...item
            })
          }
          if (needShowEquity2.includes(item.equityName)) {
            if (item.equityName == '猫眼电影') {
              item.sign = '19元起'
            }
            funMenuList.push({
              name: item.equityName,
              icon: item.equityIcon,
              ...item
            })
          }
          if (needShowEquity3.includes(item.equityName)) {
            if (item.equityName == '高德打车') {
              item.sign = '10元券'
            }
            carMenuList.push({
              name: item.equityName,
              icon: item.equityIcon,
              ...item
            })
          }
        })
      } else if (env == 'test' || env == 'beta') {
        let all = []
        equityList.forEach(el => {
          if (el.equityGoodsGroupDescDtos) {
            el.equityGoodsGroupDescDtos.forEach(item => {
              all.push(item)
            })
          }
        })
        all.forEach(item => {
          foodMenuList.push({
            name: item.equityName,
            icon: item.equityIcon,
            ...item
          })
          funMenuList.push({
            name: item.equityName,
            icon: item.equityIcon,
            ...item
          })
          carMenuList.push({
            name: item.equityName,
            icon: item.equityIcon,
            ...item
          })

        })

      }
      this.setData({
        foodMenuList,
        funMenuList,
        carMenuList
      })
    })
  },
  toPluginPage() {
    wx.p.router.go('/packagePlugin/pages/index/index', {
      notIntercept: true
    })
  },
  async getAddressInfo() {
    let addressInfo = wx.p.storage.get('address_info')
    return new Promise(resolve => {
      if (addressInfo && Date.now() - addressInfo.createTime < 2 * 60 * 1e3) resolve(addressInfo)
      else
        wx.getLocation({
          type: 'gcj02'
        }).then(({
          latitude,
          longitude
        }) => {
          mapApi.detailedAdrress([latitude, longitude]).then(detail => {
            let info = {
              ...detail,
              createTime: Date.now()
            }
            resolve(info)
            wx.p.storage.set('address_info', info)
          })
        })
    })
  },
  // 处理免费领取权益页面带来的参数---弹框显示
  handleFreeCard(bindInfo) {
    wx.p.storage.rm('bindInfo')
    const {
      code,
      errId,
      message
    } = bindInfo
    if (code == 0) {
      wx.showModal({
        title: '提示',
        content: '领取成功',
        confirmText: '确定',
        showCancel: false,
        confirmColor: '#F13C3D'
      })
    } else if (code == -1 && errId == 20321) {
      wx.showModal({
        title: '提示',
        content: message,
        confirmText: '续费开通',
        cancelText: '不用了',
        confirmColor: '#F13C3D'
      }).then(res => {
        if (res.confirm) {
          this.toBuyMember()
        }
      })
    } else if (errId == 20320 || errId == 20322) {
      wx.showModal({
        title: '提示',
        content: message,
        confirmText: '确定',
        showCancel: false,
        confirmColor: '#F13C3D'
      })
    }
  },
  toBuyMember() {
    wx.p.router.go('/packageSaveMoney/pages/open/open', {
      notIntercept: true
    })
  },
  onShareAppMessage(){}
})