perf: Optimize embedded compatibility for multi-domain verification

This commit is contained in:
fit2cloud-chenyw
2025-11-26 13:29:26 +08:00
committed by fit2cloud-chenyw
parent e881b3ef47
commit 0e418d2ee8
7 changed files with 29 additions and 9 deletions
+1 -1
View File
@@ -149,7 +149,7 @@ class AssistantOutDs:
if not domain_text:
return None
if ',' in domain_text:
return self.request_origin.strip('/') if self.request_origin else domain_text.split(',')[0].strip('/') + endpoint
return (self.request_origin.strip('/') if self.request_origin else domain_text.split(',')[0].strip('/')) + endpoint
else:
return f"{domain_text}{endpoint}"
+1 -1
View File
@@ -40,7 +40,7 @@ class TokenMiddleware(BaseHTTPMiddleware):
if validator[0]:
request.state.current_user = validator[1]
request.state.assistant = validator[2]
origin = request.headers.get("origin") or get_origin_from_referer(request)
origin = request.headers.get("X-SQLBOT-HOST-ORIGIN") or get_origin_from_referer(request)
if origin and validator[2]:
request.state.assistant.request_origin = origin
return await call_next(request)
+7 -7
View File
@@ -539,12 +539,15 @@
return
}
if (event.data?.busi == 'ready' && event.data?.ready) {
const certificate = parsrCertificate(data)
params = {
busi: 'certificate',
certificate,
eventName,
messageId: id,
hostOrigin: window.location.origin,
}
if (data.type === 1) {
const certificate = parsrCertificate(data)
params['busi'] = 'certificate'
params['certificate'] = certificate
}
const contentWindow = iframe.contentWindow
contentWindow.postMessage(params, url)
@@ -596,10 +599,7 @@
tempData['userFlag'] = userFlag
tempData['history'] = history
initsqlbot_assistant(tempData)
if (data.type == 1) {
registerMessageEvent(id, tempData)
// postMessage the certificate to iframe
}
registerMessageEvent(id, tempData)
})
.catch((e) => {
showMsg('嵌入失败', e.message)
+8
View File
@@ -21,6 +21,7 @@ interface AssistantState {
online: boolean
pageEmbedded?: boolean
history: boolean
hostOrigin: string
requestPromiseMap: Map<string, PendingRequest>
}
@@ -36,6 +37,7 @@ export const AssistantStore = defineStore('assistant', {
online: false,
pageEmbedded: false,
history: true,
hostOrigin: '',
requestPromiseMap: new Map<string, PendingRequest>(),
}
},
@@ -70,6 +72,9 @@ export const AssistantStore = defineStore('assistant', {
getEmbedded(): boolean {
return this.assistant && this.type === 4
},
getHostOrigin(): string {
return this.hostOrigin
},
},
actions: {
refreshCertificate<T>() {
@@ -138,6 +143,9 @@ export const AssistantStore = defineStore('assistant', {
setHistory(history: boolean) {
this.history = history ?? true
},
setHostOrigin(origin: string) {
this.hostOrigin = origin
},
async setChat() {
if (!this.assistant) {
return null
+6
View File
@@ -100,6 +100,9 @@ class HttpService {
if (!assistantStore.getType || assistantStore.getType === 2) {
config.headers['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
}
if (assistantStore.getHostOrigin) {
config.headers['X-SQLBOT-HOST-ORIGIN'] = assistantStore.getHostOrigin
}
}
const locale = getLocale()
if (locale) {
@@ -302,6 +305,9 @@ class HttpService {
encodeURIComponent(assistantStore.getCertificate)
)
}
if (assistantStore.getHostOrigin) {
heads['X-SQLBOT-HOST-ORIGIN'] = assistantStore.getHostOrigin
}
if (!assistantStore.getType || assistantStore.getType === 2) {
heads['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
}
+3
View File
@@ -76,6 +76,9 @@ const communicationCb = async (event: any) => {
assistantStore.setCertificate(certificate)
assistantStore.resolveCertificate(certificate)
}
if (event.data?.hostOrigin) {
assistantStore.setHostOrigin(event.data?.hostOrigin)
}
if (event.data?.busi == 'setOnline') {
setFormatOnline(event.data.online)
}
+3
View File
@@ -66,6 +66,9 @@ const communicationCb = async (event: any) => {
assistantStore.setCertificate(certificate)
assistantStore.resolveCertificate(certificate)
}
if (event.data?.hostOrigin) {
assistantStore.setHostOrigin(event.data?.hostOrigin)
}
if (event.data?.busi == 'setOnline') {
setFormatOnline(event.data.online)
}