Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get_upload_list.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. var axupimgs = new Array();
  2. var arrimg = new Array();
  3. var arrimgname = new Array();
  4. $(function() {
  5. // 左侧分组里的图片数量
  6. $('#count_'+type_id, window.parent.document).html(countimg);
  7. var img_id_upload = $.cookie("img_id_upload");
  8. if (undefined != img_id_upload && img_id_upload.length > 0) {
  9. arrimg = img_id_upload.split(",");
  10. }
  11. var imgname_id_upload = $.cookie("imgname_id_upload");
  12. if (undefined != imgname_id_upload && imgname_id_upload.length > 0){
  13. arrimgname = imgname_id_upload.split(",");
  14. }
  15. // 检测是否选择
  16. if (num > 1) {
  17. $("#file_list li").each(function(index, item) {
  18. $(item).removeClass('up-over');
  19. var val = $(item).attr("data-img");
  20. for (var i = arrimg.length - 1; i >= 0; i--) {
  21. if (arrimg[i].indexOf(val) != -1 || val.indexOf(arrimg[i]) != -1) {
  22. $(item).addClass('up-over');
  23. }
  24. }
  25. });
  26. $('.removeall').html('删除选中('+arrimg.length+')');
  27. if (arrimg.length > 1) {
  28. $('.removeall').show();
  29. } else {
  30. $('.removeall').hide();
  31. }
  32. } else {
  33. $("#file_list li").each(function(index, item) {
  34. $(item).removeClass('up-over');
  35. var val = $(item).attr("data-img");
  36. for (var i = arrimg.length - 1; i >= 0; i--) {
  37. if (arrimg[i].indexOf(val) != -1 || val.indexOf(arrimg[i]) != -1) {
  38. $(item).addClass('up-over');
  39. break;
  40. }
  41. }
  42. });
  43. }
  44. });
  45. // 删除列表图片
  46. function delimg(obj, img_id) {
  47. layer_loading('正在处理');
  48. var img_id_arr = [img_id];
  49. $.ajax({
  50. type: 'post',
  51. url: eyou_basefile + "?m="+module_name+"&c=Uploadimgnew&a=del_uploadsimg&lang=" + __lang__,
  52. data: {img_id:img_id_arr, _ajax:1},
  53. dataType: 'json',
  54. success: function (res) {
  55. layer.closeAll();
  56. if (res.code == 1) {
  57. layer.msg(res.msg, {icon: 6, time: 1000}, function() {
  58. window.location.reload();
  59. });
  60. } else {
  61. layer.msg(res.msg, {icon: 5});
  62. }
  63. },
  64. error : function(e) {
  65. layer.closeAll();
  66. layer.alert(e.responseText, {icon: 5, title: false, closeBtn: false});
  67. }
  68. });
  69. }
  70. // 删除选中的图片记录
  71. function batch_delimg(obj) {
  72. var img_id_arr = [];
  73. $('#file_list li').each(function(i,o){
  74. if($(o).hasClass('up-over')){
  75. img_id_arr.push($(o).attr('data-id'));
  76. }
  77. })
  78. if(img_id_arr.length == 0){
  79. layer.msg('请至少选择一张!', {icon: 5});
  80. return;
  81. }
  82. layer_loading('正在处理');
  83. $.ajax({
  84. type: 'post',
  85. url : eyou_basefile + "?m="+module_name+"&c=Uploadimgnew&a=del_uploadsimg&lang=" + __lang__,
  86. data: {img_id: img_id_arr, _ajax: 1},
  87. dataType: 'json',
  88. success: function (res) {
  89. layer.closeAll();
  90. if (res.code == 1) {
  91. $.cookie("img_id_upload", "");
  92. $.cookie("imgname_id_upload", "");
  93. layer.msg(res.msg, {icon: 6, time: 1000}, function() {
  94. window.location.reload();
  95. });
  96. } else {
  97. layer.msg(res.msg, {icon: 5});
  98. }
  99. },
  100. error : function(e) {
  101. layer.closeAll();
  102. layer.alert(e.responseText, {icon: 5, title: false, closeBtn: false});
  103. }
  104. });
  105. }
  106. function getLastMonth(){
  107. var now=new Date();
  108. var year = now.getFullYear();//getYear()+1900=getFullYear()
  109. var month = now.getMonth() +1;//0-11表示1-12月
  110. var day = now.getDate();
  111. var dateObj = {};
  112. if(parseInt(month)<10){
  113. month = "0"+month;
  114. }
  115. if(parseInt(day)<10){
  116. day = "0"+day;
  117. }
  118. dateObj.now = year + '-'+ month + '-' + day;
  119. if (parseInt(month) ==1) {//如果是1月份,则取上一年的12月份
  120. dateObj.last = (parseInt(year) - 1) + '-12-' + day;
  121. return dateObj;
  122. }
  123. var preSize= new Date(year, parseInt(month)-1, 0).getDate();//上月总天数
  124. if (preSize < parseInt(day)) {//上月总天数<本月日期,比如3月的30日,在2月中没有30
  125. dateObj.last = year + '-' + month + '-01';
  126. return dateObj;
  127. }
  128. if(parseInt(month) <=10){
  129. dateObj.last = year + '-0' + (parseInt(month)-1) + '-' + day;
  130. return dateObj;
  131. }else{
  132. dateObj.last = year + '-' + (parseInt(month)-1) + '-' + day;
  133. return dateObj;
  134. }
  135. }
  136. // layui 操作
  137. layui.use(function() {
  138. var layer = layui.layer,
  139. form = layui.form,
  140. $ = layui.$,
  141. element = layui.element,
  142. laydate = layui.laydate;
  143. var timeObj = getLastMonth();
  144. // 日期时间范围
  145. var laydate_value = '';
  146. if (eytime == '') {
  147. laydate_value = timeObj.last + ' - ' + timeObj.now;
  148. }
  149. laydate.render({
  150. elem: '#eytime',
  151. type: 'date',
  152. range: true,
  153. value: laydate_value,
  154. calendar: true,
  155. max: timeObj.now,//默认最大值为当前日期
  156. done: function(value) {
  157. $('#eytime').val(value);
  158. $('#searchForm').submit();
  159. }
  160. });
  161. if (eytime == '') {
  162. lay('#eytime').val('');
  163. }
  164. // 点击选中保存图片
  165. $(document).on("click", ".image-list li .picbox", function() {
  166. var li = $(this).parent('.image-list li');
  167. var val = li.attr("data-img");
  168. var title = li.attr("data-title");
  169. var img_id = li.attr("data-id");
  170. var selectlayer = li.hasClass('up-over');
  171. if (selectlayer) {
  172. li.removeClass('up-over');
  173. var indx = arrimg.indexOf(val);
  174. if(indx != -1) arrimg.splice(indx, 1);
  175. var indx = arrimgname.indexOf(title);
  176. if(indx != -1) arrimgname.splice(indx, 1);
  177. }
  178. if (num > 1) {
  179. if (!selectlayer) {
  180. li.addClass('up-over');
  181. arrimg.push(val);
  182. arrimgname.push(title);
  183. }
  184. } else {
  185. $.cookie("img_id_upload", "");
  186. $.cookie("imgname_id_upload", "");
  187. $("#file_list li").removeClass('up-over');
  188. if (!selectlayer) {
  189. li.addClass('up-over');
  190. arrimg = [];
  191. arrimg.push(val);
  192. arrimgname = [];
  193. arrimgname.push(title);
  194. }
  195. }
  196. $.cookie("img_id_upload", arrimg.join());
  197. $.cookie("imgname_id_upload", arrimgname.join());
  198. document.querySelector('.removeall').innerText = '删除选中('+arrimg.length+')';
  199. if (arrimg.length > 1) {
  200. $('.removeall').show();
  201. } else {
  202. $('.removeall').hide();
  203. }
  204. });
  205. });
  206. // 添加文件
  207. document.querySelector('#topbar .addfile').addEventListener("click", function(){
  208. var input = document.createElement('input');
  209. input.setAttribute('type', 'file');
  210. if (upload_num > 1) {
  211. input.setAttribute('multiple', 'multiple');
  212. }
  213. input.setAttribute('accept', image_accept);
  214. input.setAttribute('onchange', "addfileChange(this);");
  215. input.click();
  216. });
  217. function addfileChange(obj)
  218. {
  219. var files = obj.files;
  220. if (files.length > upload_num) {
  221. layer.alert('每次最多可上传'+upload_num+'张!', {icon: 5, title: false, closeBtn: false});
  222. return false;
  223. }
  224. if (document.querySelector('.addfiletext').innerText != '上传图片') return false;
  225. // addList(files);
  226. if ($('#file_list li').length == 0) {
  227. $('#file_list').html('');
  228. }
  229. for (let i = 0; i < files.length; i++) {
  230. axupimgs.push(files[i]);
  231. }
  232. if (axupimgs.length > 0) {
  233. layer_loading('正在上传');
  234. $('#file_list li.up-no').each(function(item) {
  235. var el = item.get(0);
  236. el.classList ? el.classList.add('up-now') : el.className+=' up-now';
  237. });
  238. document.querySelector('.addfiletext').innerText = '上传中...';
  239. upAllFiles(0);
  240. }
  241. }
  242. // 添加列表
  243. function addList(files) {
  244. var files_sum = files.length;
  245. var vDom = document.createDocumentFragment();
  246. for (let i = 0; i < files_sum; i++) {
  247. let file = files[i];
  248. let blobUrl = window.URL.createObjectURL(file);
  249. axupimgs.unshift(file);
  250. var add_time = formatDate();
  251. let li = document.createElement('li');
  252. li.setAttribute('class','up-no');
  253. li.setAttribute('data-time',file.lastModified);
  254. li.setAttribute('data-img',blobUrl);
  255. li.innerHTML='<div class="picbox"><img src="'+blobUrl+'"><div class="image-select-layer"><i class="layui-icon layui-icon-ok-circle"></i></div></div><div class="namebox" style="height: 15px;"><span class="eyou_imgtime">'+file.name+'</span></div>';
  256. vDom.appendChild(li);
  257. }
  258. if ($('#file_list li').length == 0) {
  259. $('#file_list').html('');
  260. }
  261. var list = document.querySelector('#file_list');
  262. list.insertBefore(vDom, list.childNodes[0]);
  263. }
  264. // 当前的日期时间格式
  265. function formatDate() {
  266. var date = new Date();
  267. var YY = date.getFullYear() + '-';
  268. var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  269. var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  270. var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  271. var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  272. var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  273. return YY + MM + DD +" "+hh + mm + ss;
  274. }
  275. // 图片上传
  276. var file_i = 0;
  277. function upAllFiles(n) {
  278. var len = axupimgs.length;
  279. file_i = n;
  280. if (len == n) {
  281. file_i = 0;
  282. // layer_loading('正在上传');
  283. document.querySelector('#topbar .addfiletext').innerText = '上传图片';
  284. return true;
  285. }
  286. // 上传的图片数量
  287. var img_len = file_i + 1;
  288. if (axupimgs[n] != '') {
  289. if (n > upload_num - 1) {
  290. layer.alert('最多一次性上传'+upload_num+'张!', {icon: 5, title: false, closeBtn: false});
  291. return false;
  292. }
  293. var img_id_upload_tmp = $.cookie("img_id_upload");
  294. if (undefined != img_id_upload_tmp && img_id_upload_tmp.length > 0) {
  295. arrimg = img_id_upload_tmp.split(",");
  296. } else {
  297. arrimg = [];
  298. }
  299. var imgname_id_upload_tmp = $.cookie("imgname_id_upload");
  300. if (undefined != imgname_id_upload_tmp && imgname_id_upload_tmp.length > 0) {
  301. arrimgname = imgname_id_upload_tmp.split(",");
  302. } else {
  303. arrimgname = [];
  304. }
  305. var form = new FormData();
  306. var file = axupimgs[n];
  307. form.append('_ajax', 1);
  308. form.append('file', file);
  309. form.append('type_id', type_id);
  310. $.ajax({
  311. type: 'post',
  312. url : UploadUpUrl,
  313. data: form,
  314. contentType: false,
  315. processData: false,
  316. dataType: 'json',
  317. // async: false,
  318. success: function (res) {
  319. if (res.state == 'SUCCESS') {
  320. var class_up_over = ' up-over ';
  321. if (num == 1) {
  322. $.cookie("img_id_upload", "");
  323. arrimg = [];
  324. $.cookie("imgname_id_upload", "");
  325. arrimgname = [];
  326. if (n == len - 1) {
  327. class_up_over = ' up-over ';
  328. } else {
  329. class_up_over = '';
  330. }
  331. }
  332. arrimg.push(res.url);
  333. arrimgname.push(file.name);
  334. $.cookie("img_id_upload", arrimg.join());
  335. $.cookie("imgname_id_upload", arrimgname.join());
  336. var html = '';
  337. html += '<li class="up-no '+class_up_over+'" data-time="'+file.lastModified+'" data-img="'+res.url+'" data-title="'+file.name+'">';
  338. html += ' <div class="picbox">';
  339. html += ' <img src="'+res.url+'">';
  340. html += ' <div class="image-select-layer"><i class="layui-icon layui-icon-ok-circle"></i></div>';
  341. html += ' </div>';
  342. html += ' <div class="namebox" style="height: 15px;"><span class="eyou_imgtime">'+file.name+'</span></div>';
  343. html += '</li>';
  344. $('#file_list').prepend(html);
  345. if (img_len == len) {
  346. layer.closeAll();
  347. layer.msg('上传成功', {icon: 6, time: 1000, shade: [0.2]}, function() {
  348. window.location.reload();
  349. });
  350. }
  351. } else {
  352. layer.closeAll();
  353. layer.msg(res.state, {icon: 5});
  354. }
  355. n++;
  356. upAllFiles(n);
  357. },
  358. error : function(e) {
  359. $('#file_list li.up-now').each(function(item) {
  360. var el = item.get(0);
  361. el.setAttribute('class','up-no');
  362. });
  363. layer.closeAll();
  364. layer.alert(e.responseText, {icon: 5, title: false, closeBtn: false});
  365. }
  366. })
  367. }
  368. }
  369. // 加载框
  370. function layer_loading(msg) {
  371. var loading = layer.msg(
  372. msg+'...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请勿刷新页面',
  373. {
  374. icon: 6,
  375. time: 3600000, //1小时后后自动关闭
  376. shade: [0.2] //0.1透明度的白色背景
  377. });
  378. //loading层
  379. var index = layer.load(3, {
  380. shade: [0.1,'#fff'] //0.1透明度的白色背景
  381. });
  382. return loading;
  383. }