addDialog.vue 7.68 KB
<!-- 新增积分记录 -->
<template>
  <baseDialog :dialog-config="dialogConfig"
              :is-show.sync="visible"
              class="accountDailog"
              @handleClick="handleAddConfirm">
    <div class="add-dialog">
      <glsx-combi-form class="edit-form-container"
                       ref="addFormRef"
                       label-width="150px"
                       default-width="400px"
                       :form-ref="editChildRef"
                       :form-obj="infoForm"
                       :form-data="editFormData"
                       :form-rules="formRules">
        <div slot="pointValueHtml"
             slot-scope="scope">
          <el-input-number v-model="infoForm.pointValue"
                           placeholder="输入数值"
                           :precision="1"
                           :step="0.1"
                           :min="-9.9"
                           :max="9.9"></el-input-number>
        </div>
      </glsx-combi-form>
    </div>
  </baseDialog>
</template>

<script>
import { pointRule, pointAdd, pointStaffQuery } from '@api/inStorePoints'
import { Util, deepClone } from '@/common/util'
export default {
  name: 'AddDialog',
  data() {
    // function checkPhone(rule, value, callback) {
    //   if (!Util.checkPhone(value)) {
    //     callback(new Error('请输入正确的手机号码'))
    //     return false
    //   }
    //   callback()
    // }
    function checkPointValue(rule, value, callback) {
      if (value == 0) {
        callback(new Error('积分值不能为0'))
        return false
      }
      callback()
    }
    return {
      userInfo: this.$store.state.d2admin.user.info,
      infoForm: {
        staffName: '',
        staffPhone: '',
        pointClassification: '',
        pointType: '',
        pointDescription: '',
        pointValue: 1
      },
      oldVal: {
        transferTotalAmount: ''
      },
      dialogConfig: {
        title: '新增记录',
        width: '720px'
      },
      editChildRef: 'editChildRef',
      formRules: {
        staffName: [{ required: true, message: '请填写员工姓名', trigger: 'blur' }],
        staffPhone: [{
          required: true,
          message: '请填写电话号码',
          trigger: 'blur'
        }, {
          validator: this.checkPhone,
          trigger: 'blur'
        }],
        pointClassification: [{ required: true, message: '请选择积分分类', trigger: 'blur' }],
        pointType: [{ required: true, message: '请选择积分类型', trigger: 'blur' }],
        // pointDescription: [{ required: true, message: '请填写备注', trigger: 'blur' }],
        pointValue: [{
          required: true,
          message: '请填写积分值',
          trigger: 'change'
        }, {
          validator: checkPointValue,
          trigger: 'change'
        }]
      },
      editFormData: [{
        label: "电话号码",
        name: "staffPhone",
        type: "input",
        inputType: "number",
        isClear: true,
        maxlength: "11"
      }, {
        label: '员工姓名',
        name: 'staffName',
        type: 'input',
        maxlength: 8,
        isDisabled: true,
        placeholder: '请输入电话号码匹配'
        // showLimit: true,
        // isClear: true
      }, {
        label: '积分分类',
        name: 'pointClassification',
        type: 'select',
        // isClear: true,
        optionList: [],
        optionId: 'categoryCode',
        optionLabel: 'categoryName',
        optionsName: 'pointClassificationOpt',
        changeAttr: (value, targetItem) => {
          this.baseData.pointTypeOpt = []
          if (value) {
            this.baseData.pointTypeOpt = this.baseData.pointClassificationOpt.find(item => item.categoryCode == value).typeList
          }
          this.infoForm.pointType = ""
          this.getOptionsList('editFormData', false)
        }
      }, {
        label: '积分类型',
        name: 'pointType',
        type: 'select',
        isClear: true,
        noDataText: "请先选择积分分类",
        optionList: [],
        optionId: 'typeCode',
        optionLabel: 'typeName',
        optionsName: 'pointTypeOpt'
      }, {
        label: '积分描述',
        name: 'pointDescription',
        type: 'input',
        maxlength: 50,
        inputType: 'textarea',
        showLimit: true,
        rows: 3
      }, {
        label: '积分值',
        name: 'pointValue',
        addHtml: 'pointValueHtml'
        // rules: [{ required: true, message: '请输入金额,支持输入0~999999的数字,支持小数点后2位', trigger: 'change' }]
      }],
      baseData: {
        pointClassificationOpt: [],
        pointTypeOpt: []
      },
      uploadFileList: []
    }
  },

  props: {
    addVisible: {
      type: Boolean,
      default: false
    }
  },
  watch: {

  },
  computed: {
    visible: {
      get() {
        return this.addVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'addVisible' })
      }
    }
  },

  mounted() {
    this.pointRule()
  },

  methods: {
    getOptionsList(formName, hasAll = true, refreshSigle) {
      if (refreshSigle) {
        var item = this[formName].find(k => k.name == refreshSigle)
        var index = this[formName].findIndex(k => k.name == refreshSigle)
        this.$set(this[formName][index], 'optionList', deepClone(this.baseData[item.optionsName]))
        if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
          const temp = {}
          temp[this[formName][index].optionId] = ''
          temp[this[formName][index].optionLabel] = '全部'
          this[formName][index].optionList.unshift(temp)
        }
      } else {
        this[formName].forEach((item, index) => {
          if (item.type === 'select' && this.baseData[item.optionsName].length) {
            this.$set(this[formName][index], 'optionList', deepClone(this.baseData[item.optionsName]))
            if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
              const temp = {}
              temp[this[formName][index].optionId] = ''
              temp[this[formName][index].optionLabel] = '全部'
              this[formName][index].optionList.unshift(temp)
            }
          }
        })
      }
    },
    checkPhone(rule, value, callback) {
      if (!Util.checkPhone(value)) {
        callback(new Error('请输入正确的手机号码'))
        return false
      }
      pointStaffQuery({ staffPhone: value }).then((res) => {
        console.log('res', res)
        if (res) {
          this.infoForm.staffName = res.staffName
        }
      }).catch((err) => {
        console.log('err', err)
        this.infoForm.staffName = ''
      })
      callback()
    },
    pointRule() {
      pointRule({}).then((res) => {
        this.baseData.pointClassificationOpt = res
        this.getOptionsList('editFormData', false)
      })
    },

    handleAddConfirm() {
      this.$refs.addFormRef.$refs[this.editChildRef].validate(valid => {
        if (valid) {
          var params = deepClone(this.infoForm)
          // params.pointValue = params.pointValue + '' // 后台是字符串类型接收
          pointAdd(params).then(res => {
            this.notice.okTips('操作成功')
            this.visible = true
          })
        }
      })
    }

  }
}
</script>
<style lang="scss" scoped>
.add-dialog {
  margin: 20px 0;

  .reject-input {
    ::v-deep .el-textarea__inner {
      height: 70px;
    }
  }

  .el-textarea .el-input__count {
    right: 90px;
    bottom: -10px;
    font-size: 12px;
    color: #999999;
  }

  .tips {
    color: #999999;
  }
}
</style>