upload.js 6.7 KB

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