technicianList.js 5.58 KB
// packageMine/pages/customerManagement/technicianList/technicianList.js
import { IMG_URL } from '~/utils/constantData'
import mineServiceApi from '~/api/mineApi/mineService'
import constantData from './constant/data'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    bgUrl: `${IMG_URL}/personal/empty_tran.png`,
    technicianList: [],
    total: -1,
    pageCof: {
      pageNum: 1,
      pageSize: 10
    },
    formData: {
      technicianName: ''
    },
    formItems: constantData.formItems,
    targetIdx: '-1',
    target: {},
    targetIdx: {},
    isAsk: false
  },

  handleInput(e) {
    let { attr } = e.currentTarget.dataset
    let { value } = e.detail
    this.setData({
      ['formData.' + attr]: value.replace(/(^\s*)|(\s*$)/g, "")
    })
  },

  handleDialogShow() {
    this.setData({
      isDialogShow: !this.data.isDialogShow
    })
  },

  addTech() {
    this.handleDialogShow()
  },

  editTech() {
    let { targetIdx } = this.data
    this.setData({
      [`technicianList[${targetIdx}].isEdit`]: !this.data.technicianList[targetIdx]?.isEdit
    })
  },

  editClear() {
    let { addOrEdit } = this.data
    addOrEdit == 'add' ? this.addTech() : this.editTech()
    this.setData({
      targetIdx: '-1',
      target: {},
      formData: { technicianName: '' }
    })
  },

  handleCancel() {
    this.editClear()
  },

  handleConfirm() {
    let { formData, target, customerInfo, addOrEdit } = this.data
    let apiName = {
      'add': 'addTechnician',
      'edit': 'updateTechnician',
    }
    let params = {
      ...formData,
      merchantId: customerInfo.serviceCenterMerchantId,
      merchantName: customerInfo.serviceCenterMerchantName
    }
    let validObj = wx.$util.checkRule(formData, addOrEdit == 'add' ? constantData.formRule : constantData.editFormRule)
    if (validObj) {
      wx.$api.showToast(validObj.message)
      return
    }
    if (addOrEdit == 'edit') {
      if (target.technicianName == formData.technicianName) {
        this.editClear()
        return true
      }
      params.id = target.id
    }
    wx.$api.showLoading('加载中...')
    mineServiceApi[apiName[addOrEdit]](params).then(res => {
      let { code, data, message } = res
      if (code == '0') {
        wx.hideLoading()
        wx.$api.showToast(addOrEdit == 'add' ? '新增成功' : '修改成功')
        this.editClear()
        this.handleSearch(true)
      } else {
        wx.$api.showToast(message)
        if (addOrEdit == 'edit') {
          this.editClear()
          this.handleSearch(true)
        }
      }
    })
  },

  handleEdit(e) {
    let { type, target, idx } = e.currentTarget.dataset
    this.setData({
      addOrEdit: type,
      target: e.currentTarget.dataset?.target || {},
      targetIdx: idx || 0
    })
    switch (type) {
      case 'add':
        this.addTech()
        break
      case 'edit':
        this.setData({
          ['formData.technicianName']: target.technicianName
        })
        this.editTech(target)
        break
    }
  },

  handleDelete(e) {
    let { target } = e.currentTarget.dataset
    let params = {
      id: target.id
    }
    let _this = this
    wx.showModal({
      title: '删除',
      content: '确认删除' + target.staffName + '技师吗?',
      confirmText: '确定',
      cancelText: '取消',
      confirmColor: '#3666FE',
      success(res) {
        if (res.confirm) {
          wx.$api.showLoading('加载中...')
          mineServiceApi.deleteTechnician(params).then(res => {
            let { code, data, message } = res
            if (code == '0') {
              wx.hideLoading()
              wx.$api.showToast('删除成功')
              _this.handleSearch(true)
            } else {
              wx.$api.showToast(message)
            }
          })
        }
      }
    })
  },

  handleSearch() {
    this.setData({
      ['pageCof.pageNum']: 1,
      total: -1
    })
    this.getList(true)
  },

  scrollonReachBottom() {
    this.getList()
  },

  getList(isReset = false) {
    let { technicianList, pageCof, total, customerInfo, isAsk } = this.data
    let _this = this
    if (total == technicianList.length || isAsk) {
      return true
    }
    let params = {
      ...pageCof,
      status: 0,
      merchantNoList: [customerInfo.serviceCenterMerchantId],
      // roleName: '技师'
      staffBusinessType: 1
    }
    this.setData({
      isAsk: true
    })
    mineServiceApi.getTechnicianList(params).then(res => {
      let { data, count } = res
      this.setData({
        technicianList: pageCof.pageNum == 1 ? (data || []) : technicianList.concat(data),
        ['pageCof.pageNum']: pageCof.pageNum + 1,
        triggered: false,
        total: count
      })
      if (isReset) {
        this.setData({
          scrollTop: 0
        })
      }
    }).finally(function () {
      _this.setData({
        isAsk: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$router.getData(res => {
      this.setData({
        customerInfo: res
      })
      this.getList(true)
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})