RouteModal.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="30%" @ok="handleSubmit" destroyOnClose showFooter>
  3. <a-form ref="formRef" :label-col="labelCol" :wrapper-col="wrapperCol" :model="router" :rules="validatorRules">
  4. <a-form-item label="路由ID" name="routerId">
  5. <a-input v-model:value="router.routerId" placeholder="路由唯一ID"/>
  6. </a-form-item>
  7. <a-form-item label="路由名称" name="name">
  8. <a-input v-model:value="router.name" placeholder="路由名称"/>
  9. </a-form-item>
  10. <a-form-item label="路由URI" name="uri">
  11. <a-input v-model:value="router.uri" placeholder="路由URL"/>
  12. </a-form-item>
  13. <a-form-item label="路由状态" name="status">
  14. <a-switch default-checked :checked-value="1" :un-checked-value="0" v-model:checked="router.status"/>
  15. </a-form-item>
  16. <a-form-item name="predicates" label="路由条件">
  17. <div v-for="(item,index) in router.predicates" >
  18. <!--当name在noKeyRouter时不需要指定key-->
  19. <template v-if="noKeyRouter.includes(item.name)">
  20. <a-divider>{{item.name}}
  21. <DeleteOutlined size="22" @click="removePredicate(router,index)"/>
  22. </a-divider>
  23. <div>
  24. <template v-for="(tag, tagIndx) in item.args">
  25. <a-input ref="inputRef2" v-if="tagIndx==currentTagIndex&&index==currentNameIndex" type="text" size="small" :style="{ width: '190px' }" v-model:value="state.inputValue" @change="handleInputChange" @blur="handleInputEditConfirm(item,tag,tagIndx)" @keyup.enter="handleInputEditConfirm(item,tag,tagIndx)"/>
  26. <a-tag v-else :key="tag" style="margin-bottom:2px" :closable="true" @close="() => removeTag(item,tag)" @click="editTag(item,tag,tagIndx,index)">
  27. {{ tag }}
  28. </a-tag>
  29. </template>
  30. <a-input ref="inputRef" v-if="state.inputVisible&&index==currentNameIndex" type="text" size="small" :style="{ width: '100px' }" v-model:value="state.inputValue" @change="handleInputChange" @blur="handleInputConfirm(item)" @keyup.enter="handleInputConfirm(item)"/>
  31. <a-tag v-else style="background: #fff; borderStyle: dashed;margin-bottom:2px" @click="showInput(item,index)">
  32. <PlusOutlined size="22" />
  33. 新建{{item.name}}
  34. </a-tag>
  35. </div>
  36. </template>
  37. <!--当name不在noKeyRouter时需要指定key-->
  38. <template v-if="!noKeyRouter.includes(item.name)">
  39. <a-divider>{{item.name}}
  40. <DeleteOutlined size="22" @click="removePredicate(router,index)"/>
  41. </a-divider>
  42. <div>
  43. <template v-for="(value, key) in item.args">
  44. <a-row>
  45. <a-col :span="5" style="margin-top: 8px">
  46. <span v-if="key=='header'" >Header名称</span>
  47. <span v-if="key=='regexp'">参数值</span>
  48. <span v-if="key=='param'">参数名</span>
  49. <span v-if="key=='name'">参数名</span>
  50. </a-col>
  51. <a-col :span="18">
  52. <a-input :defaultValue="value" placeholder="参数值" style="width: 70%; margin-right: 8px;margin-top: 3px" @change="(e)=>valueChange(e,item.args,key)"/>
  53. </a-col>
  54. </a-row>
  55. </template>
  56. </div>
  57. </template>
  58. </div>
  59. <p class="btn" style="padding-top: 10px">
  60. <a-dropdown>
  61. <template #overlay>
  62. <a-menu >
  63. <a-menu-item :key="item.name" v-for="item in tagArray" @click="predicatesHandleMenuClick(item)">{{item.name}}</a-menu-item>
  64. </a-menu>
  65. </template>
  66. <a-button type="dashed" style="margin-left: 8px;width:100%"> 添加路由条件
  67. <DownOutlined :size="22"/>
  68. </a-button>
  69. </a-dropdown>
  70. </p>
  71. </a-form-item>
  72. <a-form-item name="predicates" label="过滤器">
  73. <div v-for="(item,index) in router.filters">
  74. <a-divider>{{item.name}}
  75. <DeleteOutlined size="22" @click="removeFilter(router,index)"/>
  76. </a-divider>
  77. <div v-for="(tag, index) in item.args" :key="tag.key">
  78. <a-input v-model:value="tag.key" placeholder="参数键" style="width: 45%; margin-right: 8px"/>
  79. <a-input v-model:value="tag.value" placeholder="参数值" style="width: 40%; margin-right: 8px;margin-top: 3px"/>
  80. <CloseOutlined :size="22" @click="removeFilterParams(item,index)"/>
  81. </div>
  82. <a-button type="dashed" style="margin-left:28%;width: 37%;margin-top:5px" size="small" @click="addFilterParams(item)">
  83. <DownOutlined :size="22"/>
  84. 添加参数
  85. </a-button>
  86. </div>
  87. <p class="btn" style="padding-top: 10px">
  88. <a-dropdown>
  89. <template #overlay>
  90. <a-menu @click="filterHandleMenuClick">
  91. <a-menu-item :key="item.key" :name="item.name" v-for="item in filterArray">{{item.name}}</a-menu-item>
  92. </a-menu>
  93. </template>
  94. <a-button type="dashed" style="margin-left: 8px;width:100%"> 添加过滤器
  95. <DownOutlined />
  96. </a-button>
  97. </a-dropdown>
  98. </p>
  99. </a-form-item>
  100. </a-form>
  101. </BasicDrawer>
  102. </template>
  103. <script lang="ts" setup>
  104. import { ref, computed, unref, useAttrs, reactive,nextTick } from 'vue';
  105. import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  106. import { saveOrUpdateRoute } from './route.api';
  107. import {DeleteOutlined} from '@ant-design/icons-vue';
  108. import {PlusOutlined} from '@ant-design/icons-vue';
  109. import {CloseOutlined} from '@ant-design/icons-vue';
  110. import {DownOutlined} from '@ant-design/icons-vue';
  111. // 声明Emits
  112. const emit = defineEmits(['register', 'success']);
  113. const labelCol = reactive({
  114. xs: { span: 24 },
  115. sm: { span: 5 },
  116. });
  117. const wrapperCol = reactive({
  118. xs: { span: 24 },
  119. sm: { span: 16 },
  120. });
  121. const attrs = useAttrs();
  122. const isUpdate = ref(true);
  123. const inputRef = ref();
  124. const inputRef2 = ref();
  125. let state = reactive({
  126. inputVisible: false,
  127. inputValue: '',
  128. });
  129. const currentNameIndex = ref(0);
  130. const currentTagIndex = ref(-1);
  131. const validatorRules = {
  132. routerId: [
  133. { required: true, message: 'routerId不能为空', trigger: 'blur' },
  134. ],
  135. name: [
  136. { required: true, message: '路由名称不能为空', trigger: 'blur' },
  137. ],
  138. uri: [
  139. { required: true, message: 'uri不能为空', trigger: 'blur' },
  140. ],
  141. };
  142. const noKeyRouter=["Path","Host","Method","After","Before","Between","RemoteAddr"];
  143. const filterArray = [/*{ key: 0, name: '熔断器' },*/ { key: 1, name: '限流过滤器' }];
  144. const tagArray = ref([
  145. {
  146. name:'Header',
  147. args:{
  148. header:'',
  149. regexp:''
  150. }
  151. },
  152. {
  153. name:'Query',
  154. args:{
  155. param:'',
  156. regexp:''
  157. }
  158. },
  159. {
  160. name:'Method',
  161. args:[]
  162. },
  163. {
  164. name:'Host',
  165. args:[]
  166. },
  167. {
  168. name:'Cookie',
  169. args:{
  170. name:'',
  171. regexp:''
  172. }
  173. },
  174. {
  175. name:'After',
  176. args:[]
  177. },
  178. {
  179. name:'Before',
  180. args:[]
  181. },
  182. {
  183. name:'Between',
  184. args:[]
  185. },
  186. {
  187. name:'RemoteAddr',
  188. args:[]
  189. }
  190. ]);
  191. const formRef = ref();
  192. let router = reactive({});
  193. const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  194. isUpdate.value = !!data?.isUpdate;
  195. setDrawerProps({ confirmLoading: false });
  196. initRouter();
  197. if (unref(isUpdate)) {
  198. router = Object.assign(router, data.record);
  199. }
  200. });
  201. /**
  202. * 标题
  203. */
  204. const getTitle = computed(() => (!unref(isUpdate) ? '新增路由' : '编辑路由'));
  205. //删除路由条件配置项
  206. function removeTag(item, removedTag) {
  207. let tags = item.args.filter(tag => tag !== removedTag);
  208. item.args = tags;
  209. }
  210. //初始化参数
  211. function initRouter() {
  212. router = Object.assign(router, {
  213. id: '',
  214. routerId: '',
  215. name: '',
  216. uri: '',
  217. status: 1,
  218. predicates: [],
  219. filters: [],
  220. });
  221. }
  222. //添加路由选项
  223. function predicatesHandleMenuClick(e) {
  224. router.predicates.push({
  225. args: e.args,
  226. name: e.name,
  227. });
  228. }
  229. /**
  230. * 值修改事件
  231. * @param e
  232. * @param item
  233. * @param key
  234. */
  235. function valueChange(e,item,key){
  236. item[key]=e.target.value
  237. }
  238. function editTag(item,tag, tagIndex,index) {
  239. currentNameIndex.value = index;
  240. currentTagIndex.value = tagIndex;
  241. state.inputValue=tag;
  242. nextTick(() => {
  243. inputRef2.value.focus();
  244. });
  245. }
  246. //显示输入框
  247. function showInput(item, index) {
  248. state.inputValue='';
  249. state.inputVisible = true;
  250. currentNameIndex.value = index;
  251. nextTick(() => {
  252. inputRef.value.focus();
  253. });
  254. }
  255. //路由选项输入框改变事件
  256. function handleInputChange(e) {
  257. console.info("change",e)
  258. console.info("change",e.target.value)
  259. //state.value = e.target.value;
  260. //state.tag=true;
  261. }
  262. //删除路由条件
  263. function removePredicate(item, index) {
  264. item.predicates.splice(index, 1);
  265. }
  266. //删除过滤器参数
  267. function removeFilterParams(item, index) {
  268. item.args.splice(index, 1);
  269. }
  270. //删除过滤器
  271. function removeFilter(item, index) {
  272. item.filters.splice(index, 1);
  273. }
  274. //添加过滤器参数
  275. function addFilterParams(item) {
  276. item.args.push({
  277. key: 'key' + item.args.length + 1,
  278. value: '',
  279. });
  280. }
  281. //过滤器添加事件
  282. function filterHandleMenuClick(e) {
  283. if (e.key == 0) {
  284. router.filters.push({
  285. args: [{
  286. key: 'name',
  287. value: 'default',
  288. }, {
  289. key: 'fallbackUri',
  290. value: 'forward:/fallback',
  291. }],
  292. name: 'Hystrix',
  293. title: filterArray[0].name,
  294. });
  295. }
  296. console.info('test', router);
  297. if (e.key == 1) {
  298. router.filters.push({
  299. args: [{
  300. key: 'key-resolver',
  301. value: '#{@ipKeyResolver}',
  302. }, {
  303. key: 'redis-rate-limiter.replenishRate',
  304. value: 20,
  305. }, {
  306. key: 'redis-rate-limiter.burstCapacity',
  307. value: 20,
  308. }],
  309. name: 'RequestRateLimiter',
  310. title: filterArray[0].name,
  311. });
  312. }
  313. }
  314. //输入框确认
  315. function handleInputConfirm(item) {
  316. let tags = item.args;
  317. const inputValue = state.inputValue;
  318. if (inputValue && tags.indexOf(inputValue) === -1) {
  319. item.args = [...tags, state.inputValue];
  320. }
  321. state.inputVisible=false;
  322. state.inputValue='';
  323. currentTagIndex.value = -1;
  324. currentNameIndex.value = -1;
  325. }
  326. //输入框确认
  327. function handleInputEditConfirm(item, tag, index) {
  328. const inputValue = state.inputValue;
  329. if (inputValue) {
  330. item.args[index] = state.inputValue;
  331. }
  332. currentTagIndex.value = -1;
  333. currentNameIndex.value = -1;
  334. }
  335. //关闭弹窗
  336. function handleCancel() {
  337. }
  338. /**
  339. * 提交
  340. */
  341. async function handleSubmit() {
  342. await formRef.value.validate().then(() => {
  343. try {
  344. setDrawerProps({ confirmLoading: true });
  345. //重新构造表单提交对象,切记不可修改router对象,数组修改为字符串容易造成界面混乱
  346. let params=Object.assign({},router,{
  347. predicates:JSON.stringify(router.predicates),
  348. filters:JSON.stringify(router.filters),
  349. })
  350. //提交表单
  351. saveOrUpdateRoute({ router: params }).then(() => {
  352. closeDrawer();
  353. //刷新列表
  354. emit('success');
  355. });
  356. } finally {
  357. setDrawerProps({ confirmLoading: false });
  358. }
  359. });
  360. }
  361. </script>