将零散的接口封装为EIAC_Desktop_Api,新增GetConfig和FaultReporting接口。
This commit is contained in:
parent
3cec21a7f3
commit
51087027c8
276
src/EIAC_Desktop_Api.ts
Normal file
276
src/EIAC_Desktop_Api.ts
Normal 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> {
|
||||||
|
/**
|
||||||
|
* 状态。0:成功,非0:失败
|
||||||
|
*/
|
||||||
|
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();
|
45
src/index.ts
45
src/index.ts
@ -1,33 +1,6 @@
|
|||||||
import { WebviewTag } from 'electron';
|
import { WebviewTag } from 'electron';
|
||||||
import { TabGroup, Tab } from 'electron-tabs';
|
import { TabGroup, Tab } from 'electron-tabs';
|
||||||
|
import { MenuItem, ApiResponse, EIACDesktopApi } from './EIAC_Desktop_Api';
|
||||||
// 菜单项
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup;
|
const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup;
|
||||||
|
|
||||||
@ -55,18 +28,12 @@ function handleLogout() {
|
|||||||
// Get menu list
|
// Get menu list
|
||||||
async function getMenuList(): Promise<MenuItem[]> {
|
async function getMenuList(): Promise<MenuItem[]> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Menu/GetMenu', {
|
const response: ApiResponse<MenuItem[]> = await EIACDesktopApi.Menu.GetMenuAsync();
|
||||||
method: 'POST',
|
if (response.status === 0) {
|
||||||
headers: {
|
const menuList: MenuItem[] = response.data;
|
||||||
'Content-Type': 'application/json; charset=utf-8'
|
return menuList;
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await response.json() as ApiResponse<MenuItem[]>;
|
|
||||||
if (result.status === 0) {
|
|
||||||
return result.data;
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(result.msg || '获取菜单列表失败');
|
throw new Error(response.msg || '获取菜单列表失败');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取菜单列表失败:', error);
|
console.error('获取菜单列表失败:', error);
|
||||||
|
65
src/login.ts
65
src/login.ts
@ -1,75 +1,46 @@
|
|||||||
// 登录请求参数
|
import { LoginRequest, LoginResponse, EIACDesktopApi, ApiResponse } from './EIAC_Desktop_Api';
|
||||||
interface LoginRequest {
|
|
||||||
Account: string;
|
|
||||||
Password: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 登录响应数据
|
/**
|
||||||
interface LoginResponse {
|
* 登录处理函数
|
||||||
IsNeedSendSMS: boolean;
|
*/
|
||||||
Token: string;
|
async function handleLogin(request: LoginRequest): Promise<void> {
|
||||||
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> {
|
|
||||||
const errorMessage = document.getElementById('errorMessage') as HTMLDivElement;
|
const errorMessage = document.getElementById('errorMessage') as HTMLDivElement;
|
||||||
|
|
||||||
if (!account || !password) {
|
if (!request.Account || !request.Password) {
|
||||||
errorMessage.style.display = 'block';
|
errorMessage.style.display = 'block';
|
||||||
errorMessage.textContent = '请输入账号和密码';
|
errorMessage.textContent = '请输入账号和密码';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Auth/Login', {
|
const response: ApiResponse<LoginResponse> = await EIACDesktopApi.Auth.LoginAsync(request);
|
||||||
method: 'POST',
|
if (response.status !== 0) {
|
||||||
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) {
|
|
||||||
errorMessage.style.display = 'block';
|
errorMessage.style.display = 'block';
|
||||||
errorMessage.textContent = rspBody.msg || '登录失败';
|
errorMessage.textContent = response.msg || '登录失败';
|
||||||
} else {
|
} else {
|
||||||
// 登录成功,将data.Cookies拼接为字符串,存储cookie
|
// 登录成功,将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.electronAPI.setSessionStorage('cookie', cookies);
|
||||||
// 跳转到主页面
|
// 跳转到主页面
|
||||||
window.location.href = 'index.html';
|
window.location.href = 'index.html';
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errorMessage.style.display = 'block';
|
errorMessage.style.display = 'block';
|
||||||
errorMessage.textContent = '网络错误,请稍后重试';
|
errorMessage.textContent = '网络错误,请稍后重试。错误原因:' + error;
|
||||||
|
console.error('登录失败:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 页面加载完成后绑定事件
|
/**
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
* 页面加载完成后绑定事件
|
||||||
|
*/
|
||||||
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
const loginButton = document.getElementById('btnLogin');
|
const loginButton = document.getElementById('btnLogin');
|
||||||
if (loginButton) {
|
if (loginButton) {
|
||||||
loginButton.addEventListener('click', () => {
|
loginButton.addEventListener('click', async () => {
|
||||||
const account = (document.getElementById('txtAccount') as HTMLInputElement).value;
|
const account = (document.getElementById('txtAccount') as HTMLInputElement).value;
|
||||||
const password = (document.getElementById('txtPassword') as HTMLInputElement).value;
|
const password = (document.getElementById('txtPassword') as HTMLInputElement).value;
|
||||||
handleLogin(account, password);
|
await handleLogin({ Account: account, Password: password });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user