index.vue 2.7 KB

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