technicianSetup.js 6.41 KB
// tabMine/pages/technicianSetup/technicianSetup.js
import api from '../../api.js'
import {
  ImgUrl
} from '~/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL: ImgUrl,
    total: -1,
    pageCof: {
      pageNum: 1,
      pageSize: 1000
    },
    isAsk: false,
    technicianList: [
      //   {
      //   staffName: '',
      //   staffPhone: '',
      //   isEdit: true
      // }
    ]
  },
  init() {
    this.getList(true)
  },
  addTechnicalItem() {

    let {
      technicianList,
      userInfo
    } = this.data
    if (userInfo.userType == 2) return
    // 默认添加离开弹框 提交成功后取消
    wx.enableAlertBeforeUnload &&
      wx.enableAlertBeforeUnload({
        title: '温馨提示',
        message: '技师信息未提交,请提交后返回',
        success: function (res) {
          console.log('成功:', res)
        },
        fail: function (err) {
          console.log('失败:', err)
        }
      })
    technicianList.unshift({
      staffName: '',
      staffPhone: '',
      merchantId: userInfo.merchantId,
      merchantName: userInfo.merchantName,
      isPhone: true,
      isEdit: true
    })
    this.setData({
      technicianList
    })
  },
  editTechicianInfo(e) {
    let {
      userInfo
    } = this.data
    if (userInfo.userType == 2) return
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      [`technicianList[${index}].isEdit`]: true
    })
  },
  submit(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      technicianList
    } = this.data
    let params = wx.$util.deepClone(technicianList[index])
    if (!params.staffName) {
      return wx.$util.showToast('请输入姓名')
    }
    const phoneRex = /^[1]([3-9])[0-9]{9}$/
    if (!params.staffPhone || !phoneRex.test(params.staffPhone)) {
      return wx.$util.showToast('请输入正确手机号')
    }
    params.technicianName = params.staffName
    params.technicianPhone = params.staffPhone
    console.log('18775035205');
    wx.$util.showLoading('加载中')
    // this.setData({
    //   [`technicianList[${index}].isEdit`]: false
    // })
    // return
    let apiName = params.id ? 'updateTechnician' : 'addTechnician'
    // debugger
    api[apiName](params).then((res) => {
      wx.hideLoading()
      // debugger
      // console.log(res, 'res');
      let {
        code,
        data,
        message
      } = res
      if (code == 0) {
        wx.$util.showToast(params.id ? '修改成功' : '新增成功')
        technicianList[index].isEdit = false
        this.setData({
          [`technicianList[${index}].isEdit`]: false
        })
        // let {}
        debugger
        let isAllEdit = technicianList.every(item => !item.isEdit)
        isAllEdit && wx.disableAlertBeforeUnload()

      } else {
        wx.$util.showToast(message)
      }

    }).catch((err) => {

    });
    // this.setData({
    //   // [`technicianList[${index}].isEdit`]:true
    // })
  },
  deleteTechicianInfo(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      technicianList
    } = this.data
    const id = technicianList[index]?.id
    // if (id) {

    let _this = this
    wx.showModal({
      title: '温馨提示',
      content: '是否删除该技师',
      // showCancel: false,
      confirmText: '确定',
      confirmColor: '#347BFF',
      success(res) {
        if (res.confirm) {
          if (!id) {
            technicianList.splice(index, 1)
            _this.setData({
              technicianList
            })
            return
          }
          wx.$util.showLoading('加载中')
          api.deleteTechnician({
            id
          }).then((result) => {
            wx.hideLoading()
            let {
              code,
              data,
              message
            } = result
            if (code == 0) {
              technicianList.splice(index, 1)
              _this.setData({
                technicianList
              })
            } else {
              wx.$util.showToast(message)
            }
          }).catch((err) => {

            console.log(err);
          });
          // _this.getDetail(orderCode)
        }
      }
    })
    // }


  },
  input(e) {
    let {
      value
    } = e.detail
    let {
      name,
      index
    } = e.currentTarget.dataset
    let {
      technicianList
    } = this.data
    let itemChar = `technicianList[${index}].${name}`
    this.setData({
      [itemChar]: value.trim()
    })
  },
  getList(isReset = false) {
    // let user
    let {
      technicianList,
      pageCof,
      total,
      userInfo,
      isAsk
    } = this.data
    // console.log(userInfo, 'userInfo');
    let _this = this
    if (total == technicianList.length || isAsk) {
      return true
    }
    let params = {
      ...pageCof,
      status: 0,
      merchantNoList: [userInfo.merchantId],
      // roleName: '技师'
      staffBusinessType: 1
    }
    this.setData({
      isAsk: true
    })
    api.getTechnicianList(params).then(res => {
      let {
        data,
        count
      } = res.data
      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) {

  },

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

  },

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

  },

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

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

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

  },

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

  },

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

  }
})