tpupload.js
4.28 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
// components/tpupload/tpupload.js
import util from '../../utils/util'
import App from '../../utils/api'
Component({
/**
* 组件的属性列表
*/
properties: {
uploadDesc: String,
tpKey: String,
tpNum: Number,
files: Array,
},
/**
* 组件的初始数据
*/
data: {
files: [],
filesHttps: [],
token: '',
},
lifetimes: {
attached: function () {},
detached: function () {},
},
pageLifetimes: {
show() {
App.Qiniu.GetUploadToken().then(res => {
let token = res.data.token
this.setData({
token
})
})
}
},
/**
* 组件的方法列表
*/
methods: {
// 删除图片或者视频
deleteFun(e) {
var files = this.data.files;
var that = this;
var index = e.currentTarget.dataset.index;
let filesHttps = this.data.filesHttps;
util.deletelUpload(filesHttps[index].url, this.data.token, (res => {
filesHttps.splice(index, 1)
files.splice(index, 1)
this.setData({
filesHttps,
files
})
}))
that.triggerEvent('tpupload', {
files,
filesHttps
})
},
// 预览图片
previewImg(e) {
var index = e.currentTarget.dataset.index;
var files = this.data.files;
let arr = []
files.forEach(i => {
arr.push(i.path)
})
wx.previewImage({
current: files[index].path,
urls: arr
})
},
// 选择视频 上传视频 chooseMedia方法一次只能上传一个视频
uploadVideos() {
var that = this;
var files = that.data.files;
wx.chooseMedia({
mediaType: ['video'],
count: 1, // 默认9
sourceType: ['camera', 'album'],
success: function (res) {
var tempFilePaths = res.tempFiles;
if (files.length + tempFilePaths.length > that.data.tpNum) {
wx.showToast({
icon: 'none',
title: "最多可上传" + that.data.tpNum + "张"
})
return
}
let arr = []
for (var i = 0; i < tempFilePaths.length; i++) {
arr.push(tempFilePaths[i].tempFilePath);
}
that.addFun('videoNew', arr, 'video', tempFilePaths)
}
})
},
// 上传文件或者视频
// path:上传到七牛的路径名称
// arr:要上传的文件
// type:video或者image
// tempFilePaths:上传成功才能给页面加上传的图片或者视频
addFun(path, arr, type, tempFilePaths) {
var that = this;
var files = that.data.files;
var filesHttps = that.data.filesHttps;
let uploadPromises = arr.map(i => {
return util.uploadFile(i, this.data.token, path).then(res => {
return res;
});
});
Promise.all(uploadPromises).then(results => {
results.forEach(i => {
filesHttps.push({
url: i,
avinfo: type == 'video' ? true : false
})
})
for (var i = 0; i < tempFilePaths.length; i++) {
files.push({
path: tempFilePaths[i].tempFilePath,
type
});
}
that.triggerEvent('tpupload', {
files,
filesHttps
})
that.setData({
filesHttps,
files
});
});
},
// 选择图片 上传图片
uploadImages() {
var that = this;
var files = that.data.files;
wx.chooseMedia({
mediaType: ['image'],
count: this.data.tpNum, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var tempFilePaths = res.tempFiles;
if (files.length + tempFilePaths.length > that.data.tpNum) {
wx.showToast({
icon: 'none',
title: "最多可上传" + that.data.tpNum + "张"
})
return
}
let arr = []
for (var i = 0; i < tempFilePaths.length; i++) {
arr.push(tempFilePaths[i].tempFilePath);
}
that.addFun('imagesNew', arr, 'image', tempFilePaths)
}
});
},
}
})