upload.vue
2.84 KB
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
<template>
<CInput ref="label" :label="label">
<slot>
<div @click="showImagePreview">
<Upload
:max="max"
:multiple="multiple"
:action="action"
:simultaneous-uploads="1"
:value="file"
@files-added="filesAdded"
@file-submitted="fileSubmit"
@file-error="fileError"
@file-success="fileSuccess"
@file-removed="fileRemove"
>
</Upload>
</div>
</slot>
</CInput>
</template>
<script>
import Input from './input';
import {Upload} from 'cube-ui';
import {createNamespacedHelpers} from 'vuex';
const {mapState, mapActions, mapMutations} = createNamespacedHelpers('license/form');
export default {
name: 'CUpload',
props: {
value: {
type: Array,
default() {
return [];
}
},
label: {
type: String
}
},
data() {
return {
max: 1,
multiple: false,
action: {
target: 'http://upload.qiniup.com/',
data: {
token: '',
key: ''
}
},
file: this.value
};
},
methods: {
fileSubmit(file) {
this.action.data.token = this.token;
this.action.data.key = '/license/' + Date.now() + '/' + file.name;
},
filesAdded(files) {
let hasIgnore = false;
const maxSize = 10 * 1024 * 1024; // 10M
for (let file of files) {
if (file.size > maxSize) {
file.ignore = true;
hasIgnore = true;
}
}
hasIgnore && this.$createToast({
type: 'warn',
time: 1000,
txt: '上传文件大于10M'
}).show();
},
fileError() {
this.$createToast({
txt: '上传失败,请重新上传',
type: 'error',
mask: true,
time: 2000
}).show();
},
validate() {
if (!this.value.length) {
this.$refs.label.setShowError();
return false;
} else {
this.$refs.label.setShowError(false);
return true;
}
},
showImagePreview() {
if (this.value && this.value[0] && this.value[0].response && this.value[0].response.key) {
this.$createImagePreview({
imgs: ['//img01.yohoboys.com/' + this.value[0].response.key + '?imageView2/2/w/450/q/20']
}).show();
}
},
fileSuccess(file) {
file.name = file.response.key;
this.$emit('file-change');
},
fileRemove(file) {
this.$emit('file-change');
}
},
computed: {
...mapState(['token'])
},
components: {
CInput: Input,
Upload
}
};
</script>
<style lang="scss" scoped>
.cube-upload {
/deep/ .cube-upload-btn {
margin: 0 0;
}
/deep/ .cube-upload-btn-def {
background-color: #eee;
}
/deep/ .cube-upload-file-status {
font-size: 30px !important;
color: black;
}
}
</style>