api.vue 4.8 KB
Newer Older
1
<template>
2 3
  <div>
    <div class="button-box clearflex">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
4
      <el-button @click="openDialog('addApi')" type="primary">新增api</el-button>
5
    </div>
6
    <el-table :data="tableData" border stripe>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7 8
      <el-table-column label="id" min-width="60" prop="ID"></el-table-column>
      <el-table-column label="api路径" min-width="150" prop="path"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9
      <el-table-column label="api分组" min-width="150" prop="group"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
      <el-table-column label="api简介" min-width="150" prop="description"></el-table-column>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="editApi(scope.row)" size="small" type="text">编辑</el-button>
          <el-button @click="deleteApi(scope.row)" size="small" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
28 29 30 31

    <el-dialog :visible.sync="dialogFormVisible" title="新增Api">
      <el-form :inline="true" :model="form" label-width="80px">
        <el-form-item label="路径">
32
          <el-input @blur="autoGroup" autocomplete="off" v-model="form.path"></el-input>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
34 35 36 37
        <el-form-item label="api分组">
          <el-input autocomplete="off" v-model="form.group"></el-input>
        </el-form-item>
        <el-form-item label="api简介">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38 39 40 41 42 43 44 45
          <el-input autocomplete="off" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
46
  </div>
47 48
</template>

Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
49

50
<script>
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
51 52
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成

53 54 55 56 57 58 59
import {
  getApiById,
  getApiList,
  createApi,
  updataApi,
  deleteApi
} from '@/api/api'
60 61
import infoList from '@/view/superAdmin/mixins/infoList'

62
export default {
63
  name: 'Api',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
64
  mixins: [infoList],
65 66 67
  data() {
    return {
      listApi: getApiList,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68 69 70 71
      listKey: 'list',
      dialogFormVisible: false,
      form: {
        path: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
72
        group: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73 74 75
        description: ''
      },
      type: ''
76 77 78
    }
  },
  methods: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
79
    // 自动设置api分组
80
    autoGroup() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
81 82
      this.form.group = this.form.path.split('/')[1]
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
83 84 85
    initForm() {
      this.form = {
        path: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
86
        group: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        description: ''
      }
    },
    closeDialog() {
      this.initForm()
      this.dialogFormVisible = false
    },
    openDialog(type) {
      this.type = type
      this.dialogFormVisible = true
    },
    async editApi(row) {
      const res = await getApiById({ id: row.ID })
      this.form = res.data.api
      this.openDialog('edit')
    },
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    async deleteApi(row) {
      this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteApi(row)
          if (res.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getTableData()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    async enterDialog() {
      switch (this.type) {
        case 'addApi':
          {
            const res = await createApi(this.form)
            if (res.success) {
              this.$message({
                type: 'success',
                message: '添加成功',
                showClose: true
              })
            }
            this.getTableData()
            this.closeDialog()
          }
141

Mr.奇淼('s avatar
Mr.奇淼( 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          break
        case 'edit':
          {
            const res = await updataApi(this.form)
            if (res.success) {
              this.$message({
                type: 'success',
                message: '添加成功',
                showClose: true
              })
            }
            this.getTableData()
            this.closeDialog()
          }
          break
        default:
          {
            this.$message({
              type: 'error',
              message: '未知操作',
              showClose: true
            })
          }
          break
      }
    }
168
  }
169 170
}
</script>
171 172 173 174 175 176 177
<style scoped lang="scss">
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
178
</style>