223 lines
5.4 KiB
TypeScript
223 lines
5.4 KiB
TypeScript
import {
|
|
getUnixTime,
|
|
startOfDay,
|
|
endOfDay,
|
|
differenceInMonths,
|
|
} from '@seed-next/date'
|
|
|
|
import type {
|
|
ReqCouponUseParams,
|
|
ReqCouponUse,
|
|
ResCouponUse,
|
|
ResCouponList,
|
|
ResCouponListItem,
|
|
ReqCouponList,
|
|
ReqCouponListParams,
|
|
} from '#layers/types/api/couponData'
|
|
|
|
/**
|
|
* 쿠폰 관련 Composable 함수입니다.
|
|
* @returns
|
|
*/
|
|
export const useCoupon = () => {
|
|
// Refs
|
|
const couponList = ref<Array<ResCouponListItem> | null>([])
|
|
const totalCount = ref<number>(0)
|
|
const hasNoCouponList = ref<boolean>(true)
|
|
|
|
/**
|
|
* 쿠폰 등록 내역 데이터를 세팅합니다.
|
|
* @param {ResCouponList} newValue - 쿠폰 등록 내역 데이터
|
|
* @returns
|
|
*/
|
|
const setCouponList = (newValue: ResCouponList) => {
|
|
if (newValue.value.total_count === 0) {
|
|
hasNoCouponList.value = true
|
|
couponList.value = null
|
|
totalCount.value = 0
|
|
return
|
|
} else {
|
|
hasNoCouponList.value = false
|
|
couponList.value = newValue.value.list
|
|
totalCount.value = newValue.value.total_count
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 쿠폰 등록 API 호출 함수입니다.
|
|
* @param {ReqCouponUse} req - 쿠폰 등록 API 요청 데이터
|
|
* @returns {ResCouponUse} res - 쿠폰 등록 API 응답 데이터
|
|
*/
|
|
const postCouponUse = async (req: ReqCouponUse) => {
|
|
let res: ResCouponUse = {} as ResCouponUse
|
|
try {
|
|
const runtimeConfig = useRuntimeConfig()
|
|
const stoveApiUrl = runtimeConfig.public.stoveApiUrl
|
|
const userTokenType = req.user_token_type || 'web'
|
|
|
|
const headers = {
|
|
Authorization: `Bearer ${req.accessToken}`,
|
|
}
|
|
const query: ReqCouponUseParams = {
|
|
game_code: req.game_code || '',
|
|
coupon_no: req.coupon_no || '',
|
|
client_ipaddr: req.client_ipaddr || '',
|
|
world_no: req.world_no || '',
|
|
character_no: req.character_no || '',
|
|
}
|
|
|
|
const apiUrl = `${stoveApiUrl}/coupon/v3.0/${userTokenType}/coupon/use`
|
|
|
|
res = (await commonFetch('POST', apiUrl, {
|
|
headers,
|
|
query,
|
|
})) as ResCouponUse
|
|
|
|
return res
|
|
} catch (error) {
|
|
console.error('[Exception] useCoupon.postCouponUse:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 쿠폰 등록 내역 API 호출 함수입니다.
|
|
* @param {ReqCouponList} req - 쿠폰 등록 내역 API 요청 데이터
|
|
* @returns {ResCouponList} res - 쿠폰 등록 내역 API 응답 데이터
|
|
*/
|
|
const getCouponList = async (req: ReqCouponList) => {
|
|
let res: ResCouponList = {} as ResCouponList
|
|
try {
|
|
const runtimeConfig = useRuntimeConfig()
|
|
const stoveApiUrl = runtimeConfig.public.stoveApiUrl
|
|
const userTokenType = req.user_token_type || 'web'
|
|
|
|
const headers = {
|
|
Authorization: `Bearer ${req.accessToken}`,
|
|
}
|
|
const query: ReqCouponListParams = {
|
|
game_code: req.game_code,
|
|
start_date: req.start_date,
|
|
end_date: req.end_date,
|
|
use_state_code: req.use_state_code,
|
|
page_size: req.page_size,
|
|
page_no: req.page_no,
|
|
lang_code: req.lang_code,
|
|
}
|
|
|
|
const apiUrl = `${stoveApiUrl}/coupon/v3.0/${userTokenType}/couponbox/list`
|
|
|
|
res = (await commonFetch('GET', apiUrl, {
|
|
headers,
|
|
query,
|
|
})) as ResCouponList
|
|
|
|
if (res.result === '000') {
|
|
setCouponList(res)
|
|
} else {
|
|
console.error('[Exception] useCoupon.getCouponList:', res.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('[Exception] useCoupon.getCouponList:', error)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
return {
|
|
couponList,
|
|
totalCount,
|
|
hasNoCouponList,
|
|
|
|
postCouponUse,
|
|
getCouponList,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 쿠폰 조회 관련 Composable 함수입니다.
|
|
* @returns
|
|
*/
|
|
export const useCouponDate = () => {
|
|
// Refs
|
|
const startDate = ref<Date | null>(null)
|
|
const endDate = ref<Date | null>(null)
|
|
const searchStatus = ref<number>(0) // 0: 전체, 1: 사용전, 2: 사용완료/기간만료/사용마감
|
|
|
|
// Computed
|
|
const currentSearchPeriod = computed(() => {
|
|
if (!startDate.value || !endDate.value) {
|
|
return 0
|
|
}
|
|
|
|
return differenceInMonths(endDate.value, startDate.value)
|
|
})
|
|
|
|
// Setting Functions
|
|
const setCouponDate = (newDate: Date, type: 'start' | 'end') => {
|
|
if (newDate === null) {
|
|
return
|
|
}
|
|
|
|
if (type === 'start') {
|
|
startDate.value = newDate
|
|
} else {
|
|
endDate.value = newDate
|
|
}
|
|
}
|
|
|
|
const setSearchStatus = (newStatus: number) => {
|
|
searchStatus.value = newStatus
|
|
}
|
|
|
|
// Unix(초) 단위 타임스탬프 변환 (해당 날짜의 시작/종료 시간)
|
|
const toUnixTimestamp = (date: Date, type: 'start' | 'end') => {
|
|
if (date === null) {
|
|
return
|
|
}
|
|
|
|
if (type === 'start') {
|
|
return getUnixTime(startOfDay(date))
|
|
} else {
|
|
return getUnixTime(endOfDay(date))
|
|
}
|
|
}
|
|
|
|
return {
|
|
startDate,
|
|
endDate,
|
|
currentSearchPeriod,
|
|
searchStatus,
|
|
|
|
toUnixTimestamp,
|
|
setCouponDate,
|
|
setSearchStatus,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 쿠폰 페이징 관련 Composable 함수입니다.
|
|
* @returns
|
|
*/
|
|
export const useCouponPaging = () => {
|
|
const pageNo = ref(1) // 현재 페이지 번호
|
|
const pageSize = ref(20) // 페이지 사이즈
|
|
const pageBlock = ref(10) // 페이지 블록 사이즈
|
|
|
|
const updatePagination = (no: number) => {
|
|
pageNo.value = no
|
|
}
|
|
|
|
const getPageBlock = (deviceMode: string) => {
|
|
return deviceMode === 'desktop' ? pageBlock.value : 5
|
|
}
|
|
|
|
return {
|
|
pageNo,
|
|
pageSize,
|
|
pageBlock,
|
|
|
|
updatePagination,
|
|
getPageBlock,
|
|
}
|
|
}
|