Skip to content

Commit

Permalink
feat: 存储管理S3存储配置填充默认域名 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskils authored May 23, 2024
1 parent 56b1fdd commit 5897bde
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
6 changes: 6 additions & 0 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ export const isExternal = (path: string) => {
export function isHttp(url: string) {
return url.includes('http://') || url.includes('https://')
}

/** 判断 字符串 是否是 ipv4 */
export function isIPv4(ip: string): boolean {
const ipv4Pattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
return ipv4Pattern.test(ip)
}
90 changes: 57 additions & 33 deletions src/views/system/storage/StorageAddModal.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<a-drawer
v-model:visible="visible"
:title="title"
:mask-closable="false"
:esc-to-close="false"
:width="width >= 600 ? 600 : '100%'"
@before-ok="save"
@close="reset"
v-model:visible="visible"
:title="title"
:mask-closable="false"
:esc-to-close="false"
:width="width >= 600 ? 600 : '100%'"
@before-ok="save"
@close="reset"
>
<a-form ref="formRef" :model="form" :rules="rules" size="large" auto-label-width>
<a-form-item label="名称" field="name">
Expand All @@ -23,10 +23,9 @@
</a-form-item>
<a-form-item v-if="form.type === 1" label="私有密钥" field="secretKey">
<a-input
v-model.trim="form.secretKey"
placeholder="请输入私有密钥"
:max-length="255"
@input="updateSecretKey"
v-model.trim="form.secretKey"
placeholder="请输入私有密钥"
:max-length="255"
/>
</a-form-item>
<a-form-item v-if="form.type === 1" label="终端节点" field="endpoint">
Expand All @@ -37,12 +36,17 @@
</a-form-item>
<a-form-item v-if="form.type === 1" label="域名" field="domain">
<a-input v-model.trim="form.domain" placeholder="请输入域名" />
<template #extra>
<div v-if="defaultDomain">
<span>留空默认域名:{{ defaultDomain }}</span>
</div>
</template>
</a-form-item>
<a-form-item
v-if="form.type === 2"
label="域名"
field="domain"
:rules="[
v-if="form.type === 2"
label="域名"
field="domain"
:rules="[
{
required: true,
message: '请输入域名',
Expand All @@ -56,31 +60,31 @@
</a-form-item>
<a-form-item label="描述" field="description">
<a-textarea
v-model.trim="form.description"
placeholder="请输入描述"
show-word-limit
:max-length="200"
:auto-size="{ minRows: 3, maxRows: 5 }"
v-model.trim="form.description"
placeholder="请输入描述"
show-word-limit
:max-length="200"
:auto-size="{ minRows: 3, maxRows: 5 }"
/>
</a-form-item>
<a-form-item label="默认存储" field="isDefault">
<a-switch
v-model="form.isDefault"
type="round"
:checked-value="true"
:unchecked-value="false"
checked-text=""
unchecked-text=""
v-model="form.isDefault"
type="round"
:checked-value="true"
:unchecked-value="false"
checked-text=""
unchecked-text=""
/>
</a-form-item>
<a-form-item label="状态" field="status">
<a-switch
v-model="form.status"
type="round"
:checked-value="1"
:unchecked-value="2"
checked-text="启用"
unchecked-text="禁用"
v-model="form.status"
type="round"
:checked-value="1"
:unchecked-value="2"
checked-text="启用"
unchecked-text="禁用"
/>
</a-form-item>
</a-form>
Expand All @@ -94,6 +98,7 @@ import { addStorage, getStorage, updateStorage } from '@/apis'
import { useForm } from '@/hooks'
import { useDict } from '@/hooks/app'
import { encryptByRsa } from '@/utils/encrypt'
import { isIPv4 } from '@/utils/validate'
const emit = defineEmits<{
(e: 'save-success'): void
Expand Down Expand Up @@ -122,6 +127,24 @@ const { form, resetForm } = useForm({
sort: 999,
status: 1
})
/** 获取url的protocol和endpoint */
const stripProtocol = (url: string): { endpoint: string, protocol: string } => {
if (url.startsWith('http://')) {
return { endpoint: url.substring(7), protocol: 'http://' }
} else if (url.startsWith('https://')) {
return { endpoint: url.substring(8), protocol: 'https://' }
}
return { endpoint: url, protocol: 'http://' }
}
/** 按规则拼接当前默认domain */
const defaultDomain = computed(() => {
const { endpoint: initialEndpoint, bucketName, domain, type } = form
if (domain || type !== 1 || !initialEndpoint || !bucketName) {
return
}
const { endpoint, protocol } = stripProtocol(initialEndpoint)
return isIPv4(endpoint) ? `${protocol}${endpoint}/${bucketName}/` : `${protocol}${bucketName}.${endpoint}/`
})
// 重置
const reset = () => {
Expand Down Expand Up @@ -153,7 +176,8 @@ const save = async () => {
if (isInvalid) return false
const data = {
...form,
secretKey: form.type === 1 && !form.secretKey.includes('*') ? encryptByRsa(form.secretKey) : null
secretKey: form.type === 1 && !form.secretKey.includes('*') ? encryptByRsa(form.secretKey) : null,
domain: form.domain || defaultDomain.value
}
if (isUpdate.value) {
await updateStorage(data, dataId.value)
Expand Down

0 comments on commit 5897bde

Please sign in to comment.