Skip to content

Commit 0e418d2

Browse files
perf: Optimize embedded compatibility for multi-domain verification
1 parent e881b3e commit 0e418d2

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

backend/apps/system/crud/assistant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def get_complete_endpoint(self, endpoint: str) -> str | None:
149149
if not domain_text:
150150
return None
151151
if ',' in domain_text:
152-
return self.request_origin.strip('/') if self.request_origin else domain_text.split(',')[0].strip('/') + endpoint
152+
return (self.request_origin.strip('/') if self.request_origin else domain_text.split(',')[0].strip('/')) + endpoint
153153
else:
154154
return f"{domain_text}{endpoint}"
155155

backend/apps/system/middleware/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def dispatch(self, request, call_next):
4040
if validator[0]:
4141
request.state.current_user = validator[1]
4242
request.state.assistant = validator[2]
43-
origin = request.headers.get("origin") or get_origin_from_referer(request)
43+
origin = request.headers.get("X-SQLBOT-HOST-ORIGIN") or get_origin_from_referer(request)
4444
if origin and validator[2]:
4545
request.state.assistant.request_origin = origin
4646
return await call_next(request)

frontend/public/assistant.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,15 @@
539539
return
540540
}
541541
if (event.data?.busi == 'ready' && event.data?.ready) {
542-
const certificate = parsrCertificate(data)
543542
params = {
544-
busi: 'certificate',
545-
certificate,
546543
eventName,
547544
messageId: id,
545+
hostOrigin: window.location.origin,
546+
}
547+
if (data.type === 1) {
548+
const certificate = parsrCertificate(data)
549+
params['busi'] = 'certificate'
550+
params['certificate'] = certificate
548551
}
549552
const contentWindow = iframe.contentWindow
550553
contentWindow.postMessage(params, url)
@@ -596,10 +599,7 @@
596599
tempData['userFlag'] = userFlag
597600
tempData['history'] = history
598601
initsqlbot_assistant(tempData)
599-
if (data.type == 1) {
600-
registerMessageEvent(id, tempData)
601-
// postMessage the certificate to iframe
602-
}
602+
registerMessageEvent(id, tempData)
603603
})
604604
.catch((e) => {
605605
showMsg('嵌入失败', e.message)

frontend/src/stores/assistant.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface AssistantState {
2121
online: boolean
2222
pageEmbedded?: boolean
2323
history: boolean
24+
hostOrigin: string
2425
requestPromiseMap: Map<string, PendingRequest>
2526
}
2627

@@ -36,6 +37,7 @@ export const AssistantStore = defineStore('assistant', {
3637
online: false,
3738
pageEmbedded: false,
3839
history: true,
40+
hostOrigin: '',
3941
requestPromiseMap: new Map<string, PendingRequest>(),
4042
}
4143
},
@@ -70,6 +72,9 @@ export const AssistantStore = defineStore('assistant', {
7072
getEmbedded(): boolean {
7173
return this.assistant && this.type === 4
7274
},
75+
getHostOrigin(): string {
76+
return this.hostOrigin
77+
},
7378
},
7479
actions: {
7580
refreshCertificate<T>() {
@@ -138,6 +143,9 @@ export const AssistantStore = defineStore('assistant', {
138143
setHistory(history: boolean) {
139144
this.history = history ?? true
140145
},
146+
setHostOrigin(origin: string) {
147+
this.hostOrigin = origin
148+
},
141149
async setChat() {
142150
if (!this.assistant) {
143151
return null

frontend/src/utils/request.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class HttpService {
100100
if (!assistantStore.getType || assistantStore.getType === 2) {
101101
config.headers['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
102102
}
103+
if (assistantStore.getHostOrigin) {
104+
config.headers['X-SQLBOT-HOST-ORIGIN'] = assistantStore.getHostOrigin
105+
}
103106
}
104107
const locale = getLocale()
105108
if (locale) {
@@ -302,6 +305,9 @@ class HttpService {
302305
encodeURIComponent(assistantStore.getCertificate)
303306
)
304307
}
308+
if (assistantStore.getHostOrigin) {
309+
heads['X-SQLBOT-HOST-ORIGIN'] = assistantStore.getHostOrigin
310+
}
305311
if (!assistantStore.getType || assistantStore.getType === 2) {
306312
heads['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
307313
}

frontend/src/views/embedded/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const communicationCb = async (event: any) => {
7676
assistantStore.setCertificate(certificate)
7777
assistantStore.resolveCertificate(certificate)
7878
}
79+
if (event.data?.hostOrigin) {
80+
assistantStore.setHostOrigin(event.data?.hostOrigin)
81+
}
7982
if (event.data?.busi == 'setOnline') {
8083
setFormatOnline(event.data.online)
8184
}

frontend/src/views/embedded/page.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ const communicationCb = async (event: any) => {
6666
assistantStore.setCertificate(certificate)
6767
assistantStore.resolveCertificate(certificate)
6868
}
69+
if (event.data?.hostOrigin) {
70+
assistantStore.setHostOrigin(event.data?.hostOrigin)
71+
}
6972
if (event.data?.busi == 'setOnline') {
7073
setFormatOnline(event.data.online)
7174
}

0 commit comments

Comments
 (0)