Files
qitongxue-wx/src/api/coupon.ts
2026-09-29 10:57:55 +08:00

92 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { get, post } from '@/api/request'
/**
* 优惠券域用户端封装(对接后端 `api/user/v1/promo.go`)。
*
* 约定(铁律 S1):响应 **snake_case**;请求沿用端点既有字段名 ——
* /coupon/validate、/coupon/usable 的入参为 **camelCase**(userCouponId / orderType / targetKey)。
*/
/** 券型:1 免单券(走支付)/ 2 展示券(仅展示) */
export const COUPON_TYPE_FREEBIE = 1
export const COUPON_TYPE_DISPLAY = 2
/** 券状态:0 未使用 1 已使用 2 已过期 3 已作废 */
export const COUPON_STATUS_UNUSED = 0
export const COUPON_STATUS_USED = 1
export const COUPON_STATUS_EXPIRED = 2
export const COUPON_STATUS_VOID = 3
export interface CouponSummary {
usable: number
/** 可抵扣券(仅免单券) */
freebie: number
expiring_soon: number
used: number
expired: number
}
export interface UserCoupon {
id: number
coupon_id: number
title: string
sub_title: string
coupon_type: number
kind: number
value: number
threshold_cents: number
status: number
expire_at: string
granted_at: string
used_at: string
}
export interface CouponMy {
summary: CouponSummary
list: UserCoupon[]
}
/** 当前订单可选用券(**只返免单券**,约定 S7) */
export interface UsableCoupon {
user_coupon_id: number
title: string
coupon_type: number
is_freebie: boolean
expire_at: string
estimated_paid_price_cents: number
/** 是否为「当前订单已选中」的券(仅当请求带 outTradeNo 时可能为 true) */
applied: boolean
}
export interface CouponValidateResult {
valid: boolean
coupon_type: number
is_freebie: boolean
product_id: string
price_cents: number
origin_price_cents: number
paid_price_cents: number
message: string
}
/** 我的优惠券(status 可空:0 可用 / 1 已用 / 2 过期) */
export function apiCouponMy(status?: number) {
return get<CouponMy>('/coupon/my', status === undefined ? undefined : { status })
}
/**
* 当前订单可选用的券(只返免单券)。
*
* outTradeNo 可选:传入本人订单号时,后端会**保底**返回该订单当前已选中的那张券
* (标记 applied=true)——因该券下单占用后 status 变 1,会被「仅返未使用券」的过滤排除,
* 导致用户只有一张券时列表变空、看不到也换不了(P0)。缺省为空串时行为与旧版一致。
*/
export function apiCouponUsable(orderType: number, targetKey: string, outTradeNo = '') {
return get<{ list: UsableCoupon[] }>('/coupon/usable', { orderType, targetKey, outTradeNo })
}
/** 校验免单券可用性(只读,不占用) */
export function apiCouponValidate(userCouponId: number, orderType: number, targetKey: string) {
return post<CouponValidateResult>('/coupon/validate', { userCouponId, orderType, targetKey })
}