Banner.php 2.1 KB

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