index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <div class="scheduletask">
  3. <page-header :title="$t('cloudenv.text_432')" />
  4. <page-body needMarginBottom>
  5. <a-form :form="form.fc" v-bind="formItemLayout" hideRequiredMark>
  6. <a-form-item :label="$t('cloudenv.text_410', [$t('dictionary.project')])" class="mt-3" v-bind="formItemLayout">
  7. <domain-project :fc="form.fc" :decorators="{ project: decorators.project, domain: decorators.domain }" />
  8. </a-form-item>
  9. <a-form-item :label="$t('cloudenv.text_95')" :extra="$t('cloudenv.name.extra')">
  10. <a-input :placeholder="$t('cloudenv.text_190')" v-decorator="decorators.name" />
  11. <template v-slot:extra>
  12. <name-repeated res="scheduledtasks" :name="form.fd.name" />
  13. </template>
  14. </a-form-item>
  15. <a-form-item :label="$t('common.description')">
  16. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  17. </a-form-item>
  18. <scheduled-task-time ref="taskTime" :form="form" />
  19. <a-form-item :label="$t('cloudenv.text_384')">
  20. <a-select v-decorator="decorators.resourceType">
  21. <a-select-option v-for="(v, k) in resourceTypeOpts" :key="k" :value="k">{{v}}</a-select-option>
  22. </a-select>
  23. </a-form-item>
  24. <a-form-item :label="$t('cloudenv.text_425')">
  25. <a-radio-group v-decorator="decorators.action">
  26. <a-radio-button v-for="(v, k) in $t('cloudenvScheduledtaskRuleAction')" :key="k" :value="k">{{v}}</a-radio-button>
  27. </a-radio-group>
  28. </a-form-item>
  29. <a-form-item :label="$t('cloudenv.text_439')" v-show="false">
  30. <a-radio-group v-decorator="decorators.labelType">
  31. <a-radio-button v-for="(v, k) in $t('cloudenvScheduledtaskLabelType')" :key="k" :value="k">{{v}}</a-radio-button>
  32. </a-radio-group>
  33. </a-form-item>
  34. <a-form-item :label="$t('cloudenv.text_440')" v-if="form.fc.getFieldValue('labelType') === 'id'">
  35. <list-select
  36. v-decorator="decorators.servers"
  37. :list-props="resourceProps"
  38. :multiple="true"
  39. :formatter="formatter" />
  40. </a-form-item>
  41. <a-form-item :label="$t('cloudenv.text_16')" class="mb-0" v-if="form.fc.getFieldValue('labelType') === 'tag'">
  42. <tag v-decorator="decorators.tag" :extra="$t('cloudenv.text_441')" />
  43. </a-form-item>
  44. </a-form>
  45. </page-body>
  46. <page-footer>
  47. <template class="content" v-slot:right>
  48. <a-button class="mr-2" type="primary" @click="handleConfirm" :loading="loading">{{ $t('common.create') }}</a-button>
  49. <a-button @click="handleCancel">{{ $t('dialog.cancel') }}</a-button>
  50. </template>
  51. </page-footer>
  52. </div>
  53. </template>
  54. <script>
  55. import { mapGetters } from 'vuex'
  56. import ScheduledTaskTime from '@Cloudenv/sections/ScheduledTaskTime'
  57. import Tag from '@/sections/Tag'
  58. import DomainProject from '@/sections/DomainProject'
  59. import ListSelect from '@/sections/ListSelect'
  60. import validateForm, { isRequired } from '@/utils/validate'
  61. import NameRepeated from '@/sections/NameRepeated'
  62. import ResourcePropsMixin from '../mixins/resourceProps'
  63. export default {
  64. name: 'ScheduledtaskCreateIndex',
  65. components: {
  66. DomainProject,
  67. Tag,
  68. ListSelect,
  69. NameRepeated,
  70. ScheduledTaskTime,
  71. },
  72. mixins: [ResourcePropsMixin],
  73. data () {
  74. return {
  75. loading: false,
  76. resourceTypeOpts: { ...this.$t('cloudenvScheduledtaskResourceType') },
  77. decorators: {
  78. domain: [
  79. 'domain',
  80. {
  81. rules: [
  82. { validator: isRequired(), message: this.$t('rules.domain'), trigger: 'change' },
  83. ],
  84. },
  85. ],
  86. project: [
  87. 'project',
  88. {
  89. rules: [
  90. { validator: isRequired(), message: this.$t('rules.project'), trigger: 'change' },
  91. ],
  92. },
  93. ],
  94. name: [
  95. 'name',
  96. {
  97. rules: [
  98. { required: true, message: `${this.$t('common.placeholder')}${this.$t('common.name')}` },
  99. ],
  100. },
  101. ],
  102. description: ['description'],
  103. cycle_type: [
  104. 'cycleTimer.cycle_type',
  105. {
  106. initialValue: 'day',
  107. rules: [
  108. { required: true, message: this.$t('cloudenv.text_443') },
  109. ],
  110. },
  111. ],
  112. weekDays: [
  113. 'cycleTimer.week_days',
  114. {
  115. initialValue: [1, 2, 3, 4, 5],
  116. rules: [
  117. { required: true, message: this.$t('cloudenv.text_444') },
  118. ],
  119. },
  120. ],
  121. monthDays: [
  122. 'cycleTimer.month_days',
  123. {
  124. initialValue: [1],
  125. rules: [
  126. { required: true, message: this.$t('cloudenv.text_445') },
  127. ],
  128. },
  129. ],
  130. cycle_num: [
  131. 'cycleTimer.cycle_num',
  132. {
  133. initialValue: 1,
  134. rules: [
  135. { required: true, message: this.$t('cloudenv.input_cycle_num') },
  136. ],
  137. },
  138. ],
  139. cycle_type2: [
  140. 'cycleTimer.cycle_type2',
  141. {
  142. initialValue: 'month',
  143. },
  144. ],
  145. // 小时:分钟
  146. hourMinute: [
  147. 'hourMinute',
  148. {
  149. initialValue: this.$moment().startOf('day').add(2, 'h'),
  150. rules: [
  151. { required: true, message: this.$t('cloudenv.text_446') },
  152. ],
  153. },
  154. ],
  155. // 有效时间
  156. startEndTime: [
  157. 'startEndTime',
  158. {
  159. // initialValue: [1],
  160. rules: [
  161. { required: false, message: this.$t('cloudenv.text_447') },
  162. ],
  163. },
  164. ],
  165. execTime: [
  166. 'timer.execTime',
  167. {
  168. initialValue: this.$moment().add(1, 'h'),
  169. rules: [
  170. { required: true, message: this.$t('cloudenv.text_437') },
  171. ],
  172. },
  173. ],
  174. resourceType: [
  175. 'resourceType',
  176. {
  177. initialValue: 'server',
  178. rules: [
  179. { required: true, message: this.$t('cloudenv.text_448') },
  180. ],
  181. },
  182. ],
  183. action: [
  184. 'action',
  185. {
  186. initialValue: 'start',
  187. rules: [
  188. { required: true, message: this.$t('cloudenv.text_449') },
  189. ],
  190. },
  191. ],
  192. labelType: [
  193. 'labelType',
  194. {
  195. initialValue: 'id',
  196. rules: [
  197. { required: true, message: this.$t('cloudenv.text_450') },
  198. ],
  199. },
  200. ],
  201. servers: [
  202. 'servers',
  203. ],
  204. tag: [
  205. 'tag',
  206. {
  207. // initialValue: { 'user:a': '3', 'user:b': '9' },
  208. rules: [
  209. { required: true, message: this.$t('cloudenv.text_451') },
  210. { validator: validateForm('tagName') },
  211. ],
  212. },
  213. ],
  214. },
  215. form: {
  216. fc: this.$form.createForm(this, {
  217. onValuesChange: (props, values) => {
  218. Object.keys(values).forEach((key) => {
  219. this.form.fd[key] = values[key]
  220. })
  221. if (values.hasOwnProperty('project')) {
  222. this.resourceProps.list.getParams.tenant = values.project && values.project.key
  223. }
  224. },
  225. }),
  226. fd: {
  227. name: '',
  228. },
  229. },
  230. formItemLayout: {
  231. wrapperCol: {
  232. md: { span: 18 },
  233. xl: { span: 20 },
  234. xxl: { span: 22 },
  235. },
  236. labelCol: {
  237. md: { span: 6 },
  238. xl: { span: 4 },
  239. xxl: { span: 2 },
  240. },
  241. },
  242. }
  243. },
  244. computed: {
  245. ...mapGetters(['userInfo']),
  246. },
  247. created () {
  248. delete this.resourceTypeOpts.cloudaccount
  249. },
  250. methods: {
  251. disabledDate (current) {
  252. return current && current < this.$moment().subtract(1, 'd').endOf('day')
  253. },
  254. range (start, end) {
  255. const result = []
  256. for (let i = start; i < end; i++) {
  257. result.push(i)
  258. }
  259. return result
  260. },
  261. generateData (values) {
  262. const { domain, project, name, description, action, labelType, servers, resourceType } = values
  263. const params = {
  264. generate_name: name,
  265. description,
  266. resource_type: resourceType,
  267. project_domain: (domain && domain.key) || this.userInfo.projectDomainId,
  268. project_id: (project && project.key) || this.userInfo.projectId,
  269. operation: action,
  270. label_type: labelType,
  271. }
  272. this.$refs.taskTime.transformParams(params, values)
  273. if (labelType === 'id') {
  274. params.labels = servers
  275. } else if (labelType === 'tag') {
  276. const tags = Object.keys(values.tag).map((k) => {
  277. return `${k}:${values.tag[k]}`
  278. })
  279. params.labels = tags
  280. }
  281. return params
  282. },
  283. async handleConfirm () {
  284. const manager = new this.$Manager('scheduledtasks', 'v1')
  285. try {
  286. const values = await this.form.fc.validateFields()
  287. this.loading = true
  288. await manager.create({
  289. data: this.generateData(values),
  290. })
  291. this.goBack()
  292. } catch (err) {
  293. throw err
  294. } finally {
  295. this.loading = false
  296. }
  297. },
  298. handleCancel () {
  299. this.goBack()
  300. },
  301. goBack () {
  302. this.$router.push('/scheduledtask')
  303. },
  304. formatter (val) {
  305. return `${val.name}`
  306. },
  307. },
  308. }
  309. </script>