ProjectMappingRuleEdit.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('cloudenv.text_604')}}</div>
  4. <div slot="body">
  5. <a-form-model ref="ruleForm" :model="formData" :rules="rules" v-bind="layout">
  6. <!-- 条件 -->
  7. <a-form-model-item :label="$t('cloudenv.text_22')" v-bind="layout" :rules="rules.condition" prop="condition">
  8. <a-select v-model="formData.condition">
  9. <a-select-option v-for="item in resourceAndTagOptions" :value="item.value" :key="item.value" :disabled="item.value === 'and_copy' && isSecAndcopy()">
  10. {{item.name}}
  11. </a-select-option>
  12. </a-select>
  13. </a-form-model-item>
  14. <!-- 标签key -->
  15. <template v-if="formData.condition === 'and_copy'">
  16. <a-form-model-item :label="$t('cloudenv.tag_key')" v-bind="layout" :rules="rules.tag_key" prop="tag_key">
  17. <a-input v-model="formData.tag_key" />
  18. </a-form-model-item>
  19. </template>
  20. <template v-else>
  21. <!-- 标签 -->
  22. <a-form-model-item :label="$t('cloudenv.text_16')" v-bind="layout" :rules="rules.tags" prop="tags">
  23. <tag :defaultChecked="formData.defaultTags" @change="handleTagChange" />
  24. </a-form-model-item>
  25. <!-- 项目 -->
  26. <a-form-model-item :label="$t('cloudenv.belong_type')" :extra="formData.belong_type === 'project_id' ? $t('cloudenv.text_592') : $t('cloudenv.belong_project_name_tip')" v-bind="layout" :rules="rules.belong_type" prop="belong_type">
  27. <a-form-model-item class="mb-0">
  28. <a-radio-group v-model="formData.belong_type" @change="validateBt">
  29. <a-radio-button value="project_id">{{ $t('cloudenv.target_project') }}</a-radio-button>
  30. <a-radio-button value="project">{{ $t('cloudenv.target_name') }}</a-radio-button>
  31. </a-radio-group>
  32. <base-select
  33. v-if="formData.belong_type === 'project_id'"
  34. resource="projects"
  35. remote
  36. :params="projectParams"
  37. v-model="formData.project_id"
  38. :select-props="{placeholder: $t('common.tips.select', [$t('dictionary.project')])}"
  39. @change="validateBt" />
  40. <a-input v-else type="text" v-model="formData.project" :placeholder="$t('common.tips.select', [$t('dictionary.project')])" @change="validateBt" />
  41. </a-form-model-item>
  42. </a-form-model-item>
  43. </template>
  44. </a-form-model>
  45. </div>
  46. <div slot="footer">
  47. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  48. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  49. </div>
  50. </base-dialog>
  51. </template>
  52. <script>
  53. import * as R from 'ramda'
  54. import { mapGetters } from 'vuex'
  55. import DialogMixin from '@/mixins/dialog'
  56. import WindowsMixin from '@/mixins/windows'
  57. import Tag from '../components/Tag'
  58. export default {
  59. name: 'ProjectMappingRuleEditDialog',
  60. components: {
  61. Tag,
  62. },
  63. mixins: [DialogMixin, WindowsMixin],
  64. data () {
  65. const { editType } = this.params
  66. const projectDomainInitialValue = this.params.projectDomainId || this.$store.getters.userInfo.projectDomainId
  67. let initFormData = {
  68. condition: 'and',
  69. tags: {},
  70. tag_key: '',
  71. project_id: '',
  72. belong_type: 'project_id',
  73. project: '',
  74. }
  75. if (editType === 'edit') {
  76. const data = this.params.data[0]
  77. const initCondition = data.condition === 'and' && !data.hasOwnProperty('project_id') && !data.hasOwnProperty('project') ? 'and_copy' : data.condition
  78. const initBelongType = data.hasOwnProperty('project_id') ? 'project_id' : 'project'
  79. const tags = this.initTags(data.tags)
  80. if (initCondition === 'and_copy') {
  81. initFormData = { condition: initCondition, tag_key: data.tags[0]?.key }
  82. } else {
  83. initFormData = { condition: data.condition, tags: tags, defaultTags: R.clone(tags), project_id: data.project_id, project: data.project, belong_type: initBelongType }
  84. }
  85. }
  86. return {
  87. loading: false,
  88. formData: initFormData,
  89. rules: {
  90. condition: [
  91. { required: true, message: this.$t('common.tips.select', [this.$t('cloudenv.text_22')]) },
  92. ],
  93. tags: [
  94. { required: true, validator: this.validateTags },
  95. ],
  96. tag_key: [
  97. { required: true, message: this.$t('common.tips.input', [this.$t('cloudenv.tag_key')]) },
  98. ],
  99. belong_type: [
  100. { required: true, validator: this.validateBelongType, trigger: 'change' },
  101. ],
  102. },
  103. layout: {
  104. wrapperCol: {
  105. span: 20,
  106. },
  107. labelCol: {
  108. span: 4,
  109. },
  110. },
  111. resourceAndTagOptions: [
  112. {
  113. id: 1,
  114. name: this.$t('cloudenv.text_588'),
  115. value: 'and',
  116. },
  117. {
  118. id: 2,
  119. name: this.$t('cloudenv.text_587'),
  120. value: 'or',
  121. },
  122. {
  123. id: 3,
  124. name: this.$t('cloudenv.match_by_tag_key'),
  125. value: 'and_copy',
  126. },
  127. ],
  128. projectDomainId: projectDomainInitialValue,
  129. }
  130. },
  131. computed: {
  132. ...mapGetters(['isAdminMode', 'scope', 'isDomainMode', 'userInfo', 'l3PermissionEnable']),
  133. projectParams () {
  134. return {
  135. scope: this.scope,
  136. // project_domain_id: this.params.projectDomainId,
  137. limit: 20,
  138. }
  139. },
  140. editIndex () {
  141. const { data, rules } = this.params
  142. let idx = rules.length
  143. rules.map((item, index) => {
  144. if (item._XID === data[0]._XID) {
  145. idx = index
  146. }
  147. })
  148. return idx
  149. },
  150. },
  151. methods: {
  152. validateBt () {
  153. this.$refs.ruleForm.validateField('belong_type')
  154. },
  155. validateBelongType (rule, value, callback) {
  156. if (value === 'project_id' && !this.formData[value]) {
  157. callback(new Error(this.$t('common.tips.select', [this.$t('dictionary.project')])))
  158. }
  159. if (value === 'project' && !((this.formData[value] || '').trim())) {
  160. callback(new Error(this.$t('common.tips.input', [this.$t('dictionary.project')])))
  161. }
  162. callback()
  163. },
  164. initTags (tagArr = []) {
  165. const tags = {}
  166. tagArr.map(item => {
  167. const key = 'user:' + item.key
  168. if (!tags[key]) {
  169. tags[key] = []
  170. }
  171. tags[key].push(item.value)
  172. })
  173. return tags
  174. },
  175. handleTagChange (val) {
  176. console.log('tag change', val)
  177. this.formData.tags = val
  178. },
  179. isSecAndcopy () {
  180. const { editType, rules } = this.params
  181. const index = editType === 'create' ? rules.length : this.editIndex
  182. let has = false
  183. rules.map((item, idx) => {
  184. if (item.condition === 'and' && !item.hasOwnProperty('project_id') && !item.hasOwnProperty('project') && index !== idx) {
  185. has = true
  186. }
  187. })
  188. return has
  189. },
  190. getUpdateParams () {
  191. const { rules, editType } = this.params
  192. const ruleList = rules.map(item => {
  193. const ret = { condition: item.condition, tags: item.tags, auto_create_project: item.auto_create_project }
  194. if (item.project_id) {
  195. ret.project_id = item.project_id
  196. ret.project = item.project
  197. } else if (item.project) {
  198. ret.project = item.project
  199. }
  200. return ret
  201. })
  202. let newRule = {}
  203. if (this.formData.condition === 'and_copy') {
  204. newRule = {
  205. condition: 'and',
  206. tags: [{ key: this.formData.tag_key }],
  207. auto_create_project: true,
  208. }
  209. } else {
  210. newRule = {
  211. condition: this.formData.condition,
  212. tags: this.getTagValue(this.formData.tags),
  213. }
  214. newRule[this.formData.belong_type] = this.formData[this.formData.belong_type]
  215. }
  216. if (editType === 'create') {
  217. return [...ruleList, newRule]
  218. } else if (editType === 'edit') {
  219. return ruleList.map((item, index) => {
  220. if (index === this.editIndex) {
  221. return newRule
  222. }
  223. return item
  224. })
  225. }
  226. return ruleList
  227. },
  228. getTagValue (tag) {
  229. const result = []
  230. const keys = Object.keys(tag)
  231. keys.map(key => {
  232. result.push({
  233. key: R.replace(/(ext:|user:)/, '', key),
  234. value: tag[key],
  235. })
  236. })
  237. return result
  238. },
  239. validateTags (rule, value, callback) {
  240. if (value) {
  241. const keys = Object.keys(value)
  242. if (!keys.length) {
  243. callback(new Error(this.$t('common.tips.select', [this.$t('cloudenv.text_16')])))
  244. } else if (keys.length > 20) {
  245. callback(new Error(this.$t('cloudenv.text_602')))
  246. } else {
  247. callback()
  248. }
  249. } else {
  250. callback(new Error(this.$t('common.tips.select', [this.$t('cloudenv.text_16')])))
  251. }
  252. },
  253. doUpdate (data) {
  254. return new this.$Manager('project_mappings').update({ id: this.params.id, data: data })
  255. },
  256. async handleConfirm () {
  257. this.loading = true
  258. try {
  259. const validate = await this.$refs.ruleForm.validate()
  260. if (!validate) return
  261. // 获取参数
  262. const rules = this.getUpdateParams()
  263. await this.doUpdate({ rules })
  264. this.cancelDialog()
  265. this.$bus.$emit('ProjectMappingRuleUpdate')
  266. this.$message.success(this.$t('common.success'))
  267. } catch (error) {
  268. throw error
  269. } finally {
  270. this.loading = false
  271. }
  272. },
  273. },
  274. }
  275. </script>