53 lines
2.5 KiB
TypeScript
53 lines
2.5 KiB
TypeScript
// 登录状态:会话(登录前)与 /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 || '珠宝台账';
|
|||
|
|
}
|