whyczyk
2021-08-31 ba45f770d3439d8a23b89ffdaf5b4c8dcdbfa521
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
<template>
  <van-uploader :after-read="afterRead" v-model="getFileList" accept="image/png, image/jpeg"
    :max-size="10 * 1024 * 1024" @oversize="oversize" :disabled="disabled" :readonly="readonly" />
</template>
 
<script>
  import {
    uploadAlarmFile
  } from '@/assets/js/api'
  export default {
    name: "v-upload",
    props: {
      value: {
        type: String,
        default: ""
      },
      disabled: {
        type: Boolean,
        default: false
      },
      readonly: {
        type: Boolean,
        default: false
      },
      afterOrBefore: {
        type: String,
        default: 'before'
      }
    },
    data() {
      return {
        fileList: this.value
      }
    },
    model: {
      prop: "value",
      event: "valChange"
    },
    watch: {
      value(newValue) {
        this.fileList = newValue;
      },
      fileList(newValue) {
        this.$emit("valChange", newValue);
      }
    },
    computed: {
      getFileList: {
        get() {
          if (this.fileList) {
            return this.fileList.split(",").map(item => {
              return {
                url: item
              };
            });
          }
          return [];
        },
        set(newValue) {
          var fileList = [];
          newValue.forEach(item => {
            if (item.url) {
              fileList.push(item.url);
            }
          });
          this.fileList = fileList.join(",");
        }
      }
    },
    methods: {
      // 上传图片
      afterRead(file) {
        var formData = new FormData();
        let json = JSON.stringify({
          afterOrBefore: this.afterOrBefore
        })
        formData.append("file", file.file);
        formData.append("json", json);
        //上传图片接口
        uploadAlarmFile(formData).then(res => {
          let resData = JSON.parse(res.data.result);
          if (resData.code == 1) {
            if (this.fileList) {
              var arr = this.fileList.split(",");
              arr.push(resData.data[0]);
              this.fileList = arr.join(",");
              return false;
            }
            this.fileList = resData.data[0];
          }
        });
      },
      // 文件过大
      oversize(e) {
        this.$toast.fail("图片过大!");
      }
    }
  }
</script>
 
<style scoped>
</style>