merchant.js 4.49 KB
// pages/tab/outputValue/components/merchant/merchant.js
import { IMG_URL } from '~/utils/constantData'
import outputValueApi from '~/api/outputValueApi/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    searchForm: {
      type: Object,
      value: () => {
        return {
          // 服务商商户号
          partnerMerchantId: '',
          // 4S店商户号
          merchantId: '',
          // 年月(yyyy-MM)
          yearMonth: '',
          // 场景(1精品升级,3众筹,0会员权益)
          sceneType: ''
        }
      }
    },
    tabBarStyle: {
      type: Number,
      observer: function (n) {
        if (n) this.setData({ emptyDataStyle: `height: calc(100vh - ${n}px - 88rpx - 24rpx)` })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    bgUrl: `${IMG_URL}/personal/empty_tran.png`,
    formData: {},
    serviceName: '',
    totalObj: {},
    pageCof: {
      startYearMonth: '',
      endYearMonth: ''
    },
    isEnd: false,
    loading: true,
    scrollTop: 0,
    isAsk: false,
    // 1精品升级(太阳膜),3众筹,0会员权益)
    merchantList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    merchantRefresh(isReset = false) {
      if (isReset) {
        this.setData({
          ['pageCof.startYearMonth']: '',
          ['pageCof.endYearMonth']: '',
          isEnd: false,
          loading: true,
          scrollTop: 0,
          isAsk: false
        })
      }
      this.getTotal()
      this.getMerchantList(isReset)
    },
    getTotal() {
      let { userInfo } = wx.$storage.get()
      let params = {
        merchantId: userInfo.subLoginUser.isSuper ? '' : userInfo.subLoginUser.merchantId
      }
      outputValueApi.getMerchantTotal(params).then(res => {
        this.setData({
          totalObj: res || {}
        })
      })
    },
    getMore() {
      this.getMerchantList()
    },
    // month作为分页参数
    dealMonth(isReset = false, monthInterval = 1) {
      let { startTime, endTime } = this.data.searchForm
      let { startYearMonth, endYearMonth } = this.data.pageCof
      // 获取当前日期
      let _currentDate = new Date()
      // 获取前n个月的日期
      let _endMonthDate = new Date()
      let searchStart = startTime ? new Date(startTime.replace(/-/g, '/')) : ''
      // 翻页
      if (startYearMonth && endYearMonth) {
        _currentDate = new Date(startYearMonth)
        _endMonthDate = new Date(startYearMonth)
      } else if (startTime && endTime) {
        _currentDate = new Date(endTime.replace(/-/g, '/'))
        _endMonthDate = new Date(endTime.replace(/-/g, '/'))
      }
      _endMonthDate.setMonth(_currentDate.getMonth() - monthInterval)
      let _startMonthDate = new Date(isReset ? _currentDate : _endMonthDate)
      _startMonthDate.setMonth(_startMonthDate.getMonth() - monthInterval)
      endYearMonth = isReset ? _currentDate.format('yyyy-MM') : _endMonthDate.format('yyyy-MM')
      startYearMonth = searchStart > _startMonthDate ? new Date(searchStart).format('yyyy-MM') : _startMonthDate.format('yyyy-MM')
      this.setData({
        ['pageCof.startYearMonth']: startYearMonth,
        ['pageCof.endYearMonth']: endYearMonth,
        isEnd: new Date(new Date(startTime.replace(/-/g, '/'))).format('yyyy-MM') == startYearMonth
      })
    },
    getMerchantList(isReset = false) {
      let { userInfo } = wx.$storage.get()
      let { searchForm, merchantList, isAsk, isEnd } = this.data
      if (isAsk || isEnd) {
        return true
      }
      this.setData({
        serviceName: userInfo.subLoginUser.merchantName,
        loading: true,
        isAsk: true
      })
      this.dealMonth(isReset)
      let params = {
        partnerMerchantId: userInfo.subLoginUser.isSuper ? '' : userInfo.subLoginUser.merchantId,
        merchantId: searchForm.shopMerchantIds.length ? searchForm.shopMerchantIds[0] : '',
        sceneType: searchForm.sceneType,
        ...this.data.pageCof
      }
      wx.$api.showLoading('加载中...')
      const paiName = searchForm.sceneType ? 'getQuerySceneList' : 'getAllSceneList'
      outputValueApi[paiName](params).then(res => {
        this.setData({
          merchantList: isReset ? (res || []) : merchantList.concat(res || [])
        })
        if (isReset) {
          this.setData({
            scrollTop: 0
          })
        }
      }).finally(() => {
        wx.hideLoading()
        this.setData({
          triggered: false,
          loading: false,
          isAsk: false
        })
      })
    }
  }
})