|
@@ -3,11 +3,26 @@ import type {} from 'dingtalk-docs-cool-app';
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
const BASE_URL = 'https://openapi.julefood.cn:8082/api';
|
|
const BASE_URL = 'https://openapi.julefood.cn:8082/api';
|
|
|
-const USER_ID = '637758931';
|
|
|
|
|
|
|
|
|
|
interface UserInfo {
|
|
interface UserInfo {
|
|
|
userId: string;
|
|
userId: string;
|
|
|
name?: 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 {
|
|
interface ApiParam {
|
|
@@ -34,6 +49,9 @@ interface ApiResponse {
|
|
|
data?: any;
|
|
data?: any;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+let token: string = '';
|
|
|
|
|
+let currentUserInfo: UserInfo | null = null;
|
|
|
|
|
+
|
|
|
const apiClient = axios.create({
|
|
const apiClient = axios.create({
|
|
|
baseURL: BASE_URL,
|
|
baseURL: BASE_URL,
|
|
|
timeout: 30000,
|
|
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(
|
|
apiClient.interceptors.response.use(
|
|
|
(response) => response,
|
|
(response) => response,
|
|
|
(error) => {
|
|
(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[]> {
|
|
async function getApiList(): Promise<ApiItem[]> {
|
|
|
|
|
+ const userId = currentUserInfo?.userId || '';
|
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
const response = await apiClient.get('/auth/user/api_asset', {
|
|
|
- params: { userId: USER_ID }
|
|
|
|
|
|
|
+ params: { userId }
|
|
|
});
|
|
});
|
|
|
const applyList = response.data?.data || [];
|
|
const applyList = response.data?.data || [];
|
|
|
|
|
|
|
@@ -80,7 +139,7 @@ async function callApi(apiItem: Record<string, any>): Promise<ApiResponse> {
|
|
|
paramName: p.paramName,
|
|
paramName: p.paramName,
|
|
|
value: p.value
|
|
value: p.value
|
|
|
}));
|
|
}));
|
|
|
- params.userId = USER_ID;
|
|
|
|
|
|
|
+ params.userId = currentUserInfo?.userId || '';
|
|
|
|
|
|
|
|
const response = await apiClient.post('/ding/add', params);
|
|
const response = await apiClient.post('/ding/add', params);
|
|
|
return response.data;
|
|
return response.data;
|
|
@@ -106,7 +165,7 @@ function getDocumentInfo() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-DingdocsScript.registerScript('getUserInfo', getUserInfo);
|
|
|
|
|
|
|
+DingdocsScript.registerScript('login', login);
|
|
|
DingdocsScript.registerScript('getApiList', getApiList);
|
|
DingdocsScript.registerScript('getApiList', getApiList);
|
|
|
DingdocsScript.registerScript('callApi', callApi);
|
|
DingdocsScript.registerScript('callApi', callApi);
|
|
|
DingdocsScript.registerScript('getDocumentInfo', getDocumentInfo);
|
|
DingdocsScript.registerScript('getDocumentInfo', getDocumentInfo);
|