ActivityReward.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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', 'click_type', 'link', 'link_pc', 'link_h5', 'click_position', 'can_jump'];
  26. protected function getClickTypeAttribute($value): string
  27. {
  28. $value = $value ?? ($this->attributes['click_type'] ?? 'none');
  29. return in_array($value, ['none', 'external', 'internal'], true) ? $value : 'none';
  30. }
  31. protected function getLinkAttribute($value): ?string
  32. {
  33. return in_array($this->click_type, ['external', 'internal'], true) ? ($value ?? ($this->attributes['link'] ?? null)) : null;
  34. }
  35. protected function getCanJumpAttribute(): bool
  36. {
  37. $hasLink = $this->click_type === 'internal' ? !empty($this->link_pc) && !empty($this->link_h5) : $this->click_type === 'external' && !empty($this->link);
  38. return $hasLink && $this->click_position !== null;
  39. }
  40. protected function getLinkPcAttribute(): ?string { return ContentLink::normalize($this->attributes)['link_pc']; }
  41. protected function getLinkH5Attribute(): ?string { return ContentLink::normalize($this->attributes)['link_h5']; }
  42. protected function getClickPositionAttribute($value): ?string
  43. {
  44. $value = $value ?? ($this->attributes['click_position'] ?? null);
  45. return $this->click_type !== 'none' && in_array($value, ['cover', 'detail'], true) ? $value : null;
  46. }
  47. protected $casts = [
  48. 'publish_platforms' => 'array', 'register_platforms' => 'array',
  49. 'register_time_limited' => 'boolean', 'deposit_limited' => 'boolean',
  50. 'show_ineligible' => 'boolean', 'reward_quota' => 'integer', 'rewarded_count' => 'integer',
  51. 'quota_enabled' => 'boolean', 'budget' => 'decimal:2', 'budget_used' => 'decimal:2',
  52. 'deposit_min' => 'decimal:2', 'deposit_max' => 'decimal:2', 'sort' => 'integer',
  53. ];
  54. public function activityUser(): HasMany
  55. {
  56. return $this->hasMany(ActivityUser::class, 'activity_id', 'id');
  57. }
  58. protected function getTypeNameAttribute(): ?string
  59. {
  60. return ActivityCatalog::activityTypes()[(int) ($this->attributes['type'] ?? 0)] ?? null;
  61. }
  62. protected function getGameCategoryNameAttribute(): ?string
  63. {
  64. return ActivityCatalog::gameTypes()[$this->attributes['game_category'] ?? ''] ?? null;
  65. }
  66. protected function getDetailImageAttribute($value)
  67. {
  68. return Util::ensureUrl($value);
  69. }
  70. protected function setDetailImageAttribute($value)
  71. {
  72. $this->attributes['detail_image'] = Util::replacePartInUrl($value);
  73. }
  74. protected function getPcImageAttribute($value)
  75. {
  76. return Util::ensureUrl($value);
  77. }
  78. protected function setPcImageAttribute($value)
  79. {
  80. $this->attributes['pc_image'] = Util::replacePartInUrl($value);
  81. }
  82. protected function getStartTimeAttribute($value): string
  83. {
  84. return date('Y-m-d', $value);
  85. }
  86. protected function getEndTimeAttribute($value): string
  87. {
  88. return date('Y-m-d', $value);
  89. }
  90. }