index.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. {% extends "base.html" %}
  2. {% block title %}附件上传测试{% endblock %}
  3. {% block extra_styles %}
  4. <style>
  5. .attachment-upload-grid {
  6. display: flex;
  7. flex-wrap: wrap;
  8. gap: 10px;
  9. margin-bottom: 20px;
  10. }
  11. .attachment-upload-grid .btn {
  12. flex: 1 1 auto;
  13. min-width: 140px;
  14. }
  15. .attachment-preview-wrap {
  16. background: #f8f9fa;
  17. border: 1px solid #eee;
  18. border-radius: 10px;
  19. padding: 20px;
  20. min-height: 120px;
  21. }
  22. .attachment-preview-wrap.empty {
  23. color: #888;
  24. display: flex;
  25. align-items: center;
  26. justify-content: center;
  27. }
  28. .attachment-preview-wrap img,
  29. .attachment-preview-wrap video {
  30. max-width: 100%;
  31. max-height: 360px;
  32. border-radius: 8px;
  33. display: block;
  34. }
  35. .attachment-file-meta {
  36. margin-top: 12px;
  37. word-break: break-all;
  38. font-size: 0.9em;
  39. color: #555;
  40. }
  41. .attachment-actions {
  42. display: flex;
  43. flex-wrap: wrap;
  44. gap: 10px;
  45. margin-top: 16px;
  46. }
  47. .attachment-url-box {
  48. font-size: 0.85em;
  49. color: #667eea;
  50. word-break: break-all;
  51. margin-top: 8px;
  52. }
  53. </style>
  54. {% endblock %}
  55. {% block content %}
  56. <div class="module-header">
  57. <h2>📎 附件上传测试</h2>
  58. </div>
  59. <div class="sub-section">
  60. <h2 style="font-size: 1.1em; margin-bottom: 16px;">选择来源</h2>
  61. <p style="color: #666; margin-bottom: 12px; font-size: 0.95em;">每次仅保留一个附件;新上传会替换当前附件。</p>
  62. <div class="attachment-upload-grid">
  63. <button type="button" class="btn btn-primary" onclick="document.getElementById('inpCameraPhoto').click()">拍照</button>
  64. <button type="button" class="btn btn-primary" onclick="document.getElementById('inpCameraVideo').click()">拍视频</button>
  65. <button type="button" class="btn btn-info" onclick="document.getElementById('inpGalleryImage').click()">相册图片</button>
  66. <button type="button" class="btn btn-info" onclick="document.getElementById('inpGalleryVideo').click()">相册视频</button>
  67. <button type="button" class="btn btn-warning" onclick="document.getElementById('inpAnyFile').click()">选择文件</button>
  68. </div>
  69. <input type="file" id="inpCameraPhoto" accept="image/*" capture="environment" style="display:none" />
  70. <input type="file" id="inpCameraVideo" accept="video/*" capture="environment" style="display:none" />
  71. <input type="file" id="inpGalleryImage" accept="image/*" style="display:none" />
  72. <input type="file" id="inpGalleryVideo" accept="video/*" style="display:none" />
  73. <input type="file" id="inpAnyFile" style="display:none" />
  74. <h2 style="font-size: 1.1em; margin: 24px 0 12px;">当前附件</h2>
  75. <div id="previewWrap" class="attachment-preview-wrap empty">暂无附件,请使用上方按钮上传</div>
  76. <div id="previewActions" class="attachment-actions" style="display: none;">
  77. <button type="button" class="btn btn-primary" id="btnShare">分享链接</button>
  78. <button type="button" class="btn btn-secondary" id="btnDelete">删除</button>
  79. </div>
  80. <div id="urlBox" class="attachment-url-box" style="display: none;"></div>
  81. </div>
  82. {% endblock %}
  83. {% block scripts %}
  84. <script>
  85. (function () {
  86. let currentUrl = '';
  87. let currentFilename = '';
  88. const previewWrap = document.getElementById('previewWrap');
  89. const previewActions = document.getElementById('previewActions');
  90. const urlBox = document.getElementById('urlBox');
  91. function isImageName(name) {
  92. return /\.(png|jpe?g|gif|bmp|webp|heic|heif)$/i.test(name || '');
  93. }
  94. function isVideoName(name) {
  95. return /\.(mp4|mov|webm|mkv|avi)$/i.test(name || '');
  96. }
  97. function clearInputs() {
  98. ['inpCameraPhoto', 'inpCameraVideo', 'inpGalleryImage', 'inpGalleryVideo', 'inpAnyFile'].forEach(function (id) {
  99. const el = document.getElementById(id);
  100. if (el) el.value = '';
  101. });
  102. }
  103. function renderPreview(data) {
  104. currentUrl = data.url || '';
  105. currentFilename = data.filename || '';
  106. urlBox.style.display = 'none';
  107. urlBox.textContent = '';
  108. if (!data.has_file || !data.url) {
  109. previewWrap.className = 'attachment-preview-wrap empty';
  110. previewWrap.textContent = '暂无附件,请使用上方按钮上传';
  111. previewActions.style.display = 'none';
  112. return;
  113. }
  114. previewWrap.className = 'attachment-preview-wrap';
  115. previewWrap.innerHTML = '';
  116. if (isImageName(data.filename)) {
  117. const img = document.createElement('img');
  118. img.src = data.url;
  119. img.alt = data.filename;
  120. previewWrap.appendChild(img);
  121. } else if (isVideoName(data.filename)) {
  122. const v = document.createElement('video');
  123. v.src = data.url;
  124. v.controls = true;
  125. v.playsInline = true;
  126. previewWrap.appendChild(v);
  127. } else {
  128. const p = document.createElement('p');
  129. p.textContent = '已上传文件(非预览类型)';
  130. previewWrap.appendChild(p);
  131. }
  132. const meta = document.createElement('div');
  133. meta.className = 'attachment-file-meta';
  134. meta.textContent = data.filename;
  135. previewWrap.appendChild(meta);
  136. previewActions.style.display = 'flex';
  137. }
  138. async function refreshStatus() {
  139. try {
  140. const r = await fetch('/api/attachment_test/status');
  141. const data = await r.json();
  142. if (data.success) {
  143. renderPreview(data);
  144. }
  145. } catch (e) {
  146. showMessage('加载状态失败: ' + e.message, 'error');
  147. }
  148. }
  149. async function uploadFile(file) {
  150. if (!file) return;
  151. const fd = new FormData();
  152. fd.append('file', file);
  153. showLoading(true);
  154. try {
  155. const r = await fetch('/api/attachment_test/upload', { method: 'POST', body: fd });
  156. const data = await r.json();
  157. if (data.success) {
  158. showMessage(data.message || '上传成功');
  159. renderPreview({
  160. has_file: true,
  161. filename: data.filename,
  162. url: data.url,
  163. });
  164. } else {
  165. showMessage(data.message || '上传失败', 'error');
  166. }
  167. } catch (e) {
  168. showMessage('上传失败: ' + e.message, 'error');
  169. } finally {
  170. showLoading(false);
  171. clearInputs();
  172. }
  173. }
  174. function wireInput(id) {
  175. const el = document.getElementById(id);
  176. if (!el) return;
  177. el.addEventListener('change', function () {
  178. const f = this.files && this.files[0];
  179. if (f) uploadFile(f);
  180. });
  181. }
  182. ['inpCameraPhoto', 'inpCameraVideo', 'inpGalleryImage', 'inpGalleryVideo', 'inpAnyFile'].forEach(wireInput);
  183. document.getElementById('btnShare').addEventListener('click', async function () {
  184. if (!currentUrl) {
  185. showMessage('没有可分享的链接', 'error');
  186. return;
  187. }
  188. if (navigator.share) {
  189. try {
  190. await navigator.share({ title: '附件链接', text: currentFilename, url: currentUrl });
  191. return;
  192. } catch (e) {
  193. if (e.name === 'AbortError') return;
  194. }
  195. }
  196. try {
  197. await navigator.clipboard.writeText(currentUrl);
  198. showMessage('链接已复制到剪贴板');
  199. } catch (e) {
  200. urlBox.style.display = 'block';
  201. urlBox.textContent = currentUrl;
  202. showMessage('请手动复制上方链接', 'error');
  203. }
  204. });
  205. document.getElementById('btnDelete').addEventListener('click', async function () {
  206. if (!confirm('确定删除当前附件?')) return;
  207. showLoading(true);
  208. try {
  209. const r = await fetch('/api/attachment_test', { method: 'DELETE' });
  210. const data = await r.json();
  211. if (data.success) {
  212. showMessage(data.message || '已删除');
  213. renderPreview({ has_file: false });
  214. } else {
  215. showMessage(data.message || '删除失败', 'error');
  216. }
  217. } catch (e) {
  218. showMessage('删除失败: ' + e.message, 'error');
  219. } finally {
  220. showLoading(false);
  221. }
  222. });
  223. refreshStatus();
  224. })();
  225. </script>
  226. {% endblock %}