-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-gallery.js
209 lines (209 loc) · 4.34 KB
/
vue-gallery.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 依赖vue iview axios lodash
(function () {
if (Vue) {
Vue.component('n-gallery', {
render: function (h) {
const imageWrapper = []
for (let i = 0; i < this.imageList.length; i++) {
imageWrapper.push(h('img', {
attrs: {
class: 'pic',
src: this.imageList[i].image_url
}
}))
}
const selectedImageWrapper = []
for (let i = 0; i < this.selected.length; i++) {
selectedImageWrapper.push(h('img', {
attrs: {
class: 'pic',
src: this.selected[i]
}
}))
}
return h('Modal', {
attrs: {
class: 'n-gallery'
},
props: {
'mask-closable': false,
'z-index': 1040,
width: 798,
value: this.value,
title: '图库'
},
on: {
input: (v) => {
this.$emit('input', v)
},
'on-ok': this.confirm,
'on-cancel': this.cancel
}
}, [
h('upload', {
props: {
'before-upload': this.upload
}
}, [
h('i-button', {
props: {
icon: 'ios-cloud-upload-outline'
}
}, '批量上传图片')
]),
h('Row', {
props: {
type: 'flex',
justify: 'space-between'
}
}, [
h('div', [
h('Row', {
style: {
width: '550px'
},
props: {
type: 'flex'
},
nativeOn: {
click: this.selectImage
}
}, imageWrapper),
h('page', {
props: {
'show-total': true,
current: this.currentPage,
total: this.total,
'page-size': 20
},
on: {
'on-change': this.pageChange
}
})
]),
h('div', {
style: {
width: '200px'
}
}, [
h('div', `已选择图片(${this.selected.length}/${this.selectedLimit})`),
h('div', {
attrs: {
class: 'sp_category_attribute_tip'
}
}, '(点击下面图片可删除)'),
h('div', {
style: {
'overflow-y': 'scroll',
height: '440px'
},
on: {
click: this.deleteImage
}
}, selectedImageWrapper)
])
])
])
},
props: {
value: {
type: Boolean,
default: false
},
isUpload: {
type: Boolean,
default: false
},
uploadType: {
type: Number,
default: 0
},
selectedLimit: {
type: Number,
default: 10
}
},
data () {
return {
imageList: [],
selectedMap: {},
currentPage: 1,
total: 0
}
},
computed: {
selected () {
const res = []
for (let k in this.selectedMap) {
if (this.selectedMap[k]) {
res.push(k)
}
}
return res
}
},
mounted () {
this.getPicList()
},
methods: {
getPicList (currentPage = 1) {
const data = new FormData()
data.append('page_num', currentPage)
data.append('limit', 20)
axios.post('***', data)
.then(res => {
this.total = res.data.total
this.imageList = res.data.data
})
},
confirm () {
if (this.selected.length > 0) {
this.$emit('on-select-change', _.cloneDeep(this.selected))
this.cancel()
}
},
cancel () {
this.selectedMap = {}
},
selectImage (e) {
if (e.target.src && this.selected.length < this.selectedLimit) {
this.$set(this.selectedMap, e.target.src, true)
} else {
this.$Message.info('请先删除一张已选择图片再添加')
}
},
deleteImage (e) {
console.log(e)
if (e.target.src && this.selected.length > 0) {
this.$delete(this.selectedMap, e.target.src)
}
},
uploadPic (e) {
if (e) {
var files = e.target.files || e.dataTransfer.files || e.dataTransfer.getData;
var data = new FormData()
for (var i = 0; i < files.length; i++) {
data.append("files[]", files[i]);
}
data.append('type', this.uploadType)
axios.post('***', data)
.then(res => {
this.$Messgae.success('上传成功')
this.getList()
})
.catch(error => {
this.$Messgae.success('上传失败')
})
}
return false
},
pageChange (page) {
this.currentPage = page
this.getPicList(page)
}
}
})
} else {
new Error('Vue has not defined')
}
})()