Cmdb.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <a-form
  3. :form="form.fc"
  4. v-bind="formItemLayout">
  5. <a-form-item label="URL">
  6. <template v-slot:extra>
  7. <div>{{$t('system.text_579')}}</div>
  8. </template>
  9. <a-input v-decorator="decorators.webhook" />
  10. </a-form-item>
  11. <a-form-item :wrapper-col="offsetWrapperCol">
  12. <a-button type="primary" @click="handleSubmit" :loading="submiting">{{ $t('common.ok') }}</a-button>
  13. <test-button v-if="false" class="ml-3" :post="testPost" />
  14. </a-form-item>
  15. </a-form>
  16. </template>
  17. <script>
  18. import TestButton from '@/sections/TestButton'
  19. export default {
  20. name: 'CmdbRobotConfig',
  21. components: {
  22. TestButton,
  23. },
  24. props: {
  25. formItemLayout: {
  26. required: true,
  27. type: Object,
  28. },
  29. offsetWrapperCol: {
  30. required: true,
  31. type: Object,
  32. },
  33. loading: Boolean,
  34. },
  35. data () {
  36. return {
  37. submiting: false,
  38. form: {
  39. fc: this.$form.createForm(this),
  40. },
  41. decorators: {
  42. webhook: [
  43. 'webhook',
  44. {
  45. rules: [
  46. { required: true, message: this.$t('system.text_267') },
  47. ],
  48. },
  49. ],
  50. },
  51. contactData: null,
  52. }
  53. },
  54. destroyed () {
  55. this.manager = null
  56. },
  57. created () {
  58. this.manager = new this.$Manager('notifyconfigs', 'v1')
  59. this.fetchData()
  60. },
  61. methods: {
  62. async fetchData () {
  63. this.$emit('update:loading', true)
  64. try {
  65. const response = await this.manager.list({
  66. params: {
  67. type: 'webhook',
  68. },
  69. })
  70. const data = response.data.data[0] || {}
  71. this.contactData = data
  72. this.$nextTick(() => {
  73. this.form.fc.setFieldsValue(data.content)
  74. })
  75. } catch (error) {
  76. throw error
  77. } finally {
  78. this.$emit('update:loading', false)
  79. }
  80. },
  81. async handleSubmit () {
  82. const values = await this.form.fc.validateFields()
  83. try {
  84. this.submiting = true
  85. if (this.contactData && this.contactData.id) {
  86. await this.manager.update({
  87. id: this.contactData.id,
  88. data: {
  89. content: values,
  90. type: 'webhook',
  91. },
  92. })
  93. } else {
  94. await this.manager.create({
  95. data: {
  96. content: values,
  97. type: 'webhook',
  98. },
  99. })
  100. }
  101. await this.fetchData()
  102. this.$message.success(this.$t('system.text_124', [this.$t('system.text_578')]))
  103. } catch (error) {
  104. this.$message.error(this.$t('common_622', [this.$t('system.text_578')]))
  105. throw error
  106. } finally {
  107. this.submiting = false
  108. }
  109. },
  110. async testPost () {
  111. try {
  112. const values = await this.form.fc.validateFields()
  113. await new this.$Manager('notifyconfigs', 'v1').performClassAction({
  114. action: 'validate',
  115. data: {
  116. content: values,
  117. type: 'webhook',
  118. },
  119. })
  120. } catch (error) {
  121. this.$message.error(this.$t('common_623', [this.$t('common_269')]))
  122. throw error
  123. }
  124. },
  125. },
  126. }
  127. </script>