WhiteList.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\common\utility;
  3. use app\common\library\Ip;
  4. use app\common\library\Redis;
  5. use app\main\constants\CacheConstants;
  6. use app\main\constants\OrderContents;
  7. use app\main\service\OrderService;
  8. use app\main\service\UserService;
  9. use think\Config;
  10. use think\Log;
  11. /**
  12. *
  13. * 扣量白名单管理
  14. * Class WhiteList
  15. * @package app\common\utility
  16. */
  17. class WhiteList{
  18. /**
  19. * 添加用户创建时间与支付时间间隔,小于间隔时间加入用户白名单
  20. * @param int $user_id 用户ID
  21. */
  22. static function addUserCreatePayTime($user_id){
  23. try{
  24. $redis = Redis::instance();
  25. $user_create_pay_key = 'UCPT:'.$user_id;
  26. $create_pay_time = intval(Config::get('site.create_pay_time'));
  27. $redis->set($user_create_pay_key,time());
  28. $redis->expire($user_create_pay_key,60*$create_pay_time);
  29. }catch (\Exception $e){
  30. Log::error('用户创建与支付时间间隔,Error:'.$e->getMessage());
  31. }
  32. }
  33. /**
  34. * 用户充值时间间隔
  35. * @param $user_id int 用户ID
  36. */
  37. static function addUserPayTime($user_id){
  38. try{
  39. $pay_time = intval(Config::get('site.pay_time'));
  40. $redis = Redis::instance();
  41. $user_pay_key = 'UPT:'.$user_id;
  42. $redis->set($user_pay_key,time());
  43. $redis->expire($user_pay_key,60*$pay_time);
  44. //用户支付时间,KL使用
  45. $cacheTime = CacheConstants::getUserPayTimeCacheKey($user_id);
  46. $redis->set($cacheTime, time());
  47. $redis->expire($cacheTime, 86400);
  48. //用户完成订单数,KL使用
  49. $cacheCount = CacheConstants::getUserPayTimeCacheKey($user_id);
  50. if ($redis->exists($cacheCount)) {
  51. $redis->incr($cacheCount);
  52. } else {
  53. $count = OrderService::instance()->getOrderModel()
  54. ->where('user_id', $user_id)
  55. ->where('state', OrderContents::ORDER_STATE_PAID)
  56. ->count();
  57. $redis->set($cacheCount, $count + 1);
  58. }
  59. }catch (\Exception $e){
  60. Log::error('添加用户充值完成时间失败,Error:'.$e->getMessage());
  61. }
  62. }
  63. /**
  64. * 添加用户ID白名单
  65. * @param $user_id int 用户ID
  66. */
  67. static function addUserIdWhite($user_id){
  68. try{
  69. // $redis = Redis::instance();
  70. UserService::instance()->update(['is_white' => 1], ['id' => $user_id]);
  71. // model('User')->setConnect($user_id)->where('id',$user_id)->update(['is_white'=>1]);
  72. // $redis->del("UN:{$user_id}");
  73. }catch (\Exception $e){
  74. Log::error('添加用户ID白名单失败,Error:'.$e->getMessage());
  75. }
  76. }
  77. /**
  78. * 添加推广链接用户白名单
  79. * @param $referral_id int 推广链接ID
  80. * @param $user_id int 用户ID
  81. */
  82. static function addReferralUser($referral_id,$user_id){
  83. try{
  84. $redis = Redis::instance();
  85. //获取推广链接用户计数
  86. $sys_ref_num = intval(Config::get('site.referral_num'));
  87. $ref_index = intval($redis->get('KCR:'.$referral_id));
  88. if($sys_ref_num && ($ref_index < $sys_ref_num)){
  89. //未到阈值时加入用户白名单
  90. WhiteList::addUserIdWhite($user_id);
  91. Log::info("白名单:" . ' 用户ID:' . $user_id . '添加时间: '.date('Y-m-d H:i:s').' 原因:推广链接未到阈值时加入用户白名单');
  92. //添加推广链接计数
  93. $redis->incrBy('KCR:'.$referral_id,1);
  94. }
  95. }catch (\Exception $e){
  96. Log::error('添加推广链接用户白名单失败,Error:'.$e->getMessage());
  97. }
  98. }
  99. /**
  100. * 添加渠道城市白名单
  101. * @param $channel_id int 渠道ID
  102. */
  103. static function addChannelCity($channel_id){
  104. try{
  105. $redis = Redis::instance();
  106. $redis->sAdd("ALI:{$channel_id}",Ip::province());
  107. $real_channel_id = \app\main\service\AdminService::instance()->getAdminExtendModel()->getChannelId($channel_id);
  108. $redis->sAdd(CacheConstants::getChannelWhiteCityCacheKey($real_channel_id), Ip::citycode());
  109. }catch (\Exception $e){
  110. Log::error('添加渠道城市白名单失败,Error:'.$e->getMessage());
  111. }
  112. }
  113. /**
  114. * 检查KL城市
  115. * @param $admin_id
  116. * @param $province
  117. * @param $country
  118. * @return bool
  119. */
  120. static function checkedCityWhite($admin_id,$province,$country = null){
  121. try{
  122. if(is_null($country)){
  123. $country = Ip::country();
  124. }
  125. //不在中国都被指定为白名单
  126. if($country != '中国'){
  127. return true;
  128. }
  129. $redis = Redis::instance();
  130. return $redis->sIsMember("ALI:{$admin_id}",$province);
  131. }catch (\Exception $e){
  132. return true;
  133. }
  134. }
  135. /**
  136. * 添加渠道IP白名单
  137. * @param $ip string IP地址
  138. */
  139. static function addChannelIp($ip){
  140. try{
  141. $aIp = explode('.', $ip);
  142. $redis = Redis::instance();
  143. if (!$redis->sIsMember("KIP:{$aIp[0]}.{$aIp[1]}", $ip)) {
  144. $redis->sAdd("KIP:{$aIp[0]}.{$aIp[1]}",$ip);
  145. }
  146. }catch (\Exception $e){
  147. Log::error('添加渠道IP白名单出错,Error:'.$e->getMessage());
  148. }
  149. }
  150. /**
  151. * 检查IP是否在白名单
  152. * @param $ip
  153. * @return bool
  154. */
  155. static function checkedIpWhite($ip){
  156. $aIp = explode('.', $ip);
  157. $redis = Redis::instance();
  158. return $redis->sIsMember("KIP:{$aIp[0]}.{$aIp[1]}",$ip);
  159. }
  160. }