将零散的接口封装为EIAC_Desktop_Api,新增GetConfig和FaultReporting接口。

This commit is contained in:
Allen 2025-05-15 00:39:58 +08:00
parent 3cec21a7f3
commit 51087027c8
3 changed files with 300 additions and 86 deletions

276
src/EIAC_Desktop_Api.ts Normal file
View File

@ -0,0 +1,276 @@
/**
* EIAC API
*/
export class EIAC_Desktop_Api {
private host: string;
/**
* API Host
*/
constructor(host = 'http://1.12.73.211:8848/EIAC_Desktop_Api') {
this.host = host;
}
/**
*
*/
public Auth = {
/**
* /api/Auth/Login
* @param request
* @param init
* @returns
*/
LoginAsync: async (request: LoginRequest, init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Auth/Login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(request),
...init
});
return await response.json() as ApiResponse<LoginResponse>;
}
};
/**
*
*/
public Menu = {
/**
* /api/Menu/GetMenu
* @param init
* @returns
*/
GetMenuAsync: async (init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Menu/GetMenu`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
...init
});
return await response.json() as ApiResponse<MenuItem[]>;
},
/**
* /api/Menu/GetConfig
* @param configName
* @param init
* @returns
*/
GetConfigAsync: async (configName = 'TagResolution', init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Menu/GetConfig?configName=${configName}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
...init
});
return await response.json() as ApiResponse<TagResolutionConfig[]>;
}
};
/**
*
*/
public Help = {
/**
* /api/Help/FaultReporting
* @param request
* @param init
* @returns
*/
FaultReportingAsync: async (request: FaultReportingRequest, init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Help/FaultReporting`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(request),
...init
});
return await response.json() as ApiResponse<FaultReportingResponse>;
}
};
}
/**
* API
*/
export interface ApiResponse<T = any> {
/**
* 00
*/
status: number;
/**
* HTTP状态码
*/
code: number;
/**
*
*/
msg: string;
/**
*
*/
data: T;
}
/**
*
*/
export interface LoginRequest {
/**
*
*/
Account: string;
/**
*
*/
Password: string;
}
/**
*
*/
export interface LoginResponse {
/**
*
*/
IsNeedSendSMS: boolean;
/**
*
*/
Token: string;
/**
* Cookies
*/
Cookies: Array<{ Key: string, Value: string }>;
}
/**
*
*/
export interface MenuItem {
/**
*
*/
ShowName: string;
/**
*
*/
Url: string;
/**
*
*/
IconConfig: IconConfig;
/**
*
*/
Children: MenuItem[] | null;
}
/**
*
*/
export interface IconConfig {
/**
* 1x
*/
_1x: {
/**
*
*/
Default: string;
/**
*
*/
Selected: string;
};
/**
* 2x
*/
_2x: {
/**
*
*/
Default: string;
/**
*
*/
Selected: string;
};
}
/**
* TagResolution
*/
export interface TagResolutionConfig {
/**
* ','
*/
Resolutions: string;
/**
*
*/
Percentage: number;
/**
*
*/
SpecialPUrl: SpecialPUrlItem[];
}
/**
*
*/
export interface SpecialPUrlItem {
/**
* URL
*/
SPUrl: string;
/**
*
*/
SPPer: number;
}
/**
*
*/
export interface FaultReportingRequest {
/**
* IP
*/
IP: string;
/**
* URL
*/
Url: string;
/**
*
*/
Account: string;
/**
* Base64
*/
ImgBase64: string;
/**
*
*/
Explain: string;
}
/**
*
*/
export interface FaultReportingResponse {
/**
* ID
*/
FaultReportingId: string;
}
export const EIACDesktopApi = new EIAC_Desktop_Api();

View File

@ -1,33 +1,6 @@
import { WebviewTag } from 'electron';
import { TabGroup, Tab } from 'electron-tabs';
// 菜单项
interface MenuItem {
ShowName: string;
Url: string;
IconConfig: IconConfig;
Children: MenuItem[] | null;
}
// 图标配置
interface IconConfig {
_1x: {
Default: string;
Selected: string;
};
_2x: {
Default: string;
Selected: string;
};
}
// API响应接口
interface ApiResponse<T> {
status: number;
code: number;
msg: string;
data: T;
}
import { MenuItem, ApiResponse, EIACDesktopApi } from './EIAC_Desktop_Api';
const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup;
@ -55,18 +28,12 @@ function handleLogout() {
// Get menu list
async function getMenuList(): Promise<MenuItem[]> {
try {
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Menu/GetMenu', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
});
const result = await response.json() as ApiResponse<MenuItem[]>;
if (result.status === 0) {
return result.data;
const response: ApiResponse<MenuItem[]> = await EIACDesktopApi.Menu.GetMenuAsync();
if (response.status === 0) {
const menuList: MenuItem[] = response.data;
return menuList;
} else {
throw new Error(result.msg || '获取菜单列表失败');
throw new Error(response.msg || '获取菜单列表失败');
}
} catch (error) {
console.error('获取菜单列表失败:', error);

View File

@ -1,75 +1,46 @@
// 登录请求参数
interface LoginRequest {
Account: string;
Password: string;
}
import { LoginRequest, LoginResponse, EIACDesktopApi, ApiResponse } from './EIAC_Desktop_Api';
// 登录响应数据
interface LoginResponse {
IsNeedSendSMS: boolean;
Token: string;
Cookies: Array<{ Key: string, Value: string }>;
}
// API 响应类型
interface ApiResponse<T = any> {
status: number;
code: number;
msg: string;
data: T;
}
// 使用示例
// setCookies('key1=value1; key2=value2; key3=value3');
// 登录处理函数
async function handleLogin(account: string, password: string): Promise<void> {
/**
*
*/
async function handleLogin(request: LoginRequest): Promise<void> {
const errorMessage = document.getElementById('errorMessage') as HTMLDivElement;
if (!account || !password) {
if (!request.Account || !request.Password) {
errorMessage.style.display = 'block';
errorMessage.textContent = '请输入账号和密码';
return;
}
try {
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Auth/Login', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify({
Account: account,
Password: password
} as LoginRequest)
});
const rspBody = await response.json() as ApiResponse<LoginResponse>;
if (rspBody.status !== 0) {
const response: ApiResponse<LoginResponse> = await EIACDesktopApi.Auth.LoginAsync(request);
if (response.status !== 0) {
errorMessage.style.display = 'block';
errorMessage.textContent = rspBody.msg || '登录失败';
errorMessage.textContent = response.msg || '登录失败';
} else {
// 登录成功将data.Cookies拼接为字符串存储cookie
const cookies: string = JSON.stringify(rspBody.data.Cookies);
const cookies: string = JSON.stringify(response.data.Cookies);
window.electronAPI.setSessionStorage('cookie', cookies);
// 跳转到主页面
window.location.href = 'index.html';
}
} catch (error) {
errorMessage.style.display = 'block';
errorMessage.textContent = '网络错误,请稍后重试';
errorMessage.textContent = '网络错误,请稍后重试。错误原因:' + error;
console.error('登录失败:', error);
}
}
// 页面加载完成后绑定事件
document.addEventListener('DOMContentLoaded', () => {
/**
*
*/
document.addEventListener('DOMContentLoaded', async () => {
const loginButton = document.getElementById('btnLogin');
if (loginButton) {
loginButton.addEventListener('click', () => {
loginButton.addEventListener('click', async () => {
const account = (document.getElementById('txtAccount') as HTMLInputElement).value;
const password = (document.getElementById('txtPassword') as HTMLInputElement).value;
handleLogin(account, password);
await handleLogin({ Account: account, Password: password });
});
}
});