api.vue 7.2 KB
Newer Older
1
<template>
2
  <div>
3
    <div class="search-term">
4
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
5 6 7 8 9 10 11 12 13 14 15
        <el-form-item label="路径">
          <el-input placeholder="路径" v-model="searchInfo.path"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit" type="primary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="openDialog('addApi')" type="primary">新增api</el-button>
        </el-form-item>
      </el-form>
    </div>
16
    <el-table :data="tableData" border stripe>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17 18
      <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.奇淼( 已提交
19
      <el-table-column label="api分组" min-width="150" prop="group"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
20
      <el-table-column label="api简介" min-width="150" prop="description"></el-table-column>
21 22 23
      <el-table-column label="请求" min-width="150" prop="method">
        <template slot-scope="scope">
          <div>
J
style  
jinlan.du 已提交
24 25 26 27
            {{scope.row.method}}
            <el-tag
              :key="scope.row.methodFiletr"
              :type="scope.row.method|tagTypeFiletr"
28
               size="mini"
J
style  
jinlan.du 已提交
29 30 31 32
              effect="dark">
              {{scope.row.method|methodFiletr}}
            </el-tag>
            <!-- {{scope.row.method|methodFiletr}} -->
33 34 35 36
          </div>
        </template>
      </el-table-column>
      
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      <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.奇淼( 已提交
54

55
    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="新增Api">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
56 57
      <el-form :inline="true" :model="form" label-width="80px">
        <el-form-item label="路径">
58
          <el-input autocomplete="off" v-model="form.path"></el-input>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
        </el-form-item>
60 61 62 63 64 65 66 67 68 69
        <el-form-item label="请求">
          <el-select placeholder="请选择" v-model="form.method">
            <el-option
              :key="item.value"
              :label="`${item.label}(${item.value})`"
              :value="item.value"
              v-for="item in methodOptions"
            ></el-option>
          </el-select>
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70 71 72 73
        <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.奇淼( 已提交
74 75 76
          <el-input autocomplete="off" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
77 78 79
      <div class="warning">
          新增Api需要在角色管理内配置权限才可使用
      </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80 81 82 83 84
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
85
  </div>
86 87
</template>

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

89
<script>
90
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成 条件搜索时候 请把条件安好后台定制的结构体字段 放到 this.searchInfo 中即可实现条件搜索
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
91

92 93 94 95 96 97 98
import {
  getApiById,
  getApiList,
  createApi,
  updataApi,
  deleteApi
} from '@/api/api'
99
import infoList from '@/components/mixins/infoList'
100

101
const methodOptions = [
102 103
        {
          value: 'POST',
J
style  
jinlan.du 已提交
104 105
          label: '创建',
          type:'success'
106 107 108
        },
        {
          value: 'GET',
J
style  
jinlan.du 已提交
109 110
          label: '查看',
          type:''
111 112 113
        },
        {
          value: 'PUT',
J
style  
jinlan.du 已提交
114 115
          label: '更新',
          type:'warning'
116 117 118
        },
        {
          value: 'DELETE',
J
style  
jinlan.du 已提交
119 120
          label: '删除',
          type:'danger'
121 122 123
        }
      ]

124
export default {
125
  name: 'Api',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126
  mixins: [infoList],
127 128 129
  data() {
    return {
      listApi: getApiList,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
130 131 132 133
      listKey: 'list',
      dialogFormVisible: false,
      form: {
        path: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
        group: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
135 136
        description: ''
      },
137
      methodOptions: methodOptions,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
138
      type: ''
139 140 141
    }
  },
  methods: {
142
    //条件搜索前端看此方法
143 144
    onSubmit() {
      this.page = 1
145 146 147
      this.pageSize = 10
      this.getTableData()
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
148 149 150
    initForm() {
      this.form = {
        path: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
151
        group: '',
152 153
        description: '',
        method:''
Mr.奇淼('s avatar
Mr.奇淼( 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      }
    },
    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')
    },
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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.奇淼( 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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()
          }
207

Mr.奇淼('s avatar
Mr.奇淼( 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
          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
      }
    }
234 235 236 237
  },
  filters:{
    methodFiletr(value){
      const target = methodOptions.filter(item=>item.value === value)[0]
J
style  
jinlan.du 已提交
238 239 240 241 242 243
      // return target && `${target.label}(${target.value})`
      return target && `${target.label}`
    },
    tagTypeFiletr(value){
      const target = methodOptions.filter(item=>item.value === value)[0]
      return target && `${target.type}`
244
    }
245
  }
246 247
}
</script>
248 249 250 251 252 253 254
<style scoped lang="scss">
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
J
style  
jinlan.du 已提交
255 256 257
.el-tag--mini{
  margin-left: 5px;
}
258 259 260
.warning {
    color: #DC143C;
}
261
</style>