UnisKB/ui/src/views/authentication/component/EditModal.vue

171 lines
4.5 KiB
Vue
Raw Normal View History

2024-10-09 02:59:45 +00:00
template
<template>
2024-10-28 11:07:59 +00:00
<el-drawer v-model="visible" size="60%" :append-to-body="true" :destroy-on-close="true">
2024-10-09 02:59:45 +00:00
<template #header>
<div class="flex align-center" style="margin-left: -8px">
<h4>{{ currentPlatform.name + '设置' }}</h4>
</div>
</template>
<el-form
:model="currentPlatform.config"
label-width="120px"
label-position="top"
require-asterisk-position="right"
ref="formRef"
>
<el-form-item
v-for="(value, key) in currentPlatform.config"
:key="key"
:label="formatFieldName(key)"
:prop="key"
:rules="getValidationRules(key)"
>
2024-10-25 10:06:18 +00:00
<el-input
v-model="currentPlatform.config[key]"
:type="isPasswordField(key) ? 'password' : 'text'"
:show-password="isPasswordField(key)"
>
</el-input>
2024-10-09 02:59:45 +00:00
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="visible = false"> </el-button>
<el-button @click="validateConnection"> </el-button>
<el-button type="primary" @click="validateForm"> </el-button>
</span>
</template>
</el-drawer>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElForm } from 'element-plus'
import platformApi from '@/api/platform-source'
import { MsgError, MsgSuccess } from '@/utils/message'
const visible = ref(false)
const loading = ref(false)
const formRef = ref<InstanceType<typeof ElForm>>()
interface PlatformConfig {
[key: string]: string
}
interface Platform {
key: string
logoSrc: string
name: string
isActive: boolean
isValid: boolean
config: PlatformConfig
}
const currentPlatform = reactive<Platform>({
key: '',
logoSrc: '',
name: '',
isActive: false,
isValid: false,
config: {}
})
2024-10-14 10:49:33 +00:00
const formatFieldName = (key?: any): string => {
2024-10-09 02:59:45 +00:00
const fieldNames: { [key: string]: string } = {
app_key: 'APP Key',
app_secret: 'APP Secret',
corp_id: 'Corp ID',
agent_id: 'Agent ID',
callback_url: '回调地址'
}
return (
fieldNames[key as keyof typeof fieldNames] ||
(key ? key.charAt(0).toUpperCase() + key.slice(1) : '')
)
}
2024-10-14 10:49:33 +00:00
const getValidationRules = (key: any) => {
2024-10-09 02:59:45 +00:00
switch (key) {
case 'app_key':
return [{ required: true, message: '请输入 APP Key', trigger: ['blur', 'change'] }]
case 'app_secret':
return [{ required: true, message: '请输入 APP Secret', trigger: ['blur', 'change'] }]
case 'corp_id':
return [{ required: true, message: '请输入 Corp ID', trigger: ['blur', 'change'] }]
case 'agent_id':
return [{ required: true, message: '请输入 Agent ID', trigger: ['blur', 'change'] }]
case 'callback_url':
return [
{ required: true, message: '请输入回调地址', trigger: ['blur', 'change'] },
{ pattern: /^https?:\/\/.+/, message: '请输入有效的 URL 地址', trigger: ['blur', 'change'] }
]
default:
return []
}
}
const open = async (platform: Platform) => {
visible.value = true
loading.value = true
Object.assign(currentPlatform, platform)
// 设置默认的 callback_url
let defaultCallbackUrl = window.location.origin
switch (platform.key) {
case 'wecom':
case 'dingtalk':
2024-10-28 11:07:59 +00:00
if (currentPlatform.config.agent_id) {
currentPlatform.config.corp_id = currentPlatform.config.agent_id
delete currentPlatform.config.agent_id
}
2024-10-09 02:59:45 +00:00
currentPlatform.config.callback_url = defaultCallbackUrl
break
case 'lark':
currentPlatform.config.callback_url = `${defaultCallbackUrl}/api/feishu`
break
default:
break
}
2024-10-24 07:32:43 +00:00
formRef.value?.clearValidate()
2024-10-09 02:59:45 +00:00
}
defineExpose({ open })
const validateForm = () => {
formRef.value?.validate((valid) => {
if (valid) {
saveConfig()
} else {
MsgError('请填写所有必填项并确保格式正确')
}
})
}
function validateConnection() {
platformApi.validateConnection(currentPlatform, loading).then((res: any) => {
if (res.data) {
MsgSuccess('校验成功')
} else {
MsgError('校验失败')
}
})
}
2024-10-25 10:06:18 +00:00
const passwordFields = new Set(['app_secret', 'client_secret', 'secret'])
const isPasswordField = (key: any) => passwordFields.has(key)
2024-10-28 02:54:09 +00:00
const emit = defineEmits(['refresh'])
2024-10-09 02:59:45 +00:00
function saveConfig() {
platformApi.updateConfig(currentPlatform, loading).then((res: any) => {
MsgSuccess('保存成功')
2024-10-28 02:54:09 +00:00
emit('refresh')
2024-10-09 02:59:45 +00:00
visible.value = false
2024-10-24 07:32:43 +00:00
formRef.value?.clearValidate()
2024-10-09 02:59:45 +00:00
})
}
</script>
<style lang="scss" scoped>
// 保持原有的样式
</style>