index.html 14 KB

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