index.vue 7.9 KB

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