Skip to content

Commit 61e1c64

Browse files
perf: request key
1 parent 19ef65a commit 61e1c64

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

frontend/src/i18n/en.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"not_exceed_50mb": "Supports XLS, XLSX, CSV formats, file size does not exceed 50MB",
4747
"reset_password": "Reset password",
4848
"password_reset_successful": "Password reset successful",
49-
"or": "Or"
49+
"or": "Or",
50+
"refresh": "Refresh"
5051
},
5152
"dashboard": {
5253
"open_dashboard": "Open Dashboard",
@@ -562,5 +563,9 @@
562563
"remark": "Remark",
563564
"back_community": "Restore to Community Edition",
564565
"confirm_tips": "Are you sure to restore to Community Edition?"
566+
},
567+
"license": {
568+
"error_tips": "Do you want to refresh the page?",
569+
"offline_tips": "The service has been offline, please contact the administrator to restart the service!"
565570
}
566-
}
571+
}

frontend/src/i18n/zh-CN.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"not_exceed_50mb": "支持 XLS、XLSX、CSV 格式,文件大小不超过 50MB",
4747
"reset_password": "重置密码",
4848
"password_reset_successful": "重置密码成功",
49-
"or": "或者"
49+
"or": "或者",
50+
"refresh": "刷新"
5051
},
5152
"dashboard": {
5253
"open_dashboard": "打开仪表板",
@@ -562,5 +563,9 @@
562563
"remark": "备注",
563564
"back_community": "还原至社区版",
564565
"confirm_tips": "确定还原至社区版?"
566+
},
567+
"license": {
568+
"error_tips": "是否刷新页面?",
569+
"offline_tips": "服务已经下线,请联系管理员重启服务!"
565570
}
566-
}
571+
}

frontend/src/utils/request.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import axios, {
1111
import { useCache } from '@/utils/useCache'
1212
import { getLocale } from './utils'
1313
import { useAssistantStore } from '@/stores/assistant'
14+
import { i18n } from '@/i18n'
15+
const t = i18n.global.t
1416
const assistantStore = useAssistantStore()
1517
const { wsCache } = useCache()
1618
// Response data structure
@@ -63,6 +65,11 @@ class HttpService {
6365
this.setupInterceptors()
6466
}
6567

68+
private cancelCurrentRequest(message: string) {
69+
this.cancelTokenSource.cancel(message)
70+
this.cancelTokenSource = axios.CancelToken.source()
71+
}
72+
6673
private setupInterceptors() {
6774
// Request interceptor
6875
this.instance.interceptors.request.use(
@@ -100,8 +107,15 @@ class HttpService {
100107
return config
101108
}
102109

103-
const request_key = LicenseGenerator.generate()
104-
config.headers['X-SQLBOT-KEY'] = request_key
110+
try {
111+
const request_key = LicenseGenerator.generate()
112+
config.headers['X-SQLBOT-KEY'] = request_key
113+
} catch (e: any) {
114+
if (e?.message?.includes('offline')) {
115+
this.cancelCurrentRequest('license-key error detected')
116+
showLicenseKeyError()
117+
}
118+
}
105119

106120
// Request logging
107121
// console.log(`[Request] ${config.method?.toUpperCase()} ${config.url}`)
@@ -211,6 +225,9 @@ class HttpService {
211225

212226
// Show error using UI library (e.g., Element Plus, Ant Design)
213227
console.error(errorMessage)
228+
/* if (errorMessage?.includes('Invalid license key salt')) {
229+
showLicenseKeyError()
230+
} */
214231
// ElMessage.error(errorMessage)
215232
ElMessage({
216233
message: errorMessage,
@@ -265,8 +282,16 @@ class HttpService {
265282
heads['X-SQLBOT-ASSISTANT-ONLINE'] = assistantStore.getOnline
266283
}
267284
}
268-
const request_key = LicenseGenerator.generate()
269-
heads['X-SQLBOT-KEY'] = request_key
285+
286+
try {
287+
const request_key = LicenseGenerator.generate()
288+
heads['X-SQLBOT-KEY'] = request_key
289+
} catch (e: any) {
290+
if (e?.message?.includes('offline')) {
291+
controller?.abort('license-key error detected')
292+
showLicenseKeyError()
293+
}
294+
}
270295

271296
const real_url = import.meta.env.VITE_API_BASE_URL
272297
return fetch(real_url + url, {
@@ -387,3 +412,19 @@ class HttpService {
387412
export const request = new HttpService({
388413
baseURL: import.meta.env.VITE_API_BASE_URL,
389414
})
415+
416+
const showLicenseKeyError = (msg?: string) => {
417+
ElMessageBox.confirm(t('license.error_tips'), {
418+
confirmButtonType: 'primary',
419+
tip: msg || t('license.offline_tips'),
420+
confirmButtonText: t('common.refresh'),
421+
cancelButtonText: t('common.cancel'),
422+
customClass: 'confirm-no_icon',
423+
autofocus: false,
424+
callback: (value: string) => {
425+
if (value === 'confirm') {
426+
window.location.reload()
427+
}
428+
},
429+
})
430+
}

0 commit comments

Comments
 (0)