VipStore.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { APP_VIP_CLAIM, APP_VIP_CONFIG, success } from "@scripts/api";
  2. import { checkResponse } from "@scripts/api/ApiHelper";
  3. import { AppKernel } from "@scripts/app/AppKernel";
  4. import { VipRights } from "@scripts/define/VipDeine";
  5. import { Http } from "@scripts/request";
  6. import { isUnlockedRights } from "@scripts/utils/VipHelper";
  7. interface IRewardItem {
  8. claimable: boolean;
  9. claimed: boolean;
  10. name: string;
  11. reward: { clubpoint: number, gc: number, sc: number };
  12. countdownSeconds?:number;
  13. periodKey?: string;
  14. nextClaimAt?: number;
  15. }
  16. interface IVipReward {
  17. birthday_gift: IRewardItem;
  18. weekly_bonus: IRewardItem;
  19. }
  20. export interface IVipRow {
  21. content: string[],
  22. title: string,
  23. }
  24. export interface IVipConfig {
  25. /** 当前用户 VIP 等级序号 */
  26. vipLevel: number;
  27. /** 当前用户累计俱乐部积分 */
  28. clubPoints: number;
  29. /** 当前等级下 ValueType=reward 的权益奖励配置 */
  30. rewards: IVipReward;
  31. /** VIP 等级与权益对照表*/
  32. rows: IVipRow[];
  33. }
  34. interface IClaimResponse {
  35. clubPoints: number;
  36. code: string;
  37. granted: { clubpoint: number, gc: number, sc: number };
  38. reward: IRewardItem;
  39. }
  40. class VipStore {
  41. readonly rightsCount = 8;
  42. private _vipConfig: IVipConfig;
  43. private _maxVip: number;
  44. get vipConfig() { return this._vipConfig };
  45. get maxVip() { return this._maxVip; }
  46. async reqVipConfig() {
  47. const res = await Http.call<IVipConfig>(APP_VIP_CONFIG);
  48. if (success(res)) {
  49. this._vipConfig = res.data;
  50. this._maxVip = res.data.rows.reduce<number>((previous, current) => {
  51. if (current.content.length > previous) {
  52. return current.content.length;
  53. }
  54. return previous;
  55. }, 0);
  56. }
  57. }
  58. getVipRights(vipIdx: number, type: VipRights) {
  59. return this.vipConfig.rows[type].content[vipIdx];
  60. }
  61. getRightsUnlock(type: VipRights) {
  62. const content = this.vipConfig.rows[type].content;
  63. const index = content.findIndex((item) => {
  64. return isUnlockedRights(item);
  65. })
  66. return index;
  67. }
  68. getUpgradePoints(vipIdx: number) {
  69. const point = this.getVipRights(vipIdx, VipRights.UpgradePoint);
  70. return Number(point);
  71. }
  72. async reqVipClaim(code: 'weekly_bonus' | 'birthday_gift') {
  73. const params = { code }
  74. const res = await Http.call<IClaimResponse>(APP_VIP_CLAIM, params);
  75. if (checkResponse(res)) {
  76. if (this.vipConfig.rewards[code]) {
  77. this.vipConfig.rewards[code] = res.data.reward;
  78. this.vipConfig.clubPoints = res.data.clubPoints;
  79. AppKernel.events.emit('club:update');
  80. }
  81. return true;
  82. }
  83. return false;
  84. }
  85. }
  86. export const vipStore = new VipStore();