SetupNotifyReceiver.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ title }}</div>
  4. <div slot="body">
  5. <a-form
  6. class="mt-3"
  7. :form="form.fc"
  8. v-bind="formItemLayout"
  9. hideRequiredMark>
  10. <scope-radio
  11. :disabled="isUpdate"
  12. :decorators="decorators"
  13. :form="form"
  14. :label="$t('system.notify.topic.receiver.resource_scope')"
  15. @change="scopeChange" />
  16. <a-form-item :label="$t('system.text_48')">
  17. <a-radio-group v-decorator="decorators.type" @change="handleTypeChange" :disabled="isUpdate">
  18. <a-radio-button
  19. v-for="item in sub_types"
  20. :value="item.key"
  21. :key="item.key">{{ item.label }}</a-radio-button>
  22. </a-radio-group>
  23. </a-form-item>
  24. <a-form-item v-if="curType === 'receiver'" :label="$t('system.notify.subscriber.type.receiver')">
  25. <base-select
  26. v-decorator="decorators.receivers"
  27. class="w-100"
  28. :filterable="true"
  29. needParams
  30. resource="receivers"
  31. version="v1"
  32. option-label-prop="label"
  33. :isDefaultSelect="true"
  34. :params="recipientParams"
  35. :select-props="{ placeholder: $t('system.text_232', [$t('system.notify.subscriber.type.receiver')]), allowClear: false, mode: 'multiple' }">
  36. <template #optionLabelTemplate="{ item }">
  37. <a-row>
  38. <a-col span="9">{{ item.name }}</a-col>
  39. <a-col><p class="text-right text-color-secondary mr-2">{{ $t('common_624', [$t('dictionary.domain')]) }}: {{ item.project_domain }}</p></a-col>
  40. </a-row>
  41. </template>
  42. </base-select>
  43. </a-form-item>
  44. <a-form-item v-if="curType === 'role'" :label="$t('system.notify.subscriber.type.role')">
  45. <a-row type="flex">
  46. <a-col :flex="2" class="mr-2">
  47. <base-select
  48. v-decorator="decorators.role_scope"
  49. :options="roleScopeOptions"
  50. :select-props="{ placeholder: $t('system.text_232', [$t('common_235')]), allowClear: false }" />
  51. </a-col>
  52. <a-col :flex="3">
  53. <base-select
  54. v-decorator="decorators.role"
  55. :filterable="true"
  56. needParams
  57. resource="roles"
  58. version="v1"
  59. :isDefaultSelect="true"
  60. :applyOptionLabel="true"
  61. :params="recipientParams"
  62. :select-props="{ placeholder: $t('system.text_232', [$t('system.notify.subscriber.type.role')]), allowClear: false }" />
  63. </a-col>
  64. </a-row>
  65. </a-form-item>
  66. <a-form-item v-if="curType === 'robot'" :label="$t('system.notify.subscriber.type.robot')">
  67. <base-select
  68. v-decorator="decorators.robot"
  69. class="w-100"
  70. :filterable="true"
  71. needParams
  72. resource="robots"
  73. version="v1"
  74. option-label-prop="label"
  75. :isDefaultSelect="true"
  76. :params="recipientParams"
  77. :select-props="{ placeholder: $t('system.text_232', [$t('system.notify.subscriber.type.robot')]), allowClear: false }" />
  78. </a-form-item>
  79. <a-form-item :label="$t('iam.notify_silent')">
  80. <a-row type="flex">
  81. <a-col :flex="2" class="mr-2">
  82. <a-select
  83. v-decorator="decorators.group_times"
  84. allow-clear
  85. :placeholder="$t('common.tips.select', [$t('iam.notify_silent')])">
  86. <a-select-option v-for="item in groupKeyOpts" :value="item.id" :key="item.id">{{item.name}}</a-select-option>
  87. </a-select>
  88. </a-col>
  89. <a-col :flex="3"><help-tooltip :text="$t('iam.notify_silent.tip')" /></a-col>
  90. </a-row>
  91. </a-form-item>
  92. </a-form>
  93. </div>
  94. <div slot="footer">
  95. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  96. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  97. </div>
  98. </base-dialog>
  99. </template>
  100. <script>
  101. import { mapGetters } from 'vuex'
  102. import DialogMixin from '@/mixins/dialog'
  103. import WindowsMixin from '@/mixins/windows'
  104. import ScopeRadio from '@/sections/ScopeRadio'
  105. import { NOTIFY_SUBSCRIBER_TYPES, NOTIFY_ROLE_SCOPES } from '../../../constants'
  106. export default {
  107. name: 'SetupNotifyReceiverDialog',
  108. components: { ScopeRadio },
  109. mixins: [DialogMixin, WindowsMixin],
  110. data () {
  111. const initialValues = {}
  112. if (this.params.data && this.params.data.id) {
  113. // update
  114. const s = this.params.data
  115. initialValues.scope = s.resource_scope
  116. initialValues.resource_scope = s.resource_scope
  117. if (s.resource_scope === 'domain') initialValues.domain = s.resource_attribution_id
  118. if (s.resource_scope === 'project') initialValues.project = s.resource_attribution_id
  119. initialValues.type = s.type
  120. initialValues.role_scope = s.role_scope
  121. initialValues.role = s.role ? s.role.id : ''
  122. initialValues.robot = s.robot ? s.robot.id : ''
  123. initialValues.receivers = s.receivers ? s.receivers.map(r => r.id) : []
  124. initialValues.group_times = s.group_times || undefined
  125. } else {
  126. // create
  127. initialValues.scope = this.$store.getters.scope
  128. initialValues.resource_scope = this.$store.getters.scope
  129. initialValues.type = NOTIFY_SUBSCRIBER_TYPES[0].key
  130. initialValues.role_scope = 'system'
  131. initialValues.group_times = 60
  132. }
  133. return {
  134. loading: false,
  135. isAdminMode: this.$store.getters.isAdminMode,
  136. curType: initialValues.type,
  137. sub_types: NOTIFY_SUBSCRIBER_TYPES,
  138. roleScopeOptions: NOTIFY_ROLE_SCOPES,
  139. form: {
  140. fc: this.$form.createForm(this, {
  141. onValuesChange: (props, values) => {
  142. Object.keys(values).forEach((key) => {
  143. this.form.fd[key] = values[key]
  144. })
  145. },
  146. }),
  147. fd: {
  148. ...initialValues,
  149. },
  150. },
  151. decorators: {
  152. scope: [
  153. 'scope',
  154. {
  155. initialValue: initialValues.resource_scope,
  156. },
  157. ],
  158. domain: [
  159. 'domain',
  160. {
  161. initialValue: initialValues.domain,
  162. rules: [
  163. { required: !this.params.data, message: `${this.$t('common.select')}` },
  164. ],
  165. },
  166. ],
  167. project: [
  168. 'project',
  169. {
  170. initialValue: initialValues.project,
  171. rules: [
  172. { required: !this.params.data, message: `${this.$t('common.select')}` },
  173. ],
  174. },
  175. ],
  176. type: [
  177. 'type',
  178. {
  179. initialValue: initialValues.type,
  180. rules: [
  181. { required: true, message: `${this.$t('common.select')}` },
  182. ],
  183. },
  184. ],
  185. receivers: [
  186. 'receivers',
  187. {
  188. initialValue: initialValues.receivers,
  189. rules: [
  190. { required: true, message: `${this.$t('common.select')}` },
  191. ],
  192. },
  193. ],
  194. robot: [
  195. 'robot',
  196. {
  197. initialValue: initialValues.robot,
  198. rules: [
  199. { required: true, message: `${this.$t('common.select')}` },
  200. ],
  201. },
  202. ],
  203. role: [
  204. 'role',
  205. {
  206. initialValue: initialValues.role,
  207. rules: [
  208. { required: true, message: `${this.$t('common.select')}` },
  209. ],
  210. },
  211. ],
  212. role_scope: [
  213. 'role_scope',
  214. {
  215. initialValue: initialValues.role_scope,
  216. rules: [
  217. { required: true, message: `${this.$t('common.select')}` },
  218. ],
  219. },
  220. ],
  221. group_times: [
  222. 'group_times',
  223. {
  224. initialValue: initialValues.group_times,
  225. rules: [
  226. { required: false, message: `${this.$t('common.select')}` },
  227. ],
  228. },
  229. ],
  230. },
  231. formItemLayout: {
  232. wrapperCol: {
  233. span: 19,
  234. },
  235. labelCol: {
  236. span: 5,
  237. },
  238. },
  239. groupKeyOpts: [
  240. { id: 5, name: this.$t('iam.silent.minute', [5]) },
  241. { id: 10, name: this.$t('iam.silent.minute', [10]) },
  242. { id: 15, name: this.$t('iam.silent.minute', [15]) },
  243. { id: 30, name: this.$t('iam.silent.minute', [30]) },
  244. { id: 1 * 60, name: this.$t('iam.silent.hour', [1]) },
  245. { id: 2 * 60, name: this.$t('iam.silent.hour', [2]) },
  246. { id: 3 * 60, name: this.$t('iam.silent.hour', [3]) },
  247. { id: 6 * 60, name: this.$t('iam.silent.hour', [6]) },
  248. { id: 12 * 60, name: this.$t('iam.silent.hour', [12]) },
  249. { id: 24 * 60, name: this.$t('iam.silent.hour', [24]) },
  250. ],
  251. }
  252. },
  253. computed: {
  254. ...mapGetters(['userInfo']),
  255. isUpdate () {
  256. return this.params.data && this.params.data.id
  257. },
  258. title () {
  259. if (this.params.data && this.params.data.id) {
  260. return this.$t('system.text_141', [this.$t('system.notify.subscriber.type.receiver')])
  261. }
  262. return this.$t('system.text_130', [this.$t('system.notify.subscriber.type.receiver')])
  263. },
  264. recipientParams () {
  265. return {
  266. limit: 0,
  267. scope: this.$store.getters.scope,
  268. with_meta: true,
  269. enabled: true,
  270. }
  271. },
  272. },
  273. destroyed () {
  274. this.manager = null
  275. },
  276. created () {
  277. this.manager = new this.$Manager('subscribers', 'v1')
  278. },
  279. methods: {
  280. handleTypeChange (e) {
  281. this.curType = e.target.value
  282. },
  283. async handleConfirm () {
  284. this.loading = true
  285. try {
  286. const values = await this.form.fc.validateFields()
  287. let resource_attribution_id = ''
  288. if (values.scope === 'domain') resource_attribution_id = values.domain || this.userInfo.projectDomainId
  289. if (values.scope === 'project') resource_attribution_id = values.project || this.userInfo.projectId
  290. const params = {
  291. receivers: values.receivers,
  292. role_scope: values.role_scope,
  293. role: values.role,
  294. robot: values.robot,
  295. group_times: values.group_times || 0,
  296. }
  297. if (this.isUpdate) {
  298. // params.topic_id = this.params.data.topic_id || this.params.resId
  299. await this.manager.performAction({
  300. id: this.params.data.id,
  301. action: 'change',
  302. data: params,
  303. })
  304. } else {
  305. params.scope = this.$store.getters.scope
  306. params.resource_scope = values.scope
  307. params.resource_attribution_id = resource_attribution_id
  308. params.topic_id = this.params.resId
  309. params.type = values.type
  310. await this.manager.create({
  311. data: params,
  312. })
  313. }
  314. this.loading = false
  315. this.params.success()
  316. this.cancelDialog()
  317. } catch (error) {
  318. this.loading = false
  319. throw error
  320. }
  321. },
  322. },
  323. }
  324. </script>