WebChannelConfig.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Game;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Redis;
  5. define('SPECIAL_MODE_GUEST', 1);
  6. define('SPECIAL_MODE_ZERO_MONEY', 2);//注册不送钱
  7. define('SPECIAL_DISABLE_PROMOTE_INSTALL', 4);//提示安装
  8. define('SPECIAL_MODE_PWA_BONUS', 8);//安装app送钱
  9. define('SPECIAL_MODE_SMS_BONUS', 16);//手机验证送钱
  10. define('SPECIAL_MODE_MAIL_BONUS', 32);//MAIL验证送钱
  11. define('SPECIAL_MODE_FIRSTPAY_OFF30', 64);//首冲打开30%bonus
  12. class WebChannelConfig extends Model
  13. {
  14. protected $table = 'webgame.WebChannelConfig';
  15. protected $primaryKey = 'ID';
  16. public $incrementing = true;
  17. protected $keyType = 'int';
  18. protected $connection = 'mysql';
  19. protected $fillable = [
  20. 'Channel', 'PackageName', 'Remarks', 'StateNo','SpecialMode', 'PlatformName', 'PlatformID','LoginOpen','RegOpen','BonusArr','ShadowChannel','LightApk','FullApk'
  21. ];
  22. private static $key='web_channel_config:';
  23. public $timestamps = false;
  24. protected $shadow_channel=0;
  25. public function SHADOW_CHANNEL()
  26. {
  27. if($this->shadow_channel)return $this->shadow_channel;
  28. $this->shadow_channel=$this->Channel;
  29. if(!empty($this->ShadowChannel)){
  30. $channels=explode('|',$this->ShadowChannel);
  31. $allRate=0;
  32. $rates=[];
  33. foreach ($channels as $channel){
  34. $channel=explode('%',$channel);
  35. $allRate+=$channel[0];
  36. $rates[$allRate]=$channel[1];
  37. }
  38. $rand=mt_rand(0, 100);
  39. foreach($rates as $rate=>$channel){
  40. if($rand<$rate){
  41. $this->shadow_channel=$channel;
  42. return $channel;
  43. }
  44. }
  45. }
  46. return $this->shadow_channel;
  47. }
  48. public function BONUS_PWA()
  49. {
  50. return explode('|',$this->BonusArr)[3]??00;
  51. }
  52. public function BONUS_VERIFY_PHONE()
  53. {
  54. return explode('|',$this->BonusArr)[1]??00;
  55. }
  56. public function BONUS_REG()
  57. {
  58. return explode('|',$this->BonusArr)[0]??00;
  59. }
  60. public function BONUS_VERIFY_EMAIL()
  61. {
  62. return explode('|',$this->BonusArr)[2]??00;
  63. }
  64. public static function GuestOpen($config)
  65. {
  66. return ($config->SpecialMode&SPECIAL_MODE_GUEST)==SPECIAL_MODE_GUEST;
  67. }
  68. public function isFistPay30()
  69. {
  70. return ($this->SpecialMode&SPECIAL_MODE_SMS_BONUS)==SPECIAL_MODE_SMS_BONUS;
  71. }
  72. public function isSmsBonus()
  73. {
  74. return ($this->SpecialMode&SPECIAL_MODE_SMS_BONUS)==SPECIAL_MODE_SMS_BONUS;
  75. }
  76. public function isDisablePromote()
  77. {
  78. return ($this->SpecialMode&SPECIAL_DISABLE_PROMOTE_INSTALL)==SPECIAL_DISABLE_PROMOTE_INSTALL;
  79. }
  80. public function isPwaBonus()
  81. {
  82. return ($this->SpecialMode&SPECIAL_MODE_PWA_BONUS)==SPECIAL_MODE_PWA_BONUS;
  83. }
  84. public function isGuestOpen()
  85. {
  86. return ($this->SpecialMode&SPECIAL_MODE_GUEST)==SPECIAL_MODE_GUEST;
  87. }
  88. public function isRegZeroMoneyOpen()
  89. {
  90. return ($this->SpecialMode&SPECIAL_MODE_ZERO_MONEY)==SPECIAL_MODE_ZERO_MONEY;
  91. }
  92. // 获取特定 Channel 的记录
  93. public static function getByChannel($channel)
  94. {
  95. $cacheKey = self::$key . $channel;
  96. $cachedConfig = Redis::get($cacheKey);
  97. if ($cachedConfig) {
  98. return new WebChannelConfig(json_decode($cachedConfig,true));
  99. }
  100. $config = self::where('Channel', $channel)->first();
  101. if ($config) {
  102. Redis::setex($cacheKey, 60, $config->toJson());
  103. }
  104. return $config;
  105. }
  106. // 清除缓存
  107. public static function clearCache($channel)
  108. {
  109. $cacheKey = self::$key . $channel;
  110. Redis::del($cacheKey);
  111. }
  112. // 监听模型事件
  113. protected static function boot()
  114. {
  115. parent::boot();
  116. static::saved(function ($model) {
  117. self::clearCache($model->Channel);
  118. });
  119. static::deleted(function ($model) {
  120. self::clearCache($model->Channel);
  121. });
  122. }
  123. }