index.vue 11.3 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
<style scoped>
@import url("./iconfont.css");
.matter-icon {
  font-size: 25px;
}
.matter-title {
  top: -5px;
  display: inline;
  margin-left: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: relative;
}
</style>

<template>
  <div style="height: 100%">
    <el-row>
      <el-button size="mini" type="success" @click="dialogVisible = true"
        >上传文件</el-button
      >
      <el-button size="mini" type="primary" @click="createFolder"
        >新建文件夹</el-button
      >
      <!-- <el-button size="mini" type="info">移动文件</el-button>
      <el-button size="mini" type="warning">下载文件</el-button> -->
      <el-button size="mini" type="danger" @click="removeFiles"
        >删除文件</el-button
      >
    </el-row>
    <div style="margin: 20px 0px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          ><a @click.prevent="getAllFiles">全部文件</a></el-breadcrumb-item
        >
        <el-breadcrumb-item v-for="(item, index) in currentDirList" :key="index"
          ><a
            @click.prevent="viewFiles(item, index)"
            href="javascript:void(0)"
            >{{ item }}</a
          ></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      size="medium"
      stripe
      style="width: 100%"
      height="700px"
      tooltip-effect="dark"
      v-el-table-infinite-scroll="onScrollEnd"
      @selection-change="onSelectionChange"
    >
      <el-table-column
        type="selection"
        width="30"
        :selectable="onSelectable"
      ></el-table-column>
      <el-table-column label="文件">
        <template slot-scope="scope">
          <i
            v-if="scope.row.is_dir"
            class="matter-icon el-icon-folder"
            style="color: #ffc402"
          ></i>
          <i v-else :class="`iconfont matter-icon ${scope.row.icon}`"></i>
          <el-link
            :underline="false"
            class="matter-title"
            href="Javascript: void(0);"
          >
            <span @click="onNameClick(scope.row)">{{ scope.row.name }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="size"
        label="文件大小"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="create_at"
        label="创建日期"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="create_by.username"
        label="创建者"
        width="150"
      ></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" type="text" @click="onRename(scope.row)">重命名</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="上传文件" :visible.sync="dialogVisible" width="30%">
      <el-upload
        drag
        name="binfile"
        :headers="uploadHeaders"
        :data="uploadData"
        :before-upload="onBeforeUpload"
        :on-success="onUploadSuccess"
        :action="`${window.location.protocol}//${window.location.host}/api/v1/evm_store/netdisc/add`"
        multiple
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">文件不超过100mb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { download } from "@/utils/index";
import elTableInfiniteScroll from "el-table-infinite-scroll";
import { getFileList, addFile, updateFile, deleteFile } from "@/api/index";

export default {
  name: "Files",
  directives: {
    "el-table-infinite-scroll": elTableInfiniteScroll,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      dialogVisible: false,
      selectList: [],
      currentDirList: [],
      currentDir: "/",
      uploadData: {
        parent_dir: "/",
        file_type: "",
      },
      uploadHeaders: {
        Authorization: this.$store.getters.token,
      },
      urlRoot: "",
    };
  },
  watch: {
    $route: "onRouteChange",
  },
  computed: {
    window: () => window,
  },
  methods: {
    getAllFiles() {
      this.currentDir = "/";
      this.currentDirList = [];
      this.getFileList();
    },
    onSelectionChange(selection) {
      this.selectList = selection;
    },
    onScrollEnd() {
      //   if (this.total != 0 && this.rows.length == this.total) {
      //     console.log("no more");
      //     return;
      //   }
      //   this.getFileList(this.offset, this.limit);
      console.log("to do nothing");
    },
    onSelectable(row) {
      if (row.is_dir !== 1) return true;
    },
    onRename(row) {
        console.log(row)
      this.$prompt("请输入名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: !/(\\|:)+/g,
        inputErrorMessage: "文件夹格式不正确",
      })
        .then(({ value }) => {
          this.updateFile(row.uuid, { name: value });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消动作",
          });
        });
    },
    removeFiles() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            this.deleteFile()
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    onRouteChange(newVal) {
      if (this.currentDir != newVal.query.dir) {
        this.currentDir = newVal.query.dir; // change the current direction when the route changed
      }

      this.getFileList();
    },
    isOfficeFile(type) {
      let officeTypes = [
        "application/msword",
        "application/vnd.ms-excel",
        "application/vnd.ms-powerpoint",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
      ];
      return officeTypes.includes(type);
    },
    officeIcon(type) {
      let docTypes = [
        "application/msword",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      ];
      let excelTypes = [
        "application/vnd.ms-excel",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      ];
      let pptTypes = [
        "application/vnd.ms-powerpoint",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
      ];
      if (docTypes.includes(type)) {
        return "icon-doc";
      } else if (excelTypes.includes(type)) {
        return "icon-excel";
      } else if (pptTypes.includes(type)) {
        return "icon-ppt";
      }
    },
    type2icon(type) {
      let [t1, t2] = type.split("/");
      let mt = ["pdf", "html", "xml", "psd", "rtf"];
      if (mt.includes(t2)) {
        return `icon-${t2}`;
      }

      let codeTypes = ["json", "yaml", "x-yaml"];
      if (codeTypes.includes(t2)) {
        return "icon-html";
      }

      let compressedFileTypes = ["zip", "x-gzip", "tar", "gz", "rar"];
      if (compressedFileTypes.includes(t2)) {
        return "icon-compressed-file";
      }

      if (this.isOfficeFile(type)) {
        return this.officeIcon(type);
      }

      let gt = ["audio", "video", "image", "text"];
      if (gt.includes(t1)) {
        return `icon-${t1}`;
      }

      return "icon-file";
    },
    buildQuery(dir) {
      if (dir.startsWith(this.rootDir)) {
        dir = dir.replace(this.rootDir, "");
      }

      let query = !dir ? {} : { dir: dir };
      return { query: query };
    },
    onNameClick(item) {
      if (item.is_dir == 1) {
        this.currentDirList.push(item.name);
        this.currentDir = "/" + this.currentDirList.join("/");
        this.getFileList();
        return;
      }
      download(item.name, this.urlRoot + item.real_path);
    },
    convert(limit) {
      var size = "";
      if (limit < 0.1 * 1024) {
        //如果小于0.1KB转化成B
        size = limit.toFixed(2) + "B";
      } else if (limit < 0.1 * 1024 * 1024) {
        //如果小于0.1MB转化成KB
        size = (limit / 1024).toFixed(2) + "KB";
      } else if (limit < 0.1 * 1024 * 1024 * 1024) {
        //如果小于0.1GB转化成MB
        size = (limit / (1024 * 1024)).toFixed(2) + "MB";
      } else {
        //其他转化成GB
        size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
      }

      var sizestr = size + "";
      var len = sizestr.indexOf(".");
      var dec = sizestr.substr(len + 1, 2);
      if (dec == "00") {
        //当小数点后为00时 去掉小数部分
        return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
      }
      return sizestr;
    },
    createFolder() {
      this.$prompt("请输入文件夹名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: !/(\\|:)+/g,
        inputErrorMessage: "文件夹格式不正确",
      })
        .then(({ value }) => {
          this.addFile(value);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消动作",
          });
        });
    },
    onBeforeUpload(file) {
      this.uploadData.file_type = file.type;
    },
    onUploadSuccess() {
      this.getFileList();
    },
    viewFiles(dir, idx) {
      let t = this.currentDirList.slice(0, idx).join("/");
      this.currentDirList = this.currentDirList.slice(0, idx + 1);
      if (t.startsWith("/")) this.currentDir = t + "/" + dir;
      else if (t.length) this.currentDir = "/" + t + "/" + dir;
      else this.currentDir = "/" + dir;
      this.getFileList();
    },
    getFileList() {
      this.loading = true;
      getFileList({ parent_dir: this.currentDir })
        .then((res) => {
          this.tableData = res.data.map((item) => {
            item.size = this.convert(item.size);
            item.icon = this.type2icon(item.file_type);
            return item;
          });
          this.urlRoot = res.url;
        })
        .catch((err) => {
          this.tableData = [];
          console.log(err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    addFile(name) {
      addFile({ name: name, parent_dir: this.currentDir })
        .then((res) => {
          this.$message.success(res.message);
          this.getFileList();
        })
        .catch((err) => {
          this.$message.error(err.message);
        });
    },
    updateFile(id, params) {
      updateFile(id, params)
        .then((res) => {
          this.$message.success(res.message);
          this.getFileList();
        })
        .catch((err) => {
          this.$message.error(err.message);
        });
    },
    deleteFile() {
      deleteFile({ uuids: this.selectList.map(item => item.uuid) })
        .then((res) => {
          this.$message.success(res.message);
          this.getFileList();
        })
        .catch((err) => {
          this.$message.error(err.message);
        });
    },
  },
  mounted() {},
  created() {
    this.getFileList();
  },
};
</script>