RuleForm.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <div>
  3. <template v-for="(group, idx) in form[ruleFormKey]">
  4. <div :key="idx">
  5. <div>
  6. <div v-for="(rule, idx2) in group" :key="`${idx}-${idx2}`">
  7. <div class="d-flex">
  8. <div style="flex: 0 0 20%;">
  9. <!-- 字段 -->
  10. <a-form-model-item class="mb-0">
  11. <a-select v-model="rule.type" :placeholder="$t('network.field')" @change="handleTypeChange(idx, idx2)">
  12. <a-select-option v-for="item in ruleTypes" :key="item.key" :value="item.key">
  13. {{ item.label }}
  14. </a-select-option>
  15. </a-select>
  16. </a-form-model-item>
  17. </div>
  18. <div v-if="getRuleConfig(rule).isShowName" style="width:40%" class="ml-2">
  19. <!-- 名称 -->
  20. <a-form-model-item class="mb-0">
  21. <a-input v-model="rule.name" :placeholder="$t('network.text_21')" />
  22. </a-form-model-item>
  23. </div>
  24. <div style="width:25%" class="ml-2">
  25. <!-- 运算符 -->
  26. <a-form-model-item class="mb-0">
  27. <a-select v-model="rule.opt" :placeholder="$t('compute.text_745')" :disabled="getRuleConfig(rule).optDisabled" @change="handleOptChange(idx, idx2)">
  28. <a-select-option v-for="item in getRuleOpts(idx, idx2)" :key="item.key" :value="item.key">
  29. {{ item.label }}
  30. </a-select-option>
  31. </a-select>
  32. </a-form-model-item>
  33. </div>
  34. <div style="width:50%;position:relative" class="ml-2">
  35. <!-- 值 -->
  36. <a-form-model-item class="mb-0">
  37. <a-input v-if="getRuleConfig(rule).valueType === 'input'" v-model="rule.value" :placeholder="$t('network.value')" />
  38. <a-input-number v-if="getRuleConfig(rule).valueType === 'input-number'" class="w-100" v-model="rule.value" :placeholder="$t('network.value')" v-bind="getRuleConfig(rule).props" />
  39. <a-select v-if="getRuleConfig(rule).valueType === 'multi-select'" v-model="rule.value" :placeholder="$t('network.value')" mode="multiple" showSearch>
  40. <a-select-option v-for="item in getRuleConfig(rule).valueOpts" :key="item.key" :value="item.key">
  41. {{ item.label }}
  42. </a-select-option>
  43. </a-select>
  44. <a-select v-if="getRuleConfig(rule).valueType === 'single-select'" v-model="rule.value" :placeholder="$t('network.value')" showSearch>
  45. <a-select-option v-for="item in getRuleConfig(rule).valueOpts" :key="item.key" :value="item.key">
  46. {{ item.label }}
  47. </a-select-option>
  48. </a-select>
  49. <a-switch v-if="getRuleConfig(rule).valueType === 'switch'" v-model="rule.value" />
  50. </a-form-model-item>
  51. <div class="extra-text" v-if="getRuleConfig(rule).valueExtra">
  52. {{ getRuleConfig(rule).valueExtra }}
  53. </div>
  54. </div>
  55. <div><a-button @click="addRule('and', idx, idx2)" class="ml-2">And</a-button></div>
  56. <div v-if="idx === form[ruleFormKey].length - 1 && idx2 === group.length - 1"><a-button @click="addRule('or', idx, idx2)" class="ml-2">Or</a-button></div>
  57. <div v-if="allRuleLen > 1"><a-icon type="close" class="ml-2 mr-1 close-icon" @click="delRule(idx, idx2)" /></div>
  58. </div>
  59. <div class="split-and-container" v-if="idx2 !== group.length - 1">
  60. <div class="split-and">And</div>
  61. <div class="left-border" />
  62. </div>
  63. </div>
  64. </div>
  65. <div class="split-or" v-if="idx !== form[ruleFormKey].length - 1">Or</div>
  66. </div>
  67. </template>
  68. <div class="expression-container mt-3">
  69. <div class="title d-flex justify-content-between">
  70. <div class="title-text">
  71. {{ $t('network.waf.rule_expression') }}
  72. </div>
  73. <div class="title-text">
  74. <a-button type="link" @click="handleEditExpression">
  75. {{ $t('network.waf.rule_expression_edit') }}
  76. </a-button>
  77. </div>
  78. </div>
  79. <div class="content">
  80. {{ ruleExpression }}
  81. </div>
  82. </div>
  83. <div class="error" v-if="!validate">{{ $t('network.waf.rule_form.error') }}</div>
  84. </div>
  85. </template>
  86. <script>
  87. import WindowsMixin from '@/mixins/windows'
  88. import { WAF_RULE_TYPES } from '../constants'
  89. import { formatRuleValue, getRuleConfig, encodeRuleListToDescription } from '../utils'
  90. export default {
  91. name: 'RateLimitRuleForm',
  92. mixins: [WindowsMixin],
  93. props: {
  94. form: {
  95. type: Object,
  96. required: true,
  97. },
  98. ruleFormKey: {
  99. type: String,
  100. required: true,
  101. default: 'rules',
  102. },
  103. ruleType: {
  104. type: String,
  105. default: 'custom',
  106. },
  107. },
  108. data () {
  109. return {
  110. validate: true,
  111. getRuleConfig,
  112. }
  113. },
  114. computed: {
  115. allRuleLen () {
  116. return this.form[this.ruleFormKey].reduce((acc, cur) => acc + cur.length, 0)
  117. },
  118. ruleTypes () {
  119. return WAF_RULE_TYPES.map(item => ({ key: item.type, label: this.$t(`network.waf.rule_type.${item.type}`), ...item })).filter(item => {
  120. return !item.belongs || item.belongs.includes(this.ruleType)
  121. })
  122. },
  123. ruleExpression () {
  124. return encodeRuleListToDescription(this.form[this.ruleFormKey])
  125. },
  126. },
  127. watch: {
  128. },
  129. methods: {
  130. getRuleOpts (idx, idx2) {
  131. const rule = this.form[this.ruleFormKey][idx][idx2]
  132. if (rule.type) {
  133. const target = this.ruleTypes.find(item => item.key === rule.type)
  134. if (target && target.opts) {
  135. return target.opts.map(key => ({ key, label: this.$t(`network.waf.rule_opt.${key}`) }))
  136. }
  137. }
  138. return []
  139. },
  140. addRule (type, idx, idx2) {
  141. if (type === 'or') {
  142. this.form[this.ruleFormKey].push([{ type: undefined, name: undefined, opt: undefined, value: undefined }])
  143. } else {
  144. const list = []
  145. this.form[this.ruleFormKey][idx].forEach((item, i) => {
  146. list.push(item)
  147. if (i === idx2) {
  148. list.push({ type: undefined, name: undefined, opt: undefined, value: undefined })
  149. }
  150. })
  151. this.$set(this.form[this.ruleFormKey], idx, list)
  152. }
  153. },
  154. delRule (idx, idx2) {
  155. if (this.form[this.ruleFormKey][idx].length === 1) {
  156. this.form[this.ruleFormKey].splice(idx, 1)
  157. } else {
  158. this.form[this.ruleFormKey][idx].splice(idx2, 1)
  159. }
  160. },
  161. handleTypeChange (idx, idx2) {
  162. this.$nextTick(() => {
  163. const opt = this.getRuleConfig(this.form[this.ruleFormKey][idx][idx2]).opts[0]
  164. this.form[this.ruleFormKey][idx][idx2].opt = opt
  165. this.form[this.ruleFormKey][idx][idx2].value = undefined
  166. this.form[this.ruleFormKey][idx][idx2].name = undefined
  167. })
  168. },
  169. handleOptChange (idx, idx2) {
  170. this.$nextTick(() => {
  171. const valueType = this.getRuleConfig(this.form[this.ruleFormKey][idx][idx2]).valueType
  172. const originValue = this.form[this.ruleFormKey][idx][idx2].value
  173. if (this.form[this.ruleFormKey][idx][idx2].type === 'ip.src') {
  174. this.form[this.ruleFormKey][idx][idx2].value = undefined
  175. } else {
  176. this.form[this.ruleFormKey][idx][idx2].value = formatRuleValue(originValue, valueType)
  177. }
  178. })
  179. },
  180. handleEditExpression () {
  181. this.createDialog('WafRuleEditDialog', {
  182. expression: this.ruleExpression,
  183. success: (rules) => {
  184. this.form[this.ruleFormKey] = rules
  185. },
  186. })
  187. },
  188. validateRule () {
  189. const rules = this.form[this.ruleFormKey]
  190. let validate = true
  191. rules.forEach(group => {
  192. group.forEach(rule => {
  193. if (!rule.type || !rule.opt) {
  194. validate = false
  195. return
  196. }
  197. const config = getRuleConfig(rule)
  198. if (config.isShowName && !rule.name) {
  199. validate = false
  200. return
  201. }
  202. if (config.valueType === 'input' || config.valueType === 'input-number' || config.valueType === 'single-select') {
  203. if (!rule.value) {
  204. validate = false
  205. return
  206. }
  207. }
  208. if (config.valueType === 'multi-select') {
  209. if (!Array.isArray(rule.value) || rule.value.length === 0) {
  210. validate = false
  211. }
  212. }
  213. })
  214. })
  215. this.validate = validate
  216. return validate
  217. },
  218. },
  219. }
  220. </script>
  221. <style lang="less" scoped>
  222. .rule-group-container {
  223. background-color: #cacaca50;
  224. padding: 10px;
  225. }
  226. .close-icon {
  227. font-size: 18px;
  228. transform: translateY(3px);
  229. cursor: pointer;
  230. &:hover {
  231. color: var(--antd-wave-shadow-color);
  232. }
  233. }
  234. .split-or {
  235. display: inline-block;
  236. height: 30px;
  237. line-height: 30px;
  238. padding: 0 15px;
  239. // padding: 3px 10px;
  240. background-color: #cacaca50;
  241. border: solid 1px #cacaca80;
  242. margin: 15px 0;
  243. }
  244. .split-and-container {
  245. height: 30px;
  246. position: relative;
  247. .split-and {
  248. display: inline-block;
  249. height: 25px;
  250. line-height: 25px;
  251. padding: 0 10px;
  252. // background-color: #cacaca50;
  253. border: solid 1px #cacaca80;
  254. border-left: none;
  255. position: absolute;
  256. left: 0;
  257. top: 2.5px;
  258. color: #aaa;
  259. }
  260. .left-border {
  261. position: absolute;
  262. width: 1px;
  263. height: 40px;
  264. left: 0;
  265. top: -5px;
  266. font-size: 18px;
  267. border-left: solid 1px #cacaca90;
  268. }
  269. }
  270. .content {
  271. padding: 10px;
  272. background-color: #f5f5f5;
  273. border-radius: 5px;
  274. letter-spacing: 1px;
  275. }
  276. .extra-text {
  277. position: absolute;
  278. left: 0;
  279. top: 30px;
  280. font-size: 14px;
  281. color: rgba(0, 0, 0, 0.45);
  282. }
  283. .error {
  284. font-size: 14px;
  285. color: red;
  286. }
  287. </style>