zouyu
2023-11-17 d8ac6057eaad648687699e25a575f3b7b8c1b102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<template>
  <div>
    <template v-if="indexInfo">
      <el-upload
        class="upload-demo"
        drag
        action="/mes/qualityLabelPrintConfig/uploadFile"
        :headers="headers"
        accept="image/jpg,image/png,image/jpeg,image/gif"
        :limit="1"
        :auto-upload="true"
        :on-exceed="exceedFile"
        :file-list="fileList"
        :on-success="uploadSuccess"
        :with-credentials="true"
        :on-remove="handleRemove"
        :on-preview="handlePreview"
        :before-upload="submitUpload"
        :data="paramData"
        list-type="picture"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      </el-upload>
    </template>
    <template v-else>
      <div style="padding:30px 0;font-size:14px">
        请先选择检测类别
      </div>
    </template>
  </div>
</template>
 
<script>
import { getObj, putObj } from '@/api/print/config'
import { mapGetters } from 'vuex'
import { getStore } from '@/util/store.js'
 
export default {
  props: {
    indexInfo: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      headers: {
        Authorization: 'Bearer ' + getStore({ name: 'access_token' })
      },
      paramData: {},
      fileList: []
    }
  },
  components: {},
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {},
  watch: {
    indexInfo: {
      deep: true,
      handler(newVal) {
        this.fileList = []
        if (newVal && newVal.fileName) {
          const fileEl = {}
          fileEl.name = newVal.fileName
          fileEl.url = '/mes/document/file/' + newVal.fileName
          fileEl.id = 1
          fileEl.fileName = newVal.fileName
          this.fileList.push(fileEl)
        }
      }
    }
  },
  methods: {
    // 附件上传下载
    submitUpload() {
      this.paramData.printConfigId = this.indexInfo.id
    },
    getFileList() {
      this.fileList = []
      getObj(this.indexInfo.id).then((response) => {
        const v = response.data.data
        if (v && v.fileName) {
          const fileEl = {}
          fileEl.name = v.fileName
          fileEl.url = '/mes/document/file/' + v.fileName
          fileEl.id = 1
          fileEl.fileName = v.fileName
          this.fileList.push(fileEl)
        }
      })
    },
    // 上传成功给newFileList
    uploadSuccess(response, file, fileList) {
      this.getFileList()
    },
 
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: '只能选择1个文件'
      })
    },
    handlePreview(file) {
      window.open(file.url)
    },
    handleRemove(file, fileList) {
      putObj(
        Object.assign(this.indexInfo, {
          fileName: ''
        })
      ).then((data) => {
        if (data.data.code !== 0) {
          this.fileList = []
        }
      })
    }
  }
}
</script>