upload.js 12 KB

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