index.js 8.35 KB
const app = getApp()
import mapApi from '../../../utils/mapapi/mapapi'
Page({
  options: {
    styleIsolation: 'shared'
  },

  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    IMGURL: 'https://img02.glsx.com.cn/weapp/resource/miniprogram/hmd/',
    IMGURKBASE: 'equity/version/1_0_0/',
    userInfo: {},
    isOpen: true,
    menuList: [{
        title: '惠享生活',
        desc: '5折起',
        icon: 'share_icon',
        url: '/packageEquity/pages/commodity/list/list'
      },
      {
        title: '充值优惠',
        desc: '5折起',
        icon: 'charge_vip',
        url: '/packageEquity/pages/commodity/charge/charge'
      },
      {
        title: '全部服务',
        desc: '更多',
        icon: 'all_icon',
        url: '/packageEquity/pages/allEquity/allEquity'
      }
    ],
    storePackageList: [],
    showMorePack: false,
    tabList: ['附近优惠'],
    curTabIndex: 0,
    pageParam: {
      appearance: 1,
      pageSize: 0,
      pageCount: 10
    },
    commodityList: [],
    loadingMore: false,
    loadOver: false,
    // 是否登录
    showLogin: false,
    showPage: true,
    // 会员类型-背景
    vipStatus: {
      1: 'gold_vip_bg',
      2: 'silver_vip_bg',
      3: 'pt_vip_bg'
    },
    // 普通会员3 银卡2 金卡1 非会员 0
    identity: 0,
    titleClass: {
      0: '',
      1: 'gold_title',
      2: 'sliver_title',
      3: 'card_title',
    },
    dateClass: {
      0: '',
      1: 'gold_date',
      2: 'sliver_date',
      3: 'card_date',
    }
  },

  /**
   * 组件的方法列表
   */
  onLoad() {
    this.getShopPackage()
    this.getMemberCardData()
    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.setData({
      isOpen: wx.p.storage.get('sessionId') ? true : false
    })
  },
  onShow() {
    let authComponent = this.selectComponent('#authorize')
    if (authComponent) authComponent.check()
    // if (this.data.userInfo && !this.data.userInfo.phone) return
    this.toCheckMember()
  },
  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)
  },
  // 新版
  openMorePack(e) {
    const {
      show
    } = e.currentTarget.dataset
    this.setData({
      showMorePack: !show
    })
  },
  // 页面跳转
  // toAllEquityPage() {
  //  wx.p.router.go('/packageEquity/pages/allEquity/allEquity')
  // },
  toBuyMember() {
    wx.p.router.go('/packageEquity/pages/open/open', {
      notIntercept: true
    })
  },
  toNewPage(e) {
    const {
      index,
      item
    } = e.currentTarget.dataset
    wx.p.router.go(item.url, {
      notIntercept: 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
            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
    })
  },
  /**授权失败 */
  authFail() {
    wx.p.storage.set('unauthorized', true)
    this.setData({
      unauthorized: true
    })
  },
  /**授权成功 */
  authSuccess() {
    this.getLocation()
  },
  // 获取门店套餐
  getShopPackage() {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let para = {
      merchantId
    }
    wx.p.api.equityApi.storePackage(para).then(res => {
      this.setData({
        storePackageList: res || []
      })
    })
  },

  // 去套餐详情页
  toPackageDetail(e) {
    const {
      id
    } = e.currentTarget.dataset
    wx.p.router.go('/packageEquity/pages/package/package', {
      id,
      notIntercept: true
    })
  },



  // 获取用户会员信息
  toCheckMember() {
    let sid = wx.p.storage.get('sessionId')
    if (!sid) {
      this.setData({
        isOpen: false
      })
      return
    }
    wx.p.api.equityApi.checkOpenMember().then(res => {
      let flag = false,
        identity = 0 // 普通会员3 银卡2 金卡1 非会员 0
      if (res.data && res.data.validitDate) {
        res.data.time = new Date(res.data.validitDate).format('yyyy.MM.dd')
      }
      if (res.data && res.data.endTime) {
        res.data.endTime = new Date(res.data.endTime).format('yyyy.MM.dd')
      }
      // 普通会员
      if (res.code == 0 && res.data) {
        flag = true
        identity = 3
        if (res.data && res.data.cardTypeSpecial) {
          identity = res.data.cardTypeSpecial
        }
      }

      wx.p.storage.set('isVIP', flag)
      this.setData({
        isOpen: flag,
        vipInfo: res.data,
        identity
      })
    })
  },

  // 获取门店会员卡信息
  getMemberCardData() {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let para = {
      merchantId
    }
    wx.p.api.equityApi.memberCard(para).then(res => {
      this.setData({
        memberInfo: res
      })
    })
  },

  // 处理免费领取权益页面带来的参数---弹框显示
  handleFreeCard(bindInfo) {
    wx.p.storage.rm('bindInfo')
    const {
      code,
      errId,
      message
    } = bindInfo
    if (code == 0) {
      wx.showModal({
        title: '提示',
        content: '领取成功',
        confirmText: '确定',
        showCancel: false,
        confirmColor: '#F13C3D'
      })
      this.toCheckMember()
    } 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'
      })
    }
  },


  // 检测是否已经登录
  checkLogin() {
    return wx.p.authStatus
  },
  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)
          })
        })
    })
  }
})