upload.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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,type)
  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. if (!type) {
  45. type = 1;
  46. }
  47. showUploadType = type;
  48. showUploadUrl = url;
  49. var cur = $('#' + showUploadE.attr('v')).val();
  50. $.getJSON(url, {json:1,key:key,cur:cur}, function(t)
  51. {
  52. config.content = t.data;
  53. showUploadIndex = layer.open(config);
  54. })
  55. })
  56. });
  57. }
  58. function editorShowUpload(cur, url, title, func, self)
  59. {
  60. showUploadType = 2;
  61. showUploadCall = func;
  62. showUploadThis = self;
  63. layui.use(['upload','layer','element'], function() {
  64. var html = '';
  65. var layer = layui.layer;
  66. var config = {
  67. type: 0,
  68. title: title,
  69. shade: 0.1,
  70. shadeClose : true,
  71. content: html,
  72. area: '800px'
  73. };
  74. url = url.replace('save', 'view');
  75. showUploadUrl = url;
  76. $.getJSON(url, {json:1,cur:cur}, function(t)
  77. {
  78. config.content = t.data;
  79. showUploadIndex = layer.open(config);
  80. })
  81. });
  82. }
  83. function showUploadFilesSet(e, name, url, id)
  84. {
  85. $('.layui-anim-yes').addClass('layui-anim-no');
  86. $('.layui-anim-yes').removeClass('layui-anim-yes');
  87. e.parent().removeClass('layui-anim-no');
  88. e.parent().addClass('layui-anim-yes');
  89. $('.file_cur').val(url);
  90. if (showUploadType == 1) {
  91. var value = showUploadE.attr('v');
  92. loadUploadSet(showUploadE, value, {name:name,url:url});
  93. } else if (showUploadType == 2) {
  94. showUploadCall.call(showUploadThis, url, name, id);
  95. if (showUploadIndex) {
  96. layer.close(showUploadIndex);
  97. showUploadIndex = false;
  98. } else {
  99. layer.closeAll();
  100. }
  101. } else if (showUploadType == 3) {
  102. var value = showUploadE.attr('editor_id');
  103. KindEditor.insertHtml('#' + value, '<div class="dever-video"><embed src="'+url+'" type="video/x-ms-asf-plugin" width="450" height="300" autostart="false" loop="true" data-id="'+id+'"/><div style="width:450px;height:20px;border:1px solid #bababa;">'+name+':'+id+'(无法修改)</div></div>');
  104. if (showUploadIndex) {
  105. layer.close(showUploadIndex);
  106. showUploadIndex = false;
  107. } else {
  108. layer.closeAll();
  109. }
  110. }
  111. }
  112. function showUploadFilesSearch(e)
  113. {
  114. var p = e.parent();
  115. var pg = p.find('.file_pg').val();
  116. var key = p.find('.file_key').val();
  117. var name = p.find('.file_filename').val();
  118. var tag = p.find('.file_tag').val();
  119. var cate = p.find('.file_cate').val();
  120. var cur = p.find('.file_cur').val();
  121. var url = showUploadUrl;
  122. showUploadFiles(url, name, key, cate, tag, cur, pg);
  123. }
  124. function showUploadFiles(url, name, key, cate, tag, cur, pg)
  125. {
  126. if (!pg) {
  127. pg = 1;
  128. }
  129. $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur}, function(t)
  130. {
  131. var t = '<div>' + t.data + '</div>';
  132. html = $(t).find('#component-anim').html();
  133. $("#component-anim").html(html);
  134. })
  135. }
  136. function loadUpload(i,e,key,url,assets)
  137. {
  138. var value = e.attr('v');
  139. if (typeof(upload[value]) != "undefined") {
  140. //return;
  141. }
  142. upload[value] = true;
  143. upload_pic[value] = [];
  144. upload_file[value] = [];
  145. var id = e.attr('id');
  146. layui.use(['upload','layer','element'], function() {
  147. var layer = layui.layer;
  148. var layuiUpload = layui.upload;
  149. var element = layui.element;
  150. var uploadInst = layuiUpload.render({
  151. elem: '#' + id
  152. ,data: {'key' : key}
  153. ,field: 'file'
  154. ,url: url
  155. ,multiple: true
  156. ,accept:'file'
  157. ,xhr:xhrOnProgress
  158. ,progress:function(value){//上传进度回调 value进度值
  159. $('#' + id + '_progress').show();
  160. element.progress(id + '_progress', value+'%')//设置页面进度条
  161. }
  162. ,choose: function(obj){
  163. element.progress(id + '_progress', '0%')
  164. layer.load();
  165. }
  166. ,done: function(data) {
  167. element.progress(id + '_progress', '0%')
  168. $('#' + id + '_progress').hide();
  169. layer.closeAll('loading');
  170. if (data.status == 1) {
  171. loadUploadSet(e,value,data);
  172. } else {
  173. element.progress(id + '_progress', '0%')
  174. layer.alert(data.message);
  175. return false;
  176. }
  177. }
  178. ,error: function(){
  179. //请求异常回调
  180. }
  181. });
  182. });
  183. }
  184. function loadUploadSet(e,value,data)
  185. {
  186. var mul = e.attr('mul');
  187. if (mul == 'yes') {
  188. if (e.attr('t') == 'file') {
  189. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  190. upload_file[value] = $("#" + value).val().split(',');
  191. }
  192. var m = $('.' +value+'_mul');
  193. var i = m.find('li').eq(upload_file[value].length);
  194. if (i.length) {
  195. i.find('a').eq(0).html(data.name);
  196. } else {
  197. 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>');
  198. }
  199. upload_file[value].push(data.url);
  200. $("#" + value).val(upload_file.join(','));
  201. } else {
  202. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  203. upload_pic[value] = $("#" + value).val().split(',');
  204. }
  205. var m = $('.' +value+'_mul');
  206. var i = m.find('img').eq(upload_pic[value].length);
  207. if (i.length) {
  208. i.attr('src', data.url);
  209. } else {
  210. m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
  211. }
  212. upload_pic[value].push(data.url);
  213. $("#" + value).val(upload_pic[value].join(','));
  214. }
  215. } else {
  216. if (e.attr('t') == 'file') {
  217. } else {
  218. $('#show_'+value).attr('src',data.url).show();
  219. $('#show_'+value).unbind('click').bind('click', function()
  220. {
  221. picDel($(this), value, 1);
  222. })
  223. }
  224. $('#'+value).val(data.url);
  225. }
  226. }
  227. function picDel(e,v,s)
  228. {
  229. if (confirm('确定要删除图片吗?')) {
  230. if (s == 1) {
  231. e.hide();
  232. $("#" + v).val('');
  233. } else {
  234. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  235. upload_pic[v] = $("#" + v).val().split(',');
  236. }
  237. var p = e.find('img').attr('src');
  238. e.remove();
  239. upload_pic[v].remove(p);
  240. $("#" + v).val(upload_pic[v].join(','));
  241. }
  242. }
  243. }
  244. function fileDel(e,v)
  245. {
  246. if(confirm('确定要删除文件吗?'))
  247. {
  248. if(upload_file[v].length <= 0 && $("#" + v).val())
  249. {
  250. upload_file[v] = $("#" + v).val().split(',');
  251. }
  252. var parent = e.parent();
  253. var p = parent.find('a').eq(0).attr('href');
  254. parent.remove();
  255. upload_file[v].remove(p);
  256. //console.info(pic);
  257. $("#" + v).val(upload_file[v].join(','));
  258. }
  259. }