ActivityEligibility.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if ((int)($activity['type'] ?? 0) === 2) {
  18. return true;
  19. }
  20. $platforms = self::platforms($activity['publish_platforms'] ?? []);
  21. return !$platforms || in_array(self::normalizePlatform($platform), $platforms, true);
  22. }
  23. public static function evaluate(array $activity, ?array $member): array
  24. {
  25. if ((int)($activity['type'] ?? 0) === 2) {
  26. return self::result();
  27. }
  28. if (!$member) {
  29. return self::result('请先登录');
  30. }
  31. if (!empty($activity['register_time_limited'])) {
  32. $registered = strtotime($member['registered_at'] ?? '');
  33. $start = strtotime($activity['register_start_time'] ?? '');
  34. $end = strtotime($activity['register_end_time'] ?? '');
  35. if (!$registered || !$start || !$end || $registered < $start || $registered > $end) {
  36. return self::result('不符合会员注册时间条件');
  37. }
  38. }
  39. $platforms = self::platforms($activity['register_platforms'] ?? []);
  40. if ($platforms && !in_array(self::normalizePlatform($member['register_platform'] ?? null), $platforms, true)) {
  41. return self::result('不符合会员注册平台条件');
  42. }
  43. if (!empty($activity['deposit_limited'])) {
  44. $min = $activity['deposit_min'] ?? null;
  45. $max = $activity['deposit_max'] ?? null;
  46. $total = $member['deposit_total'] ?? null;
  47. if (!is_numeric($min) || !is_numeric($max) || !is_numeric($total)
  48. || bccomp((string)$total, (string)$min, 2) < 0
  49. || bccomp((string)$total, (string)$max, 2) > 0) {
  50. return self::result('不符合会员生涯存款条件');
  51. }
  52. }
  53. return self::result();
  54. }
  55. private static function platforms($value): array
  56. {
  57. return is_array($value) ? $value : (json_decode($value ?? '[]', true) ?: []);
  58. }
  59. private static function result(string $reason = ''): array
  60. {
  61. return ['is_eligible' => $reason === '', 'ineligible_reason' => $reason];
  62. }
  63. }