fix: 在调用 tt.requestAuthCode 前先调用 h5sdk.config
- 预先获取 appId 并配置 h5sdk - 避免 'invalid url' 错误
This commit is contained in:
@@ -76,23 +76,52 @@ export default function Login() {
|
|||||||
|
|
||||||
// 飞书内免登
|
// 飞书内免登
|
||||||
const attemptFeishuAutoLogin = async (companyParam?: string) => {
|
const attemptFeishuAutoLogin = async (companyParam?: string) => {
|
||||||
// 如果 h5sdk 存在,使用 h5sdk.ready() 等待环境就绪
|
// 先获取 appId
|
||||||
if (window.h5sdk?.ready) {
|
let appId: string | undefined | null
|
||||||
window.h5sdk.ready(() => {
|
const urlParams = new URLSearchParams(window.location.search)
|
||||||
console.log('h5sdk ready, tt对象:', !!window.tt)
|
appId = urlParams.get('app_id')
|
||||||
if (window.tt) {
|
|
||||||
performFeishuLogin(companyParam)
|
if (!appId && companyParam) {
|
||||||
} else {
|
try {
|
||||||
console.error('h5sdk ready 后 tt 对象仍不存在')
|
const response = await fetch(`/api/v1/auth/feishu-config?company=${companyParam}`)
|
||||||
setLoading(false)
|
const result = await response.json()
|
||||||
|
if (result.code === 0 && result.data.app_id) {
|
||||||
|
appId = result.data.app_id
|
||||||
}
|
}
|
||||||
})
|
} catch (e) {
|
||||||
|
console.error('获取 appId 失败:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!appId) {
|
||||||
|
setLoading(false)
|
||||||
|
Toast.show('飞书应用配置错误')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果 h5sdk 存在,先配置再等待 ready
|
||||||
|
if (window.h5sdk?.config) {
|
||||||
|
// 配置 h5sdk
|
||||||
|
window.h5sdk.config({
|
||||||
|
appId: appId,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
nonceStr: Math.random().toString(36).substring(2),
|
||||||
|
signature: '', // 网页应用模式下不需要签名
|
||||||
|
})
|
||||||
|
|
||||||
|
// 等待环境就绪
|
||||||
|
if (window.h5sdk.ready) {
|
||||||
|
window.h5sdk.ready(() => {
|
||||||
|
console.log('h5sdk ready, 调用免登')
|
||||||
|
performFeishuLogin(companyParam, appId)
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 如果 tt 对象已存在,直接使用
|
// 如果 tt 对象已存在,直接使用
|
||||||
if (window.tt) {
|
if (window.tt) {
|
||||||
performFeishuLogin(companyParam)
|
performFeishuLogin(companyParam, appId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +133,7 @@ export default function Login() {
|
|||||||
attempts++
|
attempts++
|
||||||
if (window.tt) {
|
if (window.tt) {
|
||||||
clearInterval(checkInterval)
|
clearInterval(checkInterval)
|
||||||
performFeishuLogin(companyParam)
|
performFeishuLogin(companyParam, appId)
|
||||||
} else if (attempts >= maxAttempts) {
|
} else if (attempts >= maxAttempts) {
|
||||||
clearInterval(checkInterval)
|
clearInterval(checkInterval)
|
||||||
console.error('等待 tt 对象超时')
|
console.error('等待 tt 对象超时')
|
||||||
@@ -114,37 +143,20 @@ export default function Login() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行飞书免登
|
// 执行飞书免登
|
||||||
const performFeishuLogin = async (companyParam?: string) => {
|
const performFeishuLogin = async (companyParam?: string, appId?: string | null) => {
|
||||||
if (!window.tt) {
|
if (!window.tt) {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
Toast.show('飞书环境异常,请刷新重试')
|
Toast.show('飞书环境异常,请刷新重试')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!appId) {
|
||||||
|
setLoading(false)
|
||||||
|
Toast.show('飞书应用配置错误')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取 appId
|
|
||||||
let appId: string | undefined | null
|
|
||||||
|
|
||||||
// 优先从 URL 参数获取
|
|
||||||
const urlParams = new URLSearchParams(window.location.search)
|
|
||||||
appId = urlParams.get('app_id')
|
|
||||||
|
|
||||||
// 如果没有,从后端获取
|
|
||||||
if (!appId && companyParam) {
|
|
||||||
const response = await fetch(`/api/v1/auth/feishu-config?company=${companyParam}`)
|
|
||||||
const result = await response.json()
|
|
||||||
if (result.code === 0 && result.data.app_id) {
|
|
||||||
appId = result.data.app_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!appId) {
|
|
||||||
console.error('appId 获取失败')
|
|
||||||
setLoading(false)
|
|
||||||
Toast.show('飞书应用配置错误')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('开始免登,appId:', appId)
|
console.log('开始免登,appId:', appId)
|
||||||
|
|
||||||
// 调用飞书免登
|
// 调用飞书免登
|
||||||
|
|||||||
Reference in New Issue
Block a user