Commit 1bb32e49 authored by StyleZhang's avatar StyleZhang

fix: text-generation token

parent 6ba806e7
...@@ -6,9 +6,9 @@ import cn from 'classnames' ...@@ -6,9 +6,9 @@ import cn from 'classnames'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useContext } from 'use-context-selector' import { useContext } from 'use-context-selector'
import produce from 'immer' import produce from 'immer'
import { useParams } from 'next/navigation'
import { useBoolean, useGetState } from 'ahooks' import { useBoolean, useGetState } from 'ahooks'
import AppUnavailable from '../../base/app-unavailable' import AppUnavailable from '../../base/app-unavailable'
import { checkOrSetAccessToken } from '../utils'
import useConversation from './hooks/use-conversation' import useConversation from './hooks/use-conversation'
import s from './style.module.css' import s from './style.module.css'
import { ToastContext } from '@/app/components/base/toast' import { ToastContext } from '@/app/components/base/toast'
...@@ -17,7 +17,6 @@ import ConfigSence from '@/app/components/share/chat/config-scence' ...@@ -17,7 +17,6 @@ import ConfigSence from '@/app/components/share/chat/config-scence'
import Header from '@/app/components/share/header' import Header from '@/app/components/share/header'
import { import {
delConversation, delConversation,
fetchAccessToken,
fetchAppInfo, fetchAppInfo,
fetchAppParams, fetchAppParams,
fetchChatList, fetchChatList,
...@@ -68,7 +67,6 @@ const Main: FC<IMainProps> = ({ ...@@ -68,7 +67,6 @@ const Main: FC<IMainProps> = ({
// in mobile, show sidebar by click button // in mobile, show sidebar by click button
const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false) const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false)
// Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client. // Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client.
const params = useParams()
useEffect(() => { useEffect(() => {
if (siteInfo?.title) { if (siteInfo?.title) {
if (plan !== 'basic') if (plan !== 'basic')
...@@ -311,31 +309,9 @@ const Main: FC<IMainProps> = ({ ...@@ -311,31 +309,9 @@ const Main: FC<IMainProps> = ({
return fetchConversations(isInstalledApp, installedAppInfo?.id, undefined, undefined, 100) return fetchConversations(isInstalledApp, installedAppInfo?.id, undefined, undefined, 100)
} }
const fetchAndSetAccessToken = async () => {
const sharedToken = params.token
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
let accessTokenJson = { [sharedToken]: '' }
try {
accessTokenJson = JSON.parse(accessToken)
}
catch (e) {
}
const res = await fetchAccessToken(sharedToken)
accessTokenJson[sharedToken] = res.access_token
localStorage.setItem('token', JSON.stringify(accessTokenJson))
location.reload()
}
const fetchInitData = async () => { const fetchInitData = async () => {
let appData: any = {} await checkOrSetAccessToken()
try {
appData = await fetchAppInfo()
}
catch (e: any) {
if (e.code === 'unauthorized')
await fetchAndSetAccessToken()
}
return Promise.all([isInstalledApp return Promise.all([isInstalledApp
? { ? {
app_id: installedAppInfo?.id, app_id: installedAppInfo?.id,
...@@ -346,7 +322,7 @@ const Main: FC<IMainProps> = ({ ...@@ -346,7 +322,7 @@ const Main: FC<IMainProps> = ({
}, },
plan: 'basic', plan: 'basic',
} }
: appData, fetchAllConversations(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) : fetchAppInfo(), fetchAllConversations(), fetchAppParams(isInstalledApp, installedAppInfo?.id)])
} }
// init // init
......
...@@ -7,6 +7,7 @@ import { useBoolean, useClickAway, useGetState } from 'ahooks' ...@@ -7,6 +7,7 @@ import { useBoolean, useClickAway, useGetState } from 'ahooks'
import { XMarkIcon } from '@heroicons/react/24/outline' import { XMarkIcon } from '@heroicons/react/24/outline'
import TabHeader from '../../base/tab-header' import TabHeader from '../../base/tab-header'
import Button from '../../base/button' import Button from '../../base/button'
import { checkOrSetAccessToken } from '../utils'
import s from './style.module.css' import s from './style.module.css'
import RunBatch from './run-batch' import RunBatch from './run-batch'
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
...@@ -76,9 +77,6 @@ const TextGeneration: FC<IMainProps> = ({ ...@@ -76,9 +77,6 @@ const TextGeneration: FC<IMainProps> = ({
const res: any = await doFetchSavedMessage(isInstalledApp, installedAppInfo?.id) const res: any = await doFetchSavedMessage(isInstalledApp, installedAppInfo?.id)
setSavedMessages(res.data) setSavedMessages(res.data)
} }
useEffect(() => {
fetchSavedMessage()
}, [])
const handleSaveMessage = async (messageId: string) => { const handleSaveMessage = async (messageId: string) => {
await saveMessage(messageId, isInstalledApp, installedAppInfo?.id) await saveMessage(messageId, isInstalledApp, installedAppInfo?.id)
notify({ type: 'success', message: t('common.api.saved') }) notify({ type: 'success', message: t('common.api.saved') })
...@@ -256,7 +254,9 @@ const TextGeneration: FC<IMainProps> = ({ ...@@ -256,7 +254,9 @@ const TextGeneration: FC<IMainProps> = ({
setAllTaskList(newAllTaskList) setAllTaskList(newAllTaskList)
} }
const fetchInitData = () => { const fetchInitData = async () => {
await checkOrSetAccessToken()
return Promise.all([isInstalledApp return Promise.all([isInstalledApp
? { ? {
app_id: installedAppInfo?.id, app_id: installedAppInfo?.id,
...@@ -267,7 +267,7 @@ const TextGeneration: FC<IMainProps> = ({ ...@@ -267,7 +267,7 @@ const TextGeneration: FC<IMainProps> = ({
}, },
plan: 'basic', plan: 'basic',
} }
: fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) : fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id), fetchSavedMessage()])
} }
useEffect(() => { useEffect(() => {
......
import { fetchAccessToken } from '@/service/share'
export const checkOrSetAccessToken = async () => {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
let accessTokenJson = { [sharedToken]: '' }
try {
accessTokenJson = JSON.parse(accessToken)
}
catch (e) {
}
if (!accessTokenJson[sharedToken]) {
const res = await fetchAccessToken(sharedToken)
accessTokenJson[sharedToken] = res.access_token
localStorage.setItem('token', JSON.stringify(accessTokenJson))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment