76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import http from "@/http/HttpClient";
|
|
import { useUser } from "@/stores/useUser";
|
|
import type { PagedData } from "@/types/api/common";
|
|
import type { Lesson } from "@/types/api/lesson";
|
|
import type { User } from "@/types/api/user";
|
|
|
|
export interface LoginRequest extends Record<string, string> {
|
|
email: string;
|
|
password: string;
|
|
remember: string;
|
|
}
|
|
|
|
export default class BussApi {
|
|
static login(params: LoginRequest): Promise<{ token: string }> {
|
|
return http
|
|
.server()
|
|
.post("/login", params)
|
|
.then((res) => res.data);
|
|
}
|
|
|
|
static profile(token: string): Promise<User> {
|
|
return http
|
|
.server()
|
|
.get("/user/online", {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
})
|
|
.then((res) => res.data);
|
|
}
|
|
|
|
static lessons(page: number = 1, limit: number = 20) {
|
|
const user = useUser();
|
|
return http
|
|
.server()
|
|
.get<PagedData<Lesson>>("/lesson/task", {
|
|
headers: {
|
|
Authorization: `Bearer ${user.token}`,
|
|
},
|
|
params: { page, limit },
|
|
})
|
|
.then((res) => {
|
|
if (user.hasJobTag("A")) {
|
|
res.data.data = res.data.data.filter(
|
|
(lesson) => lesson.user_id === user.userinfo?.id
|
|
);
|
|
}
|
|
return res.data;
|
|
});
|
|
}
|
|
|
|
static course(id: number): Promise<Lesson> {
|
|
const user = useUser();
|
|
return http
|
|
.server()
|
|
.get(`/lesson/task/${id}`, {
|
|
headers: {
|
|
Authorization: `Bearer ${user.token}`,
|
|
},
|
|
})
|
|
.then((res) => res.data);
|
|
}
|
|
|
|
static editCourse(id: number, params: Partial<Lesson>): Promise<Lesson> {
|
|
const user = useUser();
|
|
return http
|
|
.server()
|
|
.put(`/lesson/task/${id}`, params, {
|
|
headers: {
|
|
Authorization: `Bearer ${user.token}`,
|
|
},
|
|
})
|
|
.then((res) => res.data);
|
|
}
|
|
}
|