国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

214 lines
9.6KB

  1. <?php
  2. require_once(dirname(__FILE__)."/config.php");
  3. include(DEDEDATA.'/mark/inc_photowatermark_config.php');
  4. ?>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  10. <meta name="viewport" content="user-scalable=no,width=device-width,initial-scale=1.0,maximum-scale=1.0">
  11. <meta name="apple-mobile-web-app-capable" content="yes">
  12. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  13. <meta name="format-detection" content="telephone=no">
  14. <title>插入多图</title>
  15. <link rel="stylesheet" href="/static/web/css/font-awesome.min.css">
  16. <link rel="stylesheet" href="/static/web/css/bootstrap.min.css">
  17. <link rel="stylesheet" href="/static/web/css/admin.css">
  18. </head>
  19. <body>
  20. <div id="wrap">
  21. <div id="topbar">
  22. <label><input type="checkbox" name="isWater" id="isWater" <?php if ($photo_markup == '1') echo 'checked';?>> 是否水印</label>
  23. <button class="btn btn-success btn-sm addfile">添加图片</button>
  24. <button class="btn btn-success btn-sm removeall">清空图片</button>
  25. <button class="btn btn-success btn-sm upall">全部上传</button>
  26. </div>
  27. <ul id="file_list"></ul>
  28. </div>
  29. <script>
  30. var axupimgs = {};
  31. axupimgs.res = [];//存放本地文件的数组
  32. var blobInfo = {file:null}
  33. blobInfo.blob = function() {
  34. return this.file;
  35. }
  36. var upload_handler = async(blobInfo, succFun, failFun) => {
  37. var file = blobInfo.blob();
  38. formData = new FormData();
  39. formData.append('upload', file, file.name);
  40. formData.append('format', "json");
  41. if (document.querySelector('#isWater').checked) {
  42. formData.append('needwatermark', 1);
  43. } else {
  44. //formData.append('needwatermark', 0);
  45. }
  46. let res = await fetch('select_images_post.php', {
  47. method: 'POST',
  48. body: formData
  49. });
  50. let data = await res.json();
  51. if (typeof data.msg !== 'undefined' && data.msg !== '') {
  52. alert(data.msg)
  53. return;
  54. }
  55. succFun(data.url);
  56. };
  57. var upload_base_path = axupimgs.images_upload_base_path;
  58. //为列表添加排序
  59. function reSort() {
  60. document.querySelectorAll('#file_list li').forEach((el, i) => {
  61. el.setAttribute('data-num', i);
  62. });
  63. }
  64. function isFileImage(file) {
  65. return file && file['type'].split('/')[0] === 'image';
  66. }
  67. function addList(files) {
  68. var files_sum = files.length;
  69. var vDom = document.createDocumentFragment();
  70. for (let i=0;i<files_sum;i++) {
  71. let file = files[i];
  72. if (!isFileImage(file)) {
  73. alert("选择非图片文件无法上传")
  74. return;
  75. }
  76. let blobUrl = window.URL.createObjectURL(file)
  77. axupimgs.res.push({file:file,blobUrl:blobUrl,url:''});
  78. let li = document.createElement('li');
  79. li.setAttribute('class','up-no');
  80. li.setAttribute('data-time',file.lastModified);
  81. li.innerHTML='<div class="picbox"><img src="'+blobUrl+'"></div><div class="namebox"><span>'+file.name+'</span></div><div class="tools"><a class="remove"></a></div>';
  82. vDom.appendChild(li);
  83. }
  84. document.querySelector('#file_list').appendChild(vDom);
  85. //reSort();
  86. }
  87. //清空列表
  88. document.querySelector('#topbar .removeall').addEventListener('click', () => {
  89. axupimgs.res=[]
  90. document.querySelectorAll('#file_list li').forEach((el, i) => {
  91. el.parentNode.removeChild(el)
  92. });
  93. });
  94. //拖拽添加
  95. document.addEventListener('dragover', (e) => {
  96. e.stopPropagation();
  97. e.preventDefault();
  98. e.dataTransfer.dropEffect = 'copy';
  99. });
  100. document.addEventListener('drop', (e) => {
  101. e.stopPropagation();
  102. e.preventDefault();
  103. if (!e.dataTransfer.files) {
  104. return false;
  105. }
  106. var dropfiles = e.dataTransfer.files;
  107. if (!(dropfiles.length > 0)) {
  108. return false;
  109. }
  110. var exts='.png,.gif,.jpg,.jpeg'.replace(/(\s)+/g,'').toLowerCase().split(',');
  111. var files=[];
  112. for ( let file of dropfiles ) {
  113. ext = file.name.split('.');
  114. ext = '.'+ext[ext.length-1];
  115. for (let s of exts) {
  116. if (s==ext) {
  117. files.push(file);
  118. break;
  119. }
  120. }
  121. }
  122. if (files.length > 0) {
  123. addList(files)
  124. }
  125. });
  126. //添加文件
  127. document.querySelector('#topbar .addfile').addEventListener('click', () => {
  128. var input = document.createElement('input');
  129. input.setAttribute('type', 'file');
  130. input.setAttribute('multiple', 'multiple');
  131. input.setAttribute('accept', axupimgs.axupimgs_filetype);
  132. input.click();
  133. input.onchange = function() {
  134. var files = this.files;
  135. addList(files);
  136. }
  137. });
  138. var file_i = 0;
  139. function upAllFiles(n) {
  140. var len = axupimgs.res.length;
  141. file_i = n;
  142. if (len == n) {
  143. file_i=0;
  144. document.querySelector('#topbar .upall').innerText='全部上传';
  145. //返回
  146. axupimgs.res.forEach((v, k) => {
  147. let addonHTML = `<img src='${v.url}'>`;
  148. window.opener.CKEDITOR.instances["<?php echo $f ?>"].insertHtml(addonHTML);
  149. })
  150. window.close();
  151. return true;
  152. }
  153. if (axupimgs.res[n].url!='') {
  154. n++;
  155. upAllFiles(n)
  156. } else {
  157. blobInfo.file=axupimgs.res[n].file;
  158. blobInfo.isWater = document.querySelector('#isWater').checked;
  159. upload_handler(blobInfo, function(url) {
  160. if (upload_base_path) {
  161. if (upload_base_path.slice(-1)=='/' && url.substr(0,1)=='/') {
  162. url = upload_base_path + url.slice(1);
  163. } else if (upload_base_path.slice(-1)!='/' && url.substr(0,1)!='/') {
  164. url = upload_base_path + '/' + url;
  165. } else {
  166. url = upload_base_path + url;
  167. }
  168. }
  169. axupimgs.res[file_i].url = url;
  170. filename = url.split('/').pop();
  171. var li = document.querySelectorAll('#file_list li')[file_i];
  172. li.setAttribute('class','up-over');
  173. li.querySelector('.namebox span').innerText = filename;
  174. n++
  175. upAllFiles(n);
  176. },function(err) {
  177. document.querySelector('#topbar .upall').innerText='全部上传';
  178. document.querySelectorAll('#file_list li.up-now').forEach((el,i) => {
  179. el.setAttribute('class','up-no');
  180. });
  181. alert(err);
  182. });
  183. }
  184. }
  185. document.querySelector('#topbar .upall').addEventListener('click', (e) => {
  186. if (e.target.innerText!='全部上传') {
  187. return false;
  188. }
  189. if (axupimgs.res.length > 0) {
  190. document.querySelectorAll('#file_list li.up-no').forEach((el, i) => {
  191. el.classList ? el.classList.add('up-now') : el.className+=' up-now';
  192. });
  193. e.target.innerText='上传中';
  194. upAllFiles(0);
  195. }
  196. });
  197. var observ_flist = new MutationObserver( (muList, observe) => {
  198. if (muList[0].addedNodes.length > 0) {
  199. muList[0].addedNodes.forEach((el) => {
  200. el.querySelector('.remove').addEventListener('click', (e) => {
  201. var li = e.target.parentNode.parentNode;
  202. var n = li.getAttribute('data-num');
  203. var el = document.querySelectorAll('#file_list li')[n];
  204. el.parentNode.removeChild(el);
  205. axupimgs.res.splice(n, 1);
  206. });
  207. });
  208. }
  209. reSort();
  210. });
  211. observ_flist.observe(document.querySelector('#file_list'),{childList:true});
  212. </script>
  213. </body>
  214. </html>