王飞
2025-01-23 99365608dec6eade7d645a91fb0f2205a332d1f1
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
var util = require('../../lib/util');
var config = require('../../config');
var cos = require('../../lib/cos');
 
Page({
    onLoad: function () {
    },
    onShareAppMessage: function (res) {
        return {
            title: 'COS 上传示例',
            path: this.route,
        }
    },
    // 前往相册页
    uploadImage() {
        wx.chooseImage({
            count: 1,
            camera: 'back',
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                var filePath = res.tempFilePaths[0];
                if (filePath) {
                    var Key = util.getRandFileName(filePath);
                    wx.showLoading({title: '正在上传...'});
                    cos.postObject({
                        Bucket: config.Bucket,
                        Region: config.Region,
                        Key: Key,
                        FilePath: filePath,
                    }, function (err, data) {
                        wx.hideLoading();
                        if (data && data.Location) {
                            wx.navigateTo({url: '../preview/preview?type=image&url=' + encodeURIComponent('https://' + data.Location)});
                        } else {
                            wx.showToast({title: '上传失败', icon: 'error', duration: 2000});
                        }
                    });
                }
            }
        })
    },
    // 前往相册页
    uploadVideo() {
        wx.chooseVideo({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            maxDuration: 60,
            camera: 'back',
            success: function (res) {
                var filePath = res.tempFilePath;
                if (filePath) {
                    var Key = util.getRandFileName(filePath);
                    wx.showLoading({title: '正在上传...'});
                    cos.postObject({
                        Bucket: config.Bucket,
                        Region: config.Region,
                        Key: Key,
                        FilePath: filePath,
                    }, function (err, data) {
                        wx.hideLoading();
                        if (data && data.Location) {
                            wx.navigateTo({url: '../preview/preview?type=video&url=' + encodeURIComponent('https://' + data.Location)});
                        } else {
                            wx.showToast({title: '上传失败', icon: 'error', duration: 2000});
                        }
                    });
                }
            }
        });
    },
    // 前往相册页
    gotoAlbum() {
        wx.navigateTo({url: '../album/album'});
    },
});