upload.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.unbind('click').bind('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. var value = '';
  51. if (e.attr('data-value')) {
  52. value = e.attr('data-value');
  53. }
  54. $.getJSON(url, {json:1,key:key,cur:cur,value:value}, function(t)
  55. {
  56. layer.closeAll();
  57. config.content = t.data;
  58. showUploadIndex = layer.open(config);
  59. })
  60. })
  61. });
  62. }
  63. function editorShowUpload(cur, url, title, func, self)
  64. {
  65. showUploadType = 2;
  66. showUploadCall = func;
  67. showUploadThis = self;
  68. layui.use(['upload','layer','element'], function() {
  69. var html = '';
  70. var layer = layui.layer;
  71. var config = {
  72. type: 0,
  73. title: title,
  74. shade: 0.1,
  75. shadeClose : true,
  76. content: html,
  77. area: '800px'
  78. };
  79. url = url.replace('save', 'view');
  80. showUploadUrl = url;
  81. $.getJSON(url, {json:1,cur:cur}, function(t)
  82. {
  83. config.content = t.data;
  84. showUploadIndex = layer.open(config);
  85. })
  86. });
  87. }
  88. function showUploadFilesSet(e, name, url, id)
  89. {
  90. $('.layui-anim-yes').addClass('layui-anim-no');
  91. $('.layui-anim-yes').removeClass('layui-anim-yes');
  92. e.parent().removeClass('layui-anim-no');
  93. e.parent().addClass('layui-anim-yes');
  94. $('.file_cur').val(url);
  95. if (showUploadType == 1) {
  96. var value = showUploadE.attr('v');
  97. loadUploadSet(showUploadE, value, {name:name,url:url});
  98. } else if (showUploadType == 2) {
  99. showUploadCall.call(showUploadThis, url, name, id);
  100. if (showUploadIndex) {
  101. layer.close(showUploadIndex);
  102. showUploadIndex = false;
  103. } else {
  104. layer.closeAll();
  105. }
  106. } else if (showUploadType == 3) {
  107. var value = showUploadE.attr('editor_id');
  108. var key = showUploadE.attr('key');
  109. //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>');
  110. KindEditor.insertHtml('#' + value, '<img src="'+url+'" style="width:450px;height:300px;" data-id="'+id+'" data-key="'+key+'"/>');
  111. if (showUploadIndex) {
  112. layer.close(showUploadIndex);
  113. showUploadIndex = false;
  114. } else {
  115. layer.closeAll();
  116. }
  117. }
  118. }
  119. function showUploadFilesSearch(e)
  120. {
  121. var p = e.parent();
  122. var pg = p.find('.file_pg').val();
  123. var key = p.find('.file_key').val();
  124. var name = p.find('.file_filename').val();
  125. var tag = p.find('.file_tag').val();
  126. var cate = p.find('.file_cate').val();
  127. var cur = p.find('.file_cur').val();
  128. var url = showUploadUrl;
  129. showUploadFiles(url, name, key, cate, tag, cur, pg);
  130. }
  131. function showUploadFiles(url, name, key, cate, tag, cur, pg)
  132. {
  133. if (!pg) {
  134. pg = 1;
  135. }
  136. $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur}, function(t)
  137. {
  138. var t = '<div>' + t.data + '</div>';
  139. html = $(t).find('#component-anim').html();
  140. $("#component-anim").html(html);
  141. })
  142. }
  143. function loadUploadL(i,e,key,url,assets)
  144. {
  145. var value = e.attr('v');
  146. if (typeof(upload[value]) != "undefined") {
  147. //return;
  148. }
  149. upload[value] = true;
  150. upload_pic[value] = [];
  151. upload_file[value] = [];
  152. var id = e.attr('id');
  153. var token = e.attr('token');
  154. var host = e.attr('host');
  155. var domain = e.attr('domain');
  156. var path = e.attr('path');
  157. var cover = e.attr('cover');
  158. if (!cover) {
  159. cover = 1;
  160. }
  161. var uploadUrl = url;
  162. url = domain;
  163. var config = {
  164. useCdnDomain: true,
  165. disableStatisticsReport: false,
  166. retryCount: 6
  167. };
  168. var putExtra = {
  169. fname: "",
  170. params: {},
  171. mimeType: null
  172. };
  173. layui.use(['layer','element'], function() {
  174. var layer = layui.layer;
  175. var element = layui.element;
  176. $("#" + id).unbind("change").bind("change",function() {
  177. var file = this.files[0];
  178. // eslint-disable-next-line
  179. var finishedAttr = [];
  180. // eslint-disable-next-line
  181. var compareChunks = [];
  182. var observable;
  183. if (file) {
  184. element.progress(id + '_progress', '0%')
  185. layer.load();
  186. var filename = file.name;
  187. var temp = filename.split('.');
  188. putExtra.params["x:name"] = temp[0];
  189. filename = path + hex_md5(file.name) + '.' + temp[temp.length-1];
  190. //不覆盖
  191. if (cover == 2) {
  192. var timestamp = Date.parse(new Date());
  193. filename = path + hex_md5(file.name + '_' + timestamp) + '.' + temp[temp.length-1];
  194. }
  195. // 设置next,error,complete对应的操作,分别处理相应的进度信息,错误信息,以及完成后的操作
  196. var error = function(err) {
  197. //请求异常回调
  198. console.log(err);
  199. layer.alert('文件上传错误1');
  200. element.progress(id + '_progress', '0%')
  201. $('#' + id + '_progress').hide();
  202. layer.closeAll('loading');
  203. };
  204. var complete = function(res) {
  205. //&& res.key.match(/\.(jpg|jpeg|png|gif)$/)
  206. if (res.key) {
  207. element.progress(id + '_progress', '0%')
  208. $('#' + id + '_progress').hide();
  209. layer.closeAll('loading');
  210. var data = {};
  211. if (res.hash) {
  212. $.getJSON(uploadUrl.replace('save.start', 'qiniu.addFile'), {source:file.name,file:res.key, key:key});
  213. data.status = 1;
  214. data.name = res.key;
  215. data.url = host + res.key;
  216. loadUploadSet(e,value,data);
  217. } else {
  218. layer.alert('文件上传错误2');
  219. return false;
  220. }
  221. }
  222. };
  223. var next = function(response) {
  224. var chunks = response.chunks||[];
  225. var total = response.total;
  226. // 这里对每个chunk更新进度,并记录已经更新好的避免重复更新,同时对未开始更新的跳过
  227. for (var i = 0; i < chunks.length; i++) {
  228. if (chunks[i].percent === 0 || finishedAttr[i]){
  229. continue;
  230. }
  231. if (compareChunks[i].percent === chunks[i].percent){
  232. continue;
  233. }
  234. if (chunks[i].percent === 100){
  235. finishedAttr[i] = true;
  236. }
  237. }
  238. $('#' + id + '_progress').show();
  239. element.progress(id + '_progress', parseInt(total.percent)+'%')
  240. compareChunks = chunks;
  241. };
  242. var subObject = {
  243. next: next,
  244. error: error,
  245. complete: complete
  246. };
  247. var subscription;
  248. // 调用sdk上传接口获得相应的observable,控制上传和暂停
  249. observable = qiniu.upload(file, filename, token, putExtra, config);
  250. subscription = observable.subscribe(subObject);
  251. }
  252. })
  253. });
  254. }
  255. function loadUpload(i,e,key,url,assets)
  256. {
  257. var value = e.attr('v');
  258. if (typeof(upload[value]) != "undefined") {
  259. //return;
  260. }
  261. upload[value] = true;
  262. upload_pic[value] = [];
  263. upload_file[value] = [];
  264. var data = {};
  265. data.key = key;
  266. var token = e.attr('token');
  267. var host = e.attr('host');
  268. var domain = e.attr('domain');
  269. var path = e.attr('path');
  270. var cover = e.attr('cover');
  271. if (!cover) {
  272. cover = 1;
  273. }
  274. if (token) {
  275. data.token = token;
  276. }
  277. var uploadUrl = url;
  278. if (domain) {
  279. url = domain;
  280. delete data.key;
  281. //data.key = 2;
  282. }
  283. var id = e.attr('id');
  284. layui.use(['upload','layer','element'], function() {
  285. var layer = layui.layer;
  286. var layuiUpload = layui.upload;
  287. var element = layui.element;
  288. var uploadInst = layuiUpload.render({
  289. elem: '#' + id
  290. ,data: data
  291. ,field: 'file'
  292. ,url: url
  293. ,multiple: true
  294. ,accept:'file'
  295. //,auto:false
  296. ,xhr:xhrOnProgress
  297. ,progress:function(value){//上传进度回调 value进度值
  298. $('#' + id + '_progress').show();
  299. element.progress(id + '_progress', value+'%')//设置页面进度条
  300. }
  301. ,choose: function(obj, self){
  302. if (domain) {
  303. obj.editFile(path, cover);
  304. }
  305. element.progress(id + '_progress', '0%')
  306. //layer.load();
  307. }
  308. ,done: function(data, file) {
  309. if (data.hash) {
  310. data.status = 1;
  311. data.name = data.key;
  312. data.url = host + data.key;
  313. $.getJSON(uploadUrl.replace('save.start', 'qiniu.addFile'), {source:file.name,file:data.key, key:key});
  314. }
  315. element.progress(id + '_progress', '0%')
  316. $('#' + id + '_progress').hide();
  317. layer.closeAll('loading');
  318. if (data.status == 1) {
  319. loadUploadSet(e,value,data);
  320. } else {
  321. element.progress(id + '_progress', '0%')
  322. layer.alert(data.message);
  323. return false;
  324. }
  325. }
  326. ,error: function(){
  327. //请求异常回调
  328. layer.alert('文件上传错误');
  329. element.progress(id + '_progress', '0%')
  330. $('#' + id + '_progress').hide();
  331. layer.closeAll('loading');
  332. }
  333. });
  334. });
  335. }
  336. function loadUploadSet(e,value,data)
  337. {
  338. var mul = e.attr('mul');
  339. if (mul == 'yes') {
  340. if (e.attr('t') == 'file') {
  341. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  342. upload_file[value] = $("#" + value).val().split(',');
  343. }
  344. var m = $('.' +value+'_mul');
  345. var i = m.find('li').eq(upload_file[value].length);
  346. if (i.length) {
  347. i.find('a').eq(0).html(data.name);
  348. } else {
  349. 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>');
  350. }
  351. upload_file[value].push(data.url);
  352. $("#" + value).val(upload_file.join(','));
  353. } else {
  354. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  355. upload_pic[value] = $("#" + value).val().split(',');
  356. }
  357. var m = $('.' +value+'_mul');
  358. var i = m.find('img').eq(upload_pic[value].length);
  359. if (i.length) {
  360. i.attr('src', data.url);
  361. } else {
  362. m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
  363. }
  364. upload_pic[value].push(data.url);
  365. $("#" + value).val(upload_pic[value].join(','));
  366. }
  367. } else {
  368. if (e.attr('t') == 'file') {
  369. } else {
  370. $('#show_'+value).attr('src',data.url).show();
  371. $('#show_'+value).unbind('click').bind('click', function()
  372. {
  373. picDel($(this), value, 1);
  374. })
  375. }
  376. $('#'+value).val(data.url);
  377. }
  378. }
  379. function picDel(e,v,s)
  380. {
  381. if (confirm('确定要删除图片吗?')) {
  382. if (s == 1) {
  383. e.hide();
  384. $("#" + v).val('');
  385. } else {
  386. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  387. upload_pic[v] = $("#" + v).val().split(',');
  388. }
  389. var p = e.find('img').attr('src');
  390. e.remove();
  391. upload_pic[v].remove(p);
  392. $("#" + v).val(upload_pic[v].join(','));
  393. }
  394. }
  395. }
  396. function fileDel(e,v)
  397. {
  398. if(confirm('确定要删除文件吗?'))
  399. {
  400. if(upload_file[v].length <= 0 && $("#" + v).val())
  401. {
  402. upload_file[v] = $("#" + v).val().split(',');
  403. }
  404. var parent = e.parent();
  405. var p = parent.find('a').eq(0).attr('href');
  406. parent.remove();
  407. upload_file[v].remove(p);
  408. //console.info(pic);
  409. $("#" + v).val(upload_file[v].join(','));
  410. }
  411. }