index.js 5.47 KB
const navStyle = "background-color: transparent;";
import { IMG_URL } from '~/config';
import { env } from '~/api/env/index'
import { MERCHANT_STATIC_LIST} from '~/utils/constants'
Page({
  data: {
    IMG_URL,
    navStyle,
    noticeContent: '',
    bannerList: [],
    codeList: [{
      title: '官方',
      desc: '福利群',
      tip: '优惠资讯,先人一步',
      url: 'welfareGroupQRCode',
      banner:'fuliqun_banner'
    }, {
      title: '专属',
      desc: '福利官',
      tip: '超多福利,不容错过',
      url: 'welfareOfficerQRCode',
      banner:'fuliguan_banner'
    }],
    showModal: false,
    urlName: '',
    merchantName: '深圳中海美居',
    // 门店列表
    storeList: []
  },
  navbarReady(e) {
    let { height } = e.detail;
    this.setData({
      navBarHeight: height,
      user_box_style: `padding-top:${height + 10}px`,
    });
  },
  // 处理滚动事件
  onPageScroll(e) {
    const { navBarHeight } = this.data;
    this.setData({
      navStyle: e.scrollTop >= navBarHeight - 50 ? "" : navStyle,
    });
  },
  onLoad(options) {
    this.defaultMerchant = wx.$merchant_id
    const FIRST = wx.p.storage.get('first_enter_miniprograme')
    if (FIRST) {
      this.getMerchantList()
    } else {
      wx.p.eventbus.on('STORE', () => {
        this.getMerchantList()
      })
    }
    this.getStrategyList();
    this.shareTitle = "甄选福利"
  },

  // 获取省钱攻略
  getStrategyList() {
    wx.p.api.strategyApi
      .getStrategyList({
        pageNum: 1,
        pageSize: 100,
        mainFlag: 1,
      })
      .then((res) => {
        this.setData({
          strategyList: res.list,
        });
      });
  },

  selectStrategy(e) {
    const { id } = e.detail;
    wx.p.router.go("/packageStrategy/pages/strategyDetail/index", {
      id,
      notIntercept: true
    });
  },

  toShowQrCode(e) {
    const { item } = e.currentTarget.dataset;
    const { currentStore } = this.data
    this.setData({
      urlName: currentStore[item.url] || '',
      showModal: true,
      bannerName:item.banner
    });
  },
  closeModal() {
    this.setData({
      showModal: false,
      urlName: "",
    });
  },
  toHomeLife() {
    wx.p.router.go("/packageIndex/pages/homeLife/index", { notIntercept: true });
  },
  toCarLife() {
    wx.p.router.go("/packageIndex/pages/carLife/index", { notIntercept: true });
  },

  // 门店切换
  changeStore(e) {
    const index = +e.detail.value
    const { storeList } = this.data
    const currentStore = storeList[index]
    wx.$merchant_id = currentStore.merchantId
    wx.p.storage.set('merchant_info', {
      merchantId: currentStore.merchantId,
    })
    wx.p.eventbus.emit('MALL_FRESH')
    this.setData({
      currentStore
    },()=>{
      this.getMerchantIdDetail(currentStore.merchantId)
    })
  },

  // 获取门店列表
  async getMerchantList() {
    // 获取mock配置门店
    const MOCKDATA = wx.p.storage.get("mock_data");
    let MerchantList = MERCHANT_STATIC_LIST[env]
    if (MOCKDATA && MOCKDATA.add_wuye_merchant) {
      const ADD_MERCHANT_LIST = MOCKDATA.add_wuye_merchant[env]
      if (ADD_MERCHANT_LIST && ADD_MERCHANT_LIST.length) {
        MerchantList = [...MerchantList, ...ADD_MERCHANT_LIST]
      }
    }
    const HAVE_ENTER = wx.p.storage.get('first_enter_miniprograme')
    // 获取扫码链接带入的门店
    if (HAVE_ENTER && !this.fresh) {
      const merchant = wx.p.storage.get('first_enter_merchantId')
      const ISNEWMERCHANT = MerchantList.includes(JSON.parse(merchant))
      this.defaultMerchant = merchant
      if(!ISNEWMERCHANT){
        MerchantList.push(merchant)
      }
    }

    let storeList = [], index = 0
    storeList = await this.getMerchantListData(MerchantList) || []
    storeList.forEach((ele, i) => {
      ele.merchantId = ele.merchantNo
      if (this.defaultMerchant == ele.merchantId) {
        index = i
      }
    })
    const currentStore = storeList[index]
    wx.$merchant_id = currentStore.merchantId
    wx.p.storage.set('merchant_info', {
      merchantId: currentStore.merchantId,
    })
    this.setData({
      storeList,
      currentStore
    }, () => {
      this.getMerchantIdDetail(currentStore.merchantId)
    })
  },
  onPullDownRefresh() {
    this.fresh = true
    this.defaultMerchant = this.getStorageMerchantId()
    this.getMerchantList()
    this.getStrategyList();
  },
  
  onShow() {
    const FIRST = wx.p.storage.get('first_enter_miniprograme')
    this.setData({
      showStore: !FIRST
    })
  },
  // 获取门店id
  getStorageMerchantId() {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    return merchantId
  },
  // 获取门店列表
  async getMerchantListData(merchantIdList) {
    const res = await wx.p.api.shopmallApi.getStoreList({ merchantIdList })
    if(res.code==0) return res.data 
    else return false
  },
  // 获取选择门店的信息
  getMerchantIdDetail(merchantId) {
    wx.$merchant_id = merchantId
    wx.p.api.homeApi.getHomeConfig({ merchantId }).then(data => {
      let {
        noticeContent,welfareGroupQRCode,welfareOfficerQRCode
      } = data
      let bannerList = [], { currentStore } = this.data
      if (data.bannerList && data.bannerList.length) bannerList = data.bannerList
      currentStore.noticeContent = noticeContent
      currentStore.bannerList = bannerList
      currentStore.welfareGroupQRCode = welfareGroupQRCode
      currentStore.welfareOfficerQRCode = welfareOfficerQRCode
      this.setData({
        currentStore
      })
    })
  },
  onShareAppMessage(){}
});