file_list.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>文件管理</title>
  7. <link rel="stylesheet" href="../../lib/layui/css/layui.css">
  8. <style>
  9. body { padding: 15px; }
  10. .upload-area { border: 2px dashed #e6e6e6; border-radius: 8px; padding: 40px; text-align: center; margin-bottom: 15px; cursor: pointer; transition: all 0.3s; }
  11. .upload-area:hover { border-color: #667eea; background: rgba(102, 126, 234, 0.05); }
  12. .upload-area i { font-size: 48px; color: #999; }
  13. .upload-area p { margin-top: 10px; color: #999; }
  14. .file-list { display: flex; flex-wrap: wrap; gap: 15px; }
  15. .file-item { width: 180px; background: #fff; border-radius: 8px; padding: 15px; text-align: center; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
  16. .file-item img { width: 100%; height: 120px; object-fit: cover; border-radius: 4px; margin-bottom: 10px; }
  17. .file-item .file-name { font-size: 12px; color: #666; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  18. .file-item .file-actions { margin-top: 8px; }
  19. .file-item .file-actions a { margin: 0 3px; color: #667eea; cursor: pointer; }
  20. .file-item .file-actions a:hover { text-decoration: underline; }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="upload-area" id="uploadArea">
  25. <i class="layui-icon layui-icon-upload-drag"></i>
  26. <p>点击上传文件或将文件拖拽到此处</p>
  27. <p style="font-size: 12px; margin-top: 5px;">支持 jpg、png、gif、pdf、doc、docx、xls、xlsx 等格式</p>
  28. </div>
  29. <div class="layui-form" style="margin-bottom: 15px;">
  30. <div class="layui-inline">
  31. <input type="text" name="keyword" placeholder="搜索文件名" class="layui-input" style="width: 200px;">
  32. </div>
  33. <button type="button" class="layui-btn" id="btnSearch"><i class="layui-icon layui-icon-search"></i></button>
  34. </div>
  35. <div class="file-list" id="fileList">
  36. <!-- 文件列表将通过JS动态加载 -->
  37. </div>
  38. <div id="pagination" style="text-align: center; margin-top: 20px;"></div>
  39. <script src="../../lib/layui/layui.js"></script>
  40. <script src="../../js/config.js"></script>
  41. <script src="../../js/common.js"></script>
  42. <script>
  43. layui.use(['layer', 'upload', 'laypage'], function() {
  44. var layer = layui.layer;
  45. var upload = layui.upload;
  46. var laypage = layui.laypage;
  47. var $ = layui.jquery;
  48. var files = [];
  49. var currentPage = 1;
  50. var pageSize = 12;
  51. // 上传组件
  52. upload.render({
  53. elem: '#uploadArea',
  54. url: Config.getApiUrl(Config.api.file.upload),
  55. headers: function() {
  56. var token = localStorage.getItem('token');
  57. return token ? { 'Authorization': token } : {};
  58. },
  59. done: function(res) {
  60. if (res.code === 200) {
  61. Common.success('上传成功');
  62. files.unshift(res.data);
  63. renderFileList();
  64. } else {
  65. Common.error(res.msg || '上传失败');
  66. }
  67. },
  68. error: function() {
  69. Common.error('上传失败,请重试');
  70. }
  71. });
  72. // 搜索
  73. $('#btnSearch').click(function() {
  74. var keyword = $('input[name="keyword"]').val().toLowerCase();
  75. renderFileList(keyword);
  76. });
  77. // 渲染文件列表
  78. function renderFileList(keyword) {
  79. var filtered = files;
  80. if (keyword) {
  81. filtered = files.filter(function(f) {
  82. return f.originalName.toLowerCase().indexOf(keyword) !== -1;
  83. });
  84. }
  85. var start = (currentPage - 1) * pageSize;
  86. var end = start + pageSize;
  87. var pageData = filtered.slice(start, end);
  88. var html = '';
  89. pageData.forEach(function(file) {
  90. var isImage = /\.(jpg|jpeg|png|gif|bmp)$/i.test(file.originalName);
  91. var preview = isImage
  92. ? '<img src="' + file.url + '" alt="' + file.originalName + '">'
  93. : '<div style="height:120px; display:flex; align-items:center; justify-content:center; background:#f5f5f5; border-radius:4px;"><i class="layui-icon layui-icon-file-b" style="font-size:48px; color:#999;"></i></div>';
  94. html += '<div class="file-item">';
  95. html += preview;
  96. html += '<div class="file-name" title="' + file.originalName + '">' + file.originalName + '</div>';
  97. html += '<div class="file-actions">';
  98. html += '<a href="javascript:;" onclick="copyUrl(\'' + file.url + '\')">复制链接</a>';
  99. html += '<a href="javascript:;" onclick="deleteFile(\'' + file.url + '\', this)">删除</a>';
  100. html += '</div></div>';
  101. });
  102. if (!html) {
  103. html = '<div style="width:100%; text-align:center; padding:60px; color:#999;">暂无文件</div>';
  104. }
  105. $('#fileList').html(html);
  106. // 渲染分页
  107. laypage.render({
  108. elem: 'pagination',
  109. count: filtered.length,
  110. limit: pageSize,
  111. curr: currentPage,
  112. jump: function(obj, first) {
  113. if (!first) {
  114. currentPage = obj.curr;
  115. renderFileList(keyword);
  116. }
  117. }
  118. });
  119. }
  120. // 复制链接
  121. window.copyUrl = function(url) {
  122. var fullUrl = Config.baseUrl + url;
  123. var input = document.createElement('input');
  124. input.value = fullUrl;
  125. document.body.appendChild(input);
  126. input.select();
  127. document.execCommand('copy');
  128. document.body.removeChild(input);
  129. Common.success('链接已复制到剪贴板');
  130. };
  131. // 删除文件
  132. window.deleteFile = function(url, el) {
  133. Common.confirm('确定要删除这个文件吗?', function() {
  134. Common.get(Config.api.file.delete + '?url=' + encodeURIComponent(url), function(res) {
  135. Common.success('删除成功', function() {
  136. $(el).closest('.file-item').remove();
  137. });
  138. });
  139. });
  140. };
  141. // 初始化
  142. renderFileList();
  143. });
  144. </script>
  145. </body>
  146. </html>