123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <template>
- <el-dialog :close-on-click-modal="false" :title="title" :type="type" :visible.sync="isVisible" :width="width" @dragDialog="handleDrag" top="50px" v-el-drag-dialog>
- <el-form :model="attachment" :rules="rules" label-position="right" label-width="100px" ref="form">
- <el-form-item :label="$t('table.attachment.bizId')" prop="bizId">
- <el-input v-model="attachment.bizId" />
- </el-form-item>
- <el-form-item :label="$t('table.attachment.bizType')" prop="bizType">
- <el-input v-model="attachment.bizType" />
- </el-form-item>
- <el-form-item label="文件" prop="fileLength">
- <fileUpload :acceptSize="2*1024*1024" :auto-upload="false" :limit="5" @fileLengthVaild="fileLengthVaild" @setId="setIdAndSubmit" ref="fileRef">
- <el-button size="small" slot="trigger" type="primary">选取文件</el-button>
- <div class="el-upload__tip" slot="tip">文件不超过2MB</div>
- </fileUpload>
- </el-form-item>
- </el-form>
- <div class="dialog-footer" slot="footer">
- <el-button @click="isVisible = false" plain type="warning">{{ $t('common.cancel') }}</el-button>
- <el-button :disabled="disabled" @click="submitForm" plain type="primary">{{ $t('common.confirm') }}</el-button>
- </div>
- </el-dialog>
- </template>
- <script>
- import elDragDialog from '@/directive/el-drag-dialog'
- import fileUpload from "@/components/zuihou/fileUpload"
- export default {
- name: 'AttachmentEdit',
- directives: { elDragDialog, fileUpload },
- components: { fileUpload },
- props: {
- dialogVisible: {
- type: Boolean,
- default: false
- },
- type: {
- type: String,
- default: 'add'
- }
- },
- data () {
- return {
- accept: "image/jpeg, image/gif, image/png",
- attachment: this.initAttachment(),
- screenWidth: 0,
- width: this.initWidth(),
- fileLength: 0,
- disabled: false,
- rules: {
- bizType: [
- { required: true, message: this.$t('rules.require'), trigger: 'blur' },
- { min: 0, max: 255, message: this.$t('rules.range0to255'), trigger: 'blur' },
- ],
- bizId: { min: 0, max: 255, message: this.$t('rules.range0to255'), trigger: 'blur' },
- fileLength: {
- required: true, trigger: "change",
- validator: (rule, value, callback) => {
- const vm = this;
- if (vm.fileLength === 0) {
- callback(new Error("请上传文件"))
- } else if (vm.fileLength > 5) {
- callback(new Error("一次性只能上传5个文件"))
- } else {
- callback()
- }
- }
- }
- }
- }
- },
- computed: {
- isVisible: {
- get () {
- return this.dialogVisible
- },
- set () {
- this.close()
- this.reset()
- }
- },
- title () {
- return this.$t('common.upload')
- }
- },
- watch: {
- },
- mounted () {
- window.onresize = () => {
- return (() => {
- this.width = this.initWidth()
- })()
- }
- },
- methods: {
- initAttachment () {
- return {
- id: '',
- bizId: '',
- bizType: '',
- file: null,
- isSingle: false
- }
- },
- handleDrag () {
- console.log(`我被拖动了`)
- },
- // 附件长度校验
- fileLengthVaild (data) {
- const vm = this;
- vm.fileLength = data || 0;
- },
- initWidth () {
- this.screenWidth = document.body.clientWidth
- if (this.screenWidth < 991) {
- return '90%'
- } else if (this.screenWidth < 1400) {
- return '45%'
- } else {
- return '800px'
- }
- },
- setAttachment (val) {
- const vm = this
- if (val) {
- vm.attachment = { ...val }
- }
- },
- close () {
- this.$emit('close')
- },
- reset () {
- // 先清除校验,再清除表单,不然有奇怪的bug
- this.$refs.form.clearValidate()
- this.$refs.form.resetFields()
- this.attachment = this.initAttachment()
- this.$refs.fileRef.init({
- id: "",
- bizId: "",
- bizType: "",
- })
- },
- submitForm () {
- const vm = this
- this.$refs.form.validate((valid) => {
- if (valid) {
- vm.editSubmit()
- } else {
- return false
- }
- })
- },
- editSubmit () {
- const vm = this
- vm.disabled = true
- vm.$refs.fileRef.submitFile(this.attachment.id, this.attachment.bizId, this.attachment.bizType)
- },
- setIdAndSubmit (isUploadCompleted) {
- const vm = this
- if (isUploadCompleted) {
- vm.disabled = false
- vm.isVisible = false
- vm.$message({
- message: vm.$t('tips.createSuccess'),
- type: 'success'
- })
- vm.$emit('success')
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- </style>
|