index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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" id="btnCameraPhoto">拍照</button>
  64. <button type="button" class="btn btn-primary" id="btnCameraVideo">拍视频</button>
  65. <button type="button" class="btn btn-info" id="btnGalleryImage">相册图片</button>
  66. <button type="button" class="btn btn-info" id="btnGalleryVideo">相册视频</button>
  67. <button type="button" class="btn btn-warning" id="btnAnyFile">选择文件</button>
  68. </div>
  69. <p id="uniappHint" style="display:none;color:#666;font-size:0.9em;margin-bottom:12px;">当前为 UniApp WebView,已使用原生拍照/录像/相册与上传通道。</p>
  70. <input type="file" id="inpCameraPhoto" accept="image/*" capture="environment" style="display:none" />
  71. <input type="file" id="inpCameraVideo" accept="video/*" capture="environment" style="display:none" />
  72. <input type="file" id="inpGalleryImage" accept="image/*" style="display:none" />
  73. <input type="file" id="inpGalleryVideo" accept="video/*" style="display:none" />
  74. <input type="file" id="inpAnyFile" style="display:none" />
  75. <h2 style="font-size: 1.1em; margin: 24px 0 12px;">当前附件</h2>
  76. <div id="previewWrap" class="attachment-preview-wrap empty">暂无附件,请使用上方按钮上传</div>
  77. <div id="previewActions" class="attachment-actions" style="display: none;">
  78. <button type="button" class="btn btn-primary" id="btnShare">分享链接</button>
  79. <button type="button" class="btn btn-secondary" id="btnDelete">删除</button>
  80. </div>
  81. <div id="urlBox" class="attachment-url-box" style="display: none;"></div>
  82. </div>
  83. {% endblock %}
  84. {% block scripts %}
  85. <script src="{{ url_for('main.serve_templates_js', filename='im-sdk.js') }}"></script>
  86. <script>
  87. (function () {
  88. let currentUrl = '';
  89. let currentFilename = '';
  90. const previewWrap = document.getElementById('previewWrap');
  91. const previewActions = document.getElementById('previewActions');
  92. const urlBox = document.getElementById('urlBox');
  93. const uniappHint = document.getElementById('uniappHint');
  94. function useUniNative() {
  95. return window.imSDK && typeof imSDK.isUniAppIm === 'function' && imSDK.isUniAppIm();
  96. }
  97. function refreshUniHint() {
  98. if (uniappHint && useUniNative()) uniappHint.style.display = 'block';
  99. }
  100. refreshUniHint();
  101. setTimeout(refreshUniHint, 400);
  102. function isImageName(name) {
  103. return /\.(png|jpe?g|gif|bmp|webp|heic|heif)$/i.test(name || '');
  104. }
  105. function isVideoName(name) {
  106. return /\.(mp4|mov|webm|mkv|avi)$/i.test(name || '');
  107. }
  108. function clearInputs() {
  109. ['inpCameraPhoto', 'inpCameraVideo', 'inpGalleryImage', 'inpGalleryVideo', 'inpAnyFile'].forEach(function (id) {
  110. const el = document.getElementById(id);
  111. if (el) el.value = '';
  112. });
  113. }
  114. function parseNativeUploadResponse(res) {
  115. if (res == null) return null;
  116. if (typeof res === 'string') {
  117. try { return JSON.parse(res); } catch (e) { return null; }
  118. }
  119. if (typeof res.data === 'string') {
  120. try { return JSON.parse(res.data); } catch (e) { return null; }
  121. }
  122. if (res.data && typeof res.data === 'object') return res.data;
  123. return res;
  124. }
  125. function nativeTempPath(res) {
  126. if (!res) return '';
  127. if (res.tempFilePaths && res.tempFilePaths.length) return res.tempFilePaths[0];
  128. if (res.tempFilePath) return res.tempFilePath;
  129. if (res.apFilePath) return res.apFilePath;
  130. return '';
  131. }
  132. function uploadNativePath(filePath) {
  133. if (!filePath) {
  134. showMessage('未获取到本地文件路径', 'error');
  135. return;
  136. }
  137. var uploadUrl = window.location.origin + '/api/attachment_test/upload';
  138. var timeoutId = setTimeout(function () {
  139. showLoading(false);
  140. showMessage('上传超时,请检查 App 是否实现 uploadFile 与 Cookie', 'error');
  141. }, 120000);
  142. showLoading(true);
  143. imSDK.uploadFile({
  144. url: uploadUrl,
  145. filePath: filePath,
  146. name: 'file',
  147. success: function (res) {
  148. clearTimeout(timeoutId);
  149. showLoading(false);
  150. var data = parseNativeUploadResponse(res);
  151. if (data && data.success) {
  152. showMessage(data.message || '上传成功');
  153. renderPreview({
  154. has_file: true,
  155. filename: data.filename,
  156. url: data.url
  157. });
  158. } else {
  159. showMessage((data && data.message) || '上传失败', 'error');
  160. }
  161. }
  162. });
  163. }
  164. function renderPreview(data) {
  165. currentUrl = data.url || '';
  166. currentFilename = data.filename || '';
  167. urlBox.style.display = 'none';
  168. urlBox.textContent = '';
  169. if (!data.has_file || !data.url) {
  170. previewWrap.className = 'attachment-preview-wrap empty';
  171. previewWrap.textContent = '暂无附件,请使用上方按钮上传';
  172. previewActions.style.display = 'none';
  173. return;
  174. }
  175. previewWrap.className = 'attachment-preview-wrap';
  176. previewWrap.innerHTML = '';
  177. if (isImageName(data.filename)) {
  178. const img = document.createElement('img');
  179. img.src = data.url;
  180. img.alt = data.filename;
  181. previewWrap.appendChild(img);
  182. } else if (isVideoName(data.filename)) {
  183. const v = document.createElement('video');
  184. v.src = data.url;
  185. v.controls = true;
  186. v.playsInline = true;
  187. previewWrap.appendChild(v);
  188. } else {
  189. const p = document.createElement('p');
  190. p.textContent = '已上传文件(非预览类型)';
  191. previewWrap.appendChild(p);
  192. }
  193. const meta = document.createElement('div');
  194. meta.className = 'attachment-file-meta';
  195. meta.textContent = data.filename;
  196. previewWrap.appendChild(meta);
  197. previewActions.style.display = 'flex';
  198. }
  199. async function refreshStatus() {
  200. try {
  201. const r = await fetch('/api/attachment_test/status');
  202. const data = await r.json();
  203. if (data.success) {
  204. renderPreview(data);
  205. }
  206. } catch (e) {
  207. showMessage('加载状态失败: ' + e.message, 'error');
  208. }
  209. }
  210. async function uploadFile(file) {
  211. if (!file) return;
  212. const fd = new FormData();
  213. fd.append('file', file);
  214. showLoading(true);
  215. try {
  216. const r = await fetch('/api/attachment_test/upload', { method: 'POST', body: fd });
  217. const data = await r.json();
  218. if (data.success) {
  219. showMessage(data.message || '上传成功');
  220. renderPreview({
  221. has_file: true,
  222. filename: data.filename,
  223. url: data.url,
  224. });
  225. } else {
  226. showMessage(data.message || '上传失败', 'error');
  227. }
  228. } catch (e) {
  229. showMessage('上传失败: ' + e.message, 'error');
  230. } finally {
  231. showLoading(false);
  232. clearInputs();
  233. }
  234. }
  235. function wireInput(id) {
  236. const el = document.getElementById(id);
  237. if (!el) return;
  238. el.addEventListener('change', function () {
  239. const f = this.files && this.files[0];
  240. if (f) uploadFile(f);
  241. });
  242. }
  243. ['inpCameraPhoto', 'inpCameraVideo', 'inpGalleryImage', 'inpGalleryVideo', 'inpAnyFile'].forEach(wireInput);
  244. function triggerFileInput(id) {
  245. var el = document.getElementById(id);
  246. if (el) el.click();
  247. }
  248. document.getElementById('btnCameraPhoto').addEventListener('click', function () {
  249. if (useUniNative()) {
  250. imSDK.chooseImage({
  251. count: 1,
  252. sourceType: ['camera'],
  253. success: function (res) { uploadNativePath(nativeTempPath(res)); }
  254. });
  255. } else {
  256. triggerFileInput('inpCameraPhoto');
  257. }
  258. });
  259. document.getElementById('btnCameraVideo').addEventListener('click', function () {
  260. if (useUniNative()) {
  261. imSDK.chooseVideo({
  262. sourceType: ['camera'],
  263. maxDuration: 120,
  264. camera: 'back',
  265. success: function (res) { uploadNativePath(nativeTempPath(res)); }
  266. });
  267. } else {
  268. triggerFileInput('inpCameraVideo');
  269. }
  270. });
  271. document.getElementById('btnGalleryImage').addEventListener('click', function () {
  272. if (useUniNative()) {
  273. imSDK.chooseImage({
  274. count: 1,
  275. sourceType: ['album'],
  276. success: function (res) { uploadNativePath(nativeTempPath(res)); }
  277. });
  278. } else {
  279. triggerFileInput('inpGalleryImage');
  280. }
  281. });
  282. document.getElementById('btnGalleryVideo').addEventListener('click', function () {
  283. if (useUniNative()) {
  284. imSDK.chooseVideo({
  285. sourceType: ['album'],
  286. maxDuration: 120,
  287. camera: 'back',
  288. success: function (res) { uploadNativePath(nativeTempPath(res)); }
  289. });
  290. } else {
  291. triggerFileInput('inpGalleryVideo');
  292. }
  293. });
  294. document.getElementById('btnAnyFile').addEventListener('click', function () {
  295. if (useUniNative()) {
  296. imSDK.chooseFile({
  297. count: 1,
  298. extension: [],
  299. success: function (res) {
  300. var path = nativeTempPath(res);
  301. if (!path && res.tempFiles && res.tempFiles[0]) path = res.tempFiles[0].path;
  302. uploadNativePath(path);
  303. }
  304. });
  305. } else {
  306. triggerFileInput('inpAnyFile');
  307. }
  308. });
  309. document.getElementById('btnShare').addEventListener('click', async function () {
  310. if (!currentUrl) {
  311. showMessage('没有可分享的链接', 'error');
  312. return;
  313. }
  314. if (navigator.share) {
  315. try {
  316. await navigator.share({ title: '附件链接', text: currentFilename, url: currentUrl });
  317. return;
  318. } catch (e) {
  319. if (e.name === 'AbortError') return;
  320. }
  321. }
  322. try {
  323. await navigator.clipboard.writeText(currentUrl);
  324. showMessage('链接已复制到剪贴板');
  325. } catch (e) {
  326. urlBox.style.display = 'block';
  327. urlBox.textContent = currentUrl;
  328. showMessage('请手动复制上方链接', 'error');
  329. }
  330. });
  331. document.getElementById('btnDelete').addEventListener('click', async function () {
  332. if (!confirm('确定删除当前附件?')) return;
  333. showLoading(true);
  334. try {
  335. const r = await fetch('/api/attachment_test', { method: 'DELETE' });
  336. const data = await r.json();
  337. if (data.success) {
  338. showMessage(data.message || '已删除');
  339. renderPreview({ has_file: false });
  340. } else {
  341. showMessage(data.message || '删除失败', 'error');
  342. }
  343. } catch (e) {
  344. showMessage('删除失败: ' + e.message, 'error');
  345. } finally {
  346. showLoading(false);
  347. }
  348. });
  349. refreshStatus();
  350. })();
  351. </script>
  352. {% endblock %}