80 lines
3.2 KiB
TypeScript
80 lines
3.2 KiB
TypeScript
/**
|
|
* Front-end API wrapper for this module.
|
|
* Call guide: import the named API object, then call the matching method from a Vue page or composable.
|
|
*/
|
|
import api from './index'
|
|
import type { IdType } from '@/utils/id'
|
|
|
|
/**
|
|
* Purpose: CartItem supports cart.ts by holding or executing the cart item page or API role.
|
|
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
interface CartItem {
|
|
productId: IdType
|
|
quantity: number
|
|
}
|
|
|
|
/**
|
|
* cartApi groups all API calls for this page domain.
|
|
* Call guide: pass the typed payload or identifier into the method below, and use the returned Promise in the page script.
|
|
*/
|
|
export const cartApi = {
|
|
// 添加作品到购物车
|
|
/**
|
|
* `addToCart` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param item: CartItem caller-supplied value for `item: CartItem`
|
|
*/
|
|
addToCart: (item: CartItem) => {
|
|
return api.post('/cart/add', item)
|
|
},
|
|
|
|
// 从购物车移除作品
|
|
/**
|
|
* `removeFromCart` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param productId: IdType caller-supplied value for `productId: IdType`
|
|
*/
|
|
removeFromCart: (productId: IdType) => {
|
|
return api.delete(`/cart/remove/${productId}`)
|
|
},
|
|
|
|
// 批量从购物车移除作品
|
|
/**
|
|
* `batchRemoveFromCart` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param cartItemIds: IdType[] caller-supplied value for `cartItemIds: IdType[]`
|
|
*/
|
|
batchRemoveFromCart: (cartItemIds: IdType[]) => {
|
|
return api.post('/cart/batch-remove', cartItemIds)
|
|
},
|
|
|
|
// 更新购物车作品数量
|
|
/**
|
|
* `updateCartQuantity` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param item: CartItem caller-supplied value for `item: CartItem`
|
|
*/
|
|
updateCartQuantity: (item: CartItem) => {
|
|
return api.put('/cart/update', item)
|
|
},
|
|
|
|
// 获取购物车列表
|
|
/**
|
|
* `getCartList` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
*/
|
|
getCartList: () => {
|
|
return api.get('/cart/list')
|
|
},
|
|
|
|
// 清空购物车
|
|
/**
|
|
* `clearCart` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
*/
|
|
clearCart: () => {
|
|
return api.delete('/cart/clear')
|
|
}
|
|
}
|