|
@@ -4,6 +4,26 @@ import axios from 'axios';
|
|
|
|
|
|
|
|
const BASE_URL = 'https://openapi.julefood.cn:8082/api';
|
|
const BASE_URL = 'https://openapi.julefood.cn:8082/api';
|
|
|
|
|
|
|
|
|
|
+const STORAGE_PREFIX = 'aiTablePlugin_';
|
|
|
|
|
+
|
|
|
|
|
+function saveData<T>(key: string, value: T) {
|
|
|
|
|
+ localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(value));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function loadData<T>(key: string, defaultValue: T): T {
|
|
|
|
|
+ const raw = localStorage.getItem(STORAGE_PREFIX + key);
|
|
|
|
|
+ if (raw === null) return defaultValue;
|
|
|
|
|
+ try {
|
|
|
|
|
+ return JSON.parse(raw) as T;
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return defaultValue;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function removeData(key: string) {
|
|
|
|
|
+ localStorage.removeItem(STORAGE_PREFIX + key);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
interface UserInfo {
|
|
interface UserInfo {
|
|
|
userId: string;
|
|
userId: string;
|
|
|
name?: string;
|
|
name?: string;
|
|
@@ -49,8 +69,8 @@ interface ApiResponse {
|
|
|
data?: any;
|
|
data?: any;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-let token: string = localStorage.getItem('api_token') || '';
|
|
|
|
|
-let currentUserInfo: UserInfo | null = null;
|
|
|
|
|
|
|
+let token: string = loadData('token', '');
|
|
|
|
|
+let currentUserInfo: UserInfo | null = loadData('userInfo', null);
|
|
|
|
|
|
|
|
const apiClient = axios.create({
|
|
const apiClient = axios.create({
|
|
|
baseURL: BASE_URL,
|
|
baseURL: BASE_URL,
|
|
@@ -62,7 +82,7 @@ const apiClient = axios.create({
|
|
|
|
|
|
|
|
apiClient.interceptors.request.use(
|
|
apiClient.interceptors.request.use(
|
|
|
(config) => {
|
|
(config) => {
|
|
|
- const currentToken = token || localStorage.getItem('api_token') || '';
|
|
|
|
|
|
|
+ const currentToken = token || loadData('token', '');
|
|
|
if (currentToken) {
|
|
if (currentToken) {
|
|
|
config.headers['token'] = currentToken;
|
|
config.headers['token'] = currentToken;
|
|
|
}
|
|
}
|
|
@@ -93,7 +113,8 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
|
|
|
|
|
if (response.data.code === 200) {
|
|
if (response.data.code === 200) {
|
|
|
token = response.data.result.token;
|
|
token = response.data.result.token;
|
|
|
- localStorage.setItem('api_token', token);
|
|
|
|
|
|
|
+ saveData('token', token);
|
|
|
|
|
+
|
|
|
const userInfoStr = response.data.result.user_info;
|
|
const userInfoStr = response.data.result.user_info;
|
|
|
try {
|
|
try {
|
|
|
const parsedUserInfo = JSON.parse(userInfoStr);
|
|
const parsedUserInfo = JSON.parse(userInfoStr);
|
|
@@ -111,6 +132,8 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
console.error('解析用户信息失败:', e);
|
|
console.error('解析用户信息失败:', e);
|
|
|
currentUserInfo = { userId: '', name: '用户' };
|
|
currentUserInfo = { userId: '', name: '用户' };
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ saveData('userInfo', currentUserInfo);
|
|
|
return currentUserInfo!;
|
|
return currentUserInfo!;
|
|
|
} else {
|
|
} else {
|
|
|
throw new Error(response.data.msg || '登录失败');
|
|
throw new Error(response.data.msg || '登录失败');
|