whyczyk
2021-09-14 391214149f8edfc2ce9ff995cf37f06e962fd946
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<template>
    <van-uploader
        :after-read="afterRead"
        :before-delete="beforeDelete"
        v-model="getFileList"
        accept="image/png, image/jpeg"
        :max-size="10 * 1024 * 1024"
        @oversize="oversize"
        :disabled="disabled"
        :readonly="readonly"
    />
</template>
 
<script>
import { uploadAlarmFile, deleteAlarmFile } 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",
        },
        stationId: {
            type: Number,
            default: 0,
        },
    },
    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) => {
                        console.log(this.$units.getStationSrc(item))
                        return {
                            url: this.$units.getStationSrc(item),
                            url1: item,
                        }
                    })
                }
                return []
            },
            set(newValue) {
                var fileList = []
                newValue.forEach((item) => {
                    if (item.url1) {
                        fileList.push(item.url1)
                    }
                })
                this.fileList = fileList.join(",")
            },
        },
    },
    methods: {
        // 上传图片
        afterRead(file) {
            var formData = new FormData()
            let json = JSON.stringify({
                afterOrBefore: this.afterOrBefore,
                stationId: this.stationId,
            })
            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]
                }
            })
        },
        // 删除图片
        beforeDelete(file, detail) {
            let fileName = file.url1.split("/")[file.url1.split("/").length - 1]
            let postData = {
                filePath: this.stationId,
                fileName: fileName,
                fileType: this.afterOrBefore,
            }
            deleteAlarmFile(postData)
                .then((res) => {
                    let resData = JSON.parse(res.data.result)
                    if (resData.code == 1) {
                        var arr = this.fileList.split(",")
                        arr.splice(detail.index, 1)
                        this.fileList = arr.join(",")
                    } else {
                        this.$toast(resData.msg)
                        return false
                    }
                })
                .catch((err) => {
                    this.$toast("删除文件失败!")
                    console.log(err)
                    return false
                })
        },
        // 文件过大
        oversize(e) {
            this.$toast.fail("图片过大!")
        },
    },
}
</script>
 
<style scoped>
</style>