<template> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body> <el-form ref="dataForm" :rules="rules" :model="form" label-offDate="right" label-width="130px"> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="员工号" prop="staffCode"> <el-input v-model.trim="form.staffCode" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="员工名称" prop="staffName"> <el-input v-model.trim="form.staffName" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <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-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="手机号" prop="phone"> <el-input v-model.trim="form.phone" :disabled="isEditMode" type="text" placeholder="手机号" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="照片"> <div style="display: flex;"> <img :src="form.picture ? `${form.picture}`: defaultPhoto" width="150" height="150" @error="errorImg" style=" margin-top: 10px"/> <el-upload ref="upload" style="margin: 40px" v-if="dialogStatus !== 'detail'" :before-upload="handleBeforeUpload" :http-request="uploadFile" :show-file-list="false" class="avatar-uploader" action="string" accept=".jpg,.png"> <el-button size="small" icon="el-icon-folder-add">浏 览</el-button> <div slot="tip" class="el-upload__tip" style="font-size: 15px">只能上传jpg、png文件,且不超过200kb</div> </el-upload> </div> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="工作开始日期"> <el-date-picker :disabled="isEditMode" style="width: 100%" v-model="form.onDate" type="date" value-format="yyyy-MM-dd" placeholder="工作开始日期" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="工作终止日期"> <el-date-picker :disabled="isEditMode" style="width: 100%" v-model="form.offDate" type="date" value-format="yyyy-MM-dd" placeholder="工作终止日期" /> </el-form-item> </el-col> </el-row> </el-form> <div slot="footer" class="dialog-footer" v-if="!isEditMode"> <el-button :loading="btnLoading" type="primary" @click="saveData"> 保存 </el-button> <el-button @click="cancel"> 取消 </el-button> </div> </el-dialog> </template> <script> import { addStaff, updateStaff } from '@/api/person' import { getDictByCode } from '@/api/system/dict' import { 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() } return { defaultPhoto: require('@/assets/global_images/photo.png'), errorImg: require('@/assets/global_images/photo_error.png'), dialogFormVisible: false, // 对话框是否显示 dialogStatus: '', // 对话框类型:create,update isEditMode: true, form: { id: '', staffCode: '', staffName: '', phone:'', staffType:'', picture:'', onDate: '', offDate: '' }, // 表单 typeList:[], areaList:[], textMap: { update: '编辑员工', create: '新增员工', detail: '员工详情' }, // 表头显示标题 btnLoading: false, // 保存按钮的加载中状态 rules: { staffCode: [{ required: true, message: '员工号不能为空', trigger: ['blur', 'change'] }], staffName: [{ required: true, message: '员工名称不能为空', trigger: ['blur', 'change'] }], staffType : [{ required: true, message: '员工类型不能为空', trigger: ['blur', 'change'] }], phone: [{ required: false, validator: validateNum, trigger: ['blur', 'change'] }], } // 前端校验规则 } }, computed: { }, created() { this.fetchTypeList() }, methods: { // 上传前判断文件格式及大小 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 或 PNG 格式!') 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/png;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, staffName: row.staffName, phone:row.phone, staffType:row.staffType, picture:row.picture, onDate: row.onDate, offDate: row.offDate } this.isEditMode = false }else if (dialogStatus === 'detail') { this.form = { id: row.id, staffCode: row.staffCode, staffName: row.staffName, phone:row.phone, staffType:row.staffType, picture:row.picture, onDate: row.onDate, offDate: row.offDate } this.isEditMode = true } }, // 重置表单 resetForm() { this.form = { id: '', staffCode: '', staffName: '', phone:'', staffType:'', picture:'', onDate: '', offDate: '' } }, // 保存数据 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.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>