Edit.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <el-dialog :close-on-click-modal="false" :title="title" :type="type" :visible.sync="isVisible" :width="width" @dragDialog="handleDrag" top="50px" v-el-drag-dialog>
  3. <el-form :model="attachment" :rules="rules" label-position="right" label-width="100px" ref="form">
  4. <el-form-item :label="$t('table.attachment.bizId')" prop="bizId">
  5. <el-input v-model="attachment.bizId" />
  6. </el-form-item>
  7. <el-form-item :label="$t('table.attachment.bizType')" prop="bizType">
  8. <el-input v-model="attachment.bizType" />
  9. </el-form-item>
  10. <el-form-item label="文件" prop="fileLength">
  11. <fileUpload :acceptSize="2*1024*1024" :auto-upload="false" :limit="5" @fileLengthVaild="fileLengthVaild" @setId="setIdAndSubmit" ref="fileRef">
  12. <el-button size="small" slot="trigger" type="primary">选取文件</el-button>
  13. <div class="el-upload__tip" slot="tip">文件不超过2MB</div>
  14. </fileUpload>
  15. </el-form-item>
  16. </el-form>
  17. <div class="dialog-footer" slot="footer">
  18. <el-button @click="isVisible = false" plain type="warning">{{ $t('common.cancel') }}</el-button>
  19. <el-button :disabled="disabled" @click="submitForm" plain type="primary">{{ $t('common.confirm') }}</el-button>
  20. </div>
  21. </el-dialog>
  22. </template>
  23. <script>
  24. import elDragDialog from '@/directive/el-drag-dialog'
  25. import fileUpload from "@/components/zuihou/fileUpload"
  26. export default {
  27. name: 'AttachmentEdit',
  28. directives: { elDragDialog, fileUpload },
  29. components: { fileUpload },
  30. props: {
  31. dialogVisible: {
  32. type: Boolean,
  33. default: false
  34. },
  35. type: {
  36. type: String,
  37. default: 'add'
  38. }
  39. },
  40. data () {
  41. return {
  42. accept: "image/jpeg, image/gif, image/png",
  43. attachment: this.initAttachment(),
  44. screenWidth: 0,
  45. width: this.initWidth(),
  46. fileLength: 0,
  47. disabled: false,
  48. rules: {
  49. bizType: [
  50. { required: true, message: this.$t('rules.require'), trigger: 'blur' },
  51. { min: 0, max: 255, message: this.$t('rules.range0to255'), trigger: 'blur' },
  52. ],
  53. bizId: { min: 0, max: 255, message: this.$t('rules.range0to255'), trigger: 'blur' },
  54. fileLength: {
  55. required: true, trigger: "change",
  56. validator: (rule, value, callback) => {
  57. const vm = this;
  58. if (vm.fileLength === 0) {
  59. callback(new Error("请上传文件"))
  60. } else if (vm.fileLength > 5) {
  61. callback(new Error("一次性只能上传5个文件"))
  62. } else {
  63. callback()
  64. }
  65. }
  66. }
  67. }
  68. }
  69. },
  70. computed: {
  71. isVisible: {
  72. get () {
  73. return this.dialogVisible
  74. },
  75. set () {
  76. this.close()
  77. this.reset()
  78. }
  79. },
  80. title () {
  81. return this.$t('common.upload')
  82. }
  83. },
  84. watch: {
  85. },
  86. mounted () {
  87. window.onresize = () => {
  88. return (() => {
  89. this.width = this.initWidth()
  90. })()
  91. }
  92. },
  93. methods: {
  94. initAttachment () {
  95. return {
  96. id: '',
  97. bizId: '',
  98. bizType: '',
  99. file: null,
  100. isSingle: false
  101. }
  102. },
  103. handleDrag () {
  104. console.log(`我被拖动了`)
  105. },
  106. // 附件长度校验
  107. fileLengthVaild (data) {
  108. const vm = this;
  109. vm.fileLength = data || 0;
  110. },
  111. initWidth () {
  112. this.screenWidth = document.body.clientWidth
  113. if (this.screenWidth < 991) {
  114. return '90%'
  115. } else if (this.screenWidth < 1400) {
  116. return '45%'
  117. } else {
  118. return '800px'
  119. }
  120. },
  121. setAttachment (val) {
  122. const vm = this
  123. if (val) {
  124. vm.attachment = { ...val }
  125. }
  126. },
  127. close () {
  128. this.$emit('close')
  129. },
  130. reset () {
  131. // 先清除校验,再清除表单,不然有奇怪的bug
  132. this.$refs.form.clearValidate()
  133. this.$refs.form.resetFields()
  134. this.attachment = this.initAttachment()
  135. this.$refs.fileRef.init({
  136. id: "",
  137. bizId: "",
  138. bizType: "",
  139. })
  140. },
  141. submitForm () {
  142. const vm = this
  143. this.$refs.form.validate((valid) => {
  144. if (valid) {
  145. vm.editSubmit()
  146. } else {
  147. return false
  148. }
  149. })
  150. },
  151. editSubmit () {
  152. const vm = this
  153. vm.disabled = true
  154. vm.$refs.fileRef.submitFile(this.attachment.id, this.attachment.bizId, this.attachment.bizType)
  155. },
  156. setIdAndSubmit (isUploadCompleted) {
  157. const vm = this
  158. if (isUploadCompleted) {
  159. vm.disabled = false
  160. vm.isVisible = false
  161. vm.$message({
  162. message: vm.$t('tips.createSuccess'),
  163. type: 'success'
  164. })
  165. vm.$emit('success')
  166. }
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. </style>