Skip to content

Commit 39bd4de

Browse files
committed
feat: enhance script loading with error handling and cleanup, update routing for chat navigation
1 parent 75eab67 commit 39bd4de

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

ui/src/utils/common.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,42 +115,51 @@ interface LoadScriptOptions {
115115
}
116116

117117
export const loadScript = (url: string, options: LoadScriptOptions = {}): Promise<void> => {
118-
const {jsId, forceReload = false} = options
119-
const scriptId = jsId || `script-${btoa(url).slice(0, 12)}` // 生成唯一 ID
118+
const { jsId, forceReload = false } = options;
119+
const scriptId = jsId || `script-${btoa(url).slice(0, 12)}`;
120+
121+
const cleanupScript = (script: HTMLScriptElement) => {
122+
if (script && script.parentElement) {
123+
script.parentElement.removeChild(script);
124+
}
125+
};
120126

121127
return new Promise((resolve, reject) => {
122-
// 检查是否已存在且无需强制加载
123-
const existingScript = document.getElementById(scriptId) as HTMLScriptElement | null
128+
if (typeof document === 'undefined') {
129+
reject(new Error('Cannot load script in non-browser environment'));
130+
return;
131+
}
132+
133+
const existingScript = document.getElementById(scriptId) as HTMLScriptElement | null;
134+
124135
if (existingScript && !forceReload) {
125136
if (existingScript.src === url) {
126-
existingScript.onload = () => resolve() // 复用现有脚本
127-
return
137+
console.log(`[loadScript] Reuse existing script: ${url}`);
138+
resolve();
139+
return;
128140
}
129-
// URL 不同则移除旧脚本
130-
existingScript.parentElement?.removeChild(existingScript)
141+
existingScript.remove();
131142
}
132143

133-
// 创建新脚本
134-
const script = document.createElement('script')
135-
script.id = scriptId
136-
script.src = url
137-
script.async = true // 明确启用异步加载
144+
const script = document.createElement('script');
145+
script.id = scriptId;
146+
script.src = url;
147+
script.async = true;
138148

139-
// 成功回调
140149
script.onload = () => {
141-
resolve()
142-
}
150+
console.log(`[loadScript] Script loaded: ${url}`);
151+
resolve();
152+
};
143153

144-
// 错误处理(兼容性增强)
145154
script.onerror = () => {
146-
reject(new Error(`Failed to load script: ${url}`))
147-
cleanupScript(script)
148-
}
149-
150-
// 插入到 <head> 确保加载顺序
151-
document.head.appendChild(script)
152-
})
153-
}
155+
console.error(`[loadScript] Failed to load: ${url}`);
156+
cleanupScript(script);
157+
reject(new Error(`Failed to load script: ${url}`));
158+
};
159+
160+
document.head.appendChild(script);
161+
});
162+
};
154163

155164
// 清理脚本(可选)
156165
const cleanupScript = (script: HTMLScriptElement) => {

ui/src/views/chat/user-login/index.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ onBeforeMount(() => {
425425
}
426426
})
427427
declare const window: any
428-
onMounted(() => {
428+
onBeforeMount(() => {
429429
const route = useRoute()
430430
const currentUrl = ref(route.fullPath)
431431
const params = new URLSearchParams(currentUrl.value.split('?')[1])
@@ -437,7 +437,6 @@ onMounted(() => {
437437
dd.runtime.permission.requestAuthCode({corpId: code}).then((res) => {
438438
console.log('DingTalk client request success:', res)
439439
chatUser.dingOauth2Callback(res.code, accessToken).then(() => {
440-
router.push({name: 'home'})
441440
})
442441
})
443442
}
@@ -450,7 +449,11 @@ onMounted(() => {
450449
appId: appId,
451450
success: (res: any) => {
452451
chatUser.larkCallback(res.code, accessToken).then(() => {
453-
router.push({name: 'home'})
452+
router.push({
453+
name: 'chat',
454+
params: {accessToken: accessToken},
455+
query: route.query,
456+
})
454457
})
455458
},
456459
fail: (error: any) => {
@@ -459,18 +462,22 @@ onMounted(() => {
459462
})
460463
}
461464
462-
loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', {
465+
loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.44.js', {
463466
jsId: 'lark-sdk',
464467
forceReload: true,
465468
})
466469
.then(() => {
467470
if (window.tt) {
468-
window.tt.requestAccess({
471+
window.tt?.requestAccess({
469472
appID: appId,
470473
scopeList: [],
471474
success: (res: any) => {
472475
chatUser.larkCallback(res.code, accessToken).then(() => {
473-
router.push({name: 'home'})
476+
router.push({
477+
name: 'chat',
478+
params: {accessToken: accessToken},
479+
query: route.query,
480+
})
474481
})
475482
},
476483
fail: (error: any) => {

0 commit comments

Comments
 (0)