Files
weiguang-jewelry-ledger/新账系统/web/src/lib/session.ts
T
黄健铭andClaude Opus 5.5 ec5889075f 新版界面(React + TypeScript + Vite + Ant Design):登录页与开单页
共用计算经构建插件直接导入 public/ 下的 UMD 源文件;请求客户端沿用旧界面的重试、
同一操作号自动确认、恢复原操作与账号切换约定;附 vitest 单元与组件测试。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 18:58:31 -08:00

54 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 登录状态:会话(登录前)与 /api/state(登录后)读取,以及登录、演示登录、试用初始化与退出。
// 登录类接口与旧界面相同:不带操作号、不自动重试;成功后由服务端设置共用的 mm_session cookie(Path=/)。
import type { LedgerClient } from './api';
import type { LedgerState, SessionInfo } from '../shared';
export async function loadSession(client: LedgerClient): Promise<SessionInfo> {
const session = await client.api<SessionInfo>('/api/session');
client.setUser(session.user?.id || '');
return session;
}
export async function loadState(client: LedgerClient): Promise<LedgerState> {
return client.api<LedgerState>('/api/state');
}
export async function login(client: LedgerClient, member: string, password: string) {
if (!member) throw new Error('请选择登录账号');
if (!password) throw new Error('请输入密码');
return client.api('/api/login', { member, password });
}
export async function demoLogin(client: LedgerClient, member: string) {
return client.api('/api/demo-login', { member });
}
export interface SetupInput {
storeName: string;
ownerLabel: string;
ownerLoginName: string;
ownerPassword: string;
}
export function validateSetup(input: SetupInput): SetupInput {
const value = {
storeName: input.storeName.trim(),
ownerLabel: input.ownerLabel.trim(),
ownerLoginName: input.ownerLoginName.trim(),
ownerPassword: input.ownerPassword,
};
if (!value.storeName || value.storeName.length > 80) throw new Error('店铺名称请填写1~80字');
if (!value.ownerLabel || value.ownerLabel.length > 30) throw new Error('店主显示名请填写1~30字');
if (!value.ownerLoginName || value.ownerLoginName.length > 60) throw new Error('店主登录名请填写1~60字');
if (value.ownerPassword.length < 12) throw new Error('店主密码至少12个字符');
return value;
}
export async function setup(client: LedgerClient, input: SetupInput) {
// 与旧界面一致:表单原样提交(密码不去空格),校验只拦明显的空白与长度。
validateSetup(input);
return client.api('/api/setup', input);
}
export async function logout(client: LedgerClient) {
if (client.pending()) throw new Error('请先恢复上次未确认的操作,再退出');
await client.api('/api/logout', {});
client.setUser('');
}
export function shopName(state?: LedgerState | null, session?: SessionInfo | null) {
return state?.shopSettings?.storeName || state?.printing?.store || session?.brand?.storeName || '珠宝台账';
}