123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- var upload_pic = [];
- var upload_file = [];
- var upload = [];
- var showUploadE;
- var showUploadUrl;
- var showUploadType = 1;
- var showUploadCall = false;
- var showUploadThis = false;
- var showUploadIndex = false;
- var xhrOnProgress=function(fun) {
- xhrOnProgress.onprogress = fun; //绑定监听
- //使用闭包实现监听绑
- return function() {
- //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
- var xhr = $.ajaxSettings.xhr();
- //判断监听函数是否为函数
- if (typeof xhrOnProgress.onprogress !== 'function')
- return xhr;
- //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
- if (xhrOnProgress.onprogress && xhr.upload) {
- xhr.upload.onprogress = xhrOnProgress.onprogress;
- }
- return xhr;
- }
- }
- function showUpload(i,e,key,url,assets)
- {
- layui.use(['upload','layer','element'], function() {
- var html = '';
- var layer = layui.layer;
- var config = {
- type: 0,
- title: e.attr('value'),
- shade: 0.1,
- shadeClose : true,
- content: html,
- area: '800px'
- };
- e.click(function()
- {
- url = url.replace('save', 'view');
- showUploadE = e;
- showUploadType = 1;
- showUploadUrl = url;
- var cur = $('#' + showUploadE.attr('v')).val();
- $.getJSON(url, {json:1,key:key,cur:cur}, function(t)
- {
- config.content = t.data;
- showUploadIndex = layer.open(config);
- })
- })
- });
- }
- function editorShowUpload(cur, url, title, func, self)
- {
- showUploadType = 2;
- showUploadCall = func;
- showUploadThis = self;
- layui.use(['upload','layer','element'], function() {
- var html = '';
- var layer = layui.layer;
- var config = {
- type: 0,
- title: title,
- shade: 0.1,
- shadeClose : true,
- content: html,
- area: '800px'
- };
- url = url.replace('save', 'view');
- showUploadUrl = url;
- $.getJSON(url, {json:1,cur:cur}, function(t)
- {
- config.content = t.data;
- showUploadIndex = layer.open(config);
- })
- });
- }
- function showUploadFilesSet(e, name, url, id)
- {
- $('.layui-anim-yes').addClass('layui-anim-no');
- $('.layui-anim-yes').removeClass('layui-anim-yes');
- e.parent().removeClass('layui-anim-no');
- e.parent().addClass('layui-anim-yes');
- $('.file_cur').val(url);
- if (showUploadType == 1) {
- var value = showUploadE.attr('v');
- loadUploadSet(showUploadE, value, {name:name,url:url});
- } else if (showUploadType == 2) {
- showUploadCall.call(showUploadThis, url, name, id);
- if (showUploadIndex) {
- layer.close(showUploadIndex);
- showUploadIndex = false;
- } else {
- layer.closeAll();
- }
- }
- }
- function showUploadFilesSearch(e)
- {
- var p = e.parent();
- var pg = p.find('.file_pg').val();
- var key = p.find('.file_key').val();
- var name = p.find('.file_filename').val();
- var tag = p.find('.file_tag').val();
- var cate = p.find('.file_cate').val();
- var cur = p.find('.file_cur').val();
- var url = showUploadUrl;
- showUploadFiles(url, name, key, cate, tag, cur, pg);
- }
- function showUploadFiles(url, name, key, cate, tag, cur, pg)
- {
- if (!pg) {
- pg = 1;
- }
- $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur}, function(t)
- {
- var t = '<div>' + t.data + '</div>';
- html = $(t).find('#component-anim').html();
- $("#component-anim").html(html);
- })
- }
- function loadUpload(i,e,key,url,assets)
- {
- var value = e.attr('v');
- if (typeof(upload[value]) != "undefined") {
- //return;
- }
- upload[value] = true;
- upload_pic[value] = [];
- upload_file[value] = [];
- var id = e.attr('id');
- layui.use(['upload','layer','element'], function() {
- var layer = layui.layer;
- var layuiUpload = layui.upload;
- var element = layui.element;
- var uploadInst = layuiUpload.render({
- elem: '#' + id
- ,data: {'key' : key}
- ,field: 'file'
- ,url: url
- ,multiple: true
- ,accept:'file'
- ,xhr:xhrOnProgress
- ,progress:function(value){//上传进度回调 value进度值
- $('#' + id + '_progress').show();
- element.progress(id + '_progress', value+'%')//设置页面进度条
- }
- ,choose: function(obj){
- element.progress(id + '_progress', '0%')
- //layer.load();
- }
- ,done: function(data) {
- element.progress(id + '_progress', '0%')
- $('#' + id + '_progress').hide();
- //layer.closeAll('loading');
- if (data.status == 1) {
- loadUploadSet(e,value,data);
- } else {
- element.progress(id + '_progress', '0%')
- layer.alert(data.message);
- return false;
- }
- }
- ,error: function(){
- //请求异常回调
- }
- });
- });
- }
- function loadUploadSet(e,value,data)
- {
- var mul = e.attr('mul');
- if (mul == 'yes') {
- if (e.attr('t') == 'file') {
- if (upload_file[value].length <= 0 && $("#" + value).val()) {
- upload_file[value] = $("#" + value).val().split(',');
- }
- var m = $('.' +value+'_mul');
- var i = m.find('li').eq(upload_file[value].length);
- if (i.length) {
- i.find('a').eq(0).html(data.name);
- } else {
- m.append('<li><a href="'+data.url+'" target="_blank" title="点此下载" >'+data.name+'</a> <a href="javascript:;" title="点此删除" style="color:red;" onclick="fileDel($(this), \''+value+'\')">[删除]</a></li>');
- }
- upload_file[value].push(data.url);
- $("#" + value).val(upload_file.join(','));
- } else {
- if (upload_pic[value].length <= 0 && $("#" + value).val()) {
- upload_pic[value] = $("#" + value).val().split(',');
- }
- var m = $('.' +value+'_mul');
- var i = m.find('img').eq(upload_pic[value].length);
- if (i.length) {
- i.attr('src', data.url);
- } else {
- m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
- }
- upload_pic[value].push(data.url);
- $("#" + value).val(upload_pic[value].join(','));
- }
-
- } else {
- if (e.attr('t') == 'file') {
-
- } else {
- $('#show_'+value).attr('src',data.url).show();
- $('#show_'+value).unbind('click').bind('click', function()
- {
- picDel($(this), value, 1);
- })
- }
- $('#'+value).val(data.url);
- }
- }
- function picDel(e,v,s)
- {
- if (confirm('确定要删除图片吗?')) {
- if (s == 1) {
- e.hide();
- $("#" + v).val('');
- } else {
- if (upload_pic[v].length <= 0 && $("#" + v).val()) {
- upload_pic[v] = $("#" + v).val().split(',');
- }
- var p = e.find('img').attr('src');
- e.remove();
- upload_pic[v].remove(p);
- $("#" + v).val(upload_pic[v].join(','));
- }
- }
- }
- function fileDel(e,v)
- {
- if(confirm('确定要删除文件吗?'))
- {
- if(upload_file[v].length <= 0 && $("#" + v).val())
- {
- upload_file[v] = $("#" + v).val().split(',');
- }
- var parent = e.parent();
- var p = parent.find('a').eq(0).attr('href');
-
- parent.remove();
- upload_file[v].remove(p);
- //console.info(pic);
- $("#" + v).val(upload_file[v].join(','));
- }
- }
|