index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="server-create-index">
  3. <page-header :title="headerTitle" :tabs="cloudEnvOptions" :current-tab.sync="cloudEnv" />
  4. <component :is="component" :type="cloudEnv === 'onpremise' ? 'idc' : cloudEnv" :initFormData="initFormData" :isInitForm="isInitForm" />
  5. </div>
  6. </template>
  7. <script>
  8. import { getCloudEnvOptions } from '@/utils/common/hypervisor'
  9. import IDC from './form/IDC'
  10. import Public from './form/Public'
  11. import Private from './form/Private'
  12. export default {
  13. name: 'VMInstanceCreate',
  14. components: {
  15. IDC,
  16. Public,
  17. Private,
  18. },
  19. data () {
  20. const cloudEnvOptions = getCloudEnvOptions('compute_engine_brands', true)
  21. const queryType = this.$route.query.type
  22. const paramsData = this.$route.params?.data || {}
  23. let cloudEnv = queryType === 'idc' ? 'onpremise' : this.$route.query.type
  24. let routerQuery = this.$route.query.type
  25. if (!cloudEnvOptions.find(val => val.key === cloudEnv)) {
  26. cloudEnv = cloudEnvOptions[0].key
  27. routerQuery = cloudEnv === 'onpremise' ? 'idc' : cloudEnv
  28. }
  29. return {
  30. cloudEnvOptions,
  31. cloudEnv,
  32. routerQuery,
  33. initFormData: paramsData,
  34. }
  35. },
  36. computed: {
  37. isInitForm () {
  38. return !!this.initFormData?.extraData?.formType && this.$route.query.workflow
  39. },
  40. type () {
  41. const { type = 'idc' } = this.$route.query
  42. switch (type) {
  43. case 'private':
  44. return 'private'
  45. case 'public':
  46. return 'public'
  47. default:
  48. return 'idc'
  49. }
  50. },
  51. component () {
  52. const { type = 'idc' } = this.$route.query
  53. switch (type) {
  54. case 'private':
  55. return 'Private'
  56. case 'public':
  57. return 'Public'
  58. default:
  59. return 'IDC'
  60. }
  61. },
  62. headerTitle () {
  63. let res
  64. if (this.$route.query.source === 'servertemplate') {
  65. res = this.$t('compute.text_257')
  66. } else {
  67. res = this.$t('compute.text_91')
  68. }
  69. return this.$route.query.workflow ? this.$t('compute.text_1161', [res]) + `(${this.$t('common.modify_workflow')})` : this.$t('compute.text_1161', [res])
  70. },
  71. },
  72. watch: {
  73. cloudEnv (val) {
  74. this.clearInitFormData()
  75. this.$nextTick(() => {
  76. const query = this.getQuery(this.$router.history.current.query)
  77. const path = this.$router.history.current.path
  78. const newQuery = JSON.parse(JSON.stringify(query))
  79. newQuery.type = val === 'onpremise' ? 'idc' : val
  80. this.$router.push({ path, query: newQuery })
  81. })
  82. },
  83. },
  84. created () {
  85. if (this.routerQuery !== this.$route.query.type) {
  86. this.$router.push({
  87. path: this.$router.history.current.path,
  88. query: {
  89. type: this.routerQuery,
  90. },
  91. })
  92. }
  93. },
  94. mounted () {
  95. this.detectBack() // 在用户点击 back 的时候,表单的处理方案
  96. },
  97. beforeDestroy () {
  98. window.removeEventListener('popstate', this.popstate)
  99. },
  100. methods: {
  101. clearInitFormData () {
  102. this.isInitForm = false
  103. this.initFormData = {}
  104. },
  105. detectBack () {
  106. window.addEventListener('popstate', this.popstate, false)
  107. },
  108. popstate (e) {
  109. if (e.state) {
  110. // 侦测是用户触发的后退操作, dosomething
  111. // 这里刷新当前 url
  112. window.location.reload()
  113. }
  114. },
  115. getQuery (query) {
  116. if (query.sence === 'image') {
  117. return { type: query.type }
  118. }
  119. return query
  120. },
  121. },
  122. }
  123. </script>