<template> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body width="1020px"> <el-form ref="dataForm" :rules="rules" :model="form" label-off-date="right" label-width="80px"> <el-row :gutter="20"> <el-col :span="8"> <el-form-item label="员工号" prop="staffCode"> <el-input v-model.trim="form.staffCode" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="员工名称" prop="staffName"> <el-input v-model.trim="form.staffName" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="性别" prop="staffGender"> <el-select v-model="form.staffGender" placeholder="必填" :disabled="isEditMode"> <el-option value="1" label="男" /> <el-option value="2" label="女" /> </el-select> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="员工类型" prop="staffType"> <el-select v-model="form.staffType" placeholder="必填" :disabled="isEditMode"> <el-option v-for="item in typeList" :key="item.value" :label="item.name" :value="item.value" /> </el-select> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="身份证" prop="staffIdCard"> <el-input v-model.trim="form.staffIdCard" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="手机号" prop="phone"> <el-input v-model.trim="form.phone" :disabled="isEditMode" type="text" placeholder="手机号" /> </el-form-item> </el-col> <el-col :span="8"> <el-form-item label="所属组织" prop="deptName"> <el-input v-model.trim="form.deptName" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="工作日期"> <el-date-picker v-model="timeRange" style="width: 100%" :disabled="isEditMode" type="daterange" range-separator="至" value-format="yyyy-MM-dd" start-placeholder="起始日期" end-placeholder="终止日期" /> <!-- <el-date-picker--> <!-- v-model="form.onDate"--> <!-- :disabled="isEditMode"--> <!-- style="width: 100%"--> <!-- type="date"--> <!-- value-format="yyyy-MM-dd"--> <!-- placeholder="工作开始日期"--> <!-- />--> <!-- </el-form-item>--> <!-- </el-col>--> <!-- <el-col :span="12">--> <!-- <el-form-item label="工作终止日期">--> <!-- <el-date-picker--> <!-- v-model="form.offDate"--> <!-- :disabled="isEditMode"--> <!-- style="width: 100%"--> <!-- type="date"--> <!-- value-format="yyyy-MM-dd"--> <!-- placeholder="工作终止日期"--> <!-- />--> </el-form-item> </el-col> <el-col :span="20" > <el-form-item label="照片" style="margin-top: -10px"> <div style="display: flex;"> <img :src="form.picture ? `${form.picture}`: defaultPhoto" width="120" height="120" style=" margin-top: 10px" @error="errorImg"> <el-upload v-if="dialogStatus !== 'detail'" ref="upload" style="margin: 30px" :before-upload="handleBeforeUpload" :http-request="uploadFile" :show-file-list="false" class="avatar-uploader" action="string" accept=".jpg" > <el-button size="small" icon="el-icon-folder-add"> 浏 览 </el-button> <div slot="tip" class="el-upload__tip" style="font-size: 13px;float: right;margin-left: 10px;margin-top: -1px"> 只能上传jpg文件,且不超过200kb </div> </el-upload> </div> </el-form-item> </el-col> </el-row> </el-form> <div style="font-weight: bold;margin-bottom: 10px">卡片信息</div> <el-table :data="list" row-class-name="small-row-class" border max-height="200px"> <!--内容列--> <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip> <template slot-scope="scope"> <span >{{ scope.row[column.value] }}</span> </template> </el-table-column> </el-table> <div v-if="!isEditMode" slot="footer" class="dialog-footer"> <el-button :loading="btnLoading" type="primary" @click="saveData"> 保存 </el-button> <el-button @click="cancel"> 取消 </el-button> </div> </el-dialog> </template> <script> import {addStaff, updateStaff, getCardList } from '@/api/person' import { getDictByCode } from '@/api/system/dict' import { validateIDCard, validatePhone } from '@/utils/validate' export default { name: 'EditStaff', data() { const validateNum = (rule, value, callback) => { if (value !== '') { if (validatePhone(value) === true) { callback() } else { callback(new Error('请输入正确手机号')) } } callback() } const validateCard = (rule, value, callback) => { if (value !== '') { if (validateIDCard(value) === true) { callback() } else { callback(new Error('请输入正确身份证号')) } } else { callback(new Error('员工身份证号不能为空')) } } return { timeRange: [], // 时间范围 defaultPhoto: require('@/assets/global_images/photo.png'), errorImg: require('@/assets/global_images/photo_error.png'), dialogFormVisible: false, // 对话框是否显示 dialogStatus: '', // 对话框类型:create,update isEditMode: true, list: [], columns: [ { text: '卡号', value: 'cardNum', align: 'center' }, { text: '卡状态', value: 'statusName', align: 'center' }, { text: '卡起始有效时间', value: 'startTime', align: 'center' }, { text: '卡终止有效时间', value: 'endTime', align: 'center' }, { text: '生物特征', value: 'features', align: 'center' }, { text: '挂失时间', value: 'cancelTime', align: 'center' }, { text: '解挂时间', value: 'unhookTime', align: 'center' } ], // 显示列 form: { id: '', staffGender: '', staffIdCard: '', staffCode: '', staffName: '', deptName: '', phone: '', staffType: '', picture: '', onDate: '', offDate: '' }, // 表单 typeList: [], areaList: [], textMap: { update: '编辑员工', create: '新增员工', detail: '员工详情' }, // 表头显示标题 btnLoading: false, // 保存按钮的加载中状态 rules: { staffIdCard: [{ required: true, validator: validateCard, trigger: ['blur', 'change'] }], staffCode: [{ required: true, message: '员工号不能为空', trigger: ['blur', 'change'] }], staffGender: [{ required: true, message: '员工性别不能为空', trigger: ['blur', 'change'] }], staffName: [{ required: true, message: '员工名称不能为空', trigger: ['blur', 'change'] }], deptName: [{ required: true, message: '所属组织不能为空', trigger: ['blur', 'change'] }], staffType: [{ required: true, message: '员工类型不能为空', trigger: ['blur', 'change'] }], phone: [{ required: false, validator: validateNum, trigger: ['blur', 'change'] }] } // 前端校验规则 } }, watch: { timeRange(val) { if (val && val.length > 0) { this.form.onDate = val[0] this.form.offDate = val[1] } else { this.form.onDate = '' this.form.offDate = '' } } }, computed: { }, created() { this.fetchTypeList() }, methods: { fetchData() { getCardList({ staffCode: this.form.staffCode }).then(response => { if (response.code === 200) { this.list = response.data } }) }, // 上传前判断文件格式及大小 handleBeforeUpload(file) { const isJPG = (file.type === 'image/jpeg') || (file.type === 'image/png') let res = true const isLt2M = file.size / 1024 < 200 if (!isJPG) { this.$message.error('上传图片只能是 JPG!') res = false } if (!isLt2M) { this.$message.error('上传图片大小不能超过 200KB!') res = false } return res }, uploadFile(file) { console.log('uploadFile:' + file.file.name) // 转base64 this.getBase64(file.file).then(resBase64 => { this.form.picture = 'data:image/jpeg;base64,' + resBase64.split(',')[1] // 直接拿到base64信息 console.log(this.form.picture) }) }, getBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader() let fileResult = '' reader.readAsDataURL(file) // 开始转 reader.onload = function() { fileResult = reader.result } // 转 失败 reader.onerror = function(error) { reject(error) } // 转 结束 咱就 resolve 出去 reader.onloadend = function() { resolve(fileResult) } }) }, fetchTypeList() { getDictByCode('staffType').then(response => { if (response.code === 200) { this.typeList = response.data } }) getDictByCode('areaType').then(response => { if (response.code === 200) { this.areaList = response.data } }) }, // 初始化对话框 initDialog: function(dialogStatus, row = null) { this.dialogStatus = dialogStatus this.dialogFormVisible = true this.btnLoading = false if (dialogStatus === 'create') { // 如果是新增,清除验证 this.resetForm() this.isEditMode = false this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中 this.form = { id: row.id, staffCode: row.staffCode, staffGender: row.staffGender, staffIdCard: row.staffIdCard, staffName: row.staffName, deptName: row.deptName, phone: row.phone, staffType: row.staffType, picture: row.picture, onDate: row.onDate, offDate: row.offDate } this.timeRange = [row.onDate, row.offDate] this.isEditMode = false } else if (dialogStatus === 'detail') { this.form = { id: row.id, staffCode: row.staffCode, staffGender: row.staffGender, staffIdCard: row.staffIdCard, staffName: row.staffName, deptName: row.deptName, phone: row.phone, staffType: row.staffType, picture: row.picture, onDate: row.onDate, offDate: row.offDate } this.timeRange = [row.onDate, row.offDate] this.isEditMode = true } this.fetchData() }, // 重置表单 resetForm() { this.form = { id: '', staffCode: '', staffGender: '', staffIdCard: '', staffName: '', deptName: '', phone: '', staffType: '', picture: '', onDate: '', offDate: '' } this.timeRange = [] }, // 保存数据 saveData: function() { if (this.timeRange.length === 0) { this.$message.warning('请选择工作时间') return } if (this.dialogStatus === 'update') { this.updateData() } else if (this.dialogStatus === 'create') { this.createData() } }, // 新增数据 createData: function() { this.$refs['dataForm'].validate((valid) => { console.log(this.form) if (valid) { this.btnLoading = true addStaff(this.form).then(response => { if (response.code === 200) { this.$confirm('新增成功,是否继续新增?', '提示', { confirmButtonText: '是', cancelButtonText: '否', type: 'info' }).then(() => { // 选是 this.resetForm() this.btnLoading = false this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) }).catch(() => { // 选否,关闭窗口,并刷新页面 this.$emit('watchChild') this.dialogFormVisible = false }) } }).catch(_ => { // 异常情况,loading置为false this.btnLoading = false }) } }) }, // 修改数据 updateData: function() { this.$refs['dataForm'].validate((valid) => { if (valid) { this.btnLoading = true updateStaff(this.form).then(response => { if (response.code === 200) { this.$message.success('修改成功') this.$refs['dataForm'].clearValidate() this.$emit('watchChild') this.dialogFormVisible = false } }).catch(_ => { // 异常情况,loading置为false this.btnLoading = false }) } }) }, cancel: function() { this.dialogFormVisible = false this.$emit('watchChild') } } } </script> <style rel="stylesheet/scss" lang="scss" scoped> .el-dialog{ width:700px } .el-select{ width: 100%; } </style>