优化
This commit is contained in:
@@ -168,6 +168,13 @@ describe('CustomersView', () => {
|
||||
vi.mocked(showExportTaskCreatedDialog).mockResolvedValue(false)
|
||||
})
|
||||
|
||||
it('waits for the client workspace to confirm impersonation session persistence', () => {
|
||||
expect(customersSource).toContain("searchParams.set('impersonationOrigin', window.location.origin)")
|
||||
expect(customersSource).toContain("event.data?.type !== 'impersonation-complete'")
|
||||
expect(customersSource).toContain("event.data?.type === 'impersonation-failed'")
|
||||
expect(customersSource).toContain("ElMessage.success('客户工作台登录成功')")
|
||||
})
|
||||
|
||||
it('searches the customer list by customer name only', () => {
|
||||
expect(customersSource).toContain("label: '客户名称', placeholder: '请输入客户名称'")
|
||||
expect(customersSource).not.toContain('公司名称、客户编号或联系人')
|
||||
|
||||
@@ -777,12 +777,19 @@ async function submitImpersonation() {
|
||||
impersonationError.value = getApiErrorDetails(new Error('未配置客户工作台地址'), '暂未配置客户工作台地址,无法安全跳转登录')
|
||||
return
|
||||
}
|
||||
let targetOrigin: string
|
||||
try { targetOrigin = new URL(clientUrl, window.location.origin).origin } catch {
|
||||
let targetUrl: URL
|
||||
try {
|
||||
targetUrl = new URL(clientUrl, window.location.origin)
|
||||
targetUrl.pathname = `${targetUrl.pathname.replace(/\/+$/, '')}/login`
|
||||
targetUrl.search = ''
|
||||
targetUrl.hash = ''
|
||||
targetUrl.searchParams.set('impersonationOrigin', window.location.origin)
|
||||
} catch {
|
||||
impersonationError.value = getApiErrorDetails(new Error('客户工作台地址无效'), '客户工作台地址配置无效,无法安全跳转登录')
|
||||
return
|
||||
}
|
||||
const popup = window.open(clientUrl, '_blank')
|
||||
const targetOrigin = targetUrl.origin
|
||||
const popup = window.open(targetUrl.toString(), '_blank')
|
||||
if (!popup) {
|
||||
impersonationError.value = getApiErrorDetails(new Error('浏览器拦截了新窗口'), '浏览器拦截了客户工作台窗口,请允许弹窗后重试')
|
||||
return
|
||||
@@ -792,27 +799,39 @@ async function submitImpersonation() {
|
||||
let handoffPending = false
|
||||
try {
|
||||
const ticket = await createPlatformTenantImpersonation(impersonationTenant.value.id, { reason: impersonationForm.reason.trim() })
|
||||
const onReady = (event: MessageEvent) => {
|
||||
if (event.source !== popup || event.origin !== targetOrigin || event.data?.source !== 'chongchong-client' || event.data?.type !== 'impersonation-ready') return
|
||||
popup.postMessage({ source: 'chongchong-admin', type: 'impersonation-ticket', code: ticket.code }, targetOrigin)
|
||||
let ticketSent = false
|
||||
const onMessage = (event: MessageEvent) => {
|
||||
if (event.source !== popup || event.origin !== targetOrigin || event.data?.source !== 'chongchong-client') return
|
||||
if (event.data?.type === 'impersonation-ready' && !ticketSent) {
|
||||
ticketSent = true
|
||||
popup.postMessage({ source: 'chongchong-admin', type: 'impersonation-ticket', code: ticket.code }, targetOrigin)
|
||||
return
|
||||
}
|
||||
if (event.data?.type === 'impersonation-failed') {
|
||||
cleanup()
|
||||
impersonationSubmitting.value = false
|
||||
impersonationError.value = getApiErrorDetails(new Error('客户工作台未写入代登录会话'), '客户工作台登录写入失败,请关闭窗口后重试')
|
||||
return
|
||||
}
|
||||
if (event.data?.type !== 'impersonation-complete') return
|
||||
cleanup()
|
||||
impersonationSubmitting.value = false
|
||||
ElMessage.success('客户工作台已打开')
|
||||
ElMessage.success('客户工作台登录成功')
|
||||
impersonationDialogOpen.value = false
|
||||
}
|
||||
const timeout = window.setTimeout(() => {
|
||||
cleanup()
|
||||
impersonationSubmitting.value = false
|
||||
impersonationError.value = getApiErrorDetails(new Error('客户工作台未完成安全握手'), '客户工作台未完成安全握手,请关闭窗口后重试')
|
||||
}, 10000)
|
||||
impersonationError.value = getApiErrorDetails(new Error('客户工作台未确认会话写入'), '客户工作台未确认登录成功,请关闭窗口后重试')
|
||||
}, 15000)
|
||||
const cleanup = () => {
|
||||
window.clearTimeout(timeout)
|
||||
window.removeEventListener('message', onReady)
|
||||
window.removeEventListener('message', onMessage)
|
||||
if (impersonationCleanup === cleanup) impersonationCleanup = null
|
||||
}
|
||||
impersonationCleanup?.()
|
||||
impersonationCleanup = cleanup
|
||||
window.addEventListener('message', onReady)
|
||||
window.addEventListener('message', onMessage)
|
||||
handoffPending = true
|
||||
} catch (error) {
|
||||
popup.close()
|
||||
|
||||
Reference in New Issue
Block a user