collegeScene.js 9.84 KB
// packageEquity/pages/collegeScene/collegeScene.js
import { staffInfo, orderPerformanceDetail, performancePassword, performancePasswordCheck } from '../../api'
import { ImgUrl } from '~/config.js'
// setConfigInfo

const app = getApp()
Page({
  options: {
    // multipleSlots: true, // 在组件定义时的选项中启用多 slot 支持
    styleIsolation: 'shared' // 自定义覆盖vant样式
  },
  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    current: 0,
    ImgUrl: ImgUrl + '/miniProgram/',
    isStaff: true,
    forbid: true,
    hasStaffPassword: false,
    passwordPop: false,
    isSetPassword: true,
    orderList: [],
    orderListObj: [
      {
        label: '下单时间',
        key: 'orderTimeLabel'
      },
      {
        label: '支付时间',
        key: 'paySuccessTimeLabel'
      },
      {
        label: '客户姓名',
        key: 'customerName'
      },
      {
        label: '所属门店',
        key: 'merchantName'
      },
      {
        label: '转化员',
        key: 'staffName'
      },
      {
        label: '订单编号',
        key: 'orderCode'
      }
    ]
  },
  hideStaffInfo() {
    // 判断是不是 员工,是关闭
    let { isStaff } = this.data
    if (isStaff) {
      this.setData({
        hasStaffPassword: false
      })
    }
  },
  bindchange(e) {
    this.setData({ current: e.detail.current })
  },
  remarkConfirm() {
    this.setData({
      hasStaffPassword: true
    })
  },
  viewPerformance() {
    // 判断是第一次还是需要输入秘密
    let { hasStaffPassword, staffInfoObj } = this.data
    // if (userInfoObj.staffInfo.needSetPwd) {
    this.setData({
      passwordPop: true,
      isSetPassword: staffInfoObj.staffInfo.needSetPwd
    })
  },
  refresh() {
    // console.log('refresh', refresh)
    this.setData({
      orderList: [],
      currentPage: 0
    })
    this.orderPerformanceDetail()
  },
  onBottomRefresh() {
    // console.log('onBottomRefresh')
    let { currentPage, monthArray } = this.data
    if (currentPage < monthArray.length - 1) {
      this.setData({
        currentPage: currentPage + 1
      })
      this.orderPerformanceDetail()
    } else {
      wx.$util.showToast('没有更多了')
    }
  },
  orderPerformanceDetail() {
    let { currentPage, monthArray, orderList } = this.data
    let performanceMonth = monthArray[currentPage]
    // console.log(performanceMonth, 'performanceMonth')
    orderPerformanceDetail({ performanceMonth })
      .then(res => {
        console.log(res)
        let { code, data } = res

        if (code == 0 && data.performanceMonth) {
          let [year, month] = data.performanceMonth.split('-')
          data.year = year
          data.month = month
          data.orderList = data.orderList || []
          data.orderList = data.orderList.map(item => {
            item.orderGoodsName = item.orderGoodsName.split(',')
            // console.log(orderTime, paySuccessTime, 'orderTime')
            item.orderTimeLabel = wx.$util.tsFormatTime(item.orderTime, 'Y年M月D日 h:m:s')
            item.paySuccessTimeLabel = wx.$util.tsFormatTime(item.paySuccessTime, 'Y年M月D日 h:m:s')
            return item
          })
          data.orderList = data.orderList.slice(0, 100)
          // wx.$util.tsFormatTime(data.orderTime, 'Y年M月D日 h:m:s')
          orderList.push({ ...data })
          // 当获取不到数据 无法触发scroll-view 触底加载 手动触发加载
          if (!data?.orderList.length && currentPage < 2) { // 默认最近三个月数据
            this.onBottomRefresh()
          }
          // console.log(data, 'data')
          this.setData({
            orderList,
            triggered: false
          })
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  businessSchoolStaffInfo() {
    wx.$util.showLoading('加载中...')
    staffInfo({})
      .then(res => {
        // console.log(res)
        let { userInfo } = this.data
        let { code, data } = res
        if (code == 0) {
          if (userInfo.isSuper) {
            data.staffInfo = {
              entryDate: userInfo.createdDate,
              staffName: userInfo.name
            }
          }
          if (data.staffInfo?.entryDate) {
            data.staffInfo.entryDate = wx.$util.tsFormatTime(data.staffInfo.entryDate, 'Y年M月D日')
          }
          data.monthPerformanceList = data.monthPerformanceList.map(item => {
            item.performanceMonthLabel = this.extractMonth(item.performanceMonth)
            item.height = item.staffDataList.length > 3 ? item.staffDataList.length * 70 + 120 : '300'
            // console.log(Boolean(item.minimumWage && item.minimumWage > item.finalWage));
            let currentMonthYear = wx.$util.tsFormatTime(new Date(), 'Y-M')
            // 获取当前月份
            if (currentMonthYear == item.performanceMonth) { // 当月预计工资提成
              data.finalWage = item.staffDataList?.[0]?.performanceWage
            }
            item.staffDataList = item.staffDataList.map((childItem) => {
              // 工资=最终工资finalWage-绩效提成performanceWage
              childItem.wages = this.Subtr(childItem?.finalWage, childItem?.performanceWage)
              // if (Boolean(childItem.minimumWage && childItem.minimumWage > childItem.finalWage)) {
              //   childItem.finalWage = childItem.minimumWage
              //   // item.performanceWage = 0
              //   childItem.hidePerformanceWage = true
              // } else {
              //   childItem.finalWage = childItem.basicWage
              // }
              // 
              return childItem
            })


            // 处理基本工资和提成

            // let [year, month] = item.performanceMonth.split('-')
            // item.year = year
            // item.month = month
            return item
          })
          const titleLabel = {
            0: '初级驻店',
            1: '中级导师',
            2: '高级导师'
          }
          // 取多场景下最高职级显示
          data.staffInfo.titleLabel = userInfo.isSuper ? '超管' : titleLabel[Math.max(data.staffInfo.title)]
          // 如果title是2,那导师就是员工自己
          if (data.staffInfo.title) {
          }
          this.setData({
            staffInfoObj: data
          })
        }
      })
      .catch(err => {
        console.log(err)
      })
      .finally(() => {
        // wx.stopPullDownRefresh()
        wx.$util.hideLoading()
      })
  },
  // 减法运算解决精度丢失问题
  Subtr(num1, num2) {
    var r1, r2, m, n
    try {
      r1 = num1.toString().split('.')[1].length
    } catch (e) {
      r1 = 0
    }
    try {
      r2 = num2.toString().split(".")[1].length
    } catch (e) {
      r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    n = (r1 >= r2) ? r1 : r2
    return ((num1 * m - num2 * m) / m).toFixed(n)
  },
  getDetailInfo() {
    let monthArray = this.getYearMonthArray()
    this.setData({
      monthArray,
      // pageSize: monthArray.length,
      currentPage: 0
    })
    // console.log(monthArray, 'monthArray')
    // 获取工资绩效员工信息及前3月绩效信息
    this.businessSchoolStaffInfo()
    //获取工资绩效员工订单绩效明细
    this.orderPerformanceDetail()
  },
  // 获取月份
  extractMonth(dateString) {
    const regex = /(\d{4})-(\d{2})/
    const match = regex.exec(dateString)

    if (match && match.length >= 3) {
      const year = parseInt(match[1])
      const month = parseInt(match[2])
      const monthString = `${month}月份`
      return monthString
    }

    return null
  },
  //  获取年份
  getYearMonthArray() {
    let currentDate = new Date()
    let startYear = currentDate.getFullYear()
    let startMonth = currentDate.getMonth() + 1 // Add 1 to adjust for 0-based index

    let endYear = 2023
    let endMonth = 1

    let yearMonthArray = []

    while (startYear > endYear || (startYear === endYear && startMonth >= endMonth)) {
      let formattedMonth = ('0' + startMonth).slice(-2) // Add leading zero if necessary
      let yearMonth = startYear + '-' + formattedMonth
      yearMonthArray.push(yearMonth)

      startMonth--
      if (startMonth === 0) {
        startMonth = 12
        startYear--
      }
    }

    return yearMonthArray
  },
  back() {
    // let { reLaunchPath } = this.data
    // wx.$router.go(reLaunchPath)
    wx.$router.back()
  },
  init(userInfo) {
    const isStaff = app.globalData.isStaff
    // let { userInfo } = this.data
    // console.log(this.data, userInfo, 'userInfo')
    this.setData({
      isStaff: userInfo.isSuper ? false : isStaff,
      hasStaffPassword: !isStaff ? true : false
    })

    this.getDetailInfo()
  },
  onRoute(options) {
    // let { reLaunchPath, tabIndex } = options
    // this.setData({
    //   reLaunchPath,
    //   tabIndex
    // })
    // console.log(options, 'options');
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(e) { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { }
})