WorkwxRobot.vue 3.3 KB

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