基于qiniu-js封装图片上传,实现图片库,减少资源浪费

在开发中,有这样一个需求,点击上传logo,点击上传封面,点击上传头像等等,每次都上传很浪费存储资源,故要实现一个图片库的功能,点击先在图片库选择,如果图片库没有在执行上传。废话少说:上图
image.png

image.png

image.png

image.png
1. 图片库页面代码
<template>
  <div class="fileDialog">
    <el-dialog :append-to-body='appendToBody' title="选择图片" :visible.sync="imgFileDialogFlag.show" width="60%" destroy-on-close @open="openDialog"
               @close="closeDialog" :close-on-click-modal="false" :close-on-press-escape="false" :modal-append-to-body="false">
      <!-- 上传图片按钮区域 -->
      <div class="header_top">
        <span>
          <el-button size="mini" type="success" @click="vExampleAdd">上传图片</el-button>
          <form ref="vExample">
            <input type="file" style="display:none;" :accept="accept" ref="vExampleFile" @change="vExampleUpload" />
          </form>
        </span>
        <span>
          <el-input size="mini" placeholder="请输入图片名称" v-model="searchKey" @change="search" clearable>
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </span>
      </div>
      <!-- 表格 -->
      <div class="table">
        <el-table element-loading-text="正在为您拼命加载中..." :data="tableData" ref="multipleTable" style="width: 100%" @row-click="clickRow"
                  v-loading='loading' :header-cell-style="{background:'#eef1f6',color:'#38a4bd'}">
          <el-table-column label="" width="65">
            <template slot-scope="scope">
              <el-radio class="radio" v-model="templateSelection" :label="scope.$index"><br></el-radio>
            </template>
          </el-table-column>
          <!-- 图片 -->
          <el-table-column prop="videoUrl" label="图片" align="left">
            <template slot-scope="scope">
              <div class="imgBox">
                <div class="l">
                  <!-- <img :src="scope.row.videoUrl" alt="" width="100%"> -->
                  <el-image :src="scope.row.videoUrl" :preview-src-list="[scope.row.videoUrl]" fit="cover" lazy class="el-avatar">
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline"></i>
                    </div>
                  </el-image>
                </div>
                <div class="r">
                  <span>{{scope.row.videoName}}</span>
                  <span>{{scope.row.width}}*{{scope.row.height}}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <!-- 大小 -->
          <el-table-column prop="material_size" label="大小" align="center">
            <template slot-scope="scope">
              <span>{{scope.row.videoSize}}KB</span>
            </template>
          </el-table-column>
          <!-- 上传时间 -->
          <el-table-column prop="createTime" show-overflow-tooltip label="上传时间" align="right">
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <Pagination :page="page" :total="total" :page_size="page_size" :background="true" @handleCurrentChange="handleCurrentChange"
                  @handleSizeChange="handleSizeChange" />
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="imgFileDialogFlag.show = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="selectImage">确 定</el-button>
      </span>
      <el-dialog width="30%" title="上传进度" :visible.sync="innerVisible" append-to-body :show-close='false'>
        <div class="loading">
          <el-progress :text-inside="true" :stroke-width="20" :percentage="uploadPercent" status="exception"></el-progress>
        </div>

      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import * as qiniu from 'qiniu-js'
import { getFileAuth, getFileList, addFile } from "@/api/common";
export default {
  props: {
    imgFileDialogFlag: Object,
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      uploadPercent: 0,//上传进度
      innerVisible: false,
      tableHeight: document.documentElement.clientHeight - 300 || document.body.clientHeight - 300,
      searchKey: "",
      baseUrl: "",
      accept: 'image/*',
      loading: false,
      templateSelection: "",//选中的图片数据索引
      templateRadioUrl: "",//选中的图片url
      tableData: [],
      upToken: "",//上传token
      page: 1,
      page_size: 5,
      total: 0,
      imgSize: "",//图片大小
      imgWidth: "",//图片宽度
      imgHeight: "",//图片高度
      imgName: "",//图片名称
      imgType: "",//图片类型
    }
  },
  methods: {
    // 实现点击整行选中复选框
    clickRow(row) {
      this.templateSelection = this.tableData.indexOf(row);
      this.templateRadioUrl = row.videoUrl;
    },
    // 上传按钮触发
    vExampleAdd() {
      this.$refs.vExampleFile.click()
    },
    // input change 事件 上传图片
    vExampleUpload() {
      let _this = this
      let file = _this.$refs.vExampleFile.files[0]
      _this.imgName = file.name
      _this.imgSize = (file.size / 1024).toFixed(2)
      _this.imgType = file.type
      let config = {
        useCdnDomain: true,
        region: qiniu.region.z0
      };
      let putExtra = {
        fname: file,
        params: {},
        mimeType: null
      };
      let observe = {
        next(res) {
          // // console.log('已上传大小,单位为字节:' + res.total.loaded)
          // // console.log('本次上传的总量控制信息,单位为字节:' + res.total.size)
          // // console.log('当前上传进度,范围:0~100:' + res.total.percent);
          // _this.$emit("uploadPercent", res.total.percent)
          _this.uploadPercent = res.total.percent
          _this.innerVisible = true
        },
        error(err) {
          // console.log(err.code)
          console.log(err.message)
          // console.log(err.isRequestError)
          // console.log(err.reqId)
          // _this.$emit('uploaderr')
          _this.$message.error('上传失败!请勿重复上传')
          _this.innerVisible = false
        },
        complete(res) {
          //完成后的操作
          //上传成功以后会返回key 和 hash  key就是文件名了!
          let imageUrl = (_this.baseUrl + '/' + res.key)
          let img = new Image()
          img.src = imageUrl
          img.onload = function() {
            _this.imgWidth = img.width
            _this.imgHeight = img.height
            addFile({ width: _this.imgWidth, height: _this.imgHeight, categoryId: 3, videoName: _this.imgName, videoUrl: imageUrl, videoSize: _this.imgSize, mimeTypeName: _this.imgType }).then(res => {
              console.log(res);
              _this.innerVisible = false
              _this.$message.success('上传成功!')
              _this.templateSelection = ''
              _this.templateRadioUrl = ''
              _this.getFileList()
            })
          }
        }
      };
      //开始上传  token 需要找后端获取(单独的方法)
      getFileAuth().then(res => {
        this.upToken = res.data.auth
        this.baseUrl = res.data.bucket
        let key = res.data.uuid; // 文件资源名
        let headers = qiniu.getHeadersForMkFile(this.upToken)
        let observable = qiniu.upload(file, key, this.upToken, headers, putExtra, config)
        let subscription = observable.subscribe(observe)
      })
    },
    // 确认选择按钮触发
    selectImage() {
      this.$emit('selectImage', this.templateRadioUrl)
      this.templateSelection = ''
      this.templateRadioUrl = ""
    },
    // 获取图片列表
    getFileList() {
      this.loading = true
      getFileList({ pageSize: this.page_size, pageNum: this.page, mimeType: 3, videoName: this.searchKey }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    // 分页所需
    handleCurrentChange(val) {
      this.page = val;
      this.getFileList(this.searchKey);
      this.templateSelection = ''
      this.templateRadioUrl = ""
    },
    // 分页所需
    handleSizeChange(val) {
      this.page_size = val;
      // this.page = 1;
      this.getFileList(this.searchKey);
      this.templateSelection = ''
      this.templateRadioUrl = ""
    },
    // 打开弹窗触发
    openDialog() {
      this.getFileList()
    },
    closeDialog() { },
    // 搜索触发
    search() {
      this.getFileList()
    }
  }
}
</script>

<style lang="scss" scoped>
.header_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
}
.table {
  .imgBox {
    display: flex;
    .l {
      img {
        width: 68px;
        height: 48px;
        object-fit: scale-down;
        background: #fafafa;
      }
    }
    .r {
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      overflow: hidden;
      justify-content: space-between;
      span {
        text-align: left;
        &:first-child {
          font-weight: bold;
          display: block;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          color: #333;
        }
      }
    }
  }
}
::v-deep .el-dialog__header {
  background: #fafbfc;
  border-radius: 2px 2px 0 0;
  border-bottom: 1px solid #f2f2f2;
}
::v-deep .el-dialog__body {
  padding: 0px 20px;
}
.loading {
  padding-bottom: 20px;
}
</style>

2. 组件内用到了封装的分页组件,代码如下

<template>
  <div class="pagination">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[1,5, 10, 20, 30, 40, 50]" :layout="layout"
                   :current-page.sync="page" :page-size="page_size" :total="total" :background="background" small>
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "pagination",
  data() {
    return {
      page: 1
    }
  },
  props: {
    // 避免直接改变prop属性
    // 'currentPage': {
    //   required: false,
    //   default: 1
    // },
    'layout': {
      required: false,
      default: 'total, prev, pager, next, sizes'
    },
    'total': {
      required: false,
      default: 0
    },
    'page_size': {
      required: false,
      default: 10
    },
    'background': {
      required: false,
      type: Boolean,
      default: false
    }
  },
  watch: {
    currentPage(val) {
      // 改变这个值并不会触发 handleCurrentChange
      if (typeof val === "number") {
        // console.log('prop currentPage!!!');
        this.currentPage = val;
      }
    },
  },
  methods: {
    // 当前页变化
    handleCurrentChange(val) {
      this.$emit("handleCurrentChange", val);
    },
    // size变化
    handleSizeChange(val) {
      this.currentPage = 1;
      this.$emit('handleSizeChange', val);
    },
  }
}
</script>

<style lang="scss" scoped>
.pagination {
  margin: 10px 0 0 0;
  text-align: right;
}
::v-deep .el-pagination .el-select .el-input {
  margin-top: -3px;
}
::v-deep .el-pagination.is-background .el-pager li {
  border-radius: 50%;
}
::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #38a4bd;
}
::v-deep .el-pagination.is-background.el-pagination--small .btn-prev {
  border-radius: 50%;
}
::v-deep .el-pagination.is-background.el-pagination--small .btn-next {
  border-radius: 50%;
}
</style>

3. 写个容器调用组件
 <el-form-item label="LOGO">
            <div class="flex">
              <el-image :src="formDataHandle.orgLogo" :preview-src-list="[formDataHandle.orgLogo]" fit="cover" lazy class="el-avatar">
                <div slot="error" class="image-slot" @click="showImgFileDialog">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
              <div class="dot" v-if="formDataHandle.orgLogo != ''">
                <el-button class="closeicon" type="danger" circle icon="el-icon-delete" @click="delLogo"></el-button>
              </div>
            </div>

          </el-form-item>
<ImgFileDialog :appendToBody='true' @selectImage='selectImage' :imgFileDialogFlag='imgFileDialogFlag'></ImgFileDialog>

data(){
  return{
    imgFileDialogFlag: {
        show: false
      },
  }
}
methods(){
//打开图片库
    showImgFileDialog() {
      this.imgFileDialogFlag = {
        show: true
      }
    },
  selectImage(url) {
      this.formDataHandle.orgLogo = url //赋值
      this.imgFileDialogFlag = {
        show: false
      }
    },
//删除图片重新选择
    delLogo() {
      this.formDataHandle.orgLogo = ''
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270