mirror of
https://github.com/PlayEdu/h5.git
synced 2025-12-25 23:59:35 +08:00
项目初始化
This commit is contained in:
33
src/api/course.ts
Normal file
33
src/api/course.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import client from "./internal/httpClient";
|
||||
|
||||
// 线上课详情
|
||||
export function detail(id: number) {
|
||||
return client.get(`/api/v1/course/${id}`, {});
|
||||
}
|
||||
|
||||
// 线上课课时详情
|
||||
export function play(courseId: number, id: number) {
|
||||
return client.get(`/api/v1/course/${courseId}/hour/${id}`, {});
|
||||
}
|
||||
|
||||
// 获取播放地址
|
||||
export function playUrl(courseId: number, hourId: number) {
|
||||
return client.get(`/api/v1/course/${courseId}/hour/${hourId}/play`, {});
|
||||
}
|
||||
|
||||
// 记录学员观看时长
|
||||
export function record(courseId: number, hourId: number, duration: number) {
|
||||
return client.post(`/api/v1/course/${courseId}/hour/${hourId}/record`, {
|
||||
duration,
|
||||
});
|
||||
}
|
||||
|
||||
//观看ping
|
||||
export function playPing(courseId: number, hourId: number) {
|
||||
return client.post(`/api/v1/course/${courseId}/hour/${hourId}/ping`, {});
|
||||
}
|
||||
|
||||
//最近学习课程
|
||||
export function latestLearn() {
|
||||
return client.get(`/api/v1/user/latest-learn`, {});
|
||||
}
|
||||
4
src/api/index.ts
Normal file
4
src/api/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * as login from "./login";
|
||||
export * as user from "./user";
|
||||
export * as course from "./course";
|
||||
export * as system from "./system";
|
||||
143
src/api/internal/httpClient.ts
Normal file
143
src/api/internal/httpClient.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import axios, { Axios, AxiosResponse } from "axios";
|
||||
import { Toast } from "antd-mobile";
|
||||
import { getToken, clearToken } from "../../utils/index";
|
||||
|
||||
const GoLogin = () => {
|
||||
clearToken();
|
||||
window.location.href = "/login";
|
||||
};
|
||||
|
||||
export class HttpClient {
|
||||
axios: Axios;
|
||||
|
||||
constructor(url: string) {
|
||||
this.axios = axios.create({
|
||||
baseURL: url,
|
||||
timeout: 15000,
|
||||
withCredentials: false,
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
//拦截器注册
|
||||
this.axios.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = getToken();
|
||||
token && (config.headers.Authorization = "Bearer " + token);
|
||||
return config;
|
||||
},
|
||||
(err) => {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
);
|
||||
|
||||
this.axios.interceptors.response.use(
|
||||
(response: AxiosResponse) => {
|
||||
let code = response.data.code; //业务返回代码
|
||||
let msg = response.data.msg; //错误消息
|
||||
|
||||
if (code === 0) {
|
||||
return Promise.resolve(response);
|
||||
} else {
|
||||
Toast.show({
|
||||
icon: "fail",
|
||||
content: msg,
|
||||
});
|
||||
}
|
||||
return Promise.reject(response);
|
||||
},
|
||||
// 当http的状态码非0
|
||||
(error) => {
|
||||
let status = error.response.status;
|
||||
if (status === 401) {
|
||||
Toast.show({
|
||||
icon: "fail",
|
||||
content: "请重新登录",
|
||||
});
|
||||
GoLogin();
|
||||
} else if (status === 404) {
|
||||
// 跳转到404页面
|
||||
} else if (status === 403) {
|
||||
// 跳转到无权限页面
|
||||
} else if (status === 500) {
|
||||
// 跳转到500异常页面
|
||||
}
|
||||
return Promise.reject(error.response);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
get(url: string, params: object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axios
|
||||
.get(url, {
|
||||
params: params,
|
||||
})
|
||||
.then((res) => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
destroy(url: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axios
|
||||
.delete(url)
|
||||
.then((res) => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
post(url: string, params: object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axios
|
||||
.post(url, params)
|
||||
.then((res) => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
put(url: string, params: object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axios
|
||||
.put(url, params)
|
||||
.then((res) => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
request(config: object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axios
|
||||
.request(config)
|
||||
.then((res) => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const APP_URL = import.meta.env.VITE_APP_URL || "";
|
||||
|
||||
const client = new HttpClient(APP_URL);
|
||||
|
||||
export default client;
|
||||
19
src/api/login.ts
Normal file
19
src/api/login.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import client from "./internal/httpClient";
|
||||
|
||||
export function login(
|
||||
email: string,
|
||||
password: string,
|
||||
captchaKey: string,
|
||||
captchaVal: string
|
||||
) {
|
||||
return client.post("/api/v1/auth/login/password", {
|
||||
email: email,
|
||||
password: password,
|
||||
captcha_key: captchaKey,
|
||||
captcha_val: captchaVal,
|
||||
});
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
return client.post("/api/v1/auth/logout", {});
|
||||
}
|
||||
9
src/api/system.ts
Normal file
9
src/api/system.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import client from "./internal/httpClient";
|
||||
|
||||
export function config() {
|
||||
return client.get("/api/v1/system/config", {});
|
||||
}
|
||||
|
||||
export function imageCaptcha() {
|
||||
return client.get("/api/v1/system/image-captcha", {});
|
||||
}
|
||||
31
src/api/user.ts
Normal file
31
src/api/user.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import client from "./internal/httpClient";
|
||||
|
||||
export function detail() {
|
||||
return client.get("/api/v1/user/detail", {});
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
export function password(oldPassword: string, newPassword: string) {
|
||||
return client.put("/api/v1/user/password", {
|
||||
old_password: oldPassword,
|
||||
new_password: newPassword,
|
||||
});
|
||||
}
|
||||
|
||||
// 学员课程
|
||||
export function coursesCategories() {
|
||||
return client.get("/api/v1/category/all", {});
|
||||
}
|
||||
export function courses(depId: number, categoryId: number) {
|
||||
return client.get("/api/v1/user/courses", {
|
||||
dep_id: depId,
|
||||
category_id: categoryId,
|
||||
});
|
||||
}
|
||||
|
||||
// 修改头像
|
||||
export function avatar(file: any) {
|
||||
return client.put("/api/v1/user/avatar", {
|
||||
file: file,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user