WebActivity.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Game;
  3. use Illuminate\Database\Eloquent\Model;
  4. class WebActivity extends Model
  5. {
  6. protected $connection='mysql';
  7. // 指定表名,如果表名与类名的复数相同则不需要
  8. protected $table = 'webgame.activity';
  9. // 确定哪些字段可以被赋值
  10. protected $fillable = ['img','img_pt','img_es', 'title', 'link_game', 'link_module'];
  11. // 隐藏不需要返回的字段
  12. protected $hidden = ['img_es', 'img_pt'];
  13. // 关闭自动维护的时间戳,如果您没有在表中创建时间戳字段则需要
  14. public $timestamps = false;
  15. public function game()
  16. {
  17. return $this->belongsTo(GameCard::class, 'link_game', 'id');
  18. }
  19. // img 字段的访问器
  20. public function getImgAttribute($value)
  21. {
  22. //处理多语言
  23. $language=GlobalUserInfo::getLocale();
  24. switch ($language) {
  25. case 'es':
  26. $value= $this->attributes['img_es'] ?? $this->attributes['img_pt'] ?? $value;
  27. break;
  28. case 'pt':
  29. $value= $this->attributes['img_pt'] ?? $this->attributes['img_es'] ?? $value;
  30. break;
  31. }
  32. $cdn_org=["cdn.ouro777.com","cdn.moeda777.com"];
  33. $cdn_replace="24680.imgix.net";
  34. $img_add_param="?auto=format,compress&cs=srgb&dpr=2&w=500";
  35. $img_add_param="";
  36. $value=str_replace($cdn_org,$cdn_replace,$value).$img_add_param;
  37. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  38. if (strstr($origin, "cereja")) {
  39. $value=str_replace("banner/","banner_cereja/",$value);
  40. }
  41. return $value;
  42. }
  43. // 自定义序列化的数组形式
  44. public function toArray()
  45. {
  46. $array = parent::toArray();
  47. // 添加动态生成的 img 字段
  48. $array['img'] = $this->img;
  49. // 移除 img_es 和 img_pt 字段
  50. unset($array['img_es'], $array['img_pt']);
  51. return $array;
  52. }
  53. }