upload.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. var key = showUploadE.attr('key');
  104. 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+'" data-key="'+key+'"/><div style="width:450px;height:20px;">'+name+':'+id+'(无法修改)</div></div>');
  105. if (showUploadIndex) {
  106. layer.close(showUploadIndex);
  107. showUploadIndex = false;
  108. } else {
  109. layer.closeAll();
  110. }
  111. }
  112. }
  113. function showUploadFilesSearch(e)
  114. {
  115. var p = e.parent();
  116. var pg = p.find('.file_pg').val();
  117. var key = p.find('.file_key').val();
  118. var name = p.find('.file_filename').val();
  119. var tag = p.find('.file_tag').val();
  120. var cate = p.find('.file_cate').val();
  121. var cur = p.find('.file_cur').val();
  122. var url = showUploadUrl;
  123. showUploadFiles(url, name, key, cate, tag, cur, pg);
  124. }
  125. function showUploadFiles(url, name, key, cate, tag, cur, pg)
  126. {
  127. if (!pg) {
  128. pg = 1;
  129. }
  130. $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur}, function(t)
  131. {
  132. var t = '<div>' + t.data + '</div>';
  133. html = $(t).find('#component-anim').html();
  134. $("#component-anim").html(html);
  135. })
  136. }
  137. function loadUpload(i,e,key,url,assets)
  138. {
  139. var value = e.attr('v');
  140. if (typeof(upload[value]) != "undefined") {
  141. //return;
  142. }
  143. upload[value] = true;
  144. upload_pic[value] = [];
  145. upload_file[value] = [];
  146. var id = e.attr('id');
  147. layui.use(['upload','layer','element'], function() {
  148. var layer = layui.layer;
  149. var layuiUpload = layui.upload;
  150. var element = layui.element;
  151. var uploadInst = layuiUpload.render({
  152. elem: '#' + id
  153. ,data: {'key' : key}
  154. ,field: 'file'
  155. ,url: url
  156. ,multiple: true
  157. ,accept:'file'
  158. ,xhr:xhrOnProgress
  159. ,progress:function(value){//上传进度回调 value进度值
  160. $('#' + id + '_progress').show();
  161. element.progress(id + '_progress', value+'%')//设置页面进度条
  162. }
  163. ,choose: function(obj){
  164. element.progress(id + '_progress', '0%')
  165. layer.load();
  166. }
  167. ,done: function(data) {
  168. element.progress(id + '_progress', '0%')
  169. $('#' + id + '_progress').hide();
  170. layer.closeAll('loading');
  171. if (data.status == 1) {
  172. loadUploadSet(e,value,data);
  173. } else {
  174. element.progress(id + '_progress', '0%')
  175. layer.alert(data.message);
  176. return false;
  177. }
  178. }
  179. ,error: function(){
  180. //请求异常回调
  181. }
  182. });
  183. });
  184. }
  185. function loadUploadSet(e,value,data)
  186. {
  187. var mul = e.attr('mul');
  188. if (mul == 'yes') {
  189. if (e.attr('t') == 'file') {
  190. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  191. upload_file[value] = $("#" + value).val().split(',');
  192. }
  193. var m = $('.' +value+'_mul');
  194. var i = m.find('li').eq(upload_file[value].length);
  195. if (i.length) {
  196. i.find('a').eq(0).html(data.name);
  197. } else {
  198. 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>');
  199. }
  200. upload_file[value].push(data.url);
  201. $("#" + value).val(upload_file.join(','));
  202. } else {
  203. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  204. upload_pic[value] = $("#" + value).val().split(',');
  205. }
  206. var m = $('.' +value+'_mul');
  207. var i = m.find('img').eq(upload_pic[value].length);
  208. if (i.length) {
  209. i.attr('src', data.url);
  210. } else {
  211. m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
  212. }
  213. upload_pic[value].push(data.url);
  214. $("#" + value).val(upload_pic[value].join(','));
  215. }
  216. } else {
  217. if (e.attr('t') == 'file') {
  218. } else {
  219. $('#show_'+value).attr('src',data.url).show();
  220. $('#show_'+value).unbind('click').bind('click', function()
  221. {
  222. picDel($(this), value, 1);
  223. })
  224. }
  225. $('#'+value).val(data.url);
  226. }
  227. }
  228. function picDel(e,v,s)
  229. {
  230. if (confirm('确定要删除图片吗?')) {
  231. if (s == 1) {
  232. e.hide();
  233. $("#" + v).val('');
  234. } else {
  235. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  236. upload_pic[v] = $("#" + v).val().split(',');
  237. }
  238. var p = e.find('img').attr('src');
  239. e.remove();
  240. upload_pic[v].remove(p);
  241. $("#" + v).val(upload_pic[v].join(','));
  242. }
  243. }
  244. }
  245. function fileDel(e,v)
  246. {
  247. if(confirm('确定要删除文件吗?'))
  248. {
  249. if(upload_file[v].length <= 0 && $("#" + v).val())
  250. {
  251. upload_file[v] = $("#" + v).val().split(',');
  252. }
  253. var parent = e.parent();
  254. var p = parent.find('a').eq(0).attr('href');
  255. parent.remove();
  256. upload_file[v].remove(p);
  257. //console.info(pic);
  258. $("#" + v).val(upload_file[v].join(','));
  259. }
  260. }