|
@@ -49,8 +49,38 @@ interface ApiResponse {
|
|
|
data?: any;
|
|
data?: any;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-let token: string = '';
|
|
|
|
|
-let currentUserInfo: UserInfo | null = null;
|
|
|
|
|
|
|
+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 {
|
|
|
|
|
+ localStorage.setItem(USER_INFO_KEY, JSON.stringify(userInfo));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getUserInfo(): UserInfo | null {
|
|
|
|
|
+ const str = localStorage.getItem(USER_INFO_KEY);
|
|
|
|
|
+ return str ? JSON.parse(str) : 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,
|
|
@@ -62,6 +92,7 @@ const apiClient = axios.create({
|
|
|
|
|
|
|
|
apiClient.interceptors.request.use(
|
|
apiClient.interceptors.request.use(
|
|
|
(config) => {
|
|
(config) => {
|
|
|
|
|
+ const token = getToken();
|
|
|
if (token) {
|
|
if (token) {
|
|
|
config.headers['token'] = token;
|
|
config.headers['token'] = token;
|
|
|
}
|
|
}
|
|
@@ -91,11 +122,14 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
if (response.data.code === 200) {
|
|
if (response.data.code === 200) {
|
|
|
- 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;
|
|
|
try {
|
|
try {
|
|
|
const parsedUserInfo = JSON.parse(userInfoStr);
|
|
const parsedUserInfo = JSON.parse(userInfoStr);
|
|
|
- currentUserInfo = {
|
|
|
|
|
|
|
+ userInfo = {
|
|
|
userId: parsedUserInfo.userid || parsedUserInfo.id || '',
|
|
userId: parsedUserInfo.userid || parsedUserInfo.id || '',
|
|
|
name: parsedUserInfo.name || '',
|
|
name: parsedUserInfo.name || '',
|
|
|
avatar: parsedUserInfo.avatar,
|
|
avatar: parsedUserInfo.avatar,
|
|
@@ -107,16 +141,18 @@ async function login(authCode: string): Promise<UserInfo> {
|
|
|
};
|
|
};
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
console.error('解析用户信息失败:', e);
|
|
console.error('解析用户信息失败:', e);
|
|
|
- currentUserInfo = { userId: '', name: '用户' };
|
|
|
|
|
|
|
+ userInfo = { userId: '', name: '用户' };
|
|
|
}
|
|
}
|
|
|
- return currentUserInfo!;
|
|
|
|
|
|
|
+ saveUserInfo(userInfo);
|
|
|
|
|
+ return userInfo;
|
|
|
} else {
|
|
} else {
|
|
|
throw new Error(response.data.msg || '登录失败');
|
|
throw new Error(response.data.msg || '登录失败');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function getApiList(): Promise<ApiItem[]> {
|
|
async function getApiList(): Promise<ApiItem[]> {
|
|
|
- const userId = currentUserInfo?.userId || '';
|
|
|
|
|
|
|
+ const userInfo = getUserInfo();
|
|
|
|
|
+ const userId = userInfo?.userId || '';
|
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
|
params: { userId }
|
|
params: { userId }
|
|
|
});
|
|
});
|
|
@@ -139,7 +175,9 @@ async function callApi(apiItem: Record<string, any>): Promise<ApiResponse> {
|
|
|
paramName: p.paramName,
|
|
paramName: p.paramName,
|
|
|
value: p.value
|
|
value: p.value
|
|
|
}));
|
|
}));
|
|
|
- params.userId = currentUserInfo?.userId || '';
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const userInfo = getUserInfo();
|
|
|
|
|
+ params.userId = userInfo?.userId || '';
|
|
|
|
|
|
|
|
const response = await apiClient.post('/ding/add', params);
|
|
const response = await apiClient.post('/ding/add', params);
|
|
|
return response.data;
|
|
return response.data;
|
|
@@ -168,14 +206,11 @@ async function getDocumentInfo() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function getToken(): string {
|
|
|
|
|
- return token;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
DingdocsScript.registerScript('login', login);
|
|
DingdocsScript.registerScript('login', login);
|
|
|
DingdocsScript.registerScript('getToken', getToken);
|
|
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 {};
|