materialGroup.vue 7.66 KB
<template>
  <div class="material-group" :style="{ height: groupHeight + 'px' }">
    <el-button class="add_btn" icon="el-icon-plus" :size="size" @click="handleAddGroup(1)">素材组</el-button>
    <div class="all-group">
      <div @click="getList">
        <img src="@/assets/material/groupIcon.png" alt="" />
        全部素材组 ({{ groupCounter.length }})
      </div>
      <div @click="groupVisible = !groupVisible">
        <i class="el-icon-arrow-down" v-show="groupVisible"></i>
        <i class="el-icon-arrow-up" v-show="!groupVisible"></i>
      </div>
    </div>
    <ul v-show="groupVisible" :style="{ height: groupHeight - 110 + 'px' }">
      <li class="group-list" v-for="(item, index) in groupCounter" :class="index === currentIndex ? 'active' : ''" :key="index" @click="handleGroup(item, index)">
        <div>
          <img src="@/assets/material/file.png" alt="" style="vertical-align: middle;height:24px;width:24px" />
          <div class="group-name">{{ item.materialGroupName }}</div>
        </div>
        <div>
          <el-popover placement="right" trigger="click">
            <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> -->
              <el-button class="group-btn" :size="size" type="text" fn="置顶" @click="handleStick(item)">{{ item.orderTime ? '取消置顶' : '置顶' }}</el-button>
              <el-button class="group-btn" :size="size" type="text" fn="修改" @click="handleAddGroup(2, item)">修改</el-button>
              <el-button class="group-btn" :size="size" type="text" fn="删除" v-if="item.isDefault == -1" @click="handleDeleteGroup(item.id)">删除</el-button>
            </div>
            <div slot="reference">
              <img class="more-icon" src="@/assets/material/more.png" alt="" />
            </div>
          </el-popover>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import * as dayjs from 'dayjs'
export default {
  name: 'materialGroup',
  props: { eventBus: Object, groupHeight: Number },
  data() {
    return {
      // 素材组数量
      groupCounter: [],
      materialGroup: {
        id: '',
        materialGroupName: '',
        orderValue: '',
        isDefault: '' // 默认素材组 0 是 -1 否(进入页面如果素材组数量为0,就会创建默认素材组,默认素材组不可删除)
      },
      currentIndex: null,
      groupVisible: true
    }
  },
  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>
.status-words {
  margin-left: 5px;
}
.material-group {
  /deep/.el-popover {
    min-width: 50px;
    &:hover {
      cursor: pointer;
    }
  }
  .all-group {
    font-size: 16px;
    height: 44px;
    line-height: 44px;
    padding: 0 17px 0 16px;
    display: flex;
    justify-content: space-between;
    cursor: pointer;
    img {
      width: 24px;
      height: 24px;
      vertical-align: middle;
    }
    i {
      color: #999999;
    }
  }
  ul {
    overflow: auto;
    .group-list {
      cursor: pointer;
      text-align: left;
      padding: 8px 8px 8px 18px;
      margin: 0 10px;
      display: flex;
      justify-content: space-between;
      &.active {
        background-color: #ebeaff;
      }
      .more-icon {
        vertical-align: middle;
        padding-top: 3px;
      }
      &:hover {
        background-color: #ebeaff;
      }
    }
  }
}
.add_btn {
  color: #5850ff;
  border-color: #5850ff;
  margin: 20px 0 12px 16px;
}
.group-name {
  display: inline-flex;
  margin-left: 8px;
}
.group-btn {
  color: #333333;
  display: block;
  padding: 8px 0px;
  text-align: left;
  width: 100%;
  &:hover {
    background-color: #ebeaff;
  }
}
.el-button + .el-button {
  margin-left: 0;
}
</style>