| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- export type UserRole = "stu" | "teacher";
- export interface User {
- id: string;
- username: string;
- role: UserRole;
- name: string;
- avatar: string;
- }
- export interface AuthResponse {
- token: string;
- user: User;
- }
- const AUTH_USERS: Record<string, User> = {
- stu: {
- id: "231250001",
- username: "stu",
- role: "stu",
- name: "李子涵",
- avatar: "/images/profile-avatar.png",
- },
- teacher: {
- id: "tea_001",
- username: "teacher",
- role: "teacher",
- name: "刘钦",
- avatar: "/images/profile-avatar.png",
- },
- };
- const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
- export const authApi = {
- login: async (username: string, password: string): Promise<AuthResponse> => {
- await delay(300);
- if (password !== "passwd") {
- throw new Error("密码错误");
- }
- const user = AUTH_USERS[username];
- if (!user) {
- throw new Error("用户不存在");
- }
- return {
- token: `local_auth_${user.username}_${user.id}_${Date.now()}`,
- user,
- };
- },
- verifySession: async (token: string | null): Promise<User | null> => {
- await delay(120);
- if (!token) {
- return null;
- }
- return Object.values(AUTH_USERS).find((user) => token.includes(user.id)) ?? null;
- },
- };
|