upload.js 14 KB

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