From 51087027c86482e0ea9773fc8530d6a7936cb527 Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 15 May 2025 00:39:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E9=9B=B6=E6=95=A3=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=B0=81=E8=A3=85=E4=B8=BAEIAC=5FDesktop=5FApi?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9EGetConfig=E5=92=8CFaultReporting?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EIAC_Desktop_Api.ts | 276 ++++++++++++++++++++++++++++++++++++++++ src/index.ts | 45 +------ src/login.ts | 65 +++------- 3 files changed, 300 insertions(+), 86 deletions(-) create mode 100644 src/EIAC_Desktop_Api.ts diff --git a/src/EIAC_Desktop_Api.ts b/src/EIAC_Desktop_Api.ts new file mode 100644 index 0000000..c1ac81a --- /dev/null +++ b/src/EIAC_Desktop_Api.ts @@ -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; + } + }; + + /** + * 菜单接口 + */ + 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; + }, + /** + * 获取配置 /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; + } + }; + + /** + * 帮助接口 + */ + 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; + } + }; +} + +/** + * API 响应 + */ +export interface ApiResponse { + /** + * 状态。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(); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a956f6f..2055ef5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { - 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 { 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; - if (result.status === 0) { - return result.data; + const response: ApiResponse = 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); diff --git a/src/login.ts b/src/login.ts index b72b318..4ff745d 100644 --- a/src/login.ts +++ b/src/login.ts @@ -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 { - status: number; - code: number; - msg: string; - data: T; -} - -// 使用示例 -// setCookies('key1=value1; key2=value2; key3=value3'); - -// 登录处理函数 -async function handleLogin(account: string, password: string): Promise { +/** + * 登录处理函数 + */ +async function handleLogin(request: LoginRequest): Promise { 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; - - if (rspBody.status !== 0) { + const response: ApiResponse = 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 }); }); } }); \ No newline at end of file