Create.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ headerTitle }}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc"
  7. v-bind="formItemLayout"
  8. hideRequiredMark>
  9. <a-form-item :label="$t('compute.text_297', [$t('dictionary.project')])" class="mb-0" v-show="!isEdit">
  10. <domain-project :fc="form.fc" :form-layout="formItemLayout" :decorators="{ project: decorators.project, domain: decorators.domain }" />
  11. </a-form-item>
  12. <a-form-item :label="$t('common.name')">
  13. <a-input v-decorator="decorators.name" :placeholder="$t('validator.resourceName')" />
  14. <template v-slot:extra>
  15. <name-repeated
  16. res="robots"
  17. version="v1"
  18. :name="form.fd.name" />
  19. </template>
  20. </a-form-item>
  21. <a-form-item :label="$t('system.text_48')">
  22. <a-radio-group v-decorator="decorators.type" :disabled="isEdit">
  23. <a-radio-button
  24. v-for="item in typesOpts"
  25. :value="item.key"
  26. :key="item.key">{{ item.label }}</a-radio-button>
  27. </a-radio-group>
  28. </a-form-item>
  29. <a-form-item :label="form.fc.getFieldValue('type') === 'webhook' ? 'URL' : 'Webhook'">
  30. <a-input v-decorator="decorators.address" />
  31. <div slot="extra" v-show="form.fc.getFieldValue('type') !== 'webhook'">
  32. {{ $t('iam.get_params_help') }} ,{{ $t('iam.help') }} <help-link :href="DOCS_MAP.webhook(form.fd.type)">{{ $t('common_386') }}</help-link>
  33. </div>
  34. </a-form-item>
  35. <template v-if="form.fc.getFieldValue('type') === 'webhook'">
  36. <a-form-item label="header">
  37. <a-input v-decorator="decorators.header" />
  38. </a-form-item>
  39. <a-form-item label="body">
  40. <a-input v-decorator="decorators.body" />
  41. </a-form-item>
  42. <a-form-item label="msg_key">
  43. <a-input v-decorator="decorators.msg_key" />
  44. </a-form-item>
  45. <a-form-item label="secret_key">
  46. <a-input v-decorator="decorators.secret_key" />
  47. </a-form-item>
  48. </template>
  49. </a-form>
  50. </div>
  51. <div slot="footer">
  52. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  53. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  54. </div>
  55. </base-dialog>
  56. </template>
  57. <script>
  58. import DialogMixin from '@/mixins/dialog'
  59. import WindowsMixin from '@/mixins/windows'
  60. import DomainProject from '@/sections/DomainProject'
  61. import NameRepeated from '@/sections/NameRepeated/index'
  62. import { isRequired } from '@/utils/validate'
  63. import { ROBOT_TYPES } from '../constants'
  64. import { DOCS_MAP, showDocsLink } from '@/constants/docs'
  65. export default {
  66. name: 'CreateRobotDialog',
  67. components: {
  68. DomainProject,
  69. NameRepeated,
  70. },
  71. mixins: [DialogMixin, WindowsMixin],
  72. data () {
  73. const { name, domain_id, tenant_id, address, type, header, body, msg_key, secret_key } = this.params.data || {}
  74. const { projectDomainId, projectId } = this.$store.getters.userInfo
  75. const initType = type || 'dingtalk'
  76. return {
  77. DOCS_MAP,
  78. showDocsLink,
  79. typesOpts: ROBOT_TYPES,
  80. loading: false,
  81. form: {
  82. fc: this.$form.createForm(this, {
  83. onValuesChange: (props, values) => {
  84. Object.keys(values).forEach((key) => {
  85. this.form.fd[key] = values[key]
  86. })
  87. },
  88. }),
  89. fd: {
  90. name: '',
  91. type: initType,
  92. },
  93. },
  94. decorators: {
  95. domain: [
  96. 'domain',
  97. {
  98. initialValue: domain_id || projectDomainId,
  99. rules: [
  100. { validator: isRequired(), message: this.$t('rules.domain'), trigger: 'change' },
  101. ],
  102. },
  103. ],
  104. project: [
  105. 'project',
  106. {
  107. initialValue: tenant_id || projectId,
  108. rules: [
  109. { validator: isRequired(), message: this.$t('rules.project'), trigger: 'change' },
  110. ],
  111. },
  112. ],
  113. name: [
  114. 'name',
  115. {
  116. initialValue: name,
  117. rules: [
  118. { required: true, message: `${this.$t('common.placeholder')}${this.$t('common.name')}` },
  119. { validator: this.$validate('resourceName') },
  120. ],
  121. },
  122. ],
  123. type: [
  124. 'type',
  125. {
  126. initialValue: initType,
  127. rules: [
  128. { required: true, message: `${this.$t('common.select')}` },
  129. ],
  130. },
  131. ],
  132. address: [
  133. 'address',
  134. {
  135. initialValue: address,
  136. rules: [
  137. { required: true, message: `${this.$t('common.placeholder')}Webhook` },
  138. ],
  139. },
  140. ],
  141. header: [
  142. 'header',
  143. {
  144. initialValue: header ? JSON.stringify(header) : '',
  145. rules: [
  146. { validator: this.checkHeader },
  147. ],
  148. },
  149. ],
  150. body: [
  151. 'body',
  152. {
  153. initialValue: body ? JSON.stringify(body) : '',
  154. rules: [
  155. { validator: this.checkBody },
  156. ],
  157. },
  158. ],
  159. msg_key: [
  160. 'msg_key',
  161. {
  162. initialValue: msg_key || '',
  163. },
  164. ],
  165. secret_key: [
  166. 'secret_key',
  167. {
  168. initialValue: secret_key || '',
  169. },
  170. ],
  171. },
  172. formItemLayout: {
  173. wrapperCol: {
  174. span: 20,
  175. },
  176. labelCol: {
  177. span: 4,
  178. },
  179. },
  180. }
  181. },
  182. computed: {
  183. isEdit () {
  184. return this.params.data?.id
  185. },
  186. headerTitle () {
  187. if (this.isEdit) {
  188. return this.$t('system.text_141', [this.$t('system.robot')])
  189. }
  190. return this.$t('system.text_130', [this.$t('system.robot')])
  191. },
  192. },
  193. methods: {
  194. validateForm () {
  195. return new Promise((resolve, reject) => {
  196. this.form.fc.validateFields((err, values) => {
  197. if (!err) {
  198. resolve(values)
  199. } else {
  200. reject(err)
  201. }
  202. })
  203. })
  204. },
  205. doCreate (data) {
  206. return new this.$Manager('robots', 'v1').create({ data: data })
  207. },
  208. doUpdate (id, data) {
  209. return new this.$Manager('robots', 'v1').update({ id, data })
  210. },
  211. async handleConfirm () {
  212. this.loading = true
  213. try {
  214. const values = await this.validateForm()
  215. const { domain, project, name, header, body, msg_key, ...rest } = values
  216. this.loading = true
  217. if (this.isEdit) {
  218. const data = { ...rest, name }
  219. if (header) {
  220. data.header = JSON.parse(header.trim())
  221. } else {
  222. data.header = {}
  223. }
  224. if (body) {
  225. data.body = JSON.parse(body.trim())
  226. } else {
  227. data.body = {}
  228. }
  229. if (msg_key) data.msg_key = msg_key.trim()
  230. await this.doUpdate(this.params.data?.id, data)
  231. } else {
  232. const data = { ...rest, project: project?.key, domain: domain?.key, generate_name: name }
  233. if (header) data.header = JSON.parse(header.trim())
  234. if (body) data.body = JSON.parse(body.trim())
  235. if (msg_key) data.msg_key = msg_key.trim()
  236. await this.doCreate(data)
  237. }
  238. this.loading = false
  239. this.cancelDialog()
  240. this.params.refresh && this.params.refresh()
  241. this.params.success && this.params.success()
  242. } catch (error) {
  243. this.loading = false
  244. throw error
  245. }
  246. },
  247. checkHeader (rule, value, callback) {
  248. try {
  249. const v = value.trim()
  250. if (v) {
  251. JSON.parse(v)
  252. }
  253. callback()
  254. } catch (error) {
  255. // eslint-disable-next-line standard/no-callback-literal
  256. callback(this.$t('iam.robots.json_validate'))
  257. }
  258. },
  259. checkBody (rule, value, callback) {
  260. try {
  261. const v = value.trim()
  262. if (v) {
  263. JSON.parse(v)
  264. }
  265. callback()
  266. } catch (error) {
  267. // eslint-disable-next-line standard/no-callback-literal
  268. callback(this.$t('iam.robots.json_validate'))
  269. }
  270. },
  271. },
  272. }
  273. </script>