Blame view

ant-design-vue-jeecg/src/views/system/modules/GateWayRouteModal.vue 9.57 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    width="45%"
    placement="right"
    v-if="visible"
    :closable="true"
    @close="handleCancel"
    :visible="visible"
11
12
    style="overflow: auto;padding-bottom: 53px;"
  >
肖超群 authored
13
14
    <a-form-model ref="form" :layout="layout" :label-col="labelCol" :wrapper-col="wrapperCol" :model="router">
      <a-form-model-item label="路由ID">
15
        <a-input v-model="router.routerId" placeholder="路由唯一ID" />
肖超群 authored
16
17
      </a-form-model-item>
      <a-form-model-item label="路由名称">
18
        <a-input v-model="router.name" placeholder="路由名称" />
肖超群 authored
19
20
      </a-form-model-item>
      <a-form-model-item label="路由URI">
21
        <a-input v-model="router.uri" placeholder="路由URL" />
肖超群 authored
22
23
      </a-form-model-item>
      <a-form-model-item label="路由状态" prop="status">
24
        <a-switch default-checked v-model="router.status" />
肖超群 authored
25
26
      </a-form-model-item>
      <a-form-model-item prop="predicates" label="路由条件">
27
        <div v-for="(item, index) in router.predicates" :key="index">
肖超群 authored
28
          <a-divider>{{ item.name }}
29
            <a-icon type="delete" size="22" @click="removePredicate(router, index)" />
肖超群 authored
30
31
32
          </a-divider>
          <div>
            <template v-for="(tag, index) in item.args">
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
              <a-input
                v-if="index == currentTagIndex"
                ref="input"
                type="text"
                size="small"
                :style="{ width: '190px' }"
                :value="tag"
                @change="handleInputChange"
                @blur="handleInputEditConfirm(item, tag, index)"
                @keyup.enter="handleInputEditConfirm(item, tag, index)"
                :key="index"
              />
              <a-tag
                v-else
                :key="tag"
                :closable="true"
                @close="() => removeTag(item, tag)"
                @click="editTag(tag, index)"
              >
肖超群 authored
52
53
54
                {{ tag }}
              </a-tag>
            </template>
55
56
57
58
59
60
61
62
63
64
65
66
67
            <a-input
              v-if="inputVisible && index == currentNameIndex"
              ref="input"
              type="text"
              size="small"
              :style="{ width: '100px' }"
              :value="inputValue"
              @change="handleInputChange"
              @blur="handleInputConfirm(item)"
              @keyup.enter="handleInputConfirm(item)"
            />
            <a-tag v-else style="background: #fff; borderStyle: dashed;" @click="showInput(item, index)">
              <a-icon type="plus" /> 新建{{ item.name }}
肖超群 authored
68
69
70
71
72
73
            </a-tag>
          </div>
        </div>
        <p class="btn" style="padding-top: 10px">
          <a-dropdown>
            <a-menu slot="overlay" @click="predicatesHandleMenuClick">
肖超群 authored
74
              <a-menu-item :key="item" v-for="item in tagArray">{{ item }}</a-menu-item>
肖超群 authored
75
            </a-menu>
76
            <a-button type="dashed" style="margin-left: 8px;width:100%"> 添加路由条件 <a-icon type="down" /> </a-button>
肖超群 authored
77
78
79
80
          </a-dropdown>
        </p>
      </a-form-model-item>
      <a-form-model-item prop="predicates" label="过滤器">
81
        <div v-for="(item, index) in router.filters" :key="index">
肖超群 authored
82
          <a-divider>{{ item.name }}
83
            <a-icon type="delete" size="22" @click="removeFilter(router, index)" />
肖超群 authored
84
85
          </a-divider>
          <div v-for="(tag, index) in item.args" :key="tag.key">
86
87
88
            <a-input v-model="tag.key" placeholder="参数键" style="width: 45%; margin-right: 8px" />
            <a-input v-model="tag.value" placeholder="参数值" style="width: 40%; margin-right: 8px" />
            <a-icon class="dynamic-delete-button" type="minus-circle-o" @click="removeFilterParams(item, index)" />
肖超群 authored
89
90
          </div>
          <a-button type="dashed" style="margin-left:28%;width: 30%" size="small" @click="addFilterParams(item)">
91
            <a-icon type="plus" /> 添加参数 </a-button>
肖超群 authored
92
93
94
95
        </div>
        <p class="btn" style="padding-top: 10px">
          <a-dropdown>
            <a-menu slot="overlay" @click="filterHandleMenuClick">
肖超群 authored
96
              <a-menu-item :key="item.key" :name="item.name" v-for="item in filterArray">{{ item.name }}</a-menu-item>
肖超群 authored
97
            </a-menu>
98
            <a-button type="dashed" style="margin-left: 8px;width:100%"> 添加过滤器 <a-icon type="down" /> </a-button>
肖超群 authored
99
100
101
          </a-dropdown>
        </p>
      </a-form-model-item>
102
103
104
      <a-row :style="{ textAlign: 'right' }" class="drawer-bootom-button">
        <a-button :style="{ marginRight: '8px' }" @click="handleCancel"> 关闭 </a-button>
        <a-button @click="handleSubmit" type="primary"> 确定 </a-button>
肖超群 authored
105
106
107
108
109
      </a-row>
    </a-form-model>
  </a-drawer>
</template>
<script>
110
import { postAction } from '@/api/manage'
肖超群 authored
111
肖超群 authored
112
113
114
115
116
117
export default {
  name: 'GateWayRouteModal',
  components: {},
  data() {
    return {
      layout: 'horizontal',
118
119
      labelCol: { span: 3 },
      wrapperCol: { span: 14 },
肖超群 authored
120
121
122
      currentNameIndex: 0,
      currentTagIndex: -1,
      predicates: {},
123
124
125
126
      filterArray: [
        { key: 0, name: '熔断器' },
        { key: 1, name: '限流过滤器' }
      ],
肖超群 authored
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      tagArray: ['Path', 'Host', 'Cookie', 'Header', 'Method', 'Query', 'After', 'Before', 'Between', 'RemoteAddr'],
      inputVisible: false,
      inputValue: '',
      url: {
        update: '/sys/gatewayRoute/updateAll',
        clear: '/sys/gatewayRoute/clearRedis'
      },
      router: this.getRouter(),
      title: '路由编辑',
      visible: false,
      loading: false
    }
  },
  methods: {
    getRouter() {
肖超群 authored
142
      return {
肖超群 authored
143
144
145
146
147
        routerId: '',
        name: '',
        uri: '',
        predicates: [],
        filters: []
肖超群 authored
148
149
      }
    },
肖超群 authored
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    show(router) {
      if (router) {
        router.status = Boolean(router.status)
        this.router = router
      } else {
        this.router = this.getRouter()
        this.inputValue = ''
      }
      this.visible = true
      this.currentTagIndex = -1
      this.currentNameIndex = -1
    },
    close() {
      this.reset()
      this.$emit('close')
      this.$refs['form'].resetFields()
      this.visible = false
    },
    //删除路由条件配置项
    removeTag(item, removedTag) {
      const tags = item.args.filter(tag => tag !== removedTag)
      item.args = tags
    },
    //添加路由选项
    predicatesHandleMenuClick(e) {
      this.router.predicates.push({
        args: [],
        name: e.key
      })
    },
    editTag(tag, index) {
      this.currentTagIndex = index
    },
    //显示输入框
    showInput(item, index) {
      this.inputVisible = true
      this.currentNameIndex = index
    },
    //路由选项输入框失去焦点事件
    handleInputChange(e) {
      this.inputValue = e.target.value
    },
    //删除路由条件
    removePredicate(item, index) {
      item.predicates.splice(index, 1)
    },
    //删除过滤器参数
    removeFilterParams(item, index) {
      item.args.splice(index, 1)
    },
    //删除过滤器
    removeFilter(item, index) {
      item.filters.splice(index, 1)
    },
    //添加过滤器参数
    addFilterParams(item) {
      item.args.push({
        key: 'key' + item.args.length + 1,
        value: ''
      })
    },
    //过滤器添加事件
    filterHandleMenuClick(e) {
      if (e.key == 0) {
        this.router.filters.push({
215
216
217
218
219
220
221
222
223
224
          args: [
            {
              key: 'name',
              value: 'default'
            },
            {
              key: 'fallbackUri',
              value: 'forward:/fallback'
            }
          ],
肖超群 authored
225
226
          name: 'Hystrix',
          title: this.filterArray[0].name
肖超群 authored
227
        })
肖超群 authored
228
229
230
      }
      if (e.key == 1) {
        this.router.filters.push({
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
          args: [
            {
              key: 'key-resolver',
              value: '#{@ipKeyResolver}'
            },
            {
              key: 'redis-rate-limiter.replenishRate',
              value: 20
            },
            {
              key: 'redis-rate-limiter.burstCapacity',
              value: 20
            }
          ],
          name: 'RequestRateLimiter',
肖超群 authored
246
          title: this.filterArray[1].name
肖超群 authored
247
        })
肖超群 authored
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
      }
    },
    //输入框确认
    handleInputConfirm(item) {
      const inputValue = this.inputValue
      let tags = item.args
      if (inputValue && tags.indexOf(inputValue) === -1) {
        item.args = [...tags, inputValue]
      }
      console.log(tags)
      Object.assign(this, {
        tags,
        inputVisible: false,
        inputValue: ''
      })
      this.currentTagIndex = -1
    },
    //输入框确认
    handleInputEditConfirm(item, tag, index) {
      if (this.inputValue) {
肖超群 authored
268
        const inputValue = this.inputValue
肖超群 authored
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        item.args[index] = inputValue
      }
      this.currentTagIndex = -1
    },
    reset() {
      this.expandedKeysss = []
      this.checkedKeys = []
      this.defaultCheckedKeys = []
      this.loading = false
    },
    //关闭弹窗
    handleCancel() {
      this.close()
    },
    //提交路由
    handleSubmit() {
285
      let { predicates, filters, ...other } = this.router
肖超群 authored
286
287
288
289
290
291
292
293
294
295
296
297
298
299
      let router = other
      router.predicates = JSON.stringify(this.router.predicates)
      router.filters = JSON.stringify(this.router.filters)
      postAction(this.url.update, {
        router
      }).then(res => {
        if (res.success) {
          this.close()
          this.$emit('ok')
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
肖超群 authored
300
301
    }
  }
肖超群 authored
302
}
肖超群 authored
303
304
</script>
<style lang="less" scoped>
肖超群 authored
305
306
307
308
309
310
311
312
313
314
315
.drawer-bootom-button {
  position: absolute;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
肖超群 authored
316
</style>