-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
134 lines (131 loc) · 3.61 KB
/
index.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
const VodUploader = require("../../lib/vod-wx-sdk-v2.js");
const app = getApp();
Page({
data: {
fileName: "",
videoFile: null,
coverFile: null,
progress: 0,
uploader: null,
},
reset() {
this.setData({
fileName: "",
videoFile: null,
coverFile: null,
progress: 0,
uploader: null,
})
},
// 1. Demo上传之前建议在本地设置中勾选不校验合法域名,https证书选项
// 2. 如果遇到请求报某个域名不在合法域名列表, 可以参考https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html 在小程序开发者平台配置域名请求白名单
// 3. callback是一个匿名的异步回调,用于向sdk返回一个签名字符串,所以此参数必须存在
getSignature: function (callback) {
wx.request({
url: 'https://demo.vod2.myqcloud.com/ugc-upload/',
method: 'POST',
data: {
Action: 'GetUgcUploadSign'
},
dataType: 'json',
success: function (res) {
if (res.data && res.data.data.sign) {
callback(res.data.data.sign);
} else {
return '获取签名失败';
}
}
});
},
inputChange: function(evt) {
this.setData({
fileName: evt.detail.value
});
},
chooseVideo: function() {
const self = this;
wx.chooseMedia({
sourceType: ["album", "camera"],
sizeType: ["original"],
maxDuration: 60,
success: function(res) {
console.log(res.tempFiles);
self.setData({
videoFile: res.tempFiles[0]
});
console.log(`add videoFile`, res.tempFiles[0]);
}
});
},
chooseCover() {
const self = this;
wx.chooseMedia({
sourceType: ["album", "camera"],
sizeType: ["original"],
count: 1,
mediaType: ["image"],
success: function(res) {
console.log(res.tempFiles);
self.setData({
coverFile: res.tempFiles[0]
});
console.log(`add coverFile`, res.tempFiles[0]);
}
});
},
startUpload() {
wx.showLoading({
title: '处理中',
mask: true,
})
const self = this;
const uploader = VodUploader.start({
mediaFile: self.data.videoFile, //必填,把chooseVideo回调的参数(file)传进来
getSignature: self.getSignature, //必填,获取签名的函数
mediaName: self.data.fileName, //选填,视频名称,强烈推荐填写(如果不填,则默认为“来自小程序”)
coverFile: self.data.coverFile, // 选填,视频封面
error: function(result) {
console.log("error");
console.log(result);
wx.hideLoading();
wx.showModal({
title: "上传失败",
content: JSON.stringify(result),
showCancel: false
});
},
progress: function(result) {
console.log("progress");
console.log(result);
wx.hideLoading();
self.setData({
progress: parseInt(result.percent * 100)
})
// wx.showLoading({
// title: "上传中 " + result.percent * 100 + "%"
// });
},
finish: function(result) {
console.log("finish");
console.log(result);
wx.hideLoading();
wx.showModal({
title: "上传成功",
content:
"fileId:" + result.fileId + "\nvideoName:" + result.videoName,
showCancel: false
});
self.reset();
}
});
this.setData({
uploader: uploader,
})
},
cancelUpload() {
this.data.uploader.cancel();
},
resumeUpload() {
this.data.uploader.start();
}
});