ClearCache.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('network.cdn.clear_cache')}}</div>
  4. <div slot="body">
  5. <a-form-model
  6. :model="form"
  7. :rules="rules"
  8. ref="form"
  9. v-bind="formItemLayout">
  10. <a-form-model-item :label="$t('network.cdn.clear_method')" prop="clear_method" :extra="$t(`network.cdn.clear_method.${form.clear_method}.extra`)">
  11. <a-select v-model="form.clear_method" @change="handleClearMethodChange">
  12. <a-select-option v-for="item in clearMethodOptions" :key="item.key" :value="item.key">
  13. {{ item.label }}
  14. </a-select-option>
  15. </a-select>
  16. </a-form-model-item>
  17. <a-form-model-item v-if="form.clear_method !== 'all'" :label="clearListLabel" prop="clear_list">
  18. <a-textarea v-model="form.clear_list" :rows="4" :placeholder="$t('network.cdn.clear_list_placeholder', [$t(`network.cdn.clear_method.${form.clear_method}`)])" />
  19. </a-form-model-item>
  20. <a-collapse :bordered="false" v-model="collapseActive" v-if="form.clear_method === 'url'">
  21. <a-collapse-panel :header="$t('network.cdn.clear_cache_advanced')" key="1">
  22. <a-alert :message="$t('network.cdn.clear_cache_advanced_description')" type="info" />
  23. <a-form-model-item class="mt-3" :label="$t('network.cdn.clear_cache_device_type')">
  24. <a-select v-model="form.clear_cache_device_type">
  25. <a-select-option v-for="item in clearCacheDeviceTypeOptions" :key="item.key" :value="item.key">
  26. {{ item.label }}
  27. </a-select-option>
  28. </a-select>
  29. </a-form-model-item>
  30. <a-form-model-item :label="$t('network.cdn.clear_cache_country')">
  31. <a-select v-model="form.clear_cache_country">
  32. <a-select-option v-for="item in COUNTRYS" :key="item.key" :value="item.key">
  33. {{ item.label }}
  34. </a-select-option>
  35. </a-select>
  36. </a-form-model-item>
  37. </a-collapse-panel>
  38. </a-collapse>
  39. </a-form-model>
  40. </div>
  41. <div slot="footer">
  42. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  43. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  44. </div>
  45. </base-dialog>
  46. </template>
  47. <script>
  48. import DialogMixin from '@/mixins/dialog'
  49. import WindowsMixin from '@/mixins/windows'
  50. import { validateModelForm } from '@/utils/validate'
  51. import { COUNTRYS } from '../constants'
  52. export default {
  53. name: 'CdnDomainClearCacheDialog',
  54. mixins: [DialogMixin, WindowsMixin],
  55. data () {
  56. return {
  57. loading: false,
  58. formItemLayout: {
  59. wrapperCol: {
  60. span: 20,
  61. },
  62. labelCol: {
  63. span: 4,
  64. },
  65. },
  66. collapseActive: false,
  67. form: {
  68. clear_method: 'all',
  69. clear_list: '',
  70. clear_cache_device_type: undefined,
  71. clear_cache_country: undefined,
  72. },
  73. clearMethodOptions: [
  74. {
  75. key: 'all',
  76. label: this.$t('network.cdn.clear_method.all'),
  77. },
  78. {
  79. key: 'url',
  80. label: this.$t('network.cdn.clear_method.url'),
  81. },
  82. {
  83. key: 'host',
  84. label: this.$t('network.cdn.clear_method.host'),
  85. },
  86. {
  87. key: 'tag',
  88. label: this.$t('network.cdn.clear_method.tag'),
  89. },
  90. {
  91. key: 'prefix',
  92. label: this.$t('network.cdn.clear_method.prefix'),
  93. },
  94. ],
  95. clearCacheDeviceTypeOptions: [
  96. {
  97. key: 'desktop',
  98. label: this.$t('network.cdn.clear_cache_device_type.desktop'),
  99. },
  100. {
  101. key: 'tablet',
  102. label: this.$t('network.cdn.clear_cache_device_type.tablet'),
  103. },
  104. {
  105. key: 'mobile',
  106. label: this.$t('network.cdn.clear_cache_device_type.mobile'),
  107. },
  108. ],
  109. COUNTRYS,
  110. rules: {
  111. clear_method: { required: true, message: this.$t('common.tips.select', [this.$t('network.cdn.clear_method')]) },
  112. clear_list: {
  113. required: true,
  114. validator: (rule, value, callback) => {
  115. if (!value) {
  116. callback(new Error(this.$t('common.tips.input', [this.clearListLabel])))
  117. return
  118. }
  119. if (this.form.clear_method === 'url') {
  120. const files = value.split('\n')
  121. if (files.some(file => !this.validateFileURL(file))) {
  122. callback(new Error(this.$t('network.cdn.clear_url_invalid')))
  123. return
  124. }
  125. callback()
  126. }
  127. },
  128. },
  129. },
  130. }
  131. },
  132. computed: {
  133. clearListLabel () {
  134. return this.$t(`network.cdn.clear_method.${this.form.clear_method}`)
  135. },
  136. },
  137. methods: {
  138. validateFileURL (fileContent) {
  139. const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/
  140. return urlRegex.test(fileContent)
  141. },
  142. handleClearMethodChange () {
  143. this.form.clear_list = ''
  144. this.collapseActive = false
  145. },
  146. doUpdate (data) {
  147. return this.params.onManager('performAction', {
  148. id: this.params.data[0].id,
  149. managerArgs: {
  150. action: 'clear-cache',
  151. data,
  152. },
  153. })
  154. },
  155. genParams () {
  156. const ret = {}
  157. const files = this.form.clear_list.split('\n')
  158. if (this.form.clear_method === 'all') {
  159. ret.purge_everything = true
  160. }
  161. if (this.form.clear_method === 'url') {
  162. if (this.collapseActive || this.form.clear_cache_device_type || this.form.clear_cache_country) {
  163. ret.files = files.map(url => {
  164. return {
  165. url,
  166. headers: {
  167. 'CF-Device-Type': this.form.clear_cache_device_type,
  168. 'CF-IPCountry': this.form.clear_cache_country,
  169. },
  170. }
  171. })
  172. } else {
  173. ret.files = files
  174. }
  175. }
  176. if (this.form.clear_method === 'host') {
  177. ret.hosts = files
  178. }
  179. if (this.form.clear_method === 'tag') {
  180. ret.tags = files
  181. }
  182. if (this.form.clear_method === 'prefix') {
  183. ret.prefixes = files
  184. }
  185. return ret
  186. },
  187. async handleConfirm () {
  188. this.loading = true
  189. try {
  190. await validateModelForm(this.$refs.form)
  191. const values = this.genParams()
  192. await this.doUpdate(values)
  193. this.loading = false
  194. this.cancelDialog()
  195. } catch (error) {
  196. this.loading = false
  197. }
  198. },
  199. },
  200. }
  201. </script>