<template> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body> <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="130px"> <el-row :gutter="20" v-if="dialogStatus === 'detail'"> <el-col :span="20" :offset="2"> <el-form-item label="编号" prop="code"> <el-input v-model.trim="form.code" :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="姓名"> <el-input v-model.trim="form.name" :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="picture"> <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="加入黑名单原因" prop="blackReason"> <el-select v-model="form.blackReason" size="small" :disabled="isEditMode" placeholder="必填" clearable> <el-option v-for="item in reasonList" :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="备注"> <el-input v-model.trim="form.remarks" :disabled="isEditMode" :rows="3" type="textarea" 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 { addBlack, updateBlack } from '@/api/black' import { getDictByCode } from '@/api/system/dict' export default { name: 'EditBlack', data() { 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: '', code: '', name: '', picture:'', blackReason:'', remarks:'' }, // 表单 textMap: { update: '编辑黑名单', create: '新增黑名单', detail: '黑名单详情' }, // 表头显示标题 btnLoading: false, // 保存按钮的加载中状态 reasonList:[], rules: { code: [{ required: true, message: '编号不能为空', trigger: ['blur', 'change'] }], blackReason: [{ required: true, message: '加入黑名单原因不能为空', trigger: ['blur', 'change'] }], picture : [{ required: true, message: '图片不能为空', trigger: ['blur', 'change'] }] } // 前端校验规则 } }, computed: { }, created() { this.fetchOptions() }, 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) } }) }, fetchOptions() { getDictByCode('blackReason').then(response => { if (response.code === 200) { this.reasonList = 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, name: row.name, picture:row.picture, blackReason:row.blackReason, remarks:row.remarks } this.isEditMode = false }else if (dialogStatus === 'detail') { this.form = { id: row.id, name: row.name, picture:row.picture, blackReason:row.blackReason, remarks:row.remarks } this.isEditMode = true } }, // 重置表单 resetForm() { this.form = { id: '', code: '', name: '', picture:'', blackReason:'', remarks:'' } }, // 保存数据 saveData: function() { console.log(this.form) 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 addBlack(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 updateBlack(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>