DomainAdd.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <script setup lang="ts">
  2. import DirectiveEditor from '@/views/domain/ngx_conf/directive/DirectiveEditor.vue'
  3. import LocationEditor from '@/views/domain/ngx_conf/LocationEditor.vue'
  4. import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor.vue'
  5. import {useGettext} from 'vue3-gettext'
  6. import domain from '@/api/domain'
  7. import ngx from '@/api/ngx'
  8. import {computed, reactive, ref} from 'vue'
  9. import {message} from 'ant-design-vue'
  10. import {useRouter} from 'vue-router'
  11. const {$gettext, interpolate} = useGettext()
  12. const config = reactive({name: ''})
  13. let ngx_config = reactive({
  14. servers: [{
  15. directives: [],
  16. locations: []
  17. }]
  18. })
  19. const error = reactive({})
  20. const current_step = ref(0)
  21. const enabled = ref(true)
  22. const auto_cert = ref(false)
  23. const update = ref(0)
  24. init()
  25. function init() {
  26. domain.get_template().then(r => {
  27. Object.assign(ngx_config, r.tokenized)
  28. })
  29. }
  30. function save() {
  31. ngx.build_config(ngx_config).then(r => {
  32. domain.save(config.name, {content: r.content, enabled: true}).then(() => {
  33. message.success($gettext('Saved successfully'))
  34. domain.enable(config.name).then(() => {
  35. message.success($gettext('Enabled successfully'))
  36. current_step.value++
  37. window.scroll({top: 0, left: 0, behavior: 'smooth'})
  38. }).catch(r => {
  39. message.error(r.message ?? $gettext('Enable failed'), 10)
  40. })
  41. }).catch(r => {
  42. message.error(interpolate($gettext('Save error %{msg}'), {msg: r.message ?? ''}), 10)
  43. })
  44. })
  45. }
  46. const router = useRouter()
  47. function goto_modify() {
  48. router.push('/domain/' + config.name)
  49. }
  50. function create_another() {
  51. router.go(0)
  52. }
  53. const has_server_name = computed(() => {
  54. const servers = ngx_config.servers
  55. for (const server_key in servers) {
  56. for (const k in servers[server_key].directives) {
  57. const v = servers[server_key].directives[k]
  58. if (v.directive === 'server_name' && v.params.trim() !== '') {
  59. return true
  60. }
  61. }
  62. }
  63. return false
  64. })
  65. </script>
  66. <template>
  67. <a-card :title="$gettext('Add Site')">
  68. <div class="domain-add-container">
  69. <a-steps :current="current_step" size="small">
  70. <a-step :title="$gettext('Base information')"/>
  71. <a-step :title="$gettext('Configure SSL')"/>
  72. <a-step :title="$gettext('Finished')"/>
  73. </a-steps>
  74. <template v-if="current_step===0">
  75. <a-form layout="vertical">
  76. <a-form-item :label="$gettext('Configuration Name')">
  77. <a-input v-model:value="config.name"/>
  78. </a-form-item>
  79. </a-form>
  80. <directive-editor :ngx_directives="ngx_config.servers[0].directives"/>
  81. <br/>
  82. <location-editor :locations="ngx_config.servers[0].locations"/>
  83. <br/>
  84. <a-alert
  85. v-if="!has_server_name"
  86. :message="$gettext('Warning')"
  87. type="warning"
  88. show-icon
  89. >
  90. <template #description>
  91. <span v-translate>server_name parameter is required</span>
  92. </template>
  93. </a-alert>
  94. <br/>
  95. </template>
  96. <template v-else-if="current_step===1">
  97. <ngx-config-editor
  98. ref="ngx-config-editor"
  99. :ngx_config="ngx_config"
  100. v-model:auto_cert="auto_cert"
  101. :enabled="enabled"
  102. />
  103. <br/>
  104. </template>
  105. <a-space v-if="current_step<2">
  106. <a-button
  107. type="primary"
  108. @click="save"
  109. :disabled="!config.name||!has_server_name"
  110. >
  111. <translate>Next</translate>
  112. </a-button>
  113. </a-space>
  114. <a-result
  115. v-else-if="current_step===2"
  116. status="success"
  117. :title="$gettext('Domain Config Created Successfully')"
  118. >
  119. <template #extra>
  120. <a-button type="primary" @click="goto_modify">
  121. <translate>Modify Config</translate>
  122. </a-button>
  123. <a-button @click="create_another">
  124. <translate>Create Another</translate>
  125. </a-button>
  126. </template>
  127. </a-result>
  128. </div>
  129. </a-card>
  130. </template>
  131. <style lang="less" scoped>
  132. .ant-steps {
  133. padding: 10px 0 20px 0;
  134. }
  135. .domain-add-container {
  136. max-width: 800px;
  137. margin: 0 auto
  138. }
  139. </style>