import { APP_VIP_CLAIM, APP_VIP_CONFIG, success } from "@scripts/api"; import { checkResponse } from "@scripts/api/ApiHelper"; import { AppKernel } from "@scripts/app/AppKernel"; import { VipRights } from "@scripts/define/VipDeine"; import { Http } from "@scripts/request"; import { isUnlockedRights } from "@scripts/utils/VipHelper"; interface IRewardItem { claimable: boolean; claimed: boolean; name: string; reward: { clubpoint: number, gc: number, sc: number }; countdownSeconds?:number; periodKey?: string; nextClaimAt?: number; } interface IVipReward { birthday_gift: IRewardItem; weekly_bonus: IRewardItem; } export interface IVipRow { content: string[], title: string, } export interface IVipConfig { /** 当前用户 VIP 等级序号 */ vipLevel: number; /** 当前用户累计俱乐部积分 */ clubPoints: number; /** 当前等级下 ValueType=reward 的权益奖励配置 */ rewards: IVipReward; /** VIP 等级与权益对照表*/ rows: IVipRow[]; } interface IClaimResponse { clubPoints: number; code: string; granted: { clubpoint: number, gc: number, sc: number }; reward: IRewardItem; } class VipStore { readonly rightsCount = 8; private _vipConfig: IVipConfig; private _maxVip: number; get vipConfig() { return this._vipConfig }; get maxVip() { return this._maxVip; } async reqVipConfig() { const res = await Http.call(APP_VIP_CONFIG); if (success(res)) { this._vipConfig = res.data; this._maxVip = res.data.rows.reduce((previous, current) => { if (current.content.length > previous) { return current.content.length; } return previous; }, 0); } } getVipRights(vipIdx: number, type: VipRights) { return this.vipConfig.rows[type].content[vipIdx]; } getRightsUnlock(type: VipRights) { const content = this.vipConfig.rows[type].content; const index = content.findIndex((item) => { return isUnlockedRights(item); }) return index; } getUpgradePoints(vipIdx: number) { const point = this.getVipRights(vipIdx, VipRights.UpgradePoint); return Number(point); } async reqVipClaim(code: 'weekly_bonus' | 'birthday_gift') { const params = { code } const res = await Http.call(APP_VIP_CLAIM, params); if (checkResponse(res)) { if (this.vipConfig.rewards[code]) { this.vipConfig.rewards[code] = res.data.reward; this.vipConfig.clubPoints = res.data.clubPoints; AppKernel.events.emit('club:update'); } return true; } return false; } } export const vipStore = new VipStore();