ActivityEligibility.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Support;
  3. /** Framework-independent rules, mirrored in melbet_sport-api. */
  4. class ActivityEligibility
  5. {
  6. public static function normalizePlatform(?string $platform): ?string
  7. {
  8. return match (strtoupper(trim($platform ?? ''))) {
  9. 'PC' => 'PC',
  10. 'H5', 'WECHAT', '微信' => 'H5',
  11. 'APP', 'IOS', 'ANDROID' => 'APP',
  12. default => null,
  13. };
  14. }
  15. public static function isPublishedOn(array $activity, ?string $platform): bool
  16. {
  17. $platforms = self::platforms($activity['publish_platforms'] ?? []);
  18. return !$platforms || in_array(self::normalizePlatform($platform), $platforms, true);
  19. }
  20. public static function evaluate(array $activity, ?array $member): array
  21. {
  22. if ((int)($activity['type'] ?? 0) === 2) {
  23. return self::result();
  24. }
  25. if (!$member) {
  26. return self::result('请先登录');
  27. }
  28. if (!empty($activity['register_time_limited'])) {
  29. $registered = strtotime($member['registered_at'] ?? '');
  30. $start = strtotime($activity['register_start_time'] ?? '');
  31. $end = strtotime($activity['register_end_time'] ?? '');
  32. if (!$registered || !$start || !$end || $registered < $start || $registered > $end) {
  33. return self::result('不符合会员注册时间条件');
  34. }
  35. }
  36. $platforms = self::platforms($activity['register_platforms'] ?? []);
  37. if ($platforms && !in_array(self::normalizePlatform($member['register_platform'] ?? null), $platforms, true)) {
  38. return self::result('不符合会员注册平台条件');
  39. }
  40. if (!empty($activity['deposit_limited'])) {
  41. $min = $activity['deposit_min'] ?? null;
  42. $max = $activity['deposit_max'] ?? null;
  43. $total = $member['deposit_total'] ?? null;
  44. if (!is_numeric($min) || !is_numeric($max) || !is_numeric($total)
  45. || bccomp((string)$total, (string)$min, 2) < 0
  46. || bccomp((string)$total, (string)$max, 2) > 0) {
  47. return self::result('不符合会员生涯存款条件');
  48. }
  49. }
  50. return self::result();
  51. }
  52. private static function platforms($value): array
  53. {
  54. return is_array($value) ? $value : (json_decode($value ?? '[]', true) ?: []);
  55. }
  56. private static function result(string $reason = ''): array
  57. {
  58. return ['is_eligible' => $reason === '', 'ineligible_reason' => $reason];
  59. }
  60. }