materialGroup.vue 6.66 KB
<template>
  <div class="material-group">
    <div class="all-group">
      <div class="all-group-word" @click="getList">全部素材组 ({{ groupCounter.length }})</div>
      <div>
        <el-button class="add_btn" icon="el-icon-plus" :size="size" @click="handleAddGroup(1)">素材组</el-button>
      </div>
    </div>
    <ul>
      <li v-for="(item, index) in groupCounter" :class="index === currentIndex ? 'active' : ''" :key="index" @click="handleGroup(item, index)">
        <img src="@/assets/material/file.png" alt="" style="vertical-align: middle" />
        <div class="group-name">{{ item.materialGroupName }}</div>
        <el-popover placement="right" trigger="hover">
          <div>
            <GLButton class="group-btn" :size="size" type="text" fn="置顶" @click="handleStick(item)">{{ item.orderTime ? '取消置顶' : '置顶' }}</GLButton>
            <GLButton class="group-btn" :size="size" type="text" fn="修改" @click="handleAddGroup(2, item)">修改</GLButton>
            <GLButton class="group-btn" :size="size" type="text" fn="删除" v-if="item.isDefault == -1" @click="handleDeleteGroup(item.id)">删除</GLButton>
          </div>
          <i slot="reference" class="el-icon-more"></i>
        </el-popover>
      </li>
    </ul>
  </div>
</template>

<script>
import * as dayjs from 'dayjs'
export default {
  name: 'materialGroup',
  props: { eventBus: Object },
  data() {
    return {
      // 素材组数量
      groupCounter: [],
      materialGroup: {
        id: '',
        materialGroupName: '',
        orderValue: '',
        isDefault: '' // 默认素材组 0 是 -1 否(进入页面如果素材组数量为0,就会创建默认素材组,默认素材组不可删除)
      },
      currentIndex: null
    }
  },
  mounted() {
    // 新建素材组
    this.eventBus.$on('addMaterialGroup', data => {
      // data.businessType = data.businessType.toString()
      this.$apis.material.addMaterialGroup(data).then(res => {
        if (res) {
          this.getMaterialList()
        }
      })
    })
    // 编辑素材组
    this.eventBus.$on('editMaterialGroup', data => {
      // data.businessType = data.businessType.toString()
      this.$apis.material.editMaterialGroup(data).then(res => {
        if (res) {
          this.getMaterialList()
        }
      })
    })
    this.materialInsert()
    this.getMaterialList()
  },
  methods: {
    // 创建素材组
    materialInsert() {
      var params = {}
      params.isDefault = 0
      this.$apis.material.addMaterialGroup(params).then(res => {
        if (res) {
          this.getMaterialList()
        }
      })
    },
    // 获取全部素材组
    getMaterialList() {
      this.$apis.material.getMaterialList({}).then(res => {
        this.groupCounter = res
      })
    },
    // 点击素材组获取素材
    handleGroup(item, index) {
      this.currentIndex = index
      var params = {}
      params.groupId = item.id
      params.pageNum = 1
      params.pageSize = 1000
      this.$emit('getList', params, item)
    },
    // 打开1-新增, 2-编辑 素材组弹框
    handleAddGroup(addOrEdit, value) {
      console.log(addOrEdit, value)
      this.$emit('openAddDialog', addOrEdit, value, this.groupCounter)
    },
    // 素材组置顶
    handleStick(row) {
      var params = {}
      params.id = row.id
      // 置顶
      if (!row.orderTime) {
        var date = new Date()
        var stickDate = dayjs(date).format('YYYY-MM-DD HH:mm:ss')
        params.orderTime = stickDate // orderTime 排序时间
        params.orderFlag = true // orderFlag置顶标识 false 取消置顶 true 置顶或修改名称
        this.secondConfirm('提示', '确定置顶该素材组吗?', 'material', 'editMaterialGroup', params)
        return
      }
      // 取消置顶
      params.orderFlag = false
      this.secondConfirm('提示', '确定取消置顶该素材组吗?', 'material', 'editMaterialGroup', params)
    },
    // 素材组删除
    handleDeleteGroup(rowId) {
      this.$confirm('删除素材组,该素材组下的所有素材均会被删除,请谨慎操作', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        this.$apis.material.deleteMaterialList({ id: rowId }).then(res => {
          this.$message({
            message: res.code == '0' ? '操作成功' : '操作失败',
            type: res.code == '0' ? 'success' : 'error'
          })
          if (res.code == '0') {
            this.getMaterialList()
            // 素材组删除后素材列表刷新
            this.$emit('getListApi')
          }
        })
      })
    },
    // 点击全部素材组获取全部素材
    getList() {
      this.$emit('getListApi')
      this.currentIndex = ''
    },
    // 二次确认
    secondConfirm(title, txt, apiModule, apiName, apiParams, fn) {
      this.$confirm(txt, title, {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning',
        callback: action => {
          if (action == 'confirm') {
            this.$apis[apiModule][apiName](apiParams).then(res => {
              this.$message({
                message: res.code == '0' ? '操作成功' : '操作失败',
                type: res.code == '0' ? 'success' : 'error'
              })
              if (res.code == '0') {
                fn && fn()
                this.getMaterialList()
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-popover {
  min-width: 50px;
  &:hover {
    cursor: pointer;
  }
}
.material-group {
  .all-group {
    font-size: 16px;
    font-weight: 600;
    padding: 24px 16px 8px 16px;
    display: flex;
    justify-content: space-between;
    .el-button {
      margin-top: -13px;
    }
    &-word {
      cursor: pointer;
    }
  }
  ul {
    li {
      cursor: pointer;
      text-align: left;
      padding: 8px 16px 8px 18px;
      &.active {
        background-color: #ebeaff;
      }
      i {
        font-size: 16px;
        padding-top: 6px;
        position: absolute;
        right: 16px;
        transform: rotate(90deg);
      }
    }
  }
}
ul {
  padding: 0;
  text-align: center;
  margin-block-start: 0;
  margin-block-end: 0;
  li {
    list-style: none;
    position: relative;
    vertical-align: middle;
    &:nth-child(-n + 2) {
      padding-bottom: 10px;
    }
    &:hover {
      color: #5850ff;
      background-color: #ebeaff;
    }
  }
}
.add_btn {
  color: #5850ff;
}
.group-name {
  display: inline-flex;
  margin-left: 8px;
}
.group-btn {
  display: block;
  padding: 8px 10px;
  text-align: center;
  width: 100%;
  &:hover {
    background-color: #ebeaff;
  }
}
.el-button + .el-button {
  margin-left: 0;
}
</style>