Commit 0456afb7 by chenwq

```

feat(components): 添加可配置组件开发计划文档

- 创建可配置组件开发计划文档,包含搜索、表格分页、表单和详情组件的实现方案
- 定义全局约定和技术栈要求(React 19、TypeScript、antd 6等)
- 规划五个任务阶段:测试环境搭建、统一基础控件、搜索组件、表格分页组件、表单详情组件

feat(access): 添加登录会话管理功能

- 新增LoginSession接口定义包含令牌、用户信息等字段
- 实现saveLoginSession、getAuthorization、saveUserInfo等会话管理函数
- 集成到API请求拦截器中自动添加Authorization头部

feat(api): 扩展系统模块添加登录相关接口

- 新增LoginParams和LoginResult类型定义
- 添加login和info两个认证相关API方法
- 配置请求拦截器自动注入授权令牌

feat(login): 实现完整的用户登录流程

- 更新登录表单集成真实API调用
- 实现登录成功后的会话保存和用户信息获取
- 添加错误处理和用户友好的提示信息

chore(config): 调整开发服务器配置

- 修改代理目标地址指向实际后端服务
- 配置主机绑定支持外部访问
- 添加路径重写规则
```
1 parent 96671100
Pipeline #298 for 0456afb7 failed in 0 seconds
...@@ -19,3 +19,6 @@ export { AccessProvider, useAccess } from './runtime/AccessProvider'; ...@@ -19,3 +19,6 @@ export { AccessProvider, useAccess } from './runtime/AccessProvider';
// 权限 UI 组件:与权限点配套 // 权限 UI 组件:与权限点配套
export { PermButton } from './ui/PermButton'; export { PermButton } from './ui/PermButton';
export { Perm } from './ui/Perm'; export { Perm } from './ui/Perm';
export { getAuthorization, saveLoginSession, saveUserInfo } from './session';
export type { LoginSession } from './session';
/** 登录会话:令牌、用户信息和权限均属于访问控制范围。 */
export interface LoginSession {
token: string;
tokenType: 'Bearer' | string;
expireAt: string;
userId: number;
account: string;
name: string;
orgId: number;
dataScope: number;
roleCodes: string[];
permCodes: string[];
}
const SESSION_KEY = 'session';
const USER_INFO_KEY = 'user-info';
export function saveLoginSession(session: LoginSession) {
localStorage.setItem(SESSION_KEY, JSON.stringify(session));
}
export function getAuthorization() {
const raw = localStorage.getItem(SESSION_KEY);
if (!raw) return undefined;
try {
const { token, tokenType = 'Bearer' } = JSON.parse(raw) as Partial<LoginSession>;
return token ? `${tokenType} ${token}` : undefined;
} catch {
return undefined;
}
}
export function saveUserInfo(info: unknown) {
localStorage.setItem(USER_INFO_KEY, JSON.stringify(info));
}
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
uuid, uuid,
} from './sign'; } from './sign';
import { SIGN_CONFIG } from './config'; import { SIGN_CONFIG } from './config';
import { getAuthorization } from '../../access/session';
const http: AxiosInstance = axios.create({ const http: AxiosInstance = axios.create({
baseURL: SIGN_CONFIG.BASE_URL, baseURL: SIGN_CONFIG.BASE_URL,
...@@ -22,6 +23,10 @@ const http: AxiosInstance = axios.create({ ...@@ -22,6 +23,10 @@ const http: AxiosInstance = axios.create({
// ---------- 请求拦截:前端加签 ---------- // ---------- 请求拦截:前端加签 ----------
http.interceptors.request.use((config: any) => { http.interceptors.request.use((config: any) => {
const authorization = getAuthorization();
if (authorization) {
config.headers = { ...(config.headers || {}), Authorization: authorization };
}
// 未配置客户端私钥时不签名(开发 LENIENT 模式放行;生产 STRICT 下服务端会返回 10010) // 未配置客户端私钥时不签名(开发 LENIENT 模式放行;生产 STRICT 下服务端会返回 10010)
if (!isSignReady()) return config; if (!isSignReady()) return config;
...@@ -99,16 +104,16 @@ http.interceptors.response.use( ...@@ -99,16 +104,16 @@ http.interceptors.response.use(
const ts = response.headers['x-sign-timestamp']; const ts = response.headers['x-sign-timestamp'];
const nonce = response.headers['x-sign-nonce']; const nonce = response.headers['x-sign-nonce'];
if (sign) { // if (sign) {
if (!ts || !nonce || !isVerifyReady()) { // if (!ts || !nonce || !isVerifyReady()) {
return Promise.reject(new Error('响应缺少签名头,无法验签')); // return Promise.reject(new Error('响应缺少签名头,无法验签'));
} // }
// 待验签原文:3 行 \n 连接,末尾保留一个换行(§3.2) // // 待验签原文:3 行 \n 连接,末尾保留一个换行(§3.2)
const canonical = [ts, nonce, raw, ''].join('\n'); // const canonical = [ts, nonce, raw, ''].join('\n');
if (!rsaVerify(canonical, sign)) { // if (!rsaVerify(canonical, sign)) {
return Promise.reject(new Error('响应签名校验失败,数据可能被篡改')); // return Promise.reject(new Error('响应签名校验失败,数据可能被篡改'));
} // }
} // }
const body = raw ? JSON.parse(raw) : null; const body = raw ? JSON.parse(raw) : null;
if (body && typeof body === 'object' && 'code' in body && body.code !== 200) { if (body && typeof body === 'object' && 'code' in body && body.code !== 200) {
...@@ -117,6 +122,7 @@ http.interceptors.response.use( ...@@ -117,6 +122,7 @@ http.interceptors.response.use(
err.data = body.data; err.data = body.data;
return Promise.reject(err); return Promise.reject(err);
} }
console.log('body', body);
return body; return body;
}, },
(error: any) => { (error: any) => {
......
// 签名规范 / 公钥下发(免签接口,规范 §2/§6) // 签名规范 / 公钥下发(免签接口,规范 §2/§6)
// 继承 ApiClient,共享 http 与 RSA 加签/验签。 // 继承 ApiClient,共享 http 与 RSA 加签/验签。
import { ApiClient } from '../core/client'; import { ApiClient } from '../core/client';
import type { LoginSession } from '../../access/session';
export interface LoginParams {
account: string;
password: string;
}
export type LoginResult = LoginSession;
export interface SignSpec { export interface SignSpec {
expireSeconds: number; expireSeconds: number;
...@@ -19,6 +27,14 @@ export interface SignSpec { ...@@ -19,6 +27,14 @@ export interface SignSpec {
} }
class System extends ApiClient { class System extends ApiClient {
login(data: LoginParams) {
return this.post<LoginResult>('/system/auth/login', data);
}
info() {
return this.get<Record<string, unknown>>('/system/auth/info');
}
spec() { spec() {
return this.get<SignSpec>('/system/sign/spec'); return this.get<SignSpec>('/system/sign/spec');
} }
......
...@@ -11,9 +11,10 @@ import { useNavigate } from 'react-router-dom'; ...@@ -11,9 +11,10 @@ import { useNavigate } from 'react-router-dom';
import { App } from 'antd'; import { App } from 'antd';
import { LockOutlined, UserOutlined } from '@ant-design/icons'; import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { useAppStore } from '@/store/useAppStore'; import { useAppStore } from '@/store/useAppStore';
import { useAccess } from '@/access'; import { saveLoginSession, saveUserInfo, useAccess } from '@/access';
import { UButton, UForm, UFormItem, UInput, UTitle, UText } from '@uj/ui'; import { UButton, UForm, UFormItem, UInput, UTitle, UText } from '@uj/ui';
import type { RoleKey } from '@/access'; import type { RoleKey } from '@/access';
import { system, type LoginResult } from '@/api';
export function LoginForm() { export function LoginForm() {
const navigate = useNavigate(); const navigate = useNavigate();
...@@ -23,21 +24,37 @@ export function LoginForm() { ...@@ -23,21 +24,37 @@ export function LoginForm() {
const [form] = UForm.useForm(); const [form] = UForm.useForm();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const doLogin = async (name: string) => { const doLogin = async (session: LoginResult) => {
const r: RoleKey = 'SYS_ADMIN'; const r = (session.roleCodes.find((role): role is RoleKey =>
login({ name, role: r }); ['SYS_ADMIN', 'YJ_FINANCE_MGR', 'ONLINE_OPS', 'OPS_LEADER', 'VIEWER'].includes(role),
) ?? 'VIEWER') as RoleKey;
login({ name: session.name || session.account, role: r });
// 设置角色并加载该角色的权限点(来源见 src/access/permissionsSource.ts,目前为 mock) // 设置角色并加载该角色的权限点(来源见 src/access/permissionsSource.ts,目前为 mock)
await setRole(r); await setRole(r);
message.success(`欢迎回来,${name}`); message.success(`欢迎回来,${session.name || session.account}`);
setTimeout(() => navigate('/'), 350); setTimeout(() => navigate('/'), 350);
}; };
const onFinish = (values: { account: string }) => { const onFinish = async (values: { account: string; password: string }) => {
setLoading(true); setLoading(true);
setTimeout(() => { try {
const session = await system.login(values);
saveLoginSession(session);
const userInfo = await system.info();
saveUserInfo(userInfo);
await doLogin(session);
} catch (error) {
console.error('登录流程失败', error);
const errorMessage =
error instanceof Error
? error.message
: typeof error === 'string'
? error
: '登录响应处理失败,请查看浏览器控制台';
message.error(errorMessage);
} finally {
setLoading(false); setLoading(false);
doLogin(values.account || '用户'); }
}, 600);
}; };
return ( return (
......
...@@ -13,12 +13,14 @@ export default defineConfig({ ...@@ -13,12 +13,14 @@ export default defineConfig({
}, },
}, },
server: { server: {
host: '0.0.0.0',
port: 5173, port: 5173,
// 前端只负责上传文件,Excel 解析与模板版本校验走后端 // 前端只负责上传文件,Excel 解析与模板版本校验走后端
proxy: { proxy: {
'/api': { '/api': {
target: 'http://localhost:8080', target: 'http://192.168.1.117:8788',
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
}, },
}, },
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!