clarms-plugins-upload.js
7.37 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import api from '@/api/api'
import {
httpGet,
httpPost,
formdata
} from '@/api/fetch-api.js'
import Vue from 'vue';
import {
Loading
} from 'vant';
Vue.use(Loading);
export default {
props: {
// 是否显示组件
options: {
type: Object,
default() {
return {
// 名称
name: "",
// 图标编号
icon: "",
// 主单证类型
imageMainTypeID: "",
// 副单证类型
imageTypeID: "",
// toast消息
toast: "",
};
}
},
icon: {
type: String,
default: ""
}
},
data() {
return {
uploadFiles: 0,
images: [],
tipsVisible: false,
}
},
components: {},
computed: {
uploading() {
let len = this.images.length;
if (len < 0) {
return false;
}
for (let index = 0; index < len; index++) {
let d = this.images[index] || null;
if (!d) {
continue;
}
if (d.intervial) {
return true;
}
}
return false;
},
lan() {
return this.$i18n.locale;
},
i18n() {
return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
},
},
methods: {
initData() {
for (let index = 0; index < sessionStorage.length; index++) {
let key = sessionStorage.key(index);
if (key.indexOf("clarmImage." + this.options.imageTypeID) == 0) {
let str = sessionStorage.getItem(key);
sessionStorage.removeItem(key);
try {
let d = JSON.parse(str);
d.showMask = false;
this.images.push(d);
} catch (e) {
console.error(e);
}
}
}
this.uploadFiles = this.images.length;
},
cacheData() {
for (let index = 0; index < this.images.length; index++) {
let tmp = this.images[index];
if (tmp.showMask || !tmp.cacheKey) { continue; }
let key = "clarmImage." + this.options.imageTypeID + "." + index;
sessionStorage.setItem(key, JSON.stringify({
fileName: tmp.fileName,
data: tmp.data,
cacheKey: tmp.cacheKey
}));
}
},
clearCache() {
for (let index = 0; index < sessionStorage.length; index++) {
let key = sessionStorage.key(index);
if (key.indexOf("clarmImage." + this.options.imageTypeID) == 0) {
sessionStorage.removeItem(key);
}
}
this.uploadFiles = 0;
this.images = [];
},
selectMutilFile() {
if (this.uploading) {
return;
}
this.$emit("beforeUpload", {
type: this.options.imageTypeID
});
console.log("options:", this.options);
let _this = this;
let input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("multiple", "multiple");
// input.setAttribute("accept", "image/*");
input.setAttribute("accept", "image/png,image/jpeg");
input.onchange = function (val) {
let position = _this.images.length;
for (let index = 0; index < input.files.length; index++) {
setTimeout(function () {
_this.initFileUpload(input.files[index], position + index);
}, index * 50);
};
};
input.click();
},
initFileUpload(file, index) {
let _this = this;
var reader = new FileReader();
reader.onload = function (e) {
_this.compressImage(reader.result, function (data) {
let d = {
fileName: "",
file: file,
data: data,
cacheKey: "",
showMask: false,
key: _this.randomAesKey(),
intervial: false,
tips: "",
err: ""
};
_this.$set(_this.images, index, d);
_this.handleFileUpload(d, index);
});
}
reader.readAsDataURL(file);
},
handleFileUpload(item, index) {
item.intervial = setInterval(function () {
item.tips = item.tips + ".";
if (item.tips.length > 3) {
item.tips = "";
}
}, 1000);
this.$set(this, "images", this.images);
let param = {
file: item.file,
key: item.key
}
formdata({
url: api.uploadClarmsImage,
data: param
}).then(res => {
if (res && res.content && res.content.id) {
item.fileName = res.content.fileName;
item.cacheKey = res.content.id;
clearInterval(item.intervial);
item.intervial = false;
this.$set(this, "images", this.images);
this.refreshUploadNumber();
} else {
clearInterval(item.intervial);
item.intervial = false;
item.err = this.lan == 'en' ? "Upload Failed" : this.lan == 'tc' ? "失敗" : "失败";
this.$set(this, "images", this.images);
this.refreshUploadNumber();
}
}).catch(err => {
clearInterval(item.intervial);
item.intervial = false;
item.err = this.lan == 'en' ? "Upload Failed" : this.lan == 'tc' ? "失敗" : "失败";
this.$set(this, "images", this.images);
this.refreshUploadNumber();
});
},
refreshUploadNumber() {
let d = this.images.length;
let number = 0;
for (let index = 0; index < d; index++) {
let v = this.images[index] || null;
if (v && v.cacheKey) {
number++;
}
}
this.uploadFiles = number;
this.emitResult();
},
onOverHandler(event, item, index) {
item.showMask = true;
event.preventDefault();
},
onOutHandler(event, item, index) {
item.showMask = false;
event.preventDefault();
},
removeItem(index) {
let images = JSON.parse(JSON.stringify(this.images));
images.splice(index, 1);
this.$set(this, "images", images);
this.refreshUploadNumber();
},
randomAesKey() {
let chars = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(",");
let res = "";
for (let i = 0; i < 16; i++) {
let id = Math.ceil(Math.random() * (chars.length - 1));
res += chars[id];
}
return res;
},
emitResult() {
if (this.images.length == 0) {
let result = {
type: this.options.imageTypeID,
list: []
};
this.$emit("success", result);
return;
}
if (this.uploading) {
return;
}
let list = [];
let len = this.images.length;
if (len < 0) {
return false;
}
for (let index = 0; index < len; index++) {
let d = this.images[index] || null;
if (!d || !d.cacheKey) {
continue;
}
// 结果
let res = {
cloudStorageID: d.cacheKey,
imageFileName: d.fileName,
imageMainTypeID: this.options.imageMainTypeID,
imageTypeID: this.options.imageTypeID
}
list.push(res);
}
let result = {
type: this.options.imageTypeID,
list: list
};
this.$emit("success", result);
},
onShowTipsOverHandler(event) {
this.tipsVisible = true;
},
onShowTipsOutHandler(event) {
this.tipsVisible = false;
},
compressImage(src, callback) {
var img = new Image();
img.src = src;
img.onload = function () {
var that = this;
// 默认按比例压缩
var w = img.width,
h = img.height,
scale = w / h;
w = w < 50 ? w : 50;
h = (w / scale);
var quality = 0.7; // 默认图片质量为0.7
//生成canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 创建属性节点
var anw = document.createAttribute("width");
anw.nodeValue = w;
var anh = document.createAttribute("height");
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(that, 0, 0, w, h);
// quality值越小,所绘制出的图像越模糊
var base64 = canvas.toDataURL('image/*', quality);
// console.log(base64.length);
// 回调函数返回base64的值
callback(base64);
}
},
datePickCompleteHandler() {
}
},
watch: {
},
mounted() {
this.initData();
},
created() { }
}