goodsMixin.js 5.33 KB

export default {
  data() {
    return {
      currentIndex:0,
      goodsCategoryId: '',
      inputValue: '',
      tagInputValue: '',
      inputVisible: false,
      tags: [],
      goodsTableData: [],
      goodsTableParams: {
        // rowStyle: ({ row, rowIndex }) => {
        //   return row.status == 0 ? {opacity:0.5} : ''
        // },
        tableColumns: [{
            label: '权益名称',
            prop: 'equityName',
            minWidth: '200',
          },
          {
            label: '权益描述',
            prop: 'equityDesc',
            minWidth: '200',
          },
          {
            // label: '权益图标\n(80*80,支持JPG、PNG、GIF)',
            minWidth: '200',
            prop:'goodsImage',
            addHtml: 'goodsImage',
            renderHeader: h => {
              return h('div', { class: 'table-header specHeader' }, [
                h('span', '权益图标'),
                h('table-tooltip', {
                  props: { content: '80*80,支持JPG、PNG、GIF' }
                })
              ])
            },
          },
          {
            label: '品牌名称',
            prop: 'brandName',
            minWidth: '200',
          },
          {
            label: '产品提供方',
            // prop: 'supplier',
            minWidth: '200',
            formatter: (cell) => {
              return '海威'
            },
          },
          {
            label: '首页展示',
            prop: 'mainPageFlag',
            minWidth: '200',
            addHtml: 'mainPageFlag'
          },
          {
            label: '操作',
            width: 155,
            fixed: 'right',
            addHtml: 'auth-button'
          }
        ]
      },
      showMoreTags:false,
      moreTags:[]
    }
  },
  mounted() {

  },
  methods: {
    /**获取商品分类列表 */
    getCategoryList() {
      this.$apis.goodsEquityManageApi.getCategoryList().then(({ data }) => {
        let { goodsCategoryId } = this
        let { list } = data
        let id = ''
        if (goodsCategoryId) {
          list.map(e => {
            e.active = false
            if (e.id == goodsCategoryId) {
              e.active = true
              id = goodsCategoryId
            }
          })
        } else {
          list[0].active = true
          id = list[0].id
        }
        this.tags = list || []
        this.getGoodsList(id)
        this.goodsCategoryId = id
        // if(this.tags.length>5){
        //   this.moreTags = this.tags.slice(5)
        //   this.tags = this.tags.slice(0,5)
        //   this.showMoreTags = true
        // }
      })
    },
    /**商品权益列表 */
    getGoodsList(goodsCategoryId, callBack) {
      this.$apis.goodsEquityManageApi.getGoodsList({ goodsCategoryId,pageSize: this.$refs.goodsTable.page.pageSize, pageNum: this.$refs.goodsTable.page.pageNum }).then(({ data }) => {
        if (!callBack) {
          this.goodsTableData = data.list || []
          this.tableTotal = data.total
        } else {
          return callBack(data.list)
        }
      })
    },
    /**切换分类 */
    changeGoodsCategory({ id, index }) {
      this.tags.map(e => e.active = false)
      this.tags[index].active = true
      this.getGoodsList(id)
      this.goodsCategoryId = id
      this.currentIndex = index
      // if(this.tags.length>5){
      //   this.moreTags = this.tags.slice(5)
      //   this.tags = this.tags.slice(0,5)
      //   this.showMoreTags = true
      // }
      if(this.$refs.mydrown){
        this.$refs.mydrown.hide()
      }
    },
    showInput(index, val) {
      this.inputValue = val
      this.$refs[`tag-input-${index}`][0].$el.classList.add('input-tag-show')
      this.$refs[`tag-input-${index}`][0].$refs.input.focus()
    },
    changeTagName({ id, index }) {
      let goodsCategory = this.inputValue
      if (!goodsCategory) {
        this.$refs[`tag-input-${index}`][0].$el.classList.remove('input-tag-show')
        return
      }
      this.$apis.goodsEquityManageApi.categoryUpdate({ id, goodsCategory }).then(({ code, message }) => {
        if (code == 0) {
          this.getCategoryList()
        } else {
          this.$message.error(message)
        }
        this.$refs[`tag-input-${index}`][0].$el.classList.remove('input-tag-show')
      })
    },
    /**删除分类 */
    categoryDelete(id) {
      this.getGoodsList(id, res => {
        if (res.length) {
          this.$message.error('请先删除该分类下的商品权益')
        } else {
          this.$apis.goodsEquityManageApi.categoryDelete({ id }).then(_res => {
            if (_res.code == 0) {
              this.getCategoryList()
              this.changeGoodsCategory({id:this.tags[0].id,index:0})
            } else {
              this.$message.error(_res.message)
            }
          })
        }
      })
    },
    showAddInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      let inputValue = this.tagInputValue
      if (inputValue) {
        this.$apis.goodsEquityManageApi.createCategory({ goodsCategory: inputValue }).then(({ code, message }) => {
          if (code == 0) {
            this.getCategoryList()
          } else {
            this.$message.error(message)
          }
        })
      }
      this.inputVisible = false
      this.tagInputValue = ''
    }
  }
}