uploaddirect.js 11.4 KB

//评论管理
angular.module("xn/template/uploadDirect/uploadFile.html", [])
    .run(["$templateCache", function($templateCache) {
        "use strict";
        $templateCache.put("xn/template/uploadDirect/uploadFile.html",
            "<div class='clearfix'>" +
            "   <div class='clearfix'>" +
            "      <button id=\"{{browseButton}}\" class=\"btn btn-default mr_15 f-right\"  name=\"pickfiles\" >增加</button>" +
            "      <button class=\"btn btn-primary  mr_15 f-right\" ng-click=\"upClick()\" ng-show=\"upFiles.length > 0\">上传</button>" +
            "   </div>"+
            "   <div class='mt_12 clearfix' id='{{container}}'>" +
            "       <div ng-if='vm.isShow'  class=\"xn-table-wrap clearfix \" id=\"container\" >"+
            "            <table class=\"table table-bordered\">"+
            "                <thead>"+
            "                <tr>"+
            "                    <th class=\"xn-text-center xn-col-sm-8\">文件名</th>"+
            "                    <th class=\"xn-text-center xn-col-sm-3 xn-center\">格式</th>"+
            "                    <th class=\"xn-text-center xn-col-sm-6 xn-center\">文件大小</th>"+
            "                    <th class=\"xn-text-center xn-col-sm-10 xn-center\">状态</th>"+
            "                </tr>"+
            "                </thead>"+
            "                <tbody id=\"tbody\">"+
            "                <tr ng-repeat=\"file in upFiles\">"+
            "                    <td>{{file.name}}</td>"+
            "                    <td class=\"xn-center\">{{file.fileType}}</td>"+
            "                    <td class=\"xn-center\">{{file.size | fileSize}}</td>"+
            "                    <td class=\"xn-text-center\" ng-show=\"file.percent\">"+
            "                        {{file.status | fileStatusType}}【{{file.percent}}%】"+
            "                    </td>"+
            "                    <td class=\"xn-text-center\" ng-show=\"!file.percent\">"+
            "                        {{file.status | fileStatusType}}"+
            "                    </td>"+
            "                </tr>"+
            "                </tbody>"+
            "            </table>"+
            "        </div>"+
            "   </div>"+
            "<div/>"
        );
    }]);

angular.module("uploadDirect",["xn/template/uploadDirect/uploadFile.html"])
    .directive("xnUploadDirect",["uploadDirectService","dialogService","$timeout",function(uploadDirectService,dialogService,$timeout) {
        return {
            restrict: "AE",
            scope: {
                upFiles: "=ngModel",
                url: "@",
                path: "@",
                method:"&",
                isShow:"@"
            },
            replace: true,
            require: "?ngModel",
            templateUrl: "xn/template/uploadDirect/uploadFile.html",
            link: function(scope,element,atter,ngModel) {
                scope.vm={
                    isShow:true
                };
                if(scope.isShow=="false"){
                    scope.vm.isShow=false;
                }
                scope.upFiles=[];
                var timeStamp=new Date().getTime();
                scope.browseButton="browseButton"+timeStamp;
                scope.container="container"+timeStamp;

                $timeout(function () {
                    // 初始化上传
                    var uploader = new plupload.Uploader({
                        runtimes : 'html5,flash,silverlight,html4',
                        browse_button:scope.browseButton,
                        container:scope.container,
                        prevent_duplicates:true,//阻止重复文件上传
                        flash_swf_url : scope.path||'https://cdn.xiniunet.com/js/plupload/2.1.2/js/Moxie.swf',
                        silverlight_xap_url :scope.path||'https://cdn.xiniunet.com/js/plupload/2.1.2/js/Moxie.xap',
                        url : scope.url||'http://oss.aliyuncs.com',

                        init: {
                            PostInit: function() {

                            },
                            //添加文件
                            FilesAdded: function(up, files) {
                                for(var j=0; j<files.length; j++) {
                                    var file = files[j];
                                    var item = {
                                        fileId : file.id,
                                        name : file.name,
                                        fileType :file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length),
                                        status : file.status,
                                        percent : file.percent,
                                        size : file.size
                                    };
                                    scope.upFiles.push(item);
                                }

                                scope.$apply();
                                scope.upClick=function () {
                                    if(scope.upFiles.length>0) {
                                        set_upload_param(uploader, '', false);
                                        console.log(scope.upFiles);
                                    }
                                }

                            },
                            //上传前
                            BeforeUpload: function(up, file) {
                                set_upload_param(up, file.name, true);
                            },
                            //上传中
                            UploadProgress: function(up, file) {
                                var item = {};
                                for (var i = 0; i < scope.upFiles.length; i++) {
                                    var obj = scope.upFiles[i];
                                    if (file.id == obj.fileId) {
                                        item = scope.upFiles[i];
                                        break;
                                    }
                                }
                                item.status = file.status;
                                item.percent = file.percent;
                                scope.$apply();
                            },
                            //上传结束
                            FileUploaded: function(up, file, info) {
                                var item = {};
                                for (var i = 0; i < scope.upFiles.length; i++) {
                                    var obj = scope.upFiles[i];
                                    if (file.id == obj.fileId) {
                                        item = scope.upFiles[i];
                                        break;
                                    }
                                }
                                item.status = file.status;
                                item.percent = file.percent;
                                if (file.status == "5") {
                                    item.storagePath=returnObj.host + "/" + returnObj.dir + file.name;
                                    item.extension = item.fileType;
                                    item.sourceType = "DIRECT";//直传
                                    uploadDirectService.createFile(item).success(function (result) {
                                        if (result.errors === null && result.errors.length > 0) {
                                            dialogService.tip(result.errors);
                                        } else {
                                            item.id = result.id;
                                        }
                                    });
                                }
                                scope.$apply();
                            },
                            //获取异常
                            Error: function(up, err) {
                                console.log(err);
                            }
                        }
                    });

                    // 获取签名
                    uploadDirectService.getMyUploadFile().success(function(data){
                        returnObj=data;
                        g_dirname = data.dir;
                        uploader.init();
                    });
                },0);

                var g_object_name = '';
                var returnObj;
                var g_dirname;

                function set_upload_param(up, filename, ret) {
                    g_object_name = g_dirname;
                    if (filename != '') {
                        calculate_object_name(filename)
                    }
                    var new_multipart_params = {
                        'key' : g_object_name,
                        'policy': returnObj.policy,
                        'OSSAccessKeyId': returnObj.accessid,
                        'success_action_status' : '200', //让服务端返回200,不然,默认会返回204
                        'signature': returnObj.signature
                    };

                    up.setOption({
                        'url': returnObj.host,
                        'multipart_params': new_multipart_params
                    });

                    up.start();
                }

                function calculate_object_name(filename) {
                    g_object_name += "${filename}";
                    return ''
                }
            }
        };
    }])
    .filter("fileStatusType",[
        function(){
            return function(input){
                switch(input){
                    case 1:
                        input = "队列中";
                        break;
                    case 2:
                        input ="上传中";
                        break;
                    case 4:
                        input ="上传失败";
                        break;
                    case 5:
                        input ="上传完成";
                        break;
                    default :
                        break;
                }
                return input;
            }
        }
    ])
    .filter("fileSize", [
        function() {
            return function (size) {
                if(size < 1) {
                    return "0B";
                } else if(size < 1024) {
                    return size + "B";
                } else if(size < 1000 * 1000) {
                    return Math.round(size / 1024 * 10) / 10.0 + "KB";
                } else if(size < 1000 * 1000 * 1000) {
                    return Math.round(size / 1024 / 1024 * 10) / 10.0 + "MB";
                } else {
                    return Math.round(size / 1024 / 1024 / 1024 * 10) / 10.0 + "GB";
                }
            }
        }
    ])
    .factory('uploadDirectService',['$http',function($http){
        var service={};
        service.getMyUploadFile=function(request) {

            var url=xnConfig.myUrl+"api/ossSign.do";
            return $http({
                method : "get",
                // withCredentials:true,
                url : url

                //data:request
            });
        };
        service.createFile=function(request) {
            var url=xnConfig.myUrl+"api/foundation.do";
            return $http({
                method : 'POST',
                url : url,
                withCredentials:true,
                params:{"method":"api.foundation.file.create","jsonParam":request}
                //data:request
            });
        };

        return service;
    }]);