index.js 8.81 KB
import mapApi from '../../../utils/mapapi/mapapi'
Component({
  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',
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    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()
      this.setData(
        {
          'pageParam.pageSize': 0,
          loadingMore: false
        },
        () => {
          let { currentAddress } = this.data
          const { lat, lng } = currentAddress.location
          this.toGetCommodity(lat, lng)
        }
      )
    },
    onReachBottom() {
      if (this.data.unauthorized) {
        return
      }
      if (!this.data.commodityList.length) {
        return
      }
      let { pageParam } = this.data
      pageParam.pageSize++

      this.setData(
        {
          pageParam,
          loadingMore: true
        },
        () => {
          let { currentAddress } = this.data
          const { lat, lng } = currentAddress.location
          this.toGetCommodity(lat, lng)
        }
      )
    },
    navbarReady(e) {
      let { height } = e.detail
      this.setData({
        navHeight: height
      })
    },
    // 新版
    openMorePack(e) {
      const { show } = e.currentTarget.dataset
      this.setData({
        showMorePack: !show
      })
    },
    // 页面跳转
    // toAllEquityPage() {
    //   wx.navigateTo({
    //     url: '/packageEquity/pages/allEquity/allEquity'
    //   })
    // },
    toBuyMember() {
      wx.navigateTo({
        url: '/packageEquity/pages/open/open'
      })
    },
    toNewPage(e) {
      const { index, item } = e.currentTarget.dataset
      wx.navigateTo({
        url: item.url
      })
    },

    // 定位
    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, page) {
      let { pageParam, loadingMore, commodityList, loadOver } = this.data
      let para = {
        lat,
        lng,
        ...pageParam
      }
      if (loadOver) {
        return
      }
      if (page) {
        para.pageSize = 0
      }
      wx.p.api.equityApi.lasteCommodity(para).then(res => {
        if (!res) {
          this.setData({
            loadOver: true
          })
          return
        }
        let tempArr = []
        if (res) {
          res.forEach(item => {
            if (item.couponsGoodsList) {
              tempArr.push(item)
            }
          })
        }
        if (loadingMore) {
          commodityList = commodityList.concat(tempArr)
        }
        this.setData({
          commodityList: loadingMore ? commodityList : tempArr,
          loadingMore: false,
          loadOver: false
        })
      })
    },

    // 重新收起获取定位
    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.navigateTo({
        url: '/packageEquity/pages/package/package?id=' + id
      })
    },



    // 获取用户会员信息
    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.$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)
            })
          })
      })
    }
  }
})