enclosureupload.js 17.2 KB
Newer Older
doc_wei's avatar
doc_wei 已提交
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

var folderId = "";

var enclosureList = new Array();

layui.config({
	base: basePath, 
	version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'jquery', 'winui', 'webuploader', 'fsTree'], function (exports) {
	winui.renderColor();
	layui.use(['form'], function (form) {
		var index = parent.layer.getFrameIndex(window.name);
		var $ = layui.$,
			fsTree = layui.fsTree;
		
		// 附件所属目录
		folderId = "0";
		var boxId = GetUrlParam('boxId');
		enclosureList = [].concat(JSON.parse(parent.$("#" + boxId).attr(skyeyeEnclosure.enclosureListKey)));

		// 初始化上传
		loadUploadMethod();
		matchingLanguage();
		form.render();
	    form.on('submit(fileUploadStart)', function (data) {
	    	// 表单验证
	        if (winui.verifyForm(data.elem)) {
	        	uoloadObj.upload();
	        }
	        return false;
	    });
34 35 36

		var ztree = null;
		/********* tree 处理   start *************/
doc_wei's avatar
doc_wei 已提交
37 38 39 40
	    loadMyEnclosureList();
	    function loadMyEnclosureList(){
	    	fsTree.render({
	    		id: "treeDemo",
doc_wei's avatar
doc_wei 已提交
41
	    		url: reqBasePath + "sysenclosure010?loginPCIp=" + returnCitySN["cip"],
doc_wei's avatar
doc_wei 已提交
42 43 44 45 46 47 48 49 50 51 52
	    		checkEnable: true,
	    		loadEnable: false,//异步加载
	    		showLine: false,
	    		showIcon: true,
	    		onDblClick: function(){
	    		},
	    		onAsyncSuccess: function(id){
	    		},
	    		onCheck: zTreeOnCheck //选中回调函数
	    	}, function(id){
	    		ztree = $.fn.zTree.getZTreeObj(id);
53 54
	    		var zTreeChecked = ztree.getCheckedNodes(false);
			    for (var i = 0; i < zTreeChecked.length; i++) {
doc_wei's avatar
doc_wei 已提交
55
			    	for(var j = 0; j < enclosureList.length; j++){
56 57
			    		if(zTreeChecked[i].id == enclosureList[j].id){
			    			ztree.checkNode(zTreeChecked[i], true, true);
doc_wei's avatar
doc_wei 已提交
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			    		}
			    	}
			    }  
	    		loadThisFolderChild();
	    	});
	    }
	    
	    function loadThisFolderChild(){
	    	ztree.expandNode(ztree.getNodeByParam("id", "1", null));//展开指定节点-我的文档
			ztree.selectNode(ztree.getNodeByParam("id", "1", null));//选中指定节点-我的文档
	    }
	    
	    //选中或取消选中的回调函数
	    function zTreeOnCheck(event, treeId, treeNode){
	    	//获取选中节点
	    	var zTree = ztree.getCheckedNodes(true);
	    	for (var i = 0; i < zTree.length; i++) {
	    		if(!isNull(zTree[i].fileType) && zTree[i].fileType != "folder"){
    				addToArray({
    					id: zTree[i].id,
    					name: zTree[i].name,
    					fileAddress: zTree[i].fileAddress
    				});
	    		}
	    	}
	    	//获取未选中节点
	    	zTree = ztree.getCheckedNodes(false);
	    	for (var i = 0; i < zTree.length; i++) {
	    		if(!isNull(zTree[i].fileType) && zTree[i].fileType != "folder"){
    				removeToArray(zTree[i].id);
	    		}
	    	}
	    }
	    
	    /********* tree 处理   end *************/
	    
	    //取消
	    $("body").on("click", "#cancle", function(){
	    	parent.layer.close(index);
	    });
	    
	    //确定
	    $("body").on("click", "#confimChoose", function(){
	    	var nodes = ztree.getCheckedNodes(true);
	    	for(var i = 0; i < nodes.length; i++){
	    		var node = nodes[i];
				if(!isNull(node.fileType) && node.fileType != "folder"){
					addToArray({
						id: node.id,
						name: node.name,
						fileAddress: node.fileAddress
					});
				}
			}
	    	parent.layer.close(index);
			parent.$("#" + boxId).attr(skyeyeEnclosure.enclosureListKey, JSON.stringify(enclosureList));
	    	parent.refreshCode = '0';
	    });
	    
	});
});

//向集合中添加元素
//参数为json
function addToArray(data){
	var inArray = false;
	$.each(enclosureList, function(i, item){
		if(item.id === data.id){
			inArray = true;
			return false;
		}
	});
	if(!inArray){//如果该元素在集合中不存在
		enclosureList.push({
			id: data.id,
			name: data.name,
			fileAddress: data.fileAddress
		});
	}
}

//移除集合中的元素
function removeToArray(id){
	var inArray = -1;
	$.each(enclosureList, function(i, item){
		if(id === item.id){
			inArray = i;
			return false;
		}
	});
	if(inArray != -1){//如果该元素在集合中存在
		enclosureList.splice(inArray, 1);
	}
}

function loadUploadMethod(){
    $wrap = $('#uploader'),
    // 文件容器
    $queue = $('<ul class="filelist"></ul>').appendTo( $wrap.find('.queueList') ),
    // 状态栏,包括进度和控制按钮
    $statusBar = $wrap.find('.statusBar'),
    // 文件总体选择信息。
    $info = $statusBar.find('.info'),
    // 上传按钮
    $upload = $wrap.find('.uploadBtn'),
    // 没选择文件之前的内容。
    $placeHolder = $wrap.find('.placeholder'),
    // 总体进度条
    $progress = $statusBar.find('.progress').hide(),
    // 添加的文件数量
    fileCount = 0,
    // 添加的文件总大小
    fileSize = 0,
    // 优化retina, 在retina下这个值是2
    ratio = window.devicePixelRatio || 1,
    // 缩略图大小
    thumbnailWidth = 110 * ratio,
    thumbnailHeight = 110 * ratio,
    // 可能有pedding, ready, uploading, confirm, done.
    state = 'pedding',
    // 所有文件的进度信息,key为file id
    percentages = {},
    supportTransition = (function(){
        var s = document.createElement('p').style,
            r = 'transition' in s ||
                  'WebkitTransition' in s ||
                  'MozTransition' in s ||
                  'msTransition' in s ||
                  'OTransition' in s;
        s = null;
        return r;
    })(),
    // WebUploader实例
    uploader;
	if (!WebUploader.Uploader.support()) {
	    alert( 'Web Uploader 不支持您的浏览器!如果你使用的是IE浏览器,请尝试升级 flash 播放器');
	    throw new Error( 'WebUploader does not support the browser you are using.' );
	}
	
	var md5;
	//监听分块上传过程中的三个时间点
	WebUploader.Uploader.register({
		"before-send-file": "beforeSendFile",
		"before-send": "beforeSend",
		"after-send-file": "afterSendFile"
	}, {
		//时间点1:所有分块进行上传之前调用此函数
		beforeSendFile: function(file) {
			var deferred = WebUploader.Deferred();
			//1、计算文件的唯一标记,用于断点续传
			(new WebUploader.Uploader()).md5File(file, 0, 10 * 1024 * 1024)
				.progress(function(percentage) {
					$('#' + file.id).find("p.state").text("正在读取文件信息...");
				})
				.then(function(val) {
					md5 = val;
					$('#' + file.id).find("p.state").text("成功获取文件信息...");
					//获取文件信息后进入下一步
					deferred.resolve();
				});
			return deferred.promise();
		},
		//时间点2:如果有分块上传,则每个分块上传之前调用此函数
		beforeSend: function(block) {
			var deferred = WebUploader.Deferred();
			var params = {
				//文件唯一标记
				"md5": md5,
				//当前分块下标
				"chunk": block.chunk,
				//当前分块大小
				"chunkSize": block.end - block.start
			};
doc_wei's avatar
doc_wei 已提交
231
			AjaxPostUtil.request({url:reqBasePath + "sysenclosure009", params: params, type: 'json', callback: function(json){
doc_wei's avatar
doc_wei 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    			if(json.returnCode == 0){
    				//分块存在,跳过
					deferred.reject();
    			}else{
    				//分块不存在或不完整,重新发送该分块内容
					deferred.resolve();
    			}
    		}, async: false});
			this.owner.options.formData.md5 = md5;
			deferred.resolve();
			return deferred.promise();
		},
		//时间点3:所有分块上传成功后调用此函数
		afterSendFile: function (data) {
			//如果分块上传成功,则通知后台合并分块
doc_wei's avatar
doc_wei 已提交
247
			AjaxPostUtil.request({url:reqBasePath + "sysenclosure008", params: {md5: md5, folderId: folderId, name: data.name, size: data.size}, type: 'json', callback: function(json){
doc_wei's avatar
doc_wei 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    			if(json.returnCode == 0){
    				addToArray(json.bean);
    			}else{
    				winui.window.msg(json.returnMessage, {icon: 2,time: 2000});
    			}
    		}});
		 }
	});
	
	// 实例化
	uploader = WebUploader.create({
	    pick: {
	        id: '#filePicker',
	        label: '选择文件'
	    },
	    formData: {
	    	userToken: getCookie('userToken'),
	    	loginPCIp: returnCitySN["cip"],
	    	folderId: folderId
	    },
	    dnd: '#uploader .queueList',
	    paste: document.body,
	    // swf文件路径
	    swf: fileBasePath + '/assets/images/Uploader.swf',
	    disableGlobalDnd: true,//是否禁掉整个页面的拖拽功能
	    chunked: true,//是否要分片处理大文件上传
	    chunkSize: 10 * 1024 * 1024,
	    chunkRetry: 3,//网络问题上传失败后重试次数
	    threads: 1, //上传并发数
	    fileSizeLimit: 2000 * 1024 * 1024,//最大2GB
        fileSingleSizeLimit: 2000 * 1024 * 1024,
        resize: false,//不压缩
	    server: reqBasePath + 'sysenclosure007',
	    fileNumLimit: 300
	});
	// 添加“添加文件”的按钮,
	uploader.addButton({
	    id: '#filePicker2',
	    label: '继续添加'
	});
	uploader.onUploadProgress = function(file, percentage) {
	    var $li = $('#'+file.id),
	        $percent = $li.find('.progress span');
	    $percent.css('width', percentage * 100 + '%');
	    percentages[file.id][1] = percentage;
	    updateTotalProgress();
	};
	uploader.onFileQueued = function(file) {
	    fileCount++;
	    fileSize += file.size;
	    if (fileCount === 1) {
	        $placeHolder.addClass( 'element-invisible' );
	        $statusBar.show();
	    }
	    addFile(file);
	    setState('ready');
	    updateTotalProgress();
	};
	uploader.onFileDequeued = function(file) {
	    fileCount--;
	    fileSize -= file.size;
	    if (!fileCount) {
	        setState( 'pedding' );
	    }
	    removeFile(file);
	    updateTotalProgress();
	};
	uploader.on( 'all', function( type ) {
	    var stats;
	    switch( type ) {
	        case 'uploadFinished':
	            setState( 'confirm' );
	            break;
	        case 'startUpload':
	            setState( 'uploading' );
	            break;
	        case 'stopUpload':
	            setState( 'paused' );
	            break;
	    }
	});
	uploader.on('uploadBeforeSend', function(block, data, headers) {
		headers['X-Requested-With']=  'XMLHttpRequest';
		data.userToken = getCookie('userToken');
		data.loginPCIp = returnCitySN["cip"];
		data.folderId = folderId;
		data.md5 = md5;
		data.chunk = block.chunk;
		data.chunkSize = block.end - block.start;
	});
	uploader.onError = function(code) {
	    alert('Eroor: ' + code);
	};
	$upload.on('click', function() {
	    if ($(this).hasClass( 'disabled')) {
	        return false;
	    }
	    if (state === 'ready') {
	        uploader.upload();
	    } else if (state === 'paused') {
	        uploader.upload();
	    } else if (state === 'uploading') {
	        uploader.stop(true);
	    }
	});
	$info.on('click', '.retry', function() {
	    uploader.retry();
	});
	$info.on('click', '.ignore', function() {
	    alert('todo');
	});
	$upload.addClass('state-' + state);
	updateTotalProgress();
}

//当有文件添加进来时执行,负责view的创建
function addFile( file ) {
    var $li = $( '<li id="' + file.id + '">' + '<p class="title">' + file.name + '</p>' + '<p class="imgWrap"></p>' + '<p class="progress"><span></span></p>' + '</li>' ),
        $btns = $('<div class="file-panel">' + '<span class="cancel">删除</span>' + '<span class="rotateRight">向右旋转</span>' + '<span class="rotateLeft">向左旋转</span></div>').appendTo( $li ),
        $prgress = $li.find('p.progress span'),
        $wrap = $li.find( 'p.imgWrap' ),
        $info = $('<p class="error"></p>'),
        showError = function( code ) {
            switch( code ) {
                case 'exceed_size':
                    text = '文件大小超出';
                    break;

                case 'interrupt':
                    text = '上传暂停';
                    break;

                default:
                    text = '上传失败,请重试';
                    break;
            }
            $info.text( text ).appendTo( $li );
        };
    if ( file.getStatus() === 'invalid' ) {
        showError( file.statusText );
    } else {
        $wrap.text( '预览中' );
        uploader.makeThumb( file, function( error, src ) {
            if ( error ) {
                $wrap.text( '不能预览' );
                return;
            }
            var img = $('<img src="'+src+'">');
            $wrap.empty().append( img );
        }, thumbnailWidth, thumbnailHeight );
        percentages[ file.id ] = [ file.size, 0 ];
        file.rotation = 0;
    }
    file.on('statuschange', function( cur, prev ) {
        if ( prev === 'progress' ) {
            $prgress.hide().width(0);
        } else if ( prev === 'queued' ) {
            $li.off( 'mouseenter mouseleave' );
            $btns.remove();
        }
        // 成功
        if ( cur === 'error' || cur === 'invalid' ) {
            showError( file.statusText );
            percentages[ file.id ][ 1 ] = 1;
        } else if ( cur === 'interrupt' ) {
            showError( 'interrupt' );
        } else if ( cur === 'queued' ) {
            percentages[ file.id ][ 1 ] = 0;
        } else if ( cur === 'progress' ) {
            $info.remove();
            $prgress.css('display', 'block');
        } else if ( cur === 'complete' ) {
            $li.append( '<span class="success"></span>' );
        }
        $li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
    });
    $li.on( 'mouseenter', function() {
        $btns.stop().animate({height: 30});
    });
    $li.on( 'mouseleave', function() {
        $btns.stop().animate({height: 0});
    });
    $btns.on( 'click', 'span', function() {
        var index = $(this).index(), deg;
        switch ( index ) {
            case 0:
                uploader.removeFile( file );
                return;

            case 1:
                file.rotation += 90;
                break;

            case 2:
                file.rotation -= 90;
                break;
        }
        if ( supportTransition ) {
            deg = 'rotate(' + file.rotation + 'deg)';
            $wrap.css({
                '-webkit-transform': deg,
                '-mos-transform': deg,
                '-o-transform': deg,
                'transform': deg
            });
        } else {
            $wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
        }
    });
    $li.appendTo( $queue );
}

// 负责view的销毁
function removeFile(file) {
    var $li = $('#' + file.id);
    delete percentages[file.id];
    updateTotalProgress();
    $li.off().find('.file-panel').off().end().remove();
}

function updateTotalProgress() {
    var loaded = 0,
        total = 0,
        spans = $progress.children(),
        percent;
    $.each(percentages, function(k, v) {
        total += v[ 0 ];
        loaded += v[ 0 ] * v[ 1 ];
    });
    percent = total ? loaded / total : 0;
    spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
    spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
    updateStatus();
}

function updateStatus() {
    var text = '', stats;
    if (state === 'ready') {
        text = '选中' + fileCount + '个文件,共' + WebUploader.formatSize(fileSize) + '';
    } else if (state === 'confirm') {
        stats = uploader.getStats();
        if (stats.uploadFailNum) {
            text = '已成功上传' + stats.successNum+ '个照片至服务器,'+ stats.uploadFailNum + '个文件上传失败,<a class="retry" href="#">重新上传</a>失败文件或<a class="ignore" href="#">忽略</a>'
        }
    } else {
        stats = uploader.getStats();
        text = '' + fileCount + '张(' + WebUploader.formatSize(fileSize)  + '),已上传' + stats.successNum + '';
        if (stats.uploadFailNum) {
            text += ',失败' + stats.uploadFailNum + '';
        }
    }
    $info.html(text);
}

function setState(val) {
    var file, stats;
    if (val === state) {
        return;
    }
    $upload.removeClass('state-' + state);
    $upload.addClass('state-' + val);
    state = val;
    switch (state) {
        case 'pedding':
            $placeHolder.removeClass('element-invisible');
            $queue.parent().removeClass('filled');
            $queue.hide();
            $statusBar.addClass('element-invisible');
            uploader.refresh();
            break;
        case 'ready':
            $placeHolder.addClass('element-invisible');
            $( '#filePicker2' ).removeClass( 'element-invisible');
            $queue.parent().addClass('filled');
            $queue.show();
            $statusBar.removeClass('element-invisible');
            uploader.refresh();
            break;
        case 'uploading':
            $( '#filePicker2' ).addClass( 'element-invisible' );
            $progress.show();
            $upload.text( '暂停上传' );
            break;
        case 'paused':
            $progress.show();
            $upload.text( '继续上传' );
            break;
        case 'confirm':
            $progress.hide();
            $upload.text( '开始上传' ).addClass( 'disabled' );
            stats = uploader.getStats();
            if ( stats.successNum && !stats.uploadFailNum ) {
                setState( 'finish' );
                return;
            }
            break;
        case 'finish':
            stats = uploader.getStats();
            if (stats.successNum) {
                alert( '上传成功' );
            } else {
                // 没有成功的文件,重设
                state = 'done';
                location.reload();
            }
            break;
    }
    updateStatus();
}