Category.vue 8.3 KB
<template>
    <el-contaier>
        <el-row>
            <template>
                <el-table
                        :data="tableData"
                        border
                        style="width: 100%"
                        height="500">
                    <el-table-column
                            fixed
                            prop="id"
                            label="代码"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="名称"
                            min-width="200">
                    </el-table-column>
                    <el-table-column
                            fixed="right"
                            label="操作"
                            width="160"
                            align="left">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    type="primary"
                                    plain
                                    @click="handleClick(scope.row)"
                                    style="margin-right: 5px;">
                                编辑
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="danger"
                                    plain
                                    @click="applyDel(scope.row)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
        </el-row>
        <el-row>
            <el-dialog
                    :title="dialogMap[dialogApply]"
                    :visible.sync="dialogVisible"
                    width="30%"
                    :before-close="handleClose">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="任务名称" prop="name">
                            <el-input v-model="ruleForm.name"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                            <el-button @click="resetForm('ruleForm')">取消</el-button>
                        </el-form-item>
                    </el-form>
            </el-dialog>
        </el-row>
        <el-row>
            <el-col :span="2">
                <el-button type="primary" size="mini" @click="add()">新增</el-button>
            </el-col>
            <el-col :span="12">
                <div class="block">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="page"
                            :page-sizes="[10, 20, 30, 40]"
                            :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>
    </el-contaier>
</template>

<script>
    // import {delArea} from "../../../api/consigner/station";
    import {getCategoryList, updateCategory, addCategory, delCategory} from "../../../api/AiCustoms/CustomsTask"

    export default {
        data() {
            return {
                tableData: [],
                dialogVisible:false,
                ruleForm:{
                    id:'',
                    name:''
                },
                rules:{
                    name: [
                        { required: true, message: '请输入任务名称', trigger: 'blur' },
                        { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
                    ]
                },
                dialogMap: {
                    update: '编辑',
                    create: '新增'
                },
                dialogApply: 'create',
                page:1,
                pageSize:10,
                total:0,
            }
        },
        mounted() {
          this.getCategoryList()
        },
      methods: {
          // 获取数据列表
          getCategoryList(){
            getCategoryList({page:this.page, pageSize:this.pageSize}).then((response) => {
              const code = response.data.code;
              if (code != 200){
                return this.$message.error('获取列表失败!')
              }
              this.tableData = response.data.data.rows;
              this.total = response.data.data.total;
              this.$message.success('获取列表成功!')
            }).catch(error => {
              this.$message.error(error.toString())
            })
          },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.getCategoryList();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page = val;
                this.getCategoryList();
            },
            handleClick(row) {
                this.ruleForm=JSON.parse(JSON.stringify(row));
                this.dialogApply='update';
                this.dialogVisible=true;
            },
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (!valid){
                        console.log('error submit!!');
                        return false;
                    }
                    try {
                      let response;
                      if (this.dialogApply === 'create'){
                        response = await addCategory(this.ruleForm);
                      } else if (this.dialogApply === 'update'){
                        response = await updateCategory(this.ruleForm);
                      }
                      if (response.data.code !== 200){
                        return this.$message.error(this.dialogApply === 'create' ? '新增失败!' : '更新失败!')
                        return;
                      }
                      this.$message.success(this.dialogApply === 'create' ? '新增成功!' : '更新成功!')
                      this.dialogVisible = false;
                      this.getCategoryList();
                    } catch (error){
                      this.$message.error(error.toString())
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.dialogVisible=false;
            },
            handleClose(done) {
                this.$confirm('确认关闭?')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },
            // 删除
            applyDel(row) {
                // 弹框询问是否删除?
                this.$confirm('此操作永久删除该消息收发记录, 是否继续?', '警告', {
                        confirmButtonText: '确定删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    // 删除
                    const delIds = [row.id];
                    delCategory(delIds).then((response) => {
                      const code =response.data.code;
                      if (code != 200){
                        return this.$message.error('删除失败!')
                      }
                      this.$message.success('删除成功!')
                      this.getCategoryList();
                    }).catch(error => {
                      this.$message.error(error.toString())
                    })
                }).catch(() => {
                })
            },
            add(){
                this.ruleForm={
                    id:'',
                    name:''
                };
                this.dialogApply='create';
                this.dialogVisible=true;
            }
        }
    }
</script>