upload.js 7.0 KB

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