feat: 优化 OAuth 账号导入流程

This commit is contained in:
shaw
2026-05-08 11:36:09 +08:00
parent a466e80ed6
commit fda1ed459d
16 changed files with 1900 additions and 74 deletions

View File

@@ -2765,6 +2765,7 @@
:show-mobile-refresh-token-option="form.platform === 'openai'"
:show-session-token-option="false"
:show-access-token-option="false"
:show-codex-session-import-option="form.platform === 'openai'"
:platform="form.platform"
:show-project-id="geminiOAuthType === 'code_assist'"
@generate-url="handleGenerateUrl"
@@ -2772,6 +2773,7 @@
@validate-refresh-token="handleValidateRefreshToken"
@validate-mobile-refresh-token="handleOpenAIValidateMobileRT"
@validate-session-token="handleValidateSessionToken"
@import-codex-session="handleOpenAIImportCodexSession"
/>
</div>
@@ -3119,6 +3121,7 @@ import type {
AccountType,
CheckMixedChannelResponse,
CreateAccountRequest,
CodexSessionImportMessage,
OpenAICompactMode
} from '@/types'
import BaseDialog from '@/components/common/BaseDialog.vue'
@@ -3152,6 +3155,7 @@ interface OAuthFlowExposed {
sessionKey: string
refreshToken: string
sessionToken: string
codexSession: string
inputMethod: AuthInputMethod
reset: () => void
}
@@ -4631,6 +4635,113 @@ const handleOpenAIExchange = async (authCode: string) => {
// OpenAI Mobile RT client_id
const OPENAI_MOBILE_RT_CLIENT_ID = 'app_LlGpXReQgckcGGUo2JrYvtJK'
const buildOpenAICodexImportCredentialExtras = (): Record<string, unknown> | null => {
const credentials: Record<string, unknown> = {}
if (!isOpenAIModelRestrictionDisabled.value) {
const modelMapping = buildModelMappingObject(modelRestrictionMode.value, allowedModels.value, modelMappings.value)
if (modelMapping) {
credentials.model_mapping = modelMapping
}
}
const compactModelMapping = buildOpenAICompactModelMapping()
if (compactModelMapping) {
credentials.compact_model_mapping = compactModelMapping
}
if (!applyTempUnschedConfig(credentials)) {
return null
}
return credentials
}
const formatCodexImportMessages = (messages?: CodexSessionImportMessage[]) => {
return (messages || [])
.map((item) => {
const name = item.name ? ` ${item.name}` : ''
return `#${item.index}${name}: ${item.message}`
})
.join('\n')
}
const handleOpenAIImportCodexSession = async (content: string) => {
const oauthClient = openaiOAuth
const trimmed = content.trim()
if (!trimmed) {
oauthClient.error.value = t('admin.accounts.oauth.openai.codexSessionEmpty')
return
}
const credentialExtras = buildOpenAICodexImportCredentialExtras()
if (credentialExtras === null) {
return
}
oauthClient.loading.value = true
oauthClient.error.value = ''
try {
const extra = buildOpenAIExtra()
const result = await adminAPI.accounts.importCodexSession({
content: trimmed,
name: form.name,
notes: form.notes || null,
proxy_id: form.proxy_id,
concurrency: form.concurrency,
load_factor: form.load_factor ?? undefined,
priority: form.priority,
rate_multiplier: form.rate_multiplier,
group_ids: form.group_ids,
expires_at: form.expires_at,
auto_pause_on_expired: autoPauseOnExpired.value,
credential_extras: Object.keys(credentialExtras).length > 0 ? credentialExtras : undefined,
extra,
update_existing: true
})
const successCount = result.created + result.updated
const params = {
created: result.created,
updated: result.updated,
skipped: result.skipped,
failed: result.failed
}
if (successCount > 0 && result.failed === 0) {
appStore.showSuccess(t('admin.accounts.oauth.openai.codexSessionImportSuccess', params))
emit('created')
handleClose()
return
}
const errorText = formatCodexImportMessages(result.errors)
const warningText = formatCodexImportMessages(result.warnings)
oauthClient.error.value = [errorText, warningText].filter(Boolean).join('\n')
if (result.failed === 0) {
appStore.showWarning(t('admin.accounts.oauth.openai.codexSessionImportSuccess', params))
return
}
if (successCount > 0) {
appStore.showWarning(t('admin.accounts.oauth.openai.codexSessionImportPartial', params))
emit('created')
return
}
appStore.showError(t('admin.accounts.oauth.openai.codexSessionImportFailed'))
} catch (error: any) {
oauthClient.error.value =
error.response?.data?.detail ||
error.response?.data?.message ||
error.message ||
t('admin.accounts.oauth.openai.codexSessionImportFailed')
appStore.showError(oauthClient.error.value)
} finally {
oauthClient.loading.value = false
}
}
// OpenAI RT 批量验证和创建(共享逻辑)
const handleOpenAIBatchRT = async (refreshTokenInput: string, clientId?: string) => {
const oauthClient = openaiOAuth

View File

@@ -81,6 +81,17 @@
t('admin.accounts.oauth.openai.accessTokenAuth', '手动输入 AT')
}}</span>
</label>
<label v-if="showCodexSessionImportOption" class="flex cursor-pointer items-center gap-2">
<input
v-model="inputMethod"
type="radio"
value="codex_session"
class="text-blue-600 focus:ring-blue-500"
/>
<span class="text-sm text-blue-900 dark:text-blue-200">{{
t('admin.accounts.oauth.openai.codexSessionAuth')
}}</span>
</label>
</div>
</div>
@@ -168,6 +179,85 @@
</div>
</div>
<!-- Codex JSON / AT 批量输入 -->
<div v-if="inputMethod === 'codex_session'" class="space-y-4">
<div
class="rounded-lg border border-blue-300 bg-white/80 p-4 dark:border-blue-600 dark:bg-gray-800/80"
>
<p class="mb-3 text-sm text-blue-700 dark:text-blue-300">
{{ t('admin.accounts.oauth.openai.codexSessionDesc') }}
</p>
<div class="mb-4">
<label
class="mb-2 flex items-center gap-2 text-sm font-semibold text-gray-700 dark:text-gray-300"
>
<Icon name="key" size="sm" class="text-blue-500" />
{{ t('admin.accounts.oauth.openai.codexSessionInputLabel') }}
<span
v-if="parsedCodexSessionCount > 1"
class="rounded-full bg-blue-500 px-2 py-0.5 text-xs text-white"
>
{{ t('admin.accounts.oauth.keysCount', { count: parsedCodexSessionCount }) }}
</span>
</label>
<textarea
v-model="codexSessionInput"
rows="8"
class="input w-full resize-y font-mono text-sm"
:placeholder="t('admin.accounts.oauth.openai.codexSessionPlaceholder')"
spellcheck="false"
></textarea>
<p class="mt-1 text-xs text-blue-600 dark:text-blue-400">
{{ t('admin.accounts.oauth.openai.codexSessionHint') }}
</p>
</div>
<div
v-if="error"
class="mb-4 rounded-lg border border-red-200 bg-red-50 p-3 dark:border-red-700 dark:bg-red-900/30"
>
<p class="whitespace-pre-line text-sm text-red-600 dark:text-red-400">
{{ error }}
</p>
</div>
<button
type="button"
class="btn btn-primary w-full"
:disabled="loading || !codexSessionInput.trim()"
@click="handleImportCodexSession"
>
<svg
v-if="loading"
class="-ml-1 mr-2 h-4 w-4 animate-spin"
fill="none"
viewBox="0 0 24 24"
>
<circle
class="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
stroke-width="4"
></circle>
<path
class="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
<Icon v-else name="sparkles" size="sm" class="mr-2" />
{{
loading
? t('admin.accounts.oauth.openai.validating')
: t('admin.accounts.oauth.openai.codexSessionImportAndCreate')
}}
</button>
</div>
</div>
<!-- Cookie Auto-Auth Form -->
<div v-if="inputMethod === 'cookie'" class="space-y-4">
<div
@@ -561,6 +651,7 @@ interface Props {
showMobileRefreshTokenOption?: boolean // Whether to show mobile refresh token option (OpenAI only)
showSessionTokenOption?: boolean
showAccessTokenOption?: boolean
showCodexSessionImportOption?: boolean
platform?: AccountPlatform // Platform type for different UI/text
showProjectId?: boolean // New prop to control project ID visibility
}
@@ -579,6 +670,7 @@ const props = withDefaults(defineProps<Props>(), {
showMobileRefreshTokenOption: false,
showSessionTokenOption: false,
showAccessTokenOption: false,
showCodexSessionImportOption: false,
platform: 'anthropic',
showProjectId: true
})
@@ -591,6 +683,7 @@ const emit = defineEmits<{
'validate-mobile-refresh-token': [refreshToken: string]
'validate-session-token': [sessionToken: string]
'import-access-token': [accessToken: string]
'import-codex-session': [content: string]
'update:inputMethod': [method: AuthInputMethod]
}>()
@@ -630,12 +723,13 @@ const authCodeInput = ref('')
const sessionKeyInput = ref('')
const refreshTokenInput = ref('')
const sessionTokenInput = ref('')
const codexSessionInput = ref('')
const showHelpDialog = ref(false)
const oauthState = ref('')
const projectId = ref('')
// Computed: show method selection when either cookie or refresh token option is enabled
const showMethodSelection = computed(() => props.showCookieOption || props.showRefreshTokenOption || props.showMobileRefreshTokenOption || props.showSessionTokenOption || props.showAccessTokenOption)
const showMethodSelection = computed(() => props.showCookieOption || props.showRefreshTokenOption || props.showMobileRefreshTokenOption || props.showSessionTokenOption || props.showAccessTokenOption || props.showCodexSessionImportOption)
// Clipboard
const { copied, copyToClipboard } = useClipboard()
@@ -656,6 +750,16 @@ const parsedRefreshTokenCount = computed(() => {
.filter((rt) => rt).length
})
const parsedCodexSessionCount = computed(() => {
const trimmed = codexSessionInput.value.trim()
if (!trimmed) return 0
if (trimmed.startsWith('{') || trimmed.startsWith('[')) return 1
return trimmed
.split('\n')
.map((item) => item.trim())
.filter((item) => item).length
})
// Watchers
watch(inputMethod, (newVal) => {
emit('update:inputMethod', newVal)
@@ -727,6 +831,12 @@ const handleValidateRefreshToken = () => {
}
}
const handleImportCodexSession = () => {
if (codexSessionInput.value.trim()) {
emit('import-codex-session', codexSessionInput.value.trim())
}
}
// Expose methods and state
defineExpose({
authCode: authCodeInput,
@@ -735,6 +845,7 @@ defineExpose({
sessionKey: sessionKeyInput,
refreshToken: refreshTokenInput,
sessionToken: sessionTokenInput,
codexSession: codexSessionInput,
inputMethod,
reset: () => {
authCodeInput.value = ''
@@ -743,6 +854,7 @@ defineExpose({
sessionKeyInput.value = ''
refreshTokenInput.value = ''
sessionTokenInput.value = ''
codexSessionInput.value = ''
inputMethod.value = 'manual'
showHelpDialog.value = false
}

View File

@@ -5,7 +5,6 @@
<Icon name="refresh" size="md" :class="[loading ? 'animate-spin' : '']" />
</button>
<slot name="after"></slot>
<button @click="$emit('sync')" class="btn btn-secondary">{{ t('admin.accounts.syncFromCrs') }}</button>
<slot name="beforeCreate"></slot>
<button @click="$emit('create')" class="btn btn-primary">{{ t('admin.accounts.createAccount') }}</button>
<slot name="afterCreate"></slot>
@@ -17,7 +16,7 @@ import { useI18n } from 'vue-i18n'
import Icon from '@/components/icons/Icon.vue'
defineProps(['loading'])
defineEmits(['refresh', 'sync', 'create'])
defineEmits(['refresh', 'create'])
const { t } = useI18n()
</script>