addRule.vue 11.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
<template>
  <div class="add-rule">
    <div class="title">规则编辑器</div>
    <dee-form
      ref="ruleForm"
      class="condition"
      label-width="130px"
      :form="searchForm"
      :form-data="searchFormConfig"
      :rules="rules"
    />
    <div class="rule-detail">
      <div class="rule-detail-left">
        <div class="rule-detail-left-top">
          <div class="rule-detail-left-top-title">
            <div style="padding-bottom: 8px;display: flex;justify-content: center;align-items:center;font-weight: bold">
              自定义参数<img
                src="/icons/add.png"
                style="width: 18px;height: 18px;padding-left: 8px;cursor: pointer"
                @click="addParam"
              ></div>
            <div>支持数据映射:常量、变量、枚举、用户属性</div>
          </div>
          <div class="rule-detail-left-top-content">
            <div v-for="item in params" :key="item.key" style="display: flex;padding-bottom: 5px;align-items: center">
              <el-input v-model="item.name">{{ item.name }}</el-input>
              <el-select v-model="item.type" style="margin-left: 5px">
                <el-option
                  v-for="p in paramOptions"
                  :key="p.value"
                  :label="p.label"
                  :value="p.value"
                />
              </el-select>
              <img
                src="/icons/del.png"
                style="margin-left: 10px;width: 18px;height: 18px;cursor: pointer"
                @click="deleteParam(item)"
              >
            </div>
          </div>
        </div>
        <div class="rule-detail-left-bottom">
          <div class="rule-detail-left-bottom-title">内置参数</div>
          <div class="rule-detail-left-bottom-content">
            <el-tag v-for="item in args" :key="item.id" type="warning" style="margin-right: 5px">{{
              item.displayName
            }}
            </el-tag>
          </div>
        </div>
      </div>
      <div class="rule-detail-right">
        <dee-form
          ref="codeForm"
          :form="ruleForm"
          :form-data="ruleFormConfig"
          :rules="rule"
        />
      </div>
    </div>
    <div style="padding: 0 8px">
      <el-input
        v-model="compileResult"
        :class="preCompileState"
        type="textarea"
        :disabled="true"
        :rows="4"
      />
    </div>
    <div class="buttons">
      <el-button type="primary" class="button" size="small" @click="preCompile">预编译</el-button>
      <el-button type="primary" class="button" size="small" @click="closeDialog">关闭</el-button>
      <el-button type="primary" class="button" size="small" @click="saveRule">保存</el-button>
    </div>
  </div>
</template>

<script>
import { findModelDefByServer } from '@/api/modelServer'
import { getModelAttrRule } from '@/api/model'
import { generateCodeExecuteTemplate, preCompile, getAbility } from '@/api/DxDynamicCode'

export default {
  name: 'AddRule',
  components: {},
  props: {
    serverName: {
      type: Array,
      default: () => ([])
    },
    scopeName: {
      type: String,
      default: () => ''
    },
    scopeId: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      rule: {
        code: [
          { required: true, message: '请编写规则', trigger: 'change' }
        ]
      },
      rules: {
        serverName: [
          { required: true, message: '请选择业务域', trigger: 'change' }
        ],
        modelName: [
          { required: true, message: '请选择上下文对象类型', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请选择名称', trigger: 'change' }
        ],
        scopeId: [
          { required: true, message: '请选择作用域', trigger: 'change' }
        ]
      },
      paramOptions: [],
      params: [],
      searchForm: {},
      searchFormConfig: [
        {
          title: '',
          split: 4,
          data: [
            {
              key: 'name',
              title: '名称',
              component: {
                name: 'el-input'
              }
            },
            {
              key: 'serverName',
              title: '业务域',
              component: {
                name: 'el-select',
                options: this.serverName
              },
              handler: {
                change: (val) => {
                  if (val) {
                    this.serverNameChange(val)
                  }
                }
              }
            },
            {
              key: 'modelName',
              title: '上下文对象类型',
              component: {
                name: 'el-select',
                options: []
              }
            },
            {
              key: 'scopeId',
              title: '作用域',
              component: {
                name: 'el-select',
                options: this.scopeId
              },
              handler: {
                change: (val) => {
                  this.changeScope(val)
                }
              }
            }
          ]
        }
      ],
      ruleForm: {
        code: ''
      },
      ruleFormConfig: [
        {
          title: '',
          split: 1,
          data: [
            {
              key: 'code',
              title: '',
              component: {
                name: 'dee-code-edit',
                mode: 'text/x-java',
                hints: []
              }
            }
          ]
        }
      ],
      compileResult: '',
      args: [],
      DynamicCodeScopeService: this.$getService('DxDynamicCodeScope'),
      preCompileState: '',
      DxDynamicCodeService: this.$getService('DxDynamicCode')
    }
  },
  computed: {},
  watch: {
    params: {
      deep: true,
      handler: function(val) {
        this.$utils.findFormProp(this.ruleFormConfig, 'code').component.hints = val.map(x => x.name)
      }
    }
  },
  mounted() {
    this.getAttrRule()
    if (this.scopeName) {
      const v = this.scopeId.filter(x => x.name === this.scopeName)
      if (v && v.length > 0) {
        this.searchForm.scopeId = v[0].value
      }
    }
  },
  methods: {
    getAttrRule() {
      getModelAttrRule().then(res => {
        this.paramOptions = res.items.map(x => {
          return {
            label: x.typeName,
            value: x.typeName
          }
        })
      })
    },
    getTemplate() {
      if (this.searchForm.serverName && this.searchForm.scopeId) {
        this.$set(this.ruleForm, 'code', '')
        const scopeName = this.scopeId.filter(x => x.value === this.searchForm.scopeId)[0].name
        generateCodeExecuteTemplate({
          owner: this.searchForm.serverName,
          scopeName: scopeName
        }).then(res => {
          this.ruleForm.code = res.items
        })
      }
    },
    changeScope(val) {
      this.args = []
      getAbility({ scopeId: val }).then(res => {
        if (res.items) {
          this.args = res.items
        }
      })
      this.getTemplate()
    },
    deleteParam(item) {
      this.params = this.params.filter(x => x.key !== item.key)
    },
    serverNameChange(val) {
      this.$set(this.searchForm, 'modelName', '')
      findModelDefByServer({
        serverName: val,
        modelType: 'BASE'
      }).then(res => {
        const data = res.items.map(x => {
          return {
            label: x.displayName,
            value: x.name
          }
        })
        this.$utils.findFormProp(this.searchFormConfig, 'modelName').component.options = [{
          label: '全部',
          value: -1
        }, ...data]
      })
      this.getTemplate()
    },
    addParam() {
      this.params.push({ key: Math.random().toString(36).substr(3, 10), name: '', type: '' })
    },
    preCompile() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.codeForm.validate(valid => {
            if (valid) {
              const data = {
                serverName: this.searchForm.serverName,
                name: this.searchForm.name,
                dynamicCodeSource: [{
                  displayCode: this.ruleForm.code
                }]
              }
              preCompile(data).then(res => {
                if (res.code === 0) {
                  this.compileResult = res.items
                  this.preCompileState = 'pre-compile-success'
                }
              }).catch(e => {
                if (e.code === '4') {
                  this.compileResult = e.message
                  this.preCompileState = 'pre-compile-error'
                }
              })
            }
          })
        }
      })
    },
    closeDialog() {
      this.$emit('close')
    },
    saveRule() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.codeForm.validate(valid => {
            if (valid) {
              this.$utils.showConfirm('确认保存?', '提示', 'info', '确定', '取消', () => {
                const data = {
                  name: this.searchForm.name,
                  modelName: this.searchForm.modelName === -1 ? null : this.searchForm.modelName,
                  serverName: this.searchForm.serverName,
                  dynamicCodeScope: {
                    id: this.searchForm.scopeId
                  },
                  dynamicCodeSource: [{
                    displayCode: this.ruleForm.code
                  }],
                  dynamicCodeCustomVariables: this.params
                }
                this.DxDynamicCodeService.save(data).then(res => {
                  this.$utils.showMessageSuccess('添加成功!')
                  this.$emit('addSuccess')
                })
              }, () => {
              })
            }
          })
        }
      })
    }
  }
}

</script>
<style lang='scss'>
.add-rule {
  border: 1px solid #DEE1E7;

  .pre-compile-error {
    .el-textarea__inner {
      color: red;
    }
  }

  .pre-compile-success {
    .el-textarea__inner {
      color: green;
    }
  }

  .title {
    display: flex;
    justify-content: flex-start;
    padding: 15px 0 15px 25px;
    margin-bottom: 5px;
    background-color: #fff;
    border-bottom: 1px solid #DEE1E7;
    font-weight: bold
  }

  .condition {
    padding-top: 10px;
    border-bottom: 1px solid transparent;
  }

  .rule-detail {
    display: flex;
    border: 1px solid #DEE1E7;
    margin: 0 8px 8px 8px;
    background-color: #f7f7f7;

    .rule-detail-left {
      flex: 1;
      display: flex;
      flex-direction: column;
      border-right: 1px solid #DEE1E7;

      .rule-detail-left-top {
        flex: 3;
        border-bottom: 1px solid #DEE1E7;

        .rule-detail-left-top-title {
          display: flex;
          align-items: center;
          padding: 8px;
          flex-direction: column;
        }

        .rule-detail-left-top-content {
          padding: 8px 40px 8px 50px;
          overflow-y: scroll;
          max-height: 150px;
        }
      }

      .rule-detail-left-bottom {
        flex: 1;

        .rule-detail-left-bottom-title {
          display: flex;
          justify-content: center;
          padding: 8px;
          font-weight: bold;
        }

        .rule-detail-left-bottom-content {
          padding: 8px 40px;
          overflow-y: scroll;
          max-height: 40px;
        }
      }
    }

    .rule-detail-right {
      flex: 2;

      .el-form-item {
        margin-bottom: 0 !important;
      }
    }
  }

  .buttons {
    display: flex;
    justify-content: center;
    margin-top: 30px;
    margin-bottom: 80px;

    .button {
      width: 100px;
      height: 36px;
    }
  }
}
</style>