ScreenConfirmBox.js 8.74 KB
// pages/tab/output/components/ScreenConfirmBox/ScreenConfirmBox.js
import USERAPI from '~/api/usersApi/index'
import outputValueApi from '~/api/outputValueApi/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
          })
          // if (!this.data.formData.startTime || !this.data.formData.endTime) this.setDate() // 默认展示当前月份
          this.setViewData()
        }
      }
    },
    tabType: Number, // tab值 0我的门店 1我的员工
    userInfo: Object,
    statusBarHeight: Number,
    navigationBarHeight: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    servicePopupShow: false,
    merchantRange: [],
    sceneList: [
      {
        id: '',
        label: '全部',
        active: true
      },
      {
        id: '1',
        label: '太阳膜'
      },
      {
        id: '3',
        label: '售后众筹'
      },
      {
        id: '0',
        label: '会员权益/升级'
      }
    ],
    staffList: [],
    isShowCalendarMonth: true // 重置重新渲染日历组件 没有传值回显日期功能
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setViewData() {
      let { tabBarStyle } = this.data
      wx.$api.getViewInfo(
        '.btn-box',
        res => {
          this.setData({
            height: tabBarStyle,
            btnHeight: res[0]?.height
          })
        },
        this
      )
    },
    getMerchantRange() {
      let { userInfo } = this.data
      // 判断是超管才需要切换账号
      if (userInfo?.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
      this.reset('', false)
      merchantRange.forEach((item, index) => {
        item.show = true
        if (item.merchantId == merchantId) {
          subIndex = index
          item.act = true
        }
      })
      formData.partnerMerchantIds = [merchantId]
      // if (!this.data.formData.startTime || !this.data.formData.endTime) this.setDate() // 未选择时间默认展示当前月份
      this.setData({
        merchantRange,
        subIndex,
        formData
      })
      this.getStaffList()
      this.servicePicker()
    },
    // 服务商切换
    servicePicker() {
      let { userInfo } = this.data
      if (!userInfo?.isSuper) return
      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
      })
    },
    setDate() {
      let { formData } = this.data
      let month = new Date().getMonth() + 1
      let year = new Date().getFullYear()
      let day = this.getLastDay(year, month)
      formData.startTime = year + '-' + month + '-01' + ' 00:00:00'
      formData.endTime = year + '-' + month + '-' + 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 } = this.data.formData
      let { userInfo } = this.data
      console.log(partnerMerchantIds, 'partnerMerchantIds')
      let params = {
        partnerMerchantIds: partnerMerchantIds.length ? partnerMerchantIds : userInfo?.subLoginUser?.merchantId
      }
      outputValueApi.GETALLSTAFFDATAAPI(params).then(res => {
        let arr = res || []
        if (!arr?.length || arr[0]?.staffName !== '全部') {
          arr.unshift({
            active: true,
            uuid: '',
            staffName: '全部'
          })
        }
        this.setData({
          staffList: arr
        })
      })
    },
    init() {
      this.getMerchantRange()
      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 = !target[index].active
            target[0].active = false
          }
          let activeFlag = target.findIndex(val => val.active)
          if (activeFlag < 0) target[0].active = true
          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
      })
    },
    // 日期组件
    showCalendar(e) {
      let calendarMonthModule = this.selectComponent('#calendarMonth')
      calendarMonthModule.showCalendar && calendarMonthModule.showCalendar(e)
    },
    confirm() {
      if (this.data.screenConfirmFlag) return
      wx.$api.showLoading('加载中')
      let { userInfo, formData, staffList, sceneList } = this.data
      formData.sceneType = sceneList.filter(val => val.active)[0]?.id || ''
      let staffArr = []
      staffList.filter(val => val.active).map(subVal => {if (subVal.uuid) staffArr.push(subVal.uuid)})
      formData.staffUUIDs = staffArr
      this.triggerEvent('screenFlag')
      this.setData({screenConfirmFlag: true})
      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()
          // 切换服务商 清空已选的门店
          formData.shopMerchantIds = []
          formData.shopMerchantNames = '全部门店'
          // 获取服务商下门店
          this.triggerEvent('changeService')
          thisPage.setData({
            screenFormData: formData,
          })
          thisPage.searchEvent()
          this.setData({ screenConfirmFlag: false })
        })
      } else {
        wx.$api.hideLoading()
        thisPage.setData({
          screenFormData: formData
        })
        thisPage.searchEvent()
        this.setData({ screenConfirmFlag: false })
      }
    },
    reset(e, isresetTime = true) {
      let { formData, userInfo, sceneList, staffList, tabType, merchantRange, subIndex } = this.data
      formData.partnerMerchantIds = [userInfo?.merchantId]
      if (isresetTime) {
        formData.startTime = ''
        formData.endTime = ''
      }
      if (e) this.getStaffList() // 点击重置
      // this.setDate()
      // this.setData({
      //   isShowCalendarMonth: false
      // })
      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?.merchantId) {
          subIndex = index
        }
      })
      this.setData({
        formData,
        sceneList,
        staffList,
        merchantRange,
        subIndex,
        isShowCalendarMonth: true
      })
    }
  },
  attached() {
    this.init()
  }
})