完成管理员user-list
This commit is contained in:
1
tone-page-web/lib/api/admin/index.ts
Normal file
1
tone-page-web/lib/api/admin/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * as user from './user/index';
|
||||
1
tone-page-web/lib/api/admin/user/index.ts
Normal file
1
tone-page-web/lib/api/admin/user/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './list';
|
||||
22
tone-page-web/lib/api/admin/user/list.ts
Normal file
22
tone-page-web/lib/api/admin/user/list.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { User } from "@/lib/types/user"
|
||||
import fetcher from "../../fetcher"
|
||||
|
||||
export interface UserListParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export interface UserListResponse {
|
||||
items: User[],
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
export function list(params?: UserListParams): Promise<UserListResponse> {
|
||||
const searchParams = new URLSearchParams()
|
||||
if (params?.page) searchParams.set('page', params.page.toString())
|
||||
if (params?.pageSize) searchParams.set('pageSize', params.pageSize.toString())
|
||||
|
||||
return fetcher<UserListResponse>('/api/admin/user')
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import fetcher, { StanderResponse } from "../fetcher"
|
||||
import fetcher, { ApiError } from "../fetcher"
|
||||
|
||||
interface LoginParams {
|
||||
type: 'password' | 'phone' | 'email';
|
||||
@@ -9,39 +9,39 @@ interface LoginParams {
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export const login = async (data: LoginParams): Promise<StanderResponse<{ token: string }>> => {
|
||||
export const login = async (data: LoginParams): Promise<{ token: string }> => {
|
||||
if (data.type === 'password') {
|
||||
if (!data.account || !data.password) {
|
||||
return { statusCode: 400, message: '请输入账户和密码' }
|
||||
throw new ApiError(400, '请输入账户和密码')
|
||||
}
|
||||
if (data.account.length < 1 || data.account.length > 254) {
|
||||
return { statusCode: 400, message: '请输入正确的账户' }
|
||||
throw new ApiError(400, '请输入正确的账户')
|
||||
}
|
||||
if (data.password.length < 6 || data.password.length > 32) {
|
||||
return { statusCode: 400, message: '请输入正确的密码' }
|
||||
throw new ApiError(400, '请输入正确的密码')
|
||||
}
|
||||
} else if (data.type === 'phone') {
|
||||
if (!data.phone || !data.code) {
|
||||
return { statusCode: 400, message: '请输入手机号和验证码' }
|
||||
throw new ApiError(400, '请输入手机号和验证码')
|
||||
}
|
||||
if (data.phone.length !== 11) {
|
||||
return { statusCode: 400, message: '请输入正确的手机号' }
|
||||
throw new ApiError(400, '请输入正确的手机号')
|
||||
}
|
||||
if (data.code.length != 6) {
|
||||
return { statusCode: 400, message: '请输入正确的验证码' }
|
||||
throw new ApiError(400, '请输入正确的验证码')
|
||||
}
|
||||
} else if (data.type === 'email') {
|
||||
if (!data.email || !data.code) {
|
||||
return { statusCode: 400, message: '请输入邮箱和验证码' }
|
||||
throw new ApiError(400, '请输入邮箱和验证码')
|
||||
}
|
||||
if (data.email.length < 1 || data.email.length > 254) {
|
||||
return { statusCode: 400, message: '请输入正确的邮箱' }
|
||||
throw new ApiError(400, '请输入正确的邮箱')
|
||||
}
|
||||
if (data.code.length != 6) {
|
||||
return { statusCode: 400, message: '请输入正确的验证码' }
|
||||
throw new ApiError(400, '请输入正确的验证码')
|
||||
}
|
||||
} else {
|
||||
return { statusCode: 400, message: '登录方式异常' }
|
||||
throw new ApiError(400, '登录方式异常')
|
||||
}
|
||||
|
||||
return fetcher<{
|
||||
|
||||
@@ -4,7 +4,18 @@ export interface StanderResponse<T> {
|
||||
data?: T;
|
||||
}
|
||||
|
||||
const fetcher = async<T>(url: string, options?: RequestInit): Promise<StanderResponse<T>> => {
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public statusCode: number,
|
||||
message: string,
|
||||
public data?: unknown,
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
const fetcher = async<T>(url: string, options?: RequestInit): Promise<T> => {
|
||||
const res = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
@@ -17,11 +28,12 @@ const fetcher = async<T>(url: string, options?: RequestInit): Promise<StanderRes
|
||||
...options,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
return await res.json();
|
||||
const result = await res.json();
|
||||
if (result.statusCode !== 200) {
|
||||
throw new ApiError(result.statusCode, result.message, result.data);
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
return result.data as T;
|
||||
}
|
||||
|
||||
export default fetcher
|
||||
@@ -1,2 +1,3 @@
|
||||
export * as authApi from './auth/index';
|
||||
export * as verificationApi from './verification/index';
|
||||
export * as verificationApi from './verification/index';
|
||||
export * as AdminApi from './admin/index';
|
||||
@@ -8,7 +8,7 @@ interface SendVerificationCodeParam {
|
||||
}
|
||||
|
||||
export const send = async (data: SendVerificationCodeParam) => {
|
||||
return fetcher('/api/verification/send', {
|
||||
return fetcher<boolean>('/api/verification/send', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user