ScreenConfirmBox.js 7.53 KB
// pages/tab/output/components/ScreenConfirmBox/ScreenConfirmBox.js
import USERAPI from '~/api/usersApi/index'
import outputApi from '~/api/outputApi/index'
import { setUserInfo } from '~/utils/cache/storageApi'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabBarStyle: String,
    formData: Object,
    show: {
      type: Boolean,
      observer: function (n, o) {
        if (n) {
          const thisPage = wx.$router.getPageInstance()
          thisPage.setData({
            isShowFilterScreen: true
          })
          this.setViewData()
          this.updateUserInfo()
        }
      }
    },
    tabType: Number // tab值 0我的门店 1我的员工
  },

  /**
   * 组件的初始数据
   */
  data: {
    userInfo: {},
    isSuper: false,
    servicePopupShow: false,
    merchantRange: [],
    sceneList: [
      {
        id: '',
        label: '全部',
        active: true
      },
      {
        id: '1',
        label: '太阳膜'
      },
      {
        id: '3',
        label: '众筹活动'
      },
      {
        id: '0',
        label: '会员权益/升级'
      }
    ],
    staffList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateUserInfo() {
      let { userInfo } = wx.$storage.get()
      this.setData({
        userInfo,
        isSuper: userInfo.isSuper
      })
    },
    setViewData() {
      let { tabBarStyle } = this.data
      wx.$api.getViewInfo(
        '.btn-box',
        res => {
          this.setData({
            height: tabBarStyle,
            btnHeight: res[0]?.height
          })
        },
        this
      )
    },
    getMerchantRange() {
      let { isSuper, userInfo } = this.data
      // 判断是超管才需要切换账号
      if (isSuper) {
        USERAPI.adminSelectLogin()
          .then(result => {
            let { merchantList: merchantRange } = result
            merchantRange.forEach((item, index) => {
              item.show = true
            })
            this.setData({
              merchantRange
            })
          })
          .catch(err => { })
      } else {
        let merchantRange = [{ merchantId: userInfo.subLoginUser.merchantId, merchantName: userInfo.subLoginUser.merchantName }]
        this.setData({
          merchantRange
        })
      }
    },
    changeMerchantRange(e) {
      let { merchantRange, subIndex, formData } = this.data
      let { merchantId } = e.detail
      merchantRange.forEach((item, index) => {
        item.show = true
        if (item.merchantId == merchantId) {
          subIndex = index
          item.act = true
        }
      })
      formData.partnerMerchantIds = [merchantId]
      this.setData({
        merchantRange,
        subIndex,
        formData
      })
      if (this.data.tabType == 1 && merchantId !== userInfo?.subLoginUser?.merchantId) this.getStaffList()
      this.servicePicker()
    },
    // 服务商切换
    servicePicker() {
      this.setData({
        servicePopupShow: !this.data.servicePopupShow
      })
    },
    // 月份确定
    dateRangeSelect(e) {
      let { start, end } = e.detail.date
      let { formData } = this.data
      let [year, month] = end.split('-')
      let day = this.getLastDay(year, month)
      formData.startTime = start + '-01' + ' 00:00:00'
      formData.endTime = end + '-' + day + ' 23:59:59'
      this.setData({
        formData
      })
    },
    getLastDay(year, month) {
      var new_year = year //取当前的年份
      var new_month = month++ //取下一个月的第一天,方便计算(最后一天不固定)
      if (month > 12) {
        new_month -= 12 //月份减
        new_year++ //年份增
      }
      var new_date = new Date(new_year, new_month, 1) //取当年当月中的第一天
      return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).getDate() //获取当月最后一天日期
    },
    // 员工数据
    getStaffList() {
      let { partnerMerchantIds, sceneType } = this.data.formData
      outputApi.getStaffList({partnerMerchantId: partnerMerchantIds[0]}).then(res => {
        let _list = []
        // 1-会员权益/升级;2-精品升级
        let attrToNum = {
          '': 'totalStat',
          '1': 'vdStat',
          '2': 'boutiqueStat',
          '3': 'carTransformStat'
        }
        _list = res[attrToNum[sceneType || '']] || []
        let arr = []
        _list.forEach(val => {
          val.list.length && val.list.forEach(subVal => {
            arr.push(subVal)
          })
        })
        if (!arr.length || arr[0]?.userName !== '全部') {
          arr.unshift({
            active: true,
            userId: '',
            userName: '全部'
          })
        }
        this.setData({
          staffList: arr
        })
      })
    },
    init() {
      this.getMerchantRange()
      if (this.data.tabType == 1) this.getStaffList() // 我的员工有员工筛选条件
    },
    choose(e) {
      let { index, listName } = e.currentTarget.dataset
      let { formData } = this.data
      let target = this.data[listName]
      switch (listName) {
        case 'staffList':
          if (index == 0) { // 选择全部
            target.map(i => (i.active = false))
            target[index].active = true
          } else {
            target[index].active = true
            target[0].active = false
          }
          break
        case 'sceneList':
          target.map(i => (i.active = false))
          target[index].active = true
          formData.sceneType = target[index]?.id
          this.setData({
            formData
          })
        break
      }
      this.setData({
        [`${listName}`]: target,
        isReset: false
      })
    },
    confirm() {
      wx.$api.showLoading('加载中')
      let { userInfo, formData, staffList, sceneList } = this.data
      formData.sceneType = sceneList.filter(val => val.active)[0]?.id || ''
      formData.staffUUIDs = staffList.filter(val => val.active).map(subVal => {return subVal.userId})
      const thisPage = wx.$router.getPageInstance()
      if (formData?.partnerMerchantIds[0] !== userInfo.subLoginUser.merchantId) {
        USERAPI.switchSubLoginInfo({ merchantId: formData?.partnerMerchantIds[0] }).then(res => {
          setUserInfo(res)
          wx.$api.hideLoading()
          this.triggerEvent('reloadMerchantList')
          // 切换服务商 清空已选的门店
          formData.shopMerchantIds = []
          formData.shopMerchantNames = '全部门店'
          thisPage.setData({
            screenFormData: formData
          })
          thisPage.searchEvent()
        })
      } else {
        wx.$api.hideLoading()
        thisPage.setData({
          screenFormData: formData
        })
        thisPage.searchEvent()
      }
    },
    reset() {
      let { formData, userInfo, sceneList, staffList, tabType, merchantRange, subIndex } = this.data
      formData.partnerMerchantIds = [userInfo.subLoginUser.merchantId]
      formData.startTime = ''
      formData.endTime = ''
      formData.sceneType = ''
      formData.staffUUIDs = []
      if (tabType == 0) {
        sceneList.map(val => val.active = false)
        sceneList[0].active = true
      } else if (tabType == 1) {
        staffList.map(val => val.active = false)
        staffList[0].active = true
      }
      merchantRange.forEach((item, index) => {
        item.show = true
        if (item.merchantId == userInfo.subLoginUser.merchantId) {
          subIndex = index
        }
      })
      this.setData({
        formData,
        sceneList,
        staffList,
        merchantRange,
        subIndex
      })
      this.init()
    }
  },
  attached() {
    this.init()
  }
})