<template>
  <!-- 上传图片模态框 -->
  <el-dialog
    title='我的图片'
    :visible.sync='dialogImageVisible'
    :before-close='dialogHide'
    width='900px'
  >
    <!-- 新上传图片 -->
    <el-upload
      ref='upload'
      :action='actionUrl'
      :multiple='false'
      list-type='picture'
      :show-file-list='false'
      :on-success='dosuccess'
      :on-error='doerror'
      :before-upload='beforeAvatarUpload'
    >
      <el-button size='small' type='primary' :loading='uploading'>
        +&nbsp;上传图片
      </el-button>
      <div slot='tip' class='el-upload__tip'>
        只能上传jpg/png文件,且不超过500kb
      </div>
    </el-upload>
<pre><code>&lt;!-- 选择图片库已有图片 --&gt;
&lt;div v-if='groupList &amp;&amp; groupList.length &gt; 0' class='picture-content'&gt;
  &lt;ul v-loading='groupLoading' class='picture-group'&gt;
    &lt;li
      v-for='item in groupList'
      :key='item.uuid'
      :class='{&amp;#x27;active&amp;#x27;:selectGroupId === item.uuid}'
      @click='changeGroup(item.uuid)'
    &gt;
      &lt;span class='size'&gt;{{ item.count }}&lt;/span&gt;
      {{ item.name }}
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;div v-loading='loading' class='picture-main'&gt;
    &lt;ul v-show='list &amp;&amp; list.length &gt; 0' class='picture-list'&gt;
      &lt;li v-for='item in list' :key='item.uuid' @click='selectPic(item)'&gt;
        &lt;div class='pic'&gt;
          &lt;img v-if='item.pictureUrl' :src='item.pictureUrl'&gt;
        &lt;/div&gt;
        &lt;p class='name'&gt;
          {{ item.pictureName }}
        &lt;/p&gt;
        &lt;div v-show='selectPicId === item.uuid' class='seleted'&gt;
          &lt;i class='el-icon-check' /&gt;
        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;div v-show='!list || list.length === 0' class='no-pic'&gt;
      该分类下暂无图片
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div v-show='list &amp;&amp; list.length &gt; 0' class='list-page'&gt;
    &lt;el-pagination
      background
      :current-page='nowPage'
      layout='total, prev, pager, next, jumper'
      :total='total'
      @current-change='pageChange'
    /&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div v-else class='no-data'&gt;
  图片库中暂无图片
&lt;/div&gt;

&lt;div slot='footer' class='dialog-footer'&gt;
  &lt;el-button type='primary' :disabled='!selectPicUrl' @click='selectSure'&gt;
    确 定
  &lt;/el-button&gt;
  &lt;el-button @click='dialogHide'&gt;
    取 消
  &lt;/el-button&gt;
&lt;/div&gt;
</code></pre>
  </el-dialog>
</template>
<script>
import { fetchPictureGroup, fetchPictureList, upLoadUrl } from &#x27;@/api/index&#x27;

export default {
  name: &#x27;UpLoadImg&#x27;,
  props: [&#x27;dialogImageVisible&#x27;],
  data() {
    return {
      groupLoading: false, // 分组数据是否正在加载中
      groupList: [], // 分组数据
      selectGroupId: &#x27;&#x27;, // 当前选择的分类ID
      loading: false, // 图片列表数据是否正在加载中
      nowPage: 1, // 当前页
      total: 0, // 列表数据条数
      // 列表数据
      list: [],
      selectPicId: &#x27;&#x27;, // 当前选择的图片ID
      selectPicUrl: &#x27;&#x27;, // 当前选择的图片地址
      actionUrl: upLoadUrl, // 上传图片接口地址
      uploading: false // 是否正在上传图片中
    }
  },
  watch: {
    // 模态框第一次显示时获取列表数据
    dialogImageVisible(newVal, oldVal) {
      if (newVal && this.groupList.length === 0) {
        // 改图片接口暂时没有
        // this.getGroup()
      }
    }
  },
  methods: {
    // 获取图片库分组
    getGroup() {
      var qm = {
        queryParams: {
          vendorUuid: {
            operation: &#x27;EQ&#x27;,
            value: &#x27;&#x27;
          }
        }
      }
      this.groupLoading = true
      fetchPictureGroup(qm).then(res => {
        this.groupLoading = false
        let list = res.returnMessage
        if (list && list.length > 0) {
          this.groupList = list
          // 获取第一个分组的图片列表
          this.changeGroup(list[0].uuid)
        }
      })
    },
    // 切换选择的分组
    changeGroup(id) {
      this.selectGroupId = id
      this.getList()
    },
    // 获取图片库数据
    getList() {
      var qm = {
        queryPage: this.nowPage,
        pageShow: 10,
        queryParams: {
          vendorUuid: {
            operation: &#x27;EQ&#x27;,
            value: &#x27;&#x27;
          },
          groupUuid: {
            operation: &#x27;EQ&#x27;,
            value: this.selectGroupId
          }
        }
      }
      this.loading = true
      fetchPictureList(qm).then(res => {
        this.loading = false
        this.list = res.returnMessage
        this.total = res.pager.totalNum
      })
    },
    // 图片数据翻页
    pageChange(val) {
      this.nowPage = val
      this.getList()
    },
    // 选择图片库商品图片
    selectPic(item) {
      if (this.selectPicId === item.uuid) {
        this.clearSelectPic()
      } else {
        this.selectPicId = item.uuid
        this.selectPicUrl = item.pictureUrl
      }
    },
    // 清除当前选择的图片
    clearSelectPic() {
      this.selectPicId = &#x27;&#x27;
      this.selectPicUrl = &#x27;&#x27;
    },
    // 确认选择图片库图片
    selectSure() {
      this.$emit(&#x27;upLoadImgSuccess&#x27;, this.selectPicUrl)
      this.dialogHide()
      this.clearSelectPic()
    },
    // 上传图片前的验证方法
    beforeAvatarUpload(file) {
      console.log(file)
      const isJPG = file.type === &#x27;image/jpeg&#x27; || file.type === &#x27;image/png&#x27;
      const isLt500 = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error(&#x27;上传图片只能是 JPG/PNG 格式!&#x27;)
      }
      if (!isLt500) {
        this.$message.error(&#x27;上传图片大小不能超过 500kb!&#x27;)
      }
      if (isJPG && isLt500) {
        this.uploading = true
        return true
      } else {
        return false
      }
    },
    // 上传图片成功的方法
    dosuccess(response, file, fileList) {
      this.uploading = false
      this.$emit(&#x27;upLoadImgSuccess&#x27;, response.data)
      this.dialogHide()
    },
    // 上传图片失败的方法
    doerror() {
      this.$message.error(&#x27;上传失败,请稍后重试&#x27;)
      this.uploading = false
    },
    dialogHide() {
      this.$emit(&#x27;update:dialogImageVisible&#x27;, false)
    }
  }
}
</script>
<style lang='less' scoped>
.el-upload__tip {
  display: inline-block;
  vertical-align: middle;
  margin: 0 0 0 10px;
  color: #999;
}
// 图片库图片样式
.picture-content {
  margin-top: 10px;
  overflow: hidden;
  .picture-group {
    float: left;
    width: 180px;
    background: #f2f2f2;
    height: 400px;
    padding-top: 5px;
    overflow-y: auto;
    li {
      font-size: 12px;
      padding: 0 15px;
      line-height: 30px;
      cursor: pointer;
      color: #222;
      .size {
        float: right;
        color: #999;
      }
      &.active {
        background: #fff;
      }
    }
  }
  .picture-main {
    margin-left: 190px;
    height: 405px;
    overflow: hidden;
    .picture-list {
      height: 100%;
      overflow-y: auto;
      li {
        position: relative;
        float: left;
        width: 105px;
        margin-right: 10px;
        cursor: pointer;
        .pic {
          width: 105px;
          height: 105px;
          background-color: #eee;
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        .name {
          font-size: 12px;
          line-height: 30px;
          height: 30px;
          .ellipsis;
        }
        .seleted {
          position: absolute;
          left: 0;
          top: 0;
          width: 105px;
          height: 105px;
          border: 2px solid @color-1;
          box-sizing: border-box;
          &:after {
            position: absolute;
            content: &#x27; &#x27;;
            right: 0;
            top: 0;
            border-color: #07d #07d transparent transparent;
            border-style: solid;
            border-width: 14px;
            z-index: 1;
          }
          i {
            position: absolute;
            right: 1px;
            top: 1px;
            z-index: 2;
            font-size: 12px;
            color: #fff;
          }
        }
      }
    }
    .no-pic {
      padding-top: 120px;
      text-align: center;
      font-size: 12px;
      color: #999;
    }
  }
}
.list-page {
  clear: both;
  padding-top: 15px;
  text-align: right;
}
</styl

原文地址: https://www.cveoy.top/t/topic/nDWo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录