index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div class="app-container">
  3. <doc-alert title="上传下载" url="https://doc.iocoder.cn/file/"/>
  4. <!-- 搜索工作栏 -->
  5. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  6. <el-form-item label="文件路径" prop="path">
  7. <el-input v-model="queryParams.path" placeholder="请输入文件路径" clearable @keyup.enter.native="handleQuery"/>
  8. </el-form-item>
  9. <el-form-item label="创建时间" prop="createTime">
  10. <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
  11. type="daterange"
  12. range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
  13. :default-time="['00:00:00', '23:59:59']"/>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  17. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  18. </el-form-item>
  19. </el-form>
  20. <!-- 操作工具栏 -->
  21. <el-row :gutter="10" class="mb8">
  22. <el-col :span="1.5">
  23. <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">上传文件</el-button>
  24. </el-col>
  25. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  26. </el-row>
  27. <!-- 列表 -->
  28. <el-table v-loading="loading" :data="list">
  29. <el-table-column label="文件名" :show-overflow-tooltip="true" align="center" min-width="200px" prop="name"/>
  30. <el-table-column label="文件路径" :show-overflow-tooltip="true" align="center" min-width="250px" prop="path"/>
  31. <el-table-column label="文件 URL" :show-overflow-tooltip="true" align="center" min-width="300px" prop="url"/>
  32. <el-table-column label="文件大小" align="center" prop="size" min-width="120px" :formatter="sizeFormat"/>
  33. <el-table-column label="文件类型" :show-overflow-tooltip="true" align="center" prop="type" width="180px"/>
  34. <el-table-column label="文件内容" align="center" prop="content" min-width="150px">
  35. <template v-slot="scope">
  36. <image-preview v-if="scope.row.type&&scope.row.type.indexOf('image/') === 0" :src="scope.row.url"
  37. :width="'100px'"></image-preview>
  38. <video v-else-if="scope.row.type&&scope.row.type.indexOf('video/') === 0" :width="'100px'">
  39. <source :src="scope.row.url"/>
  40. </video>
  41. <i v-else>无法预览,点击
  42. <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" target="_blank"
  43. :href="getFileUrl + scope.row.configId + '/get/' + scope.row.path">下载
  44. </el-link>
  45. </i>
  46. </template>
  47. </el-table-column>
  48. <el-table-column label="上传时间" align="center" prop="createTime" min-width="170px">
  49. <template v-slot="scope">
  50. <span>{{ parseTime(scope.row.createTime) }}</span>
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100px">
  54. <template v-slot="scope">
  55. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
  56. v-hasPermi="['infra:file:delete']">删除
  57. </el-button>
  58. </template>
  59. </el-table-column>
  60. </el-table>
  61. <!-- 分页组件 -->
  62. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  63. @pagination="getList"/>
  64. <!-- 对话框(添加 / 修改) -->
  65. <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
  66. <el-upload ref="upload" :limit="1" accept=".jpg, .png, .gif" :auto-upload="false" drag
  67. :headers="upload.headers" :action="upload.url" :data="upload.data" :disabled="upload.isUploading"
  68. :on-change="handleFileChange"
  69. :on-progress="handleFileUploadProgress"
  70. :on-success="handleFileSuccess">
  71. <i class="el-icon-upload"></i>
  72. <div class="el-upload__text">
  73. 将文件拖到此处,或 <em>点击上传</em>
  74. </div>
  75. <div class="el-upload__tip" style="color:red" slot="tip">提示:仅允许导入 jpg、png、gif 格式文件!</div>
  76. </el-upload>
  77. <div slot="footer" class="dialog-footer">
  78. <el-button type="primary" @click="submitFileForm">确 定</el-button>
  79. <el-button @click="upload.open = false">取 消</el-button>
  80. </div>
  81. </el-dialog>
  82. </div>
  83. </template>
  84. <script>
  85. import {deleteFile, getFilePage} from "@/api/infra/file";
  86. import {getAccessToken} from "@/utils/auth";
  87. import ImagePreview from "@/components/ImagePreview";
  88. export default {
  89. name: "File",
  90. components: {
  91. ImagePreview
  92. },
  93. data() {
  94. return {
  95. getFileUrl: process.env.VUE_APP_BASE_API + '/admin-api/infra/file/',
  96. // 遮罩层
  97. loading: true,
  98. // 显示搜索条件
  99. showSearch: true,
  100. // 总条数
  101. total: 0,
  102. // 文件列表
  103. list: [],
  104. // 弹出层标题
  105. title: "",
  106. // 查询参数
  107. queryParams: {
  108. pageNo: 1,
  109. pageSize: 10,
  110. path: null,
  111. type: null,
  112. createTime: []
  113. },
  114. // 用户导入参数
  115. upload: {
  116. open: false, // 是否显示弹出层
  117. title: "", // 弹出层标题
  118. isUploading: false, // 是否禁用上传
  119. url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
  120. headers: {Authorization: "Bearer " + getAccessToken()}, // 设置上传的请求头部
  121. data: {} // 上传的额外数据,用于文件名
  122. },
  123. };
  124. },
  125. created() {
  126. this.getList();
  127. },
  128. methods: {
  129. /** 查询列表 */
  130. getList() {
  131. this.loading = true;
  132. // 执行查询
  133. getFilePage(this.queryParams).then(response => {
  134. this.list = response.data.list;
  135. this.total = response.data.total;
  136. this.loading = false;
  137. });
  138. },
  139. /** 取消按钮 */
  140. cancel() {
  141. this.open = false;
  142. this.reset();
  143. },
  144. /** 表单重置 */
  145. reset() {
  146. this.form = {
  147. content: undefined,
  148. };
  149. this.resetForm("form");
  150. },
  151. /** 搜索按钮操作 */
  152. handleQuery() {
  153. this.queryParams.pageNo = 1;
  154. this.getList();
  155. },
  156. /** 重置按钮操作 */
  157. resetQuery() {
  158. this.resetForm("queryForm");
  159. this.handleQuery();
  160. },
  161. /** 新增按钮操作 */
  162. handleAdd() {
  163. this.upload.open = true;
  164. this.upload.title = "上传文件";
  165. },
  166. /** 处理上传的文件发生变化 */
  167. handleFileChange(file, fileList) {
  168. },
  169. /** 处理文件上传中 */
  170. handleFileUploadProgress(event, file, fileList) {
  171. this.upload.isUploading = true; // 禁止修改
  172. },
  173. /** 发起文件上传 */
  174. submitFileForm() {
  175. this.$refs.upload.submit();
  176. },
  177. /** 文件上传成功处理 */
  178. handleFileSuccess(response, file, fileList) {
  179. // 清理
  180. this.upload.open = false;
  181. this.upload.isUploading = false;
  182. this.$refs.upload.clearFiles();
  183. // 提示成功,并刷新
  184. this.$modal.msgSuccess("上传成功");
  185. this.getList();
  186. },
  187. /** 删除按钮操作 */
  188. handleDelete(row) {
  189. const id = row.id;
  190. this.$modal.confirm('是否确认删除文件编号为"' + id + '"的数据项?').then(function () {
  191. return deleteFile(id);
  192. }).then(() => {
  193. this.getList();
  194. this.$modal.msgSuccess("删除成功");
  195. }).catch(() => {
  196. });
  197. },
  198. // 用户昵称展示
  199. sizeFormat(row, column) {
  200. const unitArr = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  201. const srcSize = parseFloat(row.size);
  202. const index = Math.floor(Math.log(srcSize) / Math.log(1024));
  203. let size = srcSize / Math.pow(1024, index);
  204. size = size.toFixed(2);//保留的小数位数
  205. return size + ' ' + unitArr[index];
  206. },
  207. }
  208. };
  209. </script>