ClassificationOfDishes.vue 6.67 KB
<template>
  <div class="classificationOfDishes">
    <a-button style="margin-bottom: 20px" type="primary" @click="showModal(1)">添加分类</a-button>
    <a-table :columns="columns" :dataSource="dataSource" rowKey="productCategoryId" :pagination="pagination">
      <span slot="productCategoryName"></span>
      <span slot="sort"></span>
      <span slot="isDisplay" slot-scope="isDisplay">
        <a-badge v-if="isDisplay === 'DISPLAY'" status="success" text="已显示" />
        <a-badge v-else-if="isDisplay === 'HIDDEN'" status="error" text="已隐藏" />
      </span>
      <span slot="action" slot-scope="text, record, index">
        <a style="margin-right: 8px" href="javascript:;" @click="showModal(2, record)">修改</a>
        <a href="javascript:;" @click="showConfirm(record, index)">{{record.isDisplay === 'DISPLAY' ? '隐藏' : '显示'}}</a>
      </span>
    </a-table>
    <a-modal
      title="增加分类"
      :visible="visible"
      @ok="saveProductCategory"
      :confirmLoading="confirmLoading"
      @cancel="handleCancel"
    >
      <div class="item">
        <label>分类名称:</label><a-input v-model="productCategoryName" placeholder="请输入分类名称"/>
      </div>
      <div class="item">
        <label>排序值:</label><a-input v-model="sort" placeholder="排序值为正整数"/>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { $http } from './../../../api/axios.js'
export default {
  name: 'classificationOfDishes',
  data () {
    return {
      columns: [{
        title: '菜品类别',
        dataIndex: 'productCategoryName'
      }, {
        title: '排序值',
        dataIndex: 'sort'
      }, {
        title: '状态',
        dataIndex: 'isDisplay',
        scopedSlots: { customRender: 'isDisplay' }
      }, {
        title: '操作',
        key: 'action',
        scopedSlots: { customRender: 'action' }
      }],
      productCategoryName: '',
      sort: '',
      dataSource: [],
      visible: false,
      modelType: 1,
      modelSource: '',
      confirmLoading: false,
      pagination: {
        current: 1,
        defaultCurrent: 1,
        defaultPageSize: 1000,
        hideOnSinglePage: true,
        total: 0
      }
    }
  },
  created: function () {
    this.getProductCategoryList()
  },
  methods: {
    getProductCategoryList () {
      $http.post(`/mingpay/v1/isv/product/list_product_category`).then((res) => {
        if (res.data.resultCode === 0) {
          this.dataSource = res.data.data
        }
      })
    },
    changeItem () {
      this.confirmLoading = true
      let updateProductCategoryData = {
        productCategoryId: this.modelSource.productCategoryId,
        productCategoryName: this.productCategoryName,
        sort: this.sort
      }
      $http.post(`/mingpay/v1/isv/product/update_product_category`, updateProductCategoryData).then((res) => {
        if (res.data.resultCode === 0) {
          this.visible = false
          this.confirmLoading = false
          this.sort = ''
          this.productCategoryName = ''
          this.getProductCategoryList()
          this.$message.success('操作成功')
        } else {
          this.$message.error('操作失败')
        }
      }).catch(() => {
        this.$message.error('操作失败')
        this.confirmLoading = false
      })
    },
    showConfirm (record, index) {
      let that = this
      let title
      let content
      if (record.isDisplay === 'DISPLAY') {
        title = '确认要隐藏该菜品分类吗?'
        content = '隐藏后该分类下的所有菜品设备端都不可见'
      } else {
        title = '确认显示该分类吗?'
        content = '显示后该分类下菜品可正常使用'
      }
      this.$confirm({
        title: title,
        content: content,
        onOk () {
          that.changeData(record, index)
        },
        onCancel () {}
      })
    },
    changeData (record, index) {
      let saveProductCategoryData = {
        productCategoryId: this.categoryName,
        sort: this.sort
      }
      if (record.isDisplay === 'DISPLAY') {
        saveProductCategoryData = {
          productCategoryId: record.productCategoryId,
          isDisplay: 'HIDDEN'
        }
      } else {
        saveProductCategoryData = {
          productCategoryId: record.productCategoryId,
          isDisplay: 'DISPLAY'
        }
      }
      $http.post(`/mingpay/v1/isv/product/update_product_category_status`, saveProductCategoryData).then((res) => {
        if (res.data.resultCode === 0) {
          this.sort = ''
          this.productCategoryName = ''
          this.getProductCategoryList()
          this.$message.success('操作成功')
        } else {
          this.$message.error('操作失败')
        }
      }).catch(() => {
        this.$message.error('操作失败')
        this.confirmLoading = false
      })
    },
    showModal (type, data) {
      this.modelSource = data
      this.modelType = type
      this.visible = true
      if (type === 2) {
        this.productCategoryName = data.productCategoryName
        this.sort = data.sort
      }
    },
    // 创建菜单
    saveProductCategory () {
      let that = this
      let reg = '^[0-9]*$'
      if (!/^[0-9]*$/.test(this.sort)) {
        this.$message.error('排序值为正整数')
        return false
      }
      if (this.productCategoryName === '') {
        this.$message.error('分类名称不能为空')
      } else if (this.sort === '') {
        this.$message.error('排序值不能为空')
      } else {
        if (this.modelType === 2) {
          this.changeItem()
          return
        }
        this.confirmLoading = true
        let saveProductCategoryData = {
          productCategoryName: this.productCategoryName,
          sort: parseInt(this.sort)
        }
        $http.post(`/mingpay/v1/isv/product/save_product_category`, saveProductCategoryData).then((res) => {
          if (res.data.resultCode === 0) {
            this.visible = false
            this.confirmLoading = false
            this.sort = ''
            this.productCategoryName = ''
            this.getProductCategoryList()
            this.$message.success('操作成功')
          } else {
            this.$message.error('操作失败')
          }
        }).catch(() => {
          this.$message.error('操作失败')
          this.confirmLoading = false
        })
      }
    },
    handleCancel (e) {
      this.visible = false
      this.sort = ''
      this.productCategoryName = ''
    }
  }
}
</script>

<style lang="less" scoped>
.item{
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px;
}
.item>label{
  width: 75px;
  text-align: right;
  margin-right: 8px;
}
.item>input{
  width: 220px;
}
</style>