From 4fa6b2c2bd8c6097dcdf0b2c699f8cc51d4a2c3b Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 28 Aug 2023 18:20:07 +0800 Subject: [PATCH] just add --- app/api/utils/common.ts | 3 ++- app/components/index.tsx | 3 +++ config/index.ts | 4 ++-- service/base.ts | 11 ++++++++++- service/index.ts | 4 ++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/api/utils/common.ts b/app/api/utils/common.ts index 109ee4b..220b884 100644 --- a/app/api/utils/common.ts +++ b/app/api/utils/common.ts @@ -1,5 +1,5 @@ import { type NextRequest } from 'next/server' -import { ChatClient } from 'dify-client' +import { ChatClient, DifyClient } from 'dify-client' import { v4 } from 'uuid' import { API_KEY, API_URL, APP_ID } from '@/config' @@ -19,3 +19,4 @@ export const setSession = (sessionId: string) => { } export const client = new ChatClient(API_KEY, API_URL || undefined) +export const commonClient = new ChatClient(API_KEY, API_URL || undefined) diff --git a/app/components/index.tsx b/app/components/index.tsx index 3ebc4b8..78e80ad 100644 --- a/app/components/index.tsx +++ b/app/components/index.tsx @@ -19,6 +19,7 @@ import Loading from '@/app/components/base/loading' import { replaceVarWithValues, userInputsFormToPromptVariables } from '@/utils/prompt' import AppUnavailable from '@/app/components/app-unavailable' import { API_KEY, APP_ID, APP_INFO, isShowPrompt, promptTemplate } from '@/config' +import { checkOrSetAccessToken } from '@/utils/access-token' const Main: FC = () => { const { t } = useTranslation() @@ -197,6 +198,8 @@ const Main: FC = () => { return } (async () => { + await checkOrSetAccessToken() + try { const [conversationData, appParams] = await Promise.all([fetchConversations(), fetchAppParams()]) diff --git a/config/index.ts b/config/index.ts index df0247c..3083877 100644 --- a/config/index.ts +++ b/config/index.ts @@ -1,6 +1,6 @@ import type { AppInfo } from '@/types/app' -export const APP_ID = '' -export const API_KEY = '' +export const APP_ID = '3caee4a2-273e-4686-b53e-59b4aaee8006' +export const API_KEY = 'app-j0LqnaHykYouvz8PnYgldWcM' export const API_URL = '' export const APP_INFO: AppInfo = { title: 'Chat APP', diff --git a/service/base.ts b/service/base.ts index dd502f9..4438073 100644 --- a/service/base.ts +++ b/service/base.ts @@ -1,4 +1,4 @@ -import { API_PREFIX } from '@/config' +import { API_PREFIX, APP_ID } from '@/config' import Toast from '@/app/components/base/toast' const TIME_OUT = 100000 @@ -102,7 +102,16 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: IOtherOptions) => { const options = Object.assign({}, baseOptions, fetchOptions) + const sharedToken = APP_ID + const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' }) + let accessTokenJson = { [sharedToken]: '' } + try { + accessTokenJson = JSON.parse(accessToken) + } + catch (e) { + } + options.headers.set('Authorization', `Bearer ${accessTokenJson[sharedToken]}`) const urlPrefix = API_PREFIX let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}` diff --git a/service/index.ts b/service/index.ts index 8028e0a..39da9c7 100644 --- a/service/index.ts +++ b/service/index.ts @@ -31,3 +31,7 @@ export const fetchAppParams = async () => { export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }) => { return post(url, { body }) } + +export const fetchAccessToken = async (appId: string) => { + return get('/passport') as Promise<{ access_token: string }> +} \ No newline at end of file