| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  | import { CONVERSATION_ID_INFO } from '../base/chat/constants' | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | import { fetchAccessToken } from '@/service/share' | 
					
						
							| 
									
										
										
										
											2025-03-31 18:55:42 +08:00
										 |  |  | import { getProcessedSystemVariablesFromUrlParams } from '../base/chat/utils' | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  | export const isTokenV1 = (token: Record<string, any>) => { | 
					
						
							|  |  |  |   return !token.version | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getInitialTokenV2 = (): Record<string, any> => ({ | 
					
						
							|  |  |  |   version: 2, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 10:55:17 +08:00
										 |  |  | export const checkOrSetAccessToken = async (appCode?: string) => { | 
					
						
							|  |  |  |   const sharedToken = appCode || globalThis.location.pathname.split('/').slice(-1)[0] | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |   const userId = (await getProcessedSystemVariablesFromUrlParams()).user_id | 
					
						
							|  |  |  |   const accessToken = localStorage.getItem('token') || JSON.stringify(getInitialTokenV2()) | 
					
						
							|  |  |  |   let accessTokenJson = getInitialTokenV2() | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |   try { | 
					
						
							|  |  |  |     accessTokenJson = JSON.parse(accessToken) | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |     if (isTokenV1(accessTokenJson)) | 
					
						
							|  |  |  |       accessTokenJson = getInitialTokenV2() | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-04-14 11:27:14 +08:00
										 |  |  |   catch { | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-06-05 10:55:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |   if (!accessTokenJson[sharedToken]?.[userId || 'DEFAULT']) { | 
					
						
							| 
									
										
										
										
											2025-06-05 10:55:17 +08:00
										 |  |  |     const webAppAccessToken = localStorage.getItem('webapp_access_token') | 
					
						
							|  |  |  |     const res = await fetchAccessToken({ appCode: sharedToken, userId, webAppAccessToken }) | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |     accessTokenJson[sharedToken] = { | 
					
						
							|  |  |  |       ...accessTokenJson[sharedToken], | 
					
						
							|  |  |  |       [userId || 'DEFAULT']: res.access_token, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |     localStorage.setItem('token', JSON.stringify(accessTokenJson)) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 10:55:17 +08:00
										 |  |  | export const setAccessToken = (sharedToken: string, token: string, user_id?: string) => { | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |   const accessToken = localStorage.getItem('token') || JSON.stringify(getInitialTokenV2()) | 
					
						
							|  |  |  |   let accessTokenJson = getInitialTokenV2() | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  |   try { | 
					
						
							|  |  |  |     accessTokenJson = JSON.parse(accessToken) | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |     if (isTokenV1(accessTokenJson)) | 
					
						
							|  |  |  |       accessTokenJson = getInitialTokenV2() | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-04-14 11:27:14 +08:00
										 |  |  |   catch { | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   localStorage.removeItem(CONVERSATION_ID_INFO) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 21:08:13 +08:00
										 |  |  |   accessTokenJson[sharedToken] = { | 
					
						
							|  |  |  |     ...accessTokenJson[sharedToken], | 
					
						
							|  |  |  |     [user_id || 'DEFAULT']: token, | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  |   localStorage.setItem('token', JSON.stringify(accessTokenJson)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const removeAccessToken = () => { | 
					
						
							| 
									
										
										
										
											2025-06-09 15:44:49 +08:00
										 |  |  |   localStorage.removeItem('token') | 
					
						
							| 
									
										
										
										
											2025-06-05 10:55:17 +08:00
										 |  |  |   localStorage.removeItem('webapp_access_token') | 
					
						
							| 
									
										
										
										
											2024-05-15 16:14:49 +08:00
										 |  |  | } |