mrzcc
2020-03-06 ecdac8a738f254db2b2ea28cbe3248f7a6a7eb2b
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
(function (vc) {
 
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            uploadVedioInfo: {
                vedio: {},
                fileName: '',
                realFileName: '',
                progress: 0
            }
        },
        watch: {
            uploadVedioInfo: {
                deep: true,
                handler: function () {
                    vc.emit($props.callBackListener, $props.callBackFunction, this.uploadVedioInfo);
                }
            }
        },
        _initMethod: function () {
 
        },
        _initEvent: function () {
            vc.on('uploadVedio', 'openAddApplicationKeyModal', function () {
 
            });
            vc.on('uploadVedio', 'clearVedio', function () {
                this.uploadVedioInfo = {
                    vedio: {},
                    fileName: '',
                    realFileName: '',
                    progress: 0
                }
            });
            vc.on('uploadVedio', 'notifyVedio', function (_fileName) {
                this.uploadVedioInfo.fileName = _fileName;
                this.uploadVedioInfo.realFileName = _fileName;
                this.uploadVedioInfo.progress = 100;
            });
 
        },
        methods: {
            _uploadVedio: function (event) {
                $("#uploadVedio").trigger("click")
            },
            _chooseVedio: function (event) {
                var photoFiles = event.target.files;
                if (photoFiles && photoFiles.length > 0) {
                    // 获取目前上传的文件
                    var file = photoFiles[0];// 文件大小校验的动作
                    if (file.size > 1024 * 1024 * 500) {
                        vc.toast("图片大小不能超过 500MB!")
                        return false;
                    }
                    this.uploadVedioInfo.fileName = file.name;
                    this._doUploadVedio(file);
                }
            },
            _doUploadVedio: function (_file) {
                var param = new FormData();
                param.append("uploadFile", _file);
                param.append('communityId', vc.getCurrentCommunity().communityId);
 
                //发送get请求
                vc.http.upload('uploadVedio',
                    'upload',
                    param,
                    {
                        emulateJSON: true,
                        //添加请求头
                        headers: {
                            "Content-Type": "multipart/form-data"
                        },
                        progress: function (event) {
                            //console.log(event);
                            //vc.toast("视频上传中,请稍后");
                            var rate = event.loaded / event.total;  //已上传的比例
                            if (rate < 0.9) {
                                //这里的进度只能表明文件已经上传到后台,但是后台有没有处理完还不知道
                                //因此不能直接显示为100%,不然用户会误以为已经上传完毕,关掉浏览器的话就可能导致上传失败
                                //等响应回来时,再将进度设为100%
                                this.uploadVedioInfo.progress = (rate * 100).toFixed(2);
                            }
                        }
                    },
                    function (json, res) {
                        if (res.status != 200) {
                            vc.toast("上传文件失败");
                            return;
                        }
                        var _json = JSON.parse(json);
                        this.uploadVedioInfo.progress = 100.00;
                        vc.toast("视频上传成功");
 
                        this.uploadVedioInfo.fileName = _json.fileName;
                        this.uploadVedioInfo.realFileName = _json.realFileName;
                        vc.emit($props.callBackListener, $props.callBackFunction, _json);
                    }, function (errInfo, error) {
                        console.log('请求失败处理', error);
                        vc.toast("上传视频失败");
                    });
            },
        }
    });
 
})(window.vc);