upload.vue 4.5 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
<template>
2
  <div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3 4
    <el-upload
      :before-upload="checkFile"
5
      :headers="{'x-token':token}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6 7
      :on-error="uploadError"
      :on-success="uploadSuccess"
8
      :show-file-list="false"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9 10 11 12 13 14 15 16 17 18 19
      action="/api/fileUploadAndDownload/upload"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
    </el-upload>
    <el-table :data="tableData" border stripe>
      <el-table-column label="预览" width="100">
        <template slot-scope="scope">
          <img :alt="scope.row.alt" :src="scope.row.url" height="80" width="80" />
        </template>
      </el-table-column>
20 21
      <el-table-column label="日期" prop="UpdatedAt" width="180">
        <template slot-scope="scope">
22
          <div>{{scope.row.UpdatedAt|formatDate}}</div>
23 24
        </template>
      </el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
      <el-table-column label="文件名" prop="name" width="180"></el-table-column>
      <el-table-column label="链接" prop="url"></el-table-column>
      <el-table-column label="标签" prop="tag" width="100">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
            disable-transitions
          >{{scope.row.tag}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="downloadFile(scope.row)" size="small" type="text">下载</el-button>
38
          <el-button @click="deleteFile(scope.row)" size="small" type="text">删除</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
52
  </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
53
</template>
54
   
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
<script>
56 57
import { mapGetters } from 'vuex'
import infoList from '@/components/mixins/infoList'
58
import { getFileList, deleteFile } from '@/api/fileUploadAndDownload'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
import { downloadImage } from '@/utils/downloadImg'
60
import { formatTimeToStr } from '@/utils/data'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
61
export default {
62 63 64 65
  name: 'Upload',
  mixins: [infoList],
  data() {
    return {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
66 67
      listApi: getFileList,
      listKey: 'list',
68 69
      tableData: [
        {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70
          UpdatedAt: '2019-10-25',
71 72 73 74 75
          name: '文件名.jpg',
          url: 'http://qmplusimg.henrongyi.top/1571321688timg.jpg',
          tag: 'jpg'
        },
        {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
76
          UpdatedAt: '2019-10-25',
77 78 79 80 81 82 83 84 85 86
          name: '文件名.jpg',
          url: 'http://qmplusimg.henrongyi.top/157162774820191015140921496.gif',
          tag: 'gif'
        }
      ]
    }
  },
  computed: {
    ...mapGetters('user', ['userInfo', 'token'])
  },
87 88 89 90 91 92 93 94 95 96
  filters: {
    formatDate: function(time) {
      if (time != null && time != '') {
        var date = new Date(time)
        return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
      } else {
        return ''
      }
    }
  },
97
  methods: {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    async deleteFile(row) {
      this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteFile(row)
          if (res.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getTableData()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
121 122
    checkFile(file) {
      const isJPG = file.type === 'image/jpeg'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
123
      const isPng = file.type === 'image/png'
124
      const isLt2M = file.size / 1024 / 1024 < 2
125
      if (!isJPG && !isPng) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126
        this.$message.error('上传头像图片只能是 JPG或png 格式!')
127 128 129 130
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
131
      return (isPng || isJPG) && isLt2M
132
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
133
    uploadSuccess(res) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134 135 136 137
      this.$message({
        type: 'success',
        message: '上传成功'
      })
138
      if (res.success) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
139 140
        this.getTableData()
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
141
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
142
    uploadError() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
143 144 145 146 147 148 149
      this.$message({
        type: 'error',
        message: '上传失败'
      })
    },
    downloadFile(row) {
      downloadImage(row.url, row.name)
150 151
    }
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
152 153
}
</script>