ActivityReward.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Models;
  3. use App\Constants\Util;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use App\Support\ActivityCatalog;
  6. use App\Support\ContentLink;
  7. /**
  8. * @property string $title
  9. * @property string sub_title
  10. * @property string detail_image
  11. * @property string start_time
  12. * @property string end_time
  13. * @property string status
  14. *
  15. */
  16. class ActivityReward extends BaseModel
  17. {
  18. const STATUS_UP = 1;
  19. const STATUS_DOWN = 0;
  20. protected $table = 'activity_rewards';
  21. protected $hidden = ['created_at', 'updated_at', 'detail_banner', 'pc_banner'];
  22. protected $fillable = ['title', 'sub_title', 'detail_image', 'start_time', 'end_time', 'status','type','lang','content', 'params','pc_image','detail_banner', 'pc_banner',
  23. 'publish_platforms', 'budget', 'reward_quota', 'sort', 'register_time_limited', 'register_start_time', 'register_end_time',
  24. 'register_platforms', 'deposit_limited', 'deposit_min', 'deposit_max', 'show_ineligible', 'game_category', 'click_type', 'link', 'link_pc', 'link_h5', 'click_position'];
  25. protected $appends = ['type_name', 'game_category_name', 'platform_label', 'click_type', 'link', 'link_pc', 'link_h5', 'click_position', 'can_jump'];
  26. protected function getPublishPlatformsAttribute($value): ?array
  27. {
  28. // 普通活动没有平台配置,历史单平台值也按全部平台返回。
  29. if ((int)($this->attributes['type'] ?? 0) === 2) {
  30. return ActivityCatalog::PUBLISH_PLATFORMS;
  31. }
  32. return $value === null ? null : $this->fromJson($value);
  33. }
  34. protected function getPlatformLabelAttribute(): string
  35. {
  36. $platforms = $this->publish_platforms ?? [];
  37. return !$platforms || !array_diff(ActivityCatalog::PUBLISH_PLATFORMS, $platforms)
  38. ? '全部平台' : implode('、', $platforms);
  39. }
  40. protected function getClickTypeAttribute($value): string
  41. {
  42. $value = $value ?? ($this->attributes['click_type'] ?? 'none');
  43. return in_array($value, ['none', 'external', 'internal'], true) ? $value : 'none';
  44. }
  45. protected function getLinkAttribute($value): ?string
  46. {
  47. return in_array($this->click_type, ['external', 'internal'], true) ? ($value ?? ($this->attributes['link'] ?? null)) : null;
  48. }
  49. protected function getCanJumpAttribute(): bool
  50. {
  51. $hasLink = $this->click_type === 'internal' ? !empty($this->link_pc) && !empty($this->link_h5) : $this->click_type === 'external' && !empty($this->link);
  52. return $hasLink && $this->click_position !== null;
  53. }
  54. protected function getLinkPcAttribute(): ?string { return ContentLink::normalize($this->attributes)['link_pc']; }
  55. protected function getLinkH5Attribute(): ?string { return ContentLink::normalize($this->attributes)['link_h5']; }
  56. protected function getClickPositionAttribute($value): ?string
  57. {
  58. $value = $value ?? ($this->attributes['click_position'] ?? null);
  59. return $this->click_type !== 'none' && in_array($value, ['cover', 'detail'], true) ? $value : null;
  60. }
  61. protected $casts = [
  62. 'publish_platforms' => 'array', 'register_platforms' => 'array',
  63. 'register_time_limited' => 'boolean', 'deposit_limited' => 'boolean',
  64. 'show_ineligible' => 'boolean', 'reward_quota' => 'integer', 'rewarded_count' => 'integer',
  65. 'quota_enabled' => 'boolean', 'budget' => 'decimal:2', 'budget_used' => 'decimal:2',
  66. 'deposit_min' => 'decimal:2', 'deposit_max' => 'decimal:2', 'sort' => 'integer',
  67. ];
  68. public function activityUser(): HasMany
  69. {
  70. return $this->hasMany(ActivityUser::class, 'activity_id', 'id');
  71. }
  72. protected function getTypeNameAttribute(): ?string
  73. {
  74. return ActivityCatalog::activityTypes()[(int) ($this->attributes['type'] ?? 0)] ?? null;
  75. }
  76. protected function getGameCategoryNameAttribute(): ?string
  77. {
  78. return ActivityCatalog::gameTypes()[$this->attributes['game_category'] ?? ''] ?? null;
  79. }
  80. protected function getDetailImageAttribute($value)
  81. {
  82. return Util::ensureUrl($value);
  83. }
  84. protected function setDetailImageAttribute($value)
  85. {
  86. $this->attributes['detail_image'] = Util::replacePartInUrl($value);
  87. }
  88. protected function getPcImageAttribute($value)
  89. {
  90. return Util::ensureUrl($value);
  91. }
  92. protected function setPcImageAttribute($value)
  93. {
  94. $this->attributes['pc_image'] = Util::replacePartInUrl($value);
  95. }
  96. protected function getStartTimeAttribute($value): string
  97. {
  98. return date('Y-m-d', $value);
  99. }
  100. protected function getEndTimeAttribute($value): string
  101. {
  102. return date('Y-m-d', $value);
  103. }
  104. }