c-upload.vue
2.3 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
<template>
<view class="c-upload" :style="[customStyle]">
<u-upload :width="width" :height="height" ref="uUpload" name="file" :form-data="getFormData"
:action="vuex_ossUrl" @on-choose-complete="getTempImgUrl" :before-upload="beforeUpload" :auto-upload="false"
:show-progress="false" :custom-btn="customBtn" @on-success="getSuccessResult" :file-list="fileList"
:deletable="deletable" :max-size="5 * 1024 * 1024" :max-count="maxCount">
<view slot="addBtn">
<slot name="addView"></slot>
</view>
</u-upload>
</view>
</template>
<script>
export default {
name: 'c-upload',
props: {
width: {
type: [String, Number],
default: 200
},
height: {
type: [String, Number],
default: 200
},
maxCount: {
type: [String, Number],
default: 52
},
customStyle: {
type: Object,
default () {
return {};
}
},
fileList: {
type: Array,
default () {
return [];
}
},
//是否显示自定义组件
customBtn: {
type: Boolean,
default: false
},
//是否显示删除按钮
deletable: {
type: Boolean,
default: false
},
},
data() {
return {
filePath: '',
getFormData: {},
}
},
watch: {
filePath(val, oldVal) {
this.updateFormData()
},
getFormData(val, oldVal) {
setTimeout(() => {
this.$refs.uUpload.upload();
}, 500)
},
},
computed: {
},
methods: {
getTempImgUrl(list, index) {
this.filePath = list[0].file.path
},
beforeUpload(index, list) {
// 只上传偶数索引的文件
console.log('beforeUpload', index, list[0])
},
updateFormData() {
this.$u.api.getOssInit({
"object_type": "resume_head_image"
}).then(res => {
this.getFormData = Object.assign({}, {
'key': '',
'OSSAccessKeyId': res.access_key_id,
'policy': res.policy,
'signature': res.signature,
'callback': res.callback_body,
'x:access_token': res.callback_token,
'success_action_status': '200',
}, {
key: this.filePath
})
})
},
getSuccessResult(data, index, lists, name) {
this.$emit("before-upload-parent", [data])
}
}
}
</script>
<style lang="scss" scoped>
</style>