index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div class="ele-body">
  3. <el-card shadow="never">
  4. <!-- 搜索表单 -->
  5. <file-search @search="reload" />
  6. <!-- 数据表格 -->
  7. <ele-pro-table
  8. ref="table"
  9. :columns="columns"
  10. :datasource="datasource"
  11. :selection.sync="selection"
  12. cache-key="systemFileTable"
  13. >
  14. <!-- 表头工具栏 -->
  15. <template v-slot:toolbar>
  16. <el-upload
  17. action=""
  18. :show-file-list="false"
  19. :before-upload="onUpload"
  20. class="ele-action ele-inline-block"
  21. >
  22. <el-button
  23. size="small"
  24. type="primary"
  25. icon="el-icon-upload2"
  26. class="ele-btn-icon"
  27. >
  28. 上传
  29. </el-button>
  30. </el-upload>
  31. <el-button
  32. size="small"
  33. type="danger"
  34. icon="el-icon-delete"
  35. class="ele-btn-icon"
  36. @click="removeBatch"
  37. >
  38. 删除
  39. </el-button>
  40. </template>
  41. <!-- 文件路径列 -->
  42. <template v-slot:path="{ row }">
  43. <a :href="row.url" target="_blank">
  44. {{ row.path }}
  45. </a>
  46. </template>
  47. <!-- 操作列 -->
  48. <template v-slot:action="{ row }">
  49. <el-link
  50. type="primary"
  51. :underline="false"
  52. icon="el-icon-download"
  53. :href="row.downloadUrl"
  54. target="_blank"
  55. >
  56. 下载
  57. </el-link>
  58. <el-popconfirm
  59. class="ele-action"
  60. title="确定要删除此文件吗?"
  61. @confirm="remove(row)"
  62. >
  63. <template v-slot:reference>
  64. <el-link type="danger" :underline="false" icon="el-icon-delete">
  65. 删除
  66. </el-link>
  67. </template>
  68. </el-popconfirm>
  69. </template>
  70. </ele-pro-table>
  71. </el-card>
  72. </div>
  73. </template>
  74. <script>
  75. import FileSearch from './components/file-search.vue';
  76. import {
  77. pageFiles,
  78. removeFile,
  79. removeFiles,
  80. uploadFile
  81. } from '@/api/system/file';
  82. export default {
  83. name: 'SystemFile',
  84. components: { FileSearch },
  85. data() {
  86. return {
  87. // 表格列配置
  88. columns: [
  89. {
  90. columnKey: 'selection',
  91. type: 'selection',
  92. width: 45,
  93. align: 'center',
  94. fixed: 'left'
  95. },
  96. {
  97. columnKey: 'index',
  98. type: 'index',
  99. width: 45,
  100. align: 'center',
  101. showOverflowTooltip: true,
  102. fixed: 'left'
  103. },
  104. {
  105. prop: 'name',
  106. label: '文件名称',
  107. sortable: 'custom',
  108. showOverflowTooltip: true
  109. },
  110. {
  111. prop: 'path',
  112. label: '文件路径',
  113. sortable: 'custom',
  114. showOverflowTooltip: true,
  115. slot: 'path'
  116. },
  117. {
  118. prop: 'length',
  119. label: '文件大小',
  120. sortable: 'custom',
  121. showOverflowTooltip: true,
  122. width: 110,
  123. formatter: (row) => {
  124. if (row.length < 1024) {
  125. return row.length + 'B';
  126. } else if (row.length < 1024 * 1024) {
  127. return (row.length / 1024).toFixed(1) + 'KB';
  128. } else if (row.length < 1024 * 1024 * 1024) {
  129. return (row.length / 1024 / 1024).toFixed(1) + 'M';
  130. } else {
  131. return (row.length / 1024 / 1024 / 1024).toFixed(1) + 'G';
  132. }
  133. }
  134. },
  135. {
  136. prop: 'createNickname',
  137. label: '上传人',
  138. sortable: 'custom',
  139. showOverflowTooltip: true,
  140. width: 110
  141. },
  142. {
  143. prop: 'createTime',
  144. label: '上传时间',
  145. sortable: 'custom',
  146. showOverflowTooltip: true,
  147. width: 160,
  148. formatter: (_row, _column, cellValue) => {
  149. return this.$util.toDateString(cellValue);
  150. }
  151. },
  152. {
  153. columnKey: 'action',
  154. label: '操作',
  155. width: 130,
  156. align: 'center',
  157. resizable: false,
  158. slot: 'action',
  159. showOverflowTooltip: true
  160. }
  161. ],
  162. // 表格选中数据
  163. selection: []
  164. };
  165. },
  166. methods: {
  167. /* 表格数据源 */
  168. datasource({ page, limit, where, order }) {
  169. return pageFiles({ ...where, ...order, page, limit });
  170. },
  171. /* 刷新表格 */
  172. reload(where) {
  173. this.$refs.table.reload({ page: 1, where: where });
  174. },
  175. /* 删除 */
  176. remove(row) {
  177. const loading = this.$loading({ lock: true });
  178. removeFile(row.id)
  179. .then((msg) => {
  180. loading.close();
  181. this.$message.success(msg);
  182. this.reload();
  183. })
  184. .catch((e) => {
  185. loading.close();
  186. // this.$message.error(e.message);
  187. });
  188. },
  189. /* 批量删除 */
  190. removeBatch() {
  191. if (!this.selection.length) {
  192. this.$message.error('请至少选择一条数据');
  193. return;
  194. }
  195. this.$confirm('确定要删除选中的文件吗?', '提示', {
  196. type: 'warning'
  197. })
  198. .then(() => {
  199. const loading = this.$loading({ lock: true });
  200. removeFiles(this.selection.map((d) => d.id))
  201. .then((msg) => {
  202. loading.close();
  203. this.$message.success(msg);
  204. this.reload();
  205. })
  206. .catch((e) => {
  207. loading.close();
  208. // this.$message.error(e.message);
  209. });
  210. })
  211. .catch(() => {});
  212. },
  213. /* 上传 */
  214. onUpload(file) {
  215. if (file.size / 1024 / 1024 > 100) {
  216. this.$message.error('大小不能超过 100MB');
  217. return false;
  218. }
  219. const loading = this.$loading({ lock: true });
  220. uploadFile(file)
  221. .then(() => {
  222. loading.close();
  223. this.$message.success('上传成功');
  224. this.reload();
  225. })
  226. .catch((e) => {
  227. loading.close();
  228. // this.$message.error(e.message);
  229. });
  230. return false;
  231. }
  232. }
  233. };
  234. </script>