ActivityReward.php 4.3 KB

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