|
| 1 | +template |
| 2 | +<template> |
| 3 | + <el-drawer |
| 4 | + v-model="visible" |
| 5 | + size="60%" |
| 6 | + :append-to-body="true" |
| 7 | + :destroy-on-close="true" |
| 8 | + @close="handleClose" |
| 9 | + > |
| 10 | + <template #header> |
| 11 | + <div class="flex align-center" style="margin-left: -8px"> |
| 12 | + <h4> |
| 13 | + {{ currentPlatform.name + $t('views.system.authentication.scanTheQRCode.setting') }} |
| 14 | + </h4> |
| 15 | + </div> |
| 16 | + </template> |
| 17 | + |
| 18 | + <el-form |
| 19 | + :model="currentPlatform.config" |
| 20 | + label-width="120px" |
| 21 | + label-position="top" |
| 22 | + require-asterisk-position="right" |
| 23 | + ref="formRef" |
| 24 | + > |
| 25 | + <el-form-item |
| 26 | + v-for="(value, key) in currentPlatform.config" |
| 27 | + :key="key" |
| 28 | + :label="formatFieldName(key)" |
| 29 | + :prop="key" |
| 30 | + :rules="getValidationRules(key)" |
| 31 | + > |
| 32 | + <el-input |
| 33 | + v-model="currentPlatform.config[key]" |
| 34 | + :type="isPasswordField(key) ? 'password' : 'text'" |
| 35 | + :show-password="isPasswordField(key)" |
| 36 | + > |
| 37 | + </el-input> |
| 38 | + </el-form-item> |
| 39 | + </el-form> |
| 40 | + <template #footer> |
| 41 | + <span class="dialog-footer"> |
| 42 | + <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button> |
| 43 | + <el-button @click="validateConnection">{{ |
| 44 | + $t('views.system.authentication.scanTheQRCode.validate') |
| 45 | + }}</el-button> |
| 46 | + <el-button type="primary" @click="validateForm">{{ $t('common.save') }}</el-button> |
| 47 | + </span> |
| 48 | + </template> |
| 49 | + </el-drawer> |
| 50 | +</template> |
| 51 | + |
| 52 | +<script setup lang="ts"> |
| 53 | +import { reactive, ref } from 'vue' |
| 54 | +import { ElForm } from 'element-plus' |
| 55 | +import platformApi from '@/api/system-settings/platform-source' |
| 56 | +import { MsgError, MsgSuccess } from '@/utils/message' |
| 57 | +import { t } from '@/locales' |
| 58 | +
|
| 59 | +const visible = ref(false) |
| 60 | +const loading = ref(false) |
| 61 | +const formRef = ref<InstanceType<typeof ElForm>>() |
| 62 | +
|
| 63 | +interface PlatformConfig { |
| 64 | + [key: string]: string |
| 65 | +} |
| 66 | +
|
| 67 | +interface Platform { |
| 68 | + key: string |
| 69 | + logoSrc: string |
| 70 | + name: string |
| 71 | + isActive: boolean |
| 72 | + isValid: boolean |
| 73 | + config: PlatformConfig |
| 74 | +} |
| 75 | +
|
| 76 | +const currentPlatform = reactive<Platform>({ |
| 77 | + key: '', |
| 78 | + logoSrc: '', |
| 79 | + name: '', |
| 80 | + isActive: false, |
| 81 | + isValid: false, |
| 82 | + config: {} |
| 83 | +}) |
| 84 | +
|
| 85 | +const formatFieldName = (key?: any): string => { |
| 86 | + const fieldNames: { [key: string]: string } = { |
| 87 | + corp_id: 'Corp ID', |
| 88 | + app_key: currentPlatform?.key != 'lark' ? 'APP Key' : 'App ID', |
| 89 | + app_secret: 'APP Secret', |
| 90 | + agent_id: 'Agent ID', |
| 91 | + callback_url: t('views.application.applicationAccess.callback') |
| 92 | + } |
| 93 | + return ( |
| 94 | + fieldNames[key as keyof typeof fieldNames] || |
| 95 | + (key ? key.charAt(0).toUpperCase() + key.slice(1) : '') |
| 96 | + ) |
| 97 | +} |
| 98 | +
|
| 99 | +const getValidationRules = (key: any) => { |
| 100 | + switch (key) { |
| 101 | + case 'app_key': |
| 102 | + return [ |
| 103 | + { |
| 104 | + required: true, |
| 105 | + message: t('views.system.authentication.scanTheQRCode.appKeyPlaceholder'), |
| 106 | + trigger: ['blur', 'change'] |
| 107 | + } |
| 108 | + ] |
| 109 | + case 'app_secret': |
| 110 | + return [ |
| 111 | + { |
| 112 | + required: true, |
| 113 | + message: t('views.system.authentication.scanTheQRCode.appSecretPlaceholder'), |
| 114 | + trigger: ['blur', 'change'] |
| 115 | + } |
| 116 | + ] |
| 117 | + case 'corp_id': |
| 118 | + return [ |
| 119 | + { |
| 120 | + required: true, |
| 121 | + message: t('views.system.authentication.scanTheQRCode.corpIdPlaceholder'), |
| 122 | + trigger: ['blur', 'change'] |
| 123 | + } |
| 124 | + ] |
| 125 | + case 'agent_id': |
| 126 | + return [ |
| 127 | + { |
| 128 | + required: true, |
| 129 | + message: t('views.system.authentication.scanTheQRCode.agentIdPlaceholder'), |
| 130 | + trigger: ['blur', 'change'] |
| 131 | + } |
| 132 | + ] |
| 133 | + case 'callback_url': |
| 134 | + return [ |
| 135 | + { |
| 136 | + required: true, |
| 137 | + message: t('views.application.applicationAccess.callbackTip'), |
| 138 | + trigger: ['blur', 'change'] |
| 139 | + }, |
| 140 | + { |
| 141 | + pattern: /^https?:\/\/.+/, |
| 142 | + message: t('views.system.authentication.scanTheQRCode.callbackWarning'), |
| 143 | + trigger: ['blur', 'change'] |
| 144 | + } |
| 145 | + ] |
| 146 | + default: |
| 147 | + return [] |
| 148 | + } |
| 149 | +} |
| 150 | +
|
| 151 | +const open = async (platform: Platform) => { |
| 152 | + visible.value = true |
| 153 | + loading.value = true |
| 154 | + Object.assign(currentPlatform, platform) |
| 155 | +
|
| 156 | + // 设置默认的 callback_url |
| 157 | + const defaultCallbackUrl = window.location.origin |
| 158 | + switch (platform.key) { |
| 159 | + case 'wecom': |
| 160 | + if (currentPlatform.config.app_key) { |
| 161 | + currentPlatform.config.agent_id = currentPlatform.config.app_key |
| 162 | + delete currentPlatform.config.app_key |
| 163 | + } |
| 164 | + currentPlatform.config.callback_url = `${defaultCallbackUrl}/api/wecom` |
| 165 | + break |
| 166 | + case 'dingtalk': |
| 167 | + if (currentPlatform.config.agent_id) { |
| 168 | + currentPlatform.config.corp_id = currentPlatform.config.agent_id |
| 169 | + delete currentPlatform.config.agent_id |
| 170 | + } |
| 171 | + currentPlatform.config = { |
| 172 | + corp_id: currentPlatform.config.corp_id, |
| 173 | + app_key: currentPlatform.config.app_key, |
| 174 | + app_secret: currentPlatform.config.app_secret, |
| 175 | + callback_url: defaultCallbackUrl |
| 176 | + } |
| 177 | + currentPlatform.config.callback_url = `${defaultCallbackUrl}/api/dingtalk` |
| 178 | + break |
| 179 | + case 'lark': |
| 180 | + currentPlatform.config.callback_url = `${defaultCallbackUrl}/api/feishu` |
| 181 | + break |
| 182 | + default: |
| 183 | + break |
| 184 | + } |
| 185 | + formRef.value?.clearValidate() |
| 186 | +} |
| 187 | +defineExpose({ open }) |
| 188 | +
|
| 189 | +const validateForm = () => { |
| 190 | + formRef.value?.validate((valid) => { |
| 191 | + if (valid) { |
| 192 | + saveConfig() |
| 193 | + } else { |
| 194 | + MsgError(t('views.system.authentication.scanTheQRCode.validateFailedTip')) |
| 195 | + } |
| 196 | + }) |
| 197 | +} |
| 198 | +
|
| 199 | +const handleClose = () => { |
| 200 | + visible.value = false |
| 201 | + formRef.value?.clearValidate() |
| 202 | + emit('refresh') |
| 203 | +} |
| 204 | +
|
| 205 | +function validateConnection() { |
| 206 | + platformApi.validateConnection(currentPlatform, loading).then((res: any) => { |
| 207 | + if (res.data) { |
| 208 | + MsgSuccess(t('views.system.authentication.scanTheQRCode.validateSuccess')) |
| 209 | + } else { |
| 210 | + MsgError(t('views.system.authentication.scanTheQRCode.validateFailed')) |
| 211 | + } |
| 212 | + }) |
| 213 | +} |
| 214 | +
|
| 215 | +const passwordFields = new Set(['app_secret', 'client_secret', 'secret']) |
| 216 | +
|
| 217 | +const isPasswordField = (key: any) => passwordFields.has(key) |
| 218 | +const emit = defineEmits(['refresh']) |
| 219 | +
|
| 220 | +function saveConfig() { |
| 221 | + platformApi.updateConfig(currentPlatform, loading).then((res: any) => { |
| 222 | + MsgSuccess(t('common.saveSuccess')) |
| 223 | + emit('refresh') |
| 224 | + visible.value = false |
| 225 | + formRef.value?.clearValidate() |
| 226 | + }) |
| 227 | +} |
| 228 | +</script> |
| 229 | + |
| 230 | +<style lang="scss" scoped></style> |
0 commit comments