119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
|
|
import { apiClient, unwrapData } from '@/shared/api/client'
|
|
import type { ApiResponse } from '@/shared/api/types'
|
|
import type {
|
|
CounterCheckoutPayload,
|
|
CreateDisputePayload,
|
|
SubmitCheckoutPayload,
|
|
UserHandoffRecord,
|
|
UserOrderDetail,
|
|
UserOrderPage,
|
|
UserPayment,
|
|
} from '@/features/orders/types'
|
|
|
|
export const MY_ORDER_PAGE_SIZE = 20
|
|
|
|
export function fetchMyOrders(page = 1, pageSize = MY_ORDER_PAGE_SIZE) {
|
|
return unwrapData<Partial<UserOrderPage>>(
|
|
apiClient.get<ApiResponse<Partial<UserOrderPage>>>('/orders', {
|
|
params: { page, page_size: pageSize },
|
|
})
|
|
).then(data => ({
|
|
items: data.items ?? [],
|
|
total: data.total ?? 0,
|
|
page: data.page ?? page,
|
|
page_size: data.page_size ?? pageSize,
|
|
}))
|
|
}
|
|
|
|
export function useMyOrders(enabled = true) {
|
|
return useInfiniteQuery({
|
|
queryKey: ['my-orders'],
|
|
queryFn: ({ pageParam }) => fetchMyOrders(pageParam, MY_ORDER_PAGE_SIZE),
|
|
initialPageParam: 1,
|
|
getNextPageParam: last =>
|
|
last.page * last.page_size < last.total ? last.page + 1 : undefined,
|
|
enabled,
|
|
})
|
|
}
|
|
|
|
export function fetchMyOrder(id: number | string) {
|
|
return unwrapData<UserOrderDetail>(apiClient.get<ApiResponse<UserOrderDetail>>(`/orders/${id}`))
|
|
}
|
|
|
|
export function useMyOrder(id?: string, enabled = true) {
|
|
return useQuery({
|
|
queryKey: ['my-order', id],
|
|
queryFn: () => fetchMyOrder(id as string),
|
|
enabled: Boolean(id) && enabled,
|
|
})
|
|
}
|
|
|
|
export async function cancelMyOrder(id: number | string) {
|
|
await apiClient.post(`/orders/${id}/cancel`)
|
|
}
|
|
|
|
export async function startOrderPayment(id: number | string, payWay: 'WXZF' | 'ZFBZF') {
|
|
return unwrapData<UserPayment>(
|
|
apiClient.post<ApiResponse<UserPayment>>(`/orders/${id}/start-payment`, {
|
|
pay_way: payWay,
|
|
jspay_flag: '',
|
|
})
|
|
)
|
|
}
|
|
|
|
export async function queryOrderPayment(id: number | string) {
|
|
return unwrapData<UserPayment>(
|
|
apiClient.get<ApiResponse<UserPayment>>(`/orders/${id}/query-payment`)
|
|
)
|
|
}
|
|
|
|
export async function submitHandoff(id: number | string, content: string) {
|
|
return unwrapData<UserHandoffRecord>(
|
|
apiClient.post<ApiResponse<UserHandoffRecord>>(`/orders/${id}/handoff`, { content })
|
|
)
|
|
}
|
|
|
|
export async function confirmReceive(id: number | string) {
|
|
await apiClient.post(`/orders/${id}/confirm-receive`)
|
|
}
|
|
|
|
export function fetchHandoffRecords(id: number | string) {
|
|
return unwrapData<{ items?: UserHandoffRecord[] }>(
|
|
apiClient.get<ApiResponse<{ items?: UserHandoffRecord[] }>>(`/orders/${id}/handoff-records`)
|
|
).then(data => data.items ?? [])
|
|
}
|
|
|
|
export function useHandoffRecords(id?: string, enabled = true) {
|
|
return useQuery({
|
|
queryKey: ['my-order-handoffs', id],
|
|
queryFn: () => fetchHandoffRecords(id as string),
|
|
enabled: Boolean(id) && enabled,
|
|
})
|
|
}
|
|
|
|
export async function submitCheckout(id: number | string, payload: SubmitCheckoutPayload) {
|
|
await apiClient.post(`/orders/${id}/checkout`, payload)
|
|
}
|
|
|
|
export async function confirmCheckout(id: number | string) {
|
|
await apiClient.post(`/orders/${id}/checkout/confirm`)
|
|
}
|
|
|
|
export async function counterCheckout(id: number | string, payload: CounterCheckoutPayload) {
|
|
await apiClient.post(`/orders/${id}/checkout/counter`, payload)
|
|
}
|
|
|
|
export async function acceptCheckout(id: number | string) {
|
|
await apiClient.post(`/orders/${id}/checkout/accept`)
|
|
}
|
|
|
|
export async function createOrderDispute(id: number | string, payload: CreateDisputePayload) {
|
|
await apiClient.post(`/orders/${id}/dispute`, payload)
|
|
}
|
|
|
|
export async function cancelOrderDispute(id: number | string) {
|
|
await apiClient.post(`/orders/${id}/dispute/cancel`)
|
|
}
|