WebChannelConfig.php 4.7 KB

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