DistMessage.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class DistMessage extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'dist_messages';
  9. protected $fillable = ['title', 'content', 'sender_id', 'target_type', 'target_ids', 'created_at', 'updated_at'];
  10. protected $casts = [
  11. 'target_ids' => 'json',
  12. 'created_at' => 'datetime:Y-m-d H:i:s',
  13. 'updated_at' => 'datetime:Y-m-d H:i:s',
  14. ];
  15. // 消息的发送者
  16. public function sender()
  17. {
  18. return $this->belongsTo(DistAdminDistributor::class, 'sender_id');
  19. }
  20. // 消息的阅读状态
  21. public function readStatuses()
  22. {
  23. return $this->hasMany(DistReadStatus::class, 'message_id');
  24. }
  25. // 检查消息是否针对某用户
  26. public function isForUser($userId)
  27. {
  28. if ($this->target_type === 'all') {
  29. return true;
  30. }
  31. if ($this->target_type === 'users' && is_array($this->target_ids)) {
  32. return in_array($userId, $this->target_ids);
  33. }
  34. return false;
  35. }
  36. }