Parcourir la source

新增自动登录功能

zhou1577692533 il y a 1 semaine
Parent
commit
3b08eacc16
3 fichiers modifiés avec 83 ajouts et 14 suppressions
  1. 1 0
      config/customConfig.js
  2. 14 5
      src/components/App.tsx
  3. 68 9
      src/script/service.ts

+ 1 - 0
config/customConfig.js

@@ -1,3 +1,4 @@
 module.exports = {
   urlDev: 'http://localhost:3000/',
+  urlProd:'https://openapi.julefood.cn:4000/'
 };

+ 14 - 5
src/components/App.tsx

@@ -32,6 +32,12 @@ interface ApiResponse {
 interface UserInfo {
   userId: string;
   name?: string;
+  avatar?: string;
+  mobile?: string;
+  jobnumber?: string;
+  department?: number[];
+  permissions?: string[];
+  [key: string]: any;
 }
 
 function App() {
@@ -58,12 +64,15 @@ function App() {
     }
   }, []);
 
-  const loadUserInfo = useCallback(async () => {
+  const handleAutoLogin = useCallback(async () => {
     try {
-      const user = await Dingdocs.script.run('getUserInfo');
+      const user = await Dingdocs.script.run('login');
       setUserInfo(user);
+      console.log('自动登录成功:', user);
     } catch (error: any) {
-      console.error('获取用户信息失败:', error);
+      console.error('自动登录失败:', error);
+      setError(error.message || '登录失败');
+      throw error;
     }
   }, []);
 
@@ -153,7 +162,7 @@ function App() {
         setError('');
         
         try {
-          loadUserInfo();
+          await handleAutoLogin();
           await loadApiList();
           loadDocumentInfo();
         } catch (error: any) {
@@ -164,7 +173,7 @@ function App() {
         }
       },
     });
-  }, [loadUserInfo, loadApiList, loadDocumentInfo]);
+  }, [handleAutoLogin, loadApiList, loadDocumentInfo]);
 
   if (loading) {
     return (

+ 68 - 9
src/script/service.ts

@@ -3,11 +3,26 @@ import type {} from 'dingtalk-docs-cool-app';
 import axios from 'axios';
 
 const BASE_URL = 'https://openapi.julefood.cn:8082/api';
-const USER_ID = '637758931';
 
 interface UserInfo {
   userId: string;
   name?: string;
+  avatar?: string;
+  mobile?: string;
+  jobnumber?: string;
+  department?: number[];
+  permissions?: string[];
+  [key: string]: any;
+}
+
+interface LoginResponse {
+  code: number;
+  result: {
+    user_info: string;
+    expiration_time: string;
+    token: string;
+  };
+  msg: string;
 }
 
 interface ApiParam {
@@ -34,6 +49,9 @@ interface ApiResponse {
   data?: any;
 }
 
+let token: string = '';
+let currentUserInfo: UserInfo | null = null;
+
 const apiClient = axios.create({
   baseURL: BASE_URL,
   timeout: 30000,
@@ -42,6 +60,18 @@ const apiClient = axios.create({
   },
 });
 
+apiClient.interceptors.request.use(
+  (config) => {
+    if (token) {
+      config.headers['token'] = token;
+    }
+    return config;
+  },
+  (error) => {
+    return Promise.reject(error);
+  }
+);
+
 apiClient.interceptors.response.use(
   (response) => response,
   (error) => {
@@ -50,16 +80,45 @@ apiClient.interceptors.response.use(
   }
 );
 
-function getUserInfo(): UserInfo {
-  return {
-    userId: USER_ID,
-    name: '用户'
-  };
+async function login(): Promise<UserInfo> {
+  const response = await axios.post<LoginResponse>(
+    `${BASE_URL}/sys_user/ddh5/login`,
+    {
+      code: 'ebaa5afafa1d3c8d8cf2223f0c2f9ec1',
+      app_id: 'dingqo0h82jhq5ewv9ow'
+    },
+    { timeout: 30000 }
+  );
+  
+  if (response.data.code === 200) {
+    token = response.data.result.token;
+    const userInfoStr = response.data.result.user_info;
+    try {
+      const parsedUserInfo = JSON.parse(userInfoStr);
+      currentUserInfo = {
+        userId: parsedUserInfo.userid || parsedUserInfo.id || '',
+        name: parsedUserInfo.name || '',
+        avatar: parsedUserInfo.avatar,
+        mobile: parsedUserInfo.mobile,
+        jobnumber: parsedUserInfo.jobnumber,
+        department: parsedUserInfo.department,
+        permissions: parsedUserInfo.permissions,
+        ...parsedUserInfo
+      };
+    } catch (e) {
+      console.error('解析用户信息失败:', e);
+      currentUserInfo = { userId: '', name: '用户' };
+    }
+    return currentUserInfo;
+  } else {
+    throw new Error(response.data.msg || '登录失败');
+  }
 }
 
 async function getApiList(): Promise<ApiItem[]> {
+  const userId = currentUserInfo?.userId || '';
   const response = await apiClient.get('/auth/user/api_asset', {
-    params: { userId: USER_ID }
+    params: { userId }
   });
   const applyList = response.data?.data || [];
   
@@ -80,7 +139,7 @@ async function callApi(apiItem: Record<string, any>): Promise<ApiResponse> {
     paramName: p.paramName,
     value: p.value
   }));
-  params.userId = USER_ID;
+  params.userId = currentUserInfo?.userId || '';
 
   const response = await apiClient.post('/ding/add', params);
   return response.data;
@@ -106,7 +165,7 @@ function getDocumentInfo() {
   }
 }
 
-DingdocsScript.registerScript('getUserInfo', getUserInfo);
+DingdocsScript.registerScript('login', login);
 DingdocsScript.registerScript('getApiList', getApiList);
 DingdocsScript.registerScript('callApi', callApi);
 DingdocsScript.registerScript('getDocumentInfo', getDocumentInfo);