upload.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. var upload_pic = [];
  2. var upload_file = [];
  3. var upload = [];
  4. var showUploadE;
  5. var showUploadUrl;
  6. var showUploadType = 1;
  7. var showUploadCall = false;
  8. var showUploadThis = false;
  9. var showUploadIndex = false;
  10. var xhrOnProgress=function(fun) {
  11. xhrOnProgress.onprogress = fun; //绑定监听
  12. //使用闭包实现监听绑
  13. return function() {
  14. //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
  15. var xhr = $.ajaxSettings.xhr();
  16. //判断监听函数是否为函数
  17. if (typeof xhrOnProgress.onprogress !== 'function')
  18. return xhr;
  19. //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
  20. if (xhrOnProgress.onprogress && xhr.upload) {
  21. xhr.upload.onprogress = xhrOnProgress.onprogress;
  22. }
  23. return xhr;
  24. }
  25. }
  26. function showUpload(i,e,key,url,assets)
  27. {
  28. layui.use(['upload','layer','element'], function() {
  29. var html = '';
  30. var layer = layui.layer;
  31. var config = {
  32. type: 0,
  33. title: e.attr('value'),
  34. shade: 0.1,
  35. shadeClose : true,
  36. content: html,
  37. area: '800px'
  38. };
  39. e.click(function()
  40. {
  41. url = url.replace('save', 'view');
  42. showUploadE = e;
  43. showUploadType = 1;
  44. showUploadUrl = url;
  45. var cur = $('#' + showUploadE.attr('v')).val();
  46. $.getJSON(url, {json:1,key:key,cur:cur}, function(t)
  47. {
  48. config.content = t.data;
  49. showUploadIndex = layer.open(config);
  50. })
  51. })
  52. });
  53. }
  54. function editorShowUpload(cur, url, title, func, self)
  55. {
  56. showUploadType = 2;
  57. showUploadCall = func;
  58. showUploadThis = self;
  59. layui.use(['upload','layer','element'], function() {
  60. var html = '';
  61. var layer = layui.layer;
  62. var config = {
  63. type: 0,
  64. title: title,
  65. shade: 0.1,
  66. shadeClose : true,
  67. content: html,
  68. area: '800px'
  69. };
  70. url = url.replace('save', 'view');
  71. showUploadUrl = url;
  72. $.getJSON(url, {json:1,cur:cur}, function(t)
  73. {
  74. config.content = t.data;
  75. showUploadIndex = layer.open(config);
  76. })
  77. });
  78. }
  79. function showUploadFilesSet(e, name, url, id)
  80. {
  81. $('.layui-anim-yes').addClass('layui-anim-no');
  82. $('.layui-anim-yes').removeClass('layui-anim-yes');
  83. e.parent().removeClass('layui-anim-no');
  84. e.parent().addClass('layui-anim-yes');
  85. $('.file_cur').val(url);
  86. if (showUploadType == 1) {
  87. var value = showUploadE.attr('v');
  88. loadUploadSet(showUploadE, value, {name:name,url:url});
  89. } else if (showUploadType == 2) {
  90. showUploadCall.call(showUploadThis, url, name, id);
  91. if (showUploadIndex) {
  92. layer.close(showUploadIndex);
  93. showUploadIndex = false;
  94. } else {
  95. layer.closeAll();
  96. }
  97. }
  98. }
  99. function showUploadFilesSearch(e)
  100. {
  101. var p = e.parent();
  102. var pg = p.find('.file_pg').val();
  103. var key = p.find('.file_key').val();
  104. var name = p.find('.file_filename').val();
  105. var tag = p.find('.file_tag').val();
  106. var cate = p.find('.file_cate').val();
  107. var cur = p.find('.file_cur').val();
  108. var url = showUploadUrl;
  109. showUploadFiles(url, name, key, cate, tag, cur, pg);
  110. }
  111. function showUploadFiles(url, name, key, cate, tag, cur, pg)
  112. {
  113. if (!pg) {
  114. pg = 1;
  115. }
  116. $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur}, function(t)
  117. {
  118. var t = '<div>' + t.data + '</div>';
  119. html = $(t).find('#component-anim').html();
  120. $("#component-anim").html(html);
  121. })
  122. }
  123. function loadUpload(i,e,key,url,assets)
  124. {
  125. var value = e.attr('v');
  126. if (typeof(upload[value]) != "undefined") {
  127. //return;
  128. }
  129. upload[value] = true;
  130. upload_pic[value] = [];
  131. upload_file[value] = [];
  132. var id = e.attr('id');
  133. layui.use(['upload','layer','element'], function() {
  134. var layer = layui.layer;
  135. var layuiUpload = layui.upload;
  136. var element = layui.element;
  137. var uploadInst = layuiUpload.render({
  138. elem: '#' + id
  139. ,data: {'key' : key}
  140. ,field: 'file'
  141. ,url: url
  142. ,multiple: true
  143. ,accept:'file'
  144. ,xhr:xhrOnProgress
  145. ,progress:function(value){//上传进度回调 value进度值
  146. $('#' + id + '_progress').show();
  147. element.progress(id + '_progress', value+'%')//设置页面进度条
  148. }
  149. ,choose: function(obj){
  150. element.progress(id + '_progress', '0%')
  151. //layer.load();
  152. }
  153. ,done: function(data) {
  154. element.progress(id + '_progress', '0%')
  155. $('#' + id + '_progress').hide();
  156. //layer.closeAll('loading');
  157. if (data.status == 1) {
  158. loadUploadSet(e,value,data);
  159. } else {
  160. element.progress(id + '_progress', '0%')
  161. layer.alert(data.message);
  162. return false;
  163. }
  164. }
  165. ,error: function(){
  166. //请求异常回调
  167. }
  168. });
  169. });
  170. }
  171. function loadUploadSet(e,value,data)
  172. {
  173. var mul = e.attr('mul');
  174. if (mul == 'yes') {
  175. if (e.attr('t') == 'file') {
  176. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  177. upload_file[value] = $("#" + value).val().split(',');
  178. }
  179. var m = $('.' +value+'_mul');
  180. var i = m.find('li').eq(upload_file[value].length);
  181. if (i.length) {
  182. i.find('a').eq(0).html(data.name);
  183. } else {
  184. m.append('<li><a href="'+data.url+'" target="_blank" title="点此下载" >'+data.name+'</a>&nbsp;&nbsp;<a href="javascript:;" title="点此删除" style="color:red;" onclick="fileDel($(this), \''+value+'\')">[删除]</a></li>');
  185. }
  186. upload_file[value].push(data.url);
  187. $("#" + value).val(upload_file.join(','));
  188. } else {
  189. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  190. upload_pic[value] = $("#" + value).val().split(',');
  191. }
  192. var m = $('.' +value+'_mul');
  193. var i = m.find('img').eq(upload_pic[value].length);
  194. if (i.length) {
  195. i.attr('src', data.url);
  196. } else {
  197. m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
  198. }
  199. upload_pic[value].push(data.url);
  200. $("#" + value).val(upload_pic[value].join(','));
  201. }
  202. } else {
  203. if (e.attr('t') == 'file') {
  204. } else {
  205. $('#show_'+value).attr('src',data.url).show();
  206. $('#show_'+value).unbind('click').bind('click', function()
  207. {
  208. picDel($(this), value, 1);
  209. })
  210. }
  211. $('#'+value).val(data.url);
  212. }
  213. }
  214. function picDel(e,v,s)
  215. {
  216. if (confirm('确定要删除图片吗?')) {
  217. if (s == 1) {
  218. e.hide();
  219. $("#" + v).val('');
  220. } else {
  221. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  222. upload_pic[v] = $("#" + v).val().split(',');
  223. }
  224. var p = e.find('img').attr('src');
  225. e.remove();
  226. upload_pic[v].remove(p);
  227. $("#" + v).val(upload_pic[v].join(','));
  228. }
  229. }
  230. }
  231. function fileDel(e,v)
  232. {
  233. if(confirm('确定要删除文件吗?'))
  234. {
  235. if(upload_file[v].length <= 0 && $("#" + v).val())
  236. {
  237. upload_file[v] = $("#" + v).val().split(',');
  238. }
  239. var parent = e.parent();
  240. var p = parent.find('a').eq(0).attr('href');
  241. parent.remove();
  242. upload_file[v].remove(p);
  243. //console.info(pic);
  244. $("#" + v).val(upload_file[v].join(','));
  245. }
  246. }