|
@@ -49,55 +49,6 @@ interface ApiResponse {
|
|
|
data?: any;
|
|
data?: any;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const TOKEN_KEY = 'julefood_token';
|
|
|
|
|
-const USER_INFO_KEY = 'julefood_user_info';
|
|
|
|
|
-
|
|
|
|
|
-function saveToken(token: string): void {
|
|
|
|
|
- localStorage.setItem(TOKEN_KEY, token);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function getToken(): string {
|
|
|
|
|
- return localStorage.getItem(TOKEN_KEY) || '';
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function removeToken(): void {
|
|
|
|
|
- localStorage.removeItem(TOKEN_KEY);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function saveUserInfo(userInfo: UserInfo): void {
|
|
|
|
|
- const jsonStr = JSON.stringify(userInfo);
|
|
|
|
|
- console.log('[DEBUG] saveUserInfo - saving:', jsonStr);
|
|
|
|
|
- localStorage.setItem(USER_INFO_KEY, jsonStr);
|
|
|
|
|
- const saved = localStorage.getItem(USER_INFO_KEY);
|
|
|
|
|
- console.log('[DEBUG] saveUserInfo - verified saved:', saved);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function getUserInfo(): UserInfo | null {
|
|
|
|
|
- const str = localStorage.getItem(USER_INFO_KEY);
|
|
|
|
|
- console.log('[DEBUG] getUserInfo - raw value:', str);
|
|
|
|
|
- if (!str) {
|
|
|
|
|
- console.warn('[DEBUG] getUserInfo - localStorage is empty');
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- try {
|
|
|
|
|
- const result = JSON.parse(str);
|
|
|
|
|
- console.log('[DEBUG] getUserInfo - parsed userId:', result?.userId);
|
|
|
|
|
- return result;
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.error('[DEBUG] getUserInfo - JSON parse failed:', e);
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function removeUserInfo(): void {
|
|
|
|
|
- localStorage.removeItem(USER_INFO_KEY);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function clearAuth(): void {
|
|
|
|
|
- removeToken();
|
|
|
|
|
- removeUserInfo();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
const apiClient = axios.create({
|
|
const apiClient = axios.create({
|
|
|
baseURL: BASE_URL,
|
|
baseURL: BASE_URL,
|
|
|
timeout: 30000,
|
|
timeout: 30000,
|
|
@@ -106,28 +57,7 @@ const apiClient = axios.create({
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-apiClient.interceptors.request.use(
|
|
|
|
|
- (config) => {
|
|
|
|
|
- const token = getToken();
|
|
|
|
|
- if (token) {
|
|
|
|
|
- config.headers['token'] = token;
|
|
|
|
|
- }
|
|
|
|
|
- return config;
|
|
|
|
|
- },
|
|
|
|
|
- (error) => {
|
|
|
|
|
- return Promise.reject(error);
|
|
|
|
|
- }
|
|
|
|
|
-);
|
|
|
|
|
-
|
|
|
|
|
-apiClient.interceptors.response.use(
|
|
|
|
|
- (response) => response,
|
|
|
|
|
- (error) => {
|
|
|
|
|
- console.error('API 请求错误:', error);
|
|
|
|
|
- return Promise.reject(error);
|
|
|
|
|
- }
|
|
|
|
|
-);
|
|
|
|
|
-
|
|
|
|
|
-async function login(authCode: string): Promise<UserInfo> {
|
|
|
|
|
|
|
+async function login(authCode: string): Promise<{ token: string; userInfo: UserInfo }> {
|
|
|
const response = await axios.post<LoginResponse>(
|
|
const response = await axios.post<LoginResponse>(
|
|
|
`${BASE_URL}/sys_user/ddh5/login`,
|
|
`${BASE_URL}/sys_user/ddh5/login`,
|
|
|
{
|
|
{
|
|
@@ -139,8 +69,6 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
|
|
|
|
|
if (response.data.code === 200) {
|
|
if (response.data.code === 200) {
|
|
|
const token = response.data.result.token;
|
|
const token = response.data.result.token;
|
|
|
- saveToken(token);
|
|
|
|
|
-
|
|
|
|
|
const userInfoStr = response.data.result.user_info;
|
|
const userInfoStr = response.data.result.user_info;
|
|
|
let userInfo: UserInfo;
|
|
let userInfo: UserInfo;
|
|
|
try {
|
|
try {
|
|
@@ -159,18 +87,16 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
console.error('解析用户信息失败:', e);
|
|
console.error('解析用户信息失败:', e);
|
|
|
userInfo = { userId: '', name: '用户' };
|
|
userInfo = { userId: '', name: '用户' };
|
|
|
}
|
|
}
|
|
|
- saveUserInfo(userInfo);
|
|
|
|
|
- return userInfo;
|
|
|
|
|
|
|
+ return { token, userInfo };
|
|
|
} else {
|
|
} else {
|
|
|
throw new Error(response.data.msg || '登录失败');
|
|
throw new Error(response.data.msg || '登录失败');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function getApiList(): Promise<ApiItem[]> {
|
|
|
|
|
- const userInfo = getUserInfo();
|
|
|
|
|
- const userId = userInfo?.userId || '';
|
|
|
|
|
|
|
+async function getApiList(token: string, userId: string): Promise<ApiItem[]> {
|
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
|
- params: { userId }
|
|
|
|
|
|
|
+ params: { userId },
|
|
|
|
|
+ headers: { token }
|
|
|
});
|
|
});
|
|
|
const applyList = response.data?.data || [];
|
|
const applyList = response.data?.data || [];
|
|
|
|
|
|
|
@@ -185,18 +111,17 @@ async function getApiList(): Promise<ApiItem[]> {
|
|
|
return Array.from(uniqueMap.values());
|
|
return Array.from(uniqueMap.values());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function callApi(apiItem: Record<string, any>): Promise<ApiResponse> {
|
|
|
|
|
|
|
+async function callApi(apiItem: Record<string, any>, token: string, userId: string): Promise<ApiResponse> {
|
|
|
const params = { ...apiItem };
|
|
const params = { ...apiItem };
|
|
|
params.apiParamAuth = params.apiParamAuth.map((p: any) => ({
|
|
params.apiParamAuth = params.apiParamAuth.map((p: any) => ({
|
|
|
paramName: p.paramName,
|
|
paramName: p.paramName,
|
|
|
value: p.value
|
|
value: p.value
|
|
|
}));
|
|
}));
|
|
|
-
|
|
|
|
|
- const userInfo = getUserInfo();
|
|
|
|
|
- params.userId = userInfo?.userId || '';
|
|
|
|
|
- console.log('[DEBUG] callApi - userId:', params.userId, 'userInfo:', userInfo);
|
|
|
|
|
|
|
+ params.userId = userId;
|
|
|
|
|
|
|
|
- const response = await apiClient.post('/ding/add', params);
|
|
|
|
|
|
|
+ const response = await apiClient.post('/ding/add', params, {
|
|
|
|
|
+ headers: { token }
|
|
|
|
|
+ });
|
|
|
return response.data;
|
|
return response.data;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -224,10 +149,8 @@ async function getDocumentInfo() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
DingdocsScript.registerScript('login', login);
|
|
DingdocsScript.registerScript('login', login);
|
|
|
-DingdocsScript.registerScript('getToken', getToken);
|
|
|
|
|
DingdocsScript.registerScript('getApiList', getApiList);
|
|
DingdocsScript.registerScript('getApiList', getApiList);
|
|
|
DingdocsScript.registerScript('callApi', callApi);
|
|
DingdocsScript.registerScript('callApi', callApi);
|
|
|
DingdocsScript.registerScript('getDocumentInfo', getDocumentInfo);
|
|
DingdocsScript.registerScript('getDocumentInfo', getDocumentInfo);
|
|
|
-DingdocsScript.registerScript('clearAuth', clearAuth);
|
|
|
|
|
|
|
|
|
|
-export {};
|
|
|
|
|
|
|
+export {};
|