调整前端目录结构
This commit is contained in:
1
tone-page-web/lib/api/auth/index.ts
Normal file
1
tone-page-web/lib/api/auth/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './login';
|
||||
53
tone-page-web/lib/api/auth/login.ts
Normal file
53
tone-page-web/lib/api/auth/login.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import fetcher, { StanderResponse } from "../fetcher"
|
||||
|
||||
interface LoginParams {
|
||||
type: 'password' | 'phone' | 'email';
|
||||
account?: string;
|
||||
password?: string;
|
||||
phone?: string;
|
||||
email?: string;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export const login = async (data: LoginParams): Promise<StanderResponse<{ token: string }>> => {
|
||||
if (data.type === 'password') {
|
||||
if (!data.account || !data.password) {
|
||||
return { statusCode: 400, message: '请输入账户和密码' }
|
||||
}
|
||||
if (data.account.length < 1 || data.account.length > 254) {
|
||||
return { statusCode: 400, message: '请输入正确的账户' }
|
||||
}
|
||||
if (data.password.length < 6 || data.password.length > 32) {
|
||||
return { statusCode: 400, message: '请输入正确的密码' }
|
||||
}
|
||||
} else if (data.type === 'phone') {
|
||||
if (!data.phone || !data.code) {
|
||||
return { statusCode: 400, message: '请输入手机号和验证码' }
|
||||
}
|
||||
if (data.phone.length !== 11) {
|
||||
return { statusCode: 400, message: '请输入正确的手机号' }
|
||||
}
|
||||
if (data.code.length != 6) {
|
||||
return { statusCode: 400, message: '请输入正确的验证码' }
|
||||
}
|
||||
} else if (data.type === 'email') {
|
||||
if (!data.email || !data.code) {
|
||||
return { statusCode: 400, message: '请输入邮箱和验证码' }
|
||||
}
|
||||
if (data.email.length < 1 || data.email.length > 254) {
|
||||
return { statusCode: 400, message: '请输入正确的邮箱' }
|
||||
}
|
||||
if (data.code.length != 6) {
|
||||
return { statusCode: 400, message: '请输入正确的验证码' }
|
||||
}
|
||||
} else {
|
||||
return { statusCode: 400, message: '登录方式异常' }
|
||||
}
|
||||
|
||||
return fetcher<{
|
||||
token: string;
|
||||
}>('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
}
|
||||
27
tone-page-web/lib/api/fetcher.ts
Normal file
27
tone-page-web/lib/api/fetcher.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export interface StanderResponse<T> {
|
||||
statusCode: number;
|
||||
message: string;
|
||||
data?: T;
|
||||
}
|
||||
|
||||
const fetcher = async<T>(url: string, options?: RequestInit): Promise<StanderResponse<T>> => {
|
||||
const res = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
// 自动带上 token
|
||||
...(typeof window !== 'undefined' && localStorage.getItem('token')
|
||||
? { Authorization: `Bearer ${localStorage.getItem('token')}` }
|
||||
: {}),
|
||||
},
|
||||
...options,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
export default fetcher
|
||||
2
tone-page-web/lib/api/index.ts
Normal file
2
tone-page-web/lib/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * as authApi from './auth/index';
|
||||
export * as verificationApi from './verification/index';
|
||||
1
tone-page-web/lib/api/verification/index.ts
Normal file
1
tone-page-web/lib/api/verification/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './send';
|
||||
15
tone-page-web/lib/api/verification/send.ts
Normal file
15
tone-page-web/lib/api/verification/send.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import fetcher from "../fetcher";
|
||||
|
||||
interface SendVerificationCodeParam {
|
||||
targetType: 'phone' | 'email';
|
||||
type: 'login';
|
||||
phone?: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
export const send = async (data: SendVerificationCodeParam) => {
|
||||
return fetcher('/api/verification/send', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user