missingReception.js 10.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
// packageEquity/pages/missingReception/missingReception.js
var util = require('~/utils/timeFormat/time-utils.js')
import dayjs from 'dayjs'
import {
  checkMissingReception,
  getMissingOptions,
  addMissingReception,
  getMissingCustomerList
} from "../../receptionRecordsApi"
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
const dayLimit = 7
import reportPageTrackMixin from '~/mixins/reportPageTrackMixin'

Page({
  behaviors: [reportPageTrackMixin],
  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    timeBean: {},
    selectWeek: 0,
    missingOptions: [],
    list: [],
    detailObj: [],
    currentDate: '',
    track_event_code: 'mini_pad_missing_reception', //埋点动作类型
    scene: 0
  },
  jump () {
    wx.$router.back()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options)
    this.initData(options.reportDate)
    this.getMissingOptions()

  },
  async getMissingOptions () {
    try {
      const {
        code,
        data,
        message
      } = await getMissingOptions()
      if (code == 0) this.setData({
        missingOptions: data.map(e => {
          return {
            reason: e.reason,
            reasonCode: e.reasonCode
          }
        })
      })
      else throw message
    } catch (error) {
      wx.$util.showToast(error)
    }
  },
  navbarReady (e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
  },
  /**
   * 点击了上一周,选择周数字减一,然后直接调用工具类中一个方法获取到数据
   */
  lastWeek: function (e) {
    var selectWeek = --this.data.selectWeek
    var timeBean = this.data.timeBean
    timeBean = util.getWeekDayList(selectWeek)
    if (selectWeek != 0) {
      timeBean.selectDay = 0
    }

    this.setData({
      timeBean,
      selectWeek
    })
    this.setReportDate()
  },
  initData (reportDate = '') {
    if (reportDate) {
      let dateisSunday = dayjs(reportDate).toDate(),
        countSundays = this.countSundays(
          reportDate,
          dayjs().format('YYYY-MM-DD')
        )
      if (new Date().getDay() === 0) {
        countSundays = countSundays - 1
      }
      this.setData({
        timeBean: util.getWeekDayList(0 - countSundays),
        selectWeek: 0 - countSundays,
        ['timeBean.selectDay']: dateisSunday.getDay() == 0 ? 6 : dateisSunday.getDay() - 1,
        selectWeek: dateisSunday.getDay() === 0 ? -1 : 0
      })
    } else {
      this.setData({
        timeBean: util.getWeekDayList(this.data.selectWeek)
      })
    }
    this.setReportDate()
  },
  countSundays (startDate, endDate) {
    let start = new Date(startDate)
    let end = new Date(endDate)
    let sundays = 0
    // 遍历日期范围
    for (
      let current = start; current <= end; current.setDate(current.getDate() + 1)
    ) {
      // 检查是否为周日 (0 表示周日)
      if (current.getDay() === 0) {
        sundays++
      }
    }
    return sundays
  },
  /**
   * 点击了下一周,选择周数字加一,然后直接调用工具类中一个方法获取到数据
   */
  nextWeek: function (e) {
    var selectWeek = ++this.data.selectWeek
    var timeBean = this.data.timeBean
    timeBean = util.getWeekDayList(selectWeek)
    if (selectWeek != 0) {
      timeBean.selectDay = 0
    }
    this.setData({
      timeBean,
      selectWeek
    })
    this.setReportDate()
  },
  /**
   * 选中了某一日,改变selectDay为选中日
   */
  dayClick: function (e) {
    let {
      weekDayList,
    } = this.data.timeBean,
      selectDay = e.detail,
      currentDate = weekDayList[selectDay].date
    this.setData({
      ['timeBean.yearMonth']: dayjs(currentDate).format('YYYY-MM'),
      ['timeBean.selectDay']: selectDay,
      currentDate
    })
    this.getMissingCustomerList()
  },
  //  设置报告日期
  async setReportDate () {
    try {
      wx.$util.showLoading()
      const {
        timeBean,
        userInfo
      } = this.data, {
        uuid,
        merchantId,
        shopMerchantId,
        sceneType
      } = userInfo
      const {
        code,
        data,
        message
      } = await checkMissingReception({
        merchantId: shopMerchantId,
        yqMerchantId: merchantId,
        staffUuid: uuid,
        sceneType: sceneType,
        reportStartDate: timeBean.reportStartDate,
        reportEndDate: timeBean.reportEndDate,
      })
      if (code == 0) {
        let {
          weekDayList,
          selectDay
        } = timeBean,
          detailObj = {},
          currentDate = weekDayList[selectDay].date
        weekDayList.map(e => {
          data.map(i => {
            detailObj[i.reportDate] = i
            if (i.reportDate == e.date) e.hasDailyValue = i.hasReceptionReport || !i.missingReceptionRecord
          })
        })
        this.setData({
          ['timeBean.weekDayList']: weekDayList,
          detailObj,
          currentDate
        })
        this.getMissingCustomerList()
      } else throw message
    } catch (error) {
      wx.hideLoading()
      wx.$util.showToast(error)
    }
  },
  async getMissingCustomerList () {
    try {
      wx.$util.showLoading()
      this.setData({
        list: [],
        scrollTop: 0
      })

      let {
        currentDate,
        detailObj,
        list
      } = this.data, {
        uuid,
        shopMerchantName,
        shopMerchantId,
        sceneType
      } = this.data.userInfo, {
        code,
        data,
        message
      } = await getMissingCustomerList({
        staffUuid: uuid,
        merchantId: shopMerchantId,
        merchantName: shopMerchantName,
        reportDate: currentDate,
        sceneType: sceneType
      })
      wx.hideLoading()
      let total = 0
      if (code == 0) {
        let responseList = data.list
        total = data.total
        if (responseList.length) {
          list = responseList.map(e => {
            return {
              customerName: e.customerName,
              reasonText: e.reason,
              disabled: true
            }
          })
        } else {
          let item = detailObj[currentDate],
            {
              totalSalesCustomerCount,
              recordingCount
            } = item,
            len = totalSalesCustomerCount - recordingCount
          if (len) {
            for (let index = 0; index < len; index++) {
              list.push({
                customerName: `客户${index + 1}`,
              })
            }
          }
        }
      } else wx.$util.showToast(message)
      this.setData({
        list,
        total,
        isDateWithin7Days: dayjs().diff(dayjs(currentDate), 'day') < dayLimit
      })
    } catch (error) {
      wx.hideLoading()
      console.log(error)
      if (error) wx.$util.showToast(error)
    }
  },
  inputBlur (e) {
    let {
      index
    } = e.target.dataset, {
      value
    } = e.detail
    this.setData({
      [`list[${index}].customerName`]: value,
      [`list[${index}].inputErrorText`]: value ? '' : '必填不能为空'
    })
  },
  changeReason (e) {
    const {
      missingOptions
    } = this.data, {
      index
    } = e.target.dataset, {
      value
    } = e.detail,
      item = missingOptions[value]
    this.setData({
      [`list[${index}].errorText`]: '',
      [`list[${index}].reasonText`]: item.reason,
      [`list[${index}].reasonCode`]: item.reasonCode,
    })
  },
  cancel () {
    wx.showModal({
      title: '温馨提示',
      content: '接待录音缺失将影响工资绩效,是否确认放弃报备?',
      confirmColor: '#347BFF',
      success: (res) => {
        if (res.confirm) {
          wx.$router.back()
        }
      }
    })
  },
  async submit () {
    try {
      const {
        list,
        userInfo,
        currentDate
      } = this.data, {
        shopMerchantName,
        shopMerchantId,
        phone,
        uuid,
        name,
        sceneType
      } = userInfo
      list.map((e, index) => {
        if (!e.reasonCode) this.setData({
          [`list[${index}].errorText`]: '必填不能为空'
        })
        if (!e.customerName) this.setData({
          [`list[${index}].inputErrorText`]: '必填不能为空'
        })
      })
      list.map(e => {
        if (!e.reasonCode || !e.customerName) throw false
      })
      let missingReceptionReportAddRequests = []
      list.map(e => {
        missingReceptionReportAddRequests.push({
          customerName: e.customerName,
          reasonCode: e.reasonCode,
        })
      })
      wx.$track.setTrackData(0, 'mini_add_pad_missing_reception')
      wx.$util.showLoading('提交中...')
      // 分批提交,每批最多50条
      const batchSize = 50
      for (let i = 0; i < missingReceptionReportAddRequests.length; i += batchSize) {
        const batch = missingReceptionReportAddRequests.slice(i, i + batchSize)
        const {
          code,
          message
        } = await addMissingReception({
          missingReceptionReportAddRequests: batch,
          reportDate: currentDate,
          merchantId: shopMerchantId,
          merchantName: shopMerchantName,
          staffUuid: uuid,
          staffName: name,
          staffPhone: phone,
          sceneType: sceneType
        })
        if (code != 0) throw message
      }
      wx.hideLoading()
      let _this = this
      wx.showModal({
        title: '提示',
        content: '提交成功',
        showCancel: false,
        confirmColor: '#347BFF',
        success: (res) => {
          if (res.confirm) {
            _this.setReportDate()
            // wx.$router.back()
          }
        }
      })
    } catch (error) {
      wx.hideLoading()
      if (error) wx.$util.showToast(error)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

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

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

  },

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

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

  },

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

  },

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

  }
})