国内流行的内容管理系统(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.

215 lines
11KB

  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/font/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. <style>#wrap{padding:10px}#topbar{padding:10px 0;border-bottom:1px solid #ccc;text-align:right}#file_list{display:grid;grid-gap:10px;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));padding-top:10px}#file_list:empty:after{content:'拖拽图片到此处上传'}#file_list li{display:block;position:relative;padding:10px;vertical-align:top;border-radius:0.5rem}#file_list li:hover{background:#f5f5f5}#file_list li .picbox{display:flex;flex:0 0 auto;position:relative;padding-top:90%;width:100%;align-items:center;justify-content:center;overflow:hidden}#file_list li .picbox img{display:block;max-width:100%;max-height:100%;position:absolute;top:50%;left:50%;transform:translateX(-50%) translateY(-50%);border-radius:0.5rem}#file_list li .namebox{display:flex;padding:10px;justify-content:center;align-items:flex-start}#file_list li.up-over .picbox:after{position:absolute;bottom:10px;right:0;font-family:'FontAwesome';font-size:18px;color:#1eb867;content:'\f058';z-index:9}#file_list li .tools{display:none;position:absolute;bottom:12px;right:10px;z-index:99}#file_list li:hover .tools{display:block}#file_list li .tools .remove{cursor:pointer}#file_list li .tools .remove:after{font-family:'FontAwesome';font-size:18px;color:#dc3545;content:'\f1f8'}</style>
  19. </head>
  20. <body>
  21. <div id="wrap">
  22. <div id="topbar">
  23. <label class="mr-2"><input type="checkbox" name="isWater" id="isWater" <?php if ($photo_markup == '1') echo "checked";?>> 是否水印</label>
  24. <button class="btn btn-success btn-sm addfile">添加图片</button>
  25. <button class="btn btn-success btn-sm removeall">清空图片</button>
  26. <button class="btn btn-success btn-sm upall">全部上传</button>
  27. </div>
  28. <ul id="file_list"></ul>
  29. </div>
  30. <script>
  31. var axupimgs = {};
  32. axupimgs.res = [];//存放本地文件的数组
  33. var blobInfo = {file:null}
  34. blobInfo.blob = function() {
  35. return this.file;
  36. }
  37. var upload_handler = async(blobInfo, succFun, failFun) => {
  38. var file = blobInfo.blob();
  39. formData = new FormData();
  40. formData.append('upload', file, file.name);
  41. formData.append('format', "json");
  42. if (document.querySelector('#isWater').checked) {
  43. formData.append('needwatermark', 1);
  44. } else {
  45. //formData.append('needwatermark', 0);
  46. }
  47. let res = await fetch('select_images_post.php', {
  48. method: 'POST',
  49. body: formData
  50. });
  51. let data = await res.json();
  52. if (typeof data.msg !== 'undefined' && data.msg !== '') {
  53. alert(data.msg)
  54. return;
  55. }
  56. succFun(data.url);
  57. };
  58. var upload_base_path = axupimgs.images_upload_base_path;
  59. //为列表添加排序
  60. function reSort() {
  61. document.querySelectorAll('#file_list li').forEach((el,i) => {
  62. el.setAttribute('data-num',i);
  63. });
  64. }
  65. function isFileImage(file) {
  66. return file && file['type'].split('/')[0] === 'image';
  67. }
  68. function addList(files) {
  69. var files_sum = files.length;
  70. var vDom = document.createDocumentFragment();
  71. for (let i=0;i<files_sum;i++) {
  72. let file = files[i];
  73. if (!isFileImage(file)) {
  74. alert("选择非图片文件无法上传")
  75. return;
  76. }
  77. let blobUrl = window.URL.createObjectURL(file)
  78. axupimgs.res.push({file:file,blobUrl:blobUrl,url:''});
  79. let li = document.createElement('li');
  80. li.setAttribute('class','up-no');
  81. li.setAttribute('data-time',file.lastModified);
  82. 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>';
  83. vDom.appendChild(li);
  84. }
  85. document.querySelector('#file_list').appendChild(vDom);
  86. //reSort();
  87. }
  88. //清空列表
  89. document.querySelector('#topbar .removeall').addEventListener('click',() => {
  90. axupimgs.res=[]
  91. document.querySelectorAll('#file_list li').forEach((el,i) => {
  92. el.parentNode.removeChild(el)
  93. });
  94. });
  95. //拖拽添加
  96. document.addEventListener('dragover', (e) => {
  97. e.stopPropagation();
  98. e.preventDefault();
  99. e.dataTransfer.dropEffect = 'copy';
  100. });
  101. document.addEventListener('drop', (e) => {
  102. e.stopPropagation();
  103. e.preventDefault();
  104. if (!e.dataTransfer.files) {
  105. return false;
  106. }
  107. var dropfiles = e.dataTransfer.files;
  108. if (!(dropfiles.length > 0)) {
  109. return false;
  110. }
  111. var exts='.png,.gif,.jpg,.jpeg'.replace(/(\s)+/g,'').toLowerCase().split(',');
  112. var files=[];
  113. for ( let file of dropfiles ) {
  114. ext = file.name.split('.');
  115. ext = '.'+ext[ext.length-1];
  116. for (let s of exts) {
  117. if (s==ext) {
  118. files.push(file);
  119. break;
  120. }
  121. }
  122. }
  123. if (files.length > 0) {
  124. addList(files)
  125. }
  126. });
  127. //添加文件
  128. document.querySelector('#topbar .addfile').addEventListener('click',() => {
  129. var input = document.createElement('input');
  130. input.setAttribute('type', 'file');
  131. input.setAttribute('multiple', 'multiple');
  132. input.setAttribute('accept', axupimgs.axupimgs_filetype);
  133. input.click();
  134. input.onchange = function() {
  135. var files = this.files;
  136. addList(files);
  137. }
  138. });
  139. var file_i = 0;
  140. function upAllFiles(n) {
  141. var len = axupimgs.res.length;
  142. file_i = n;
  143. if (len == n) {
  144. file_i=0;
  145. document.querySelector('#topbar .upall').innerText='全部上传';
  146. //返回
  147. axupimgs.res.forEach((v,k) => {
  148. let addonHTML = `<img src='${v.url}'>`;
  149. window.opener.CKEDITOR.instances["<?php echo $f ?>"].insertHtml(addonHTML);
  150. })
  151. window.close();
  152. return true;
  153. }
  154. if (axupimgs.res[n].url!='') {
  155. n++;
  156. upAllFiles(n)
  157. } else {
  158. blobInfo.file=axupimgs.res[n].file;
  159. blobInfo.isWater = document.querySelector('#isWater').checked;
  160. upload_handler(blobInfo,function(url) {
  161. if (upload_base_path) {
  162. if (upload_base_path.slice(-1)=='/' && url.substr(0,1)=='/') {
  163. url = upload_base_path + url.slice(1);
  164. } else if (upload_base_path.slice(-1)!='/' && url.substr(0,1)!='/') {
  165. url = upload_base_path + '/' + url;
  166. } else {
  167. url = upload_base_path + url;
  168. }
  169. }
  170. axupimgs.res[file_i].url = url;
  171. filename = url.split('/').pop();
  172. var li = document.querySelectorAll('#file_list li')[file_i];
  173. li.setAttribute('class','up-over');
  174. li.querySelector('.namebox span').innerText = filename;
  175. n++
  176. upAllFiles(n);
  177. },function(err) {
  178. document.querySelector('#topbar .upall').innerText='全部上传';
  179. document.querySelectorAll('#file_list li.up-now').forEach((el,i) => {
  180. el.setAttribute('class','up-no');
  181. });
  182. alert(err);
  183. });
  184. }
  185. }
  186. document.querySelector('#topbar .upall').addEventListener('click',(e) => {
  187. if (e.target.innerText!='全部上传') {
  188. return false;
  189. }
  190. if (axupimgs.res.length > 0) {
  191. document.querySelectorAll('#file_list li.up-no').forEach((el,i) => {
  192. el.classList ? el.classList.add('up-now') : el.className+=' up-now';
  193. });
  194. e.target.innerText='上传中';
  195. upAllFiles(0);
  196. }
  197. });
  198. var observ_flist = new MutationObserver( (muList,observe) => {
  199. if (muList[0].addedNodes.length > 0) {
  200. muList[0].addedNodes.forEach((el) => {
  201. el.querySelector('.remove').addEventListener('click',(e) => {
  202. var li = e.target.parentNode.parentNode;
  203. var n = li.getAttribute('data-num');
  204. var el = document.querySelectorAll('#file_list li')[n];
  205. el.parentNode.removeChild(el);
  206. axupimgs.res.splice(n,1);
  207. });
  208. });
  209. }
  210. reSort();
  211. });
  212. observ_flist.observe(document.querySelector('#file_list'),{childList:true});
  213. </script>
  214. </body>
  215. </html>