Newer
Older
baseResourceFront / src / views / carinfo / editGpsDevice.vue
zhangyingjie on 24 Mar 2021 5 KB 合并master分支
<template>
  <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body width="700px">
    <el-form ref="dataForm" :rules="rules" :model="deviceInfoForm" label-position="right" size="small" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12" offset="6">
          <el-form-item label="IMEI" prop="imei">
            <el-input v-model="deviceInfoForm.imei" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'IMEI'" filterable type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" offset="6">
          <el-form-item label="物联网卡号" prop="iot">
            <el-input v-model.trim="deviceInfoForm.iot" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'物联网卡号'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" offset="6">
          <el-form-item label="SIM卡号" prop="sim">
            <el-input v-model.trim="deviceInfoForm.sim" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'SIM卡号'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="dialogStatus !== 'detail'" slot="footer" class="dialog-footer">
      <el-button type="primary" style="width: 100px;font-size: 16px;" @click="saveData">保 存</el-button>
      <el-button style="width: 100px;font-size: 16px" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDeviceInfo, updateDeviceInfo } from '@/api/device'
export default {
  name: 'EditGpsDevice',
  data() {
    return {
      deptShow: true,
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      deviceInfoForm: {
        id: '',
        imei: '',
        iot: '',
        sim: ''
      }, // 表单
      textMap: {
        update: '编辑GPS设备',
        create: '新增GPS设备',
        detail: 'GPS设备详情'
      }, // 表头显示标题
      rules: {
        imei: [{ required: true, message: 'IMEI不能为空', trigger: ['blur', 'change'] }],
        iot: [{ required: true, message: '物联网卡号不能为空', trigger: ['blur', 'change'] }],
        sim: [{ required: true, message: 'SIM卡号不能为空', trigger: ['blur', 'change'] }],
        userPhone: [{
          required: false,
          pattern: /^1[34578]\d{9}$/, // 可以写正则表达式
          message: '请输入正确的手机号',
          trigger: ['blur', 'change'] }],
        chargePhone: [{
          required: false,
          pattern: /^1[34578]\d{9}$/, // 可以写正则表达式
          message: '请输入正确的手机号',
          trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改,将row中数据填写到输入框中
        this.deviceInfoForm = {
          id: row.id,
          imei: row.imei,
          iot: row.iot,
          sim: row.sim
        }
      }
    },
    // 重置表单
    resetForm() {
      this.deviceInfoForm = {
        id: '',
        imei: '',
        iot: '',
        sim: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.deviceInfoForm)
        if (valid) {
          addDeviceInfo(this.deviceInfoForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.deviceInfoForm)
          updateDeviceInfo(this.deviceInfoForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    changeiot(val) {
      this.deviceInfoForm.iot = val.iot
      this.deviceInfoForm.deviceId = val.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>