upload-file-modal.ctrl.js 5.71 KB
Newer Older
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
vatModule.controller('uploadFileModalController', ['$scope', '$log', '$translate', '$timeout', 'apiInterceptor', 'Upload', '$uibModal',
    'vatOutputInvoiceManageService',
    function ($scope, $log, $translate, $timeout, apiInterceptor, Upload, $uibModal, vatOutputInvoiceManageService) {

        var uploadFileController = function () {
            $scope.uploadOption = {};
            $scope.uploadOption.uploadFiles = null;

            $scope.confirmUpload = function () {
                $scope.uploadFileOption.files = $scope.files;
                $scope.modalInstance.dismiss('confirm');
                $scope.uploadFileType = null;

            };

            $scope.concelUpload = function () {
                $scope.modalInstance.dismiss('cancel');

                $scope.uploadFileType = null;
            };

            $scope.$watch('uploadOption.uploadFiles', function (newValue, oldValue) {
                if (newValue && newValue !== oldValue) {
                    doUploadFile();
                }
            });
        };

        $scope.chunkSize = 10 * 1024 * 1024;
        $scope.success = false;

        var resumable = true;
        var allFilesCount = 0;
        var successCount = 0;

        var updateProgressToZero = function () {
            $scope.percentNumber = '0%';
            $scope.showLoading = false;
        };

        $scope.files = [];
        var uploadSuccess = function (resp) {
            successCount++;
            $scope.files.push(resp);
            if (successCount === allFilesCount) {
                $scope.showLoading = false;
            }
        };

        var changeloadingTitle = function (title) {
            $scope.loadingTitle = $translate.instant(title);
        };

        var doUploadFile = function () {
            changeloadingTitle('fileUploading');
            uploadfile($scope.uploadOption.uploadFiles);
            $scope.uploadOption.uploadFiles = null;
        };

        //更新滚动条
        var updateProgress = function (evt) {
            $scope.showLoading = true;


            // console.log(evt.loaded + '/' + evt.total);
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);

            // 不知道为什么evt.loaded有可能比evt.total还要大---julius 2017/11/7
            if (progressPercentage > 100){
                progressPercentage = 100;
            }

            $scope.percentNumber = progressPercentage + '%';
        };

        //执行上传文件操作。
        var uploadfile = function (files) {
            if (files && files.length) {
                allFilesCount = files.length;
                successCount = 0;
                var uploadSuccessCallBack = function () {
                    updateProgressToZero();
                }
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];

                    if (!file.$error) {
                        var tempFileName = PWC.newGuid() + '.dat';
                        var token = $('input[name="__RequestVerificationToken"]').val();
                        updateProgressToZero();
                        Upload.upload({
                            url: apiInterceptor.webApiHostUrl + '/outputInvoicePrinted/uploadEvidenceFile',
                            data: {
                                cancel: false,
                                filename: file.name,
                                tempFileName: tempFileName,
                                file: file,
                                period: $scope.period
                            },
                            resumeChunkSize: resumable ? $scope.chunkSize : null,
                            headers: {
                                'Access-Control-Allow-Origin': '*',
103
                                Authorization: apiInterceptor.tokenType + ' ' + apiInterceptor.apiToken()
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
                            },
                            __RequestVerificationToken: token,
                            withCredentials: true
                        }).then(uploadSuccess, uploadSuccessCallBack, updateProgress);
                    }
                }
            }
        };

        //删除证据文件
        $scope.deleteEvidenceFile = function (fileId) {
            var fileList = [fileId];
            vatOutputInvoiceManageService.deleteEvidenceFile(fileList).then(function () {
                $scope.files = _.filter($scope.files, function (item) {
                    return item.data.fileID != fileId;
                });
            });
        }

        //下载证据文件
        $scope.downloadEvidenceFile = function (data) {
            var queryDto = {};
            queryDto.fileName = data.fileName;
            queryDto.filePath = data.filePath;
            vatOutputInvoiceManageService.downloadEvidenceFile(queryDto);
        }

        $scope.$watch('uploadFileType', function (newValue,oldValue) {
            if (newValue) {
                if (newValue === 'multiple') {
                    $scope.isMultiple = true;
                } else {
                    $scope.isMultiple = false;
                }
                $scope.files = [];
                if ($scope.uploadFileOption.files) {
                    _.each($scope.uploadFileOption.files, function (item) {
                        $scope.files.push(item);
                    });
                }

                var parentElem = angular.element($('#upload-file-modal-wrapper'));
                $scope.modalInstance = $uibModal.open({
                    backdrop: 'static',
                    templateUrl: 'uploadFileModal.html',
                    controller: uploadFileController,
                    appendTo: parentElem,
                    scope: $scope
                });
            }
        });
    }]);