123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\common\utility;
- use app\common\library\Ip;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\constants\OrderContents;
- use app\main\service\OrderService;
- use app\main\service\UserService;
- use think\Config;
- use think\Log;
- /**
- *
- * 扣量白名单管理
- * Class WhiteList
- * @package app\common\utility
- */
- class WhiteList{
- /**
- * 添加用户创建时间与支付时间间隔,小于间隔时间加入用户白名单
- * @param int $user_id 用户ID
- */
- static function addUserCreatePayTime($user_id){
- try{
- $redis = Redis::instance();
- $user_create_pay_key = 'UCPT:'.$user_id;
- $create_pay_time = intval(Config::get('site.create_pay_time'));
- $redis->set($user_create_pay_key,time());
- $redis->expire($user_create_pay_key,60*$create_pay_time);
- }catch (\Exception $e){
- Log::error('用户创建与支付时间间隔,Error:'.$e->getMessage());
- }
- }
- /**
- * 用户充值时间间隔
- * @param $user_id int 用户ID
- */
- static function addUserPayTime($user_id){
- try{
- $pay_time = intval(Config::get('site.pay_time'));
- $redis = Redis::instance();
- $user_pay_key = 'UPT:'.$user_id;
- $redis->set($user_pay_key,time());
- $redis->expire($user_pay_key,60*$pay_time);
- //用户支付时间,KL使用
- $cacheTime = CacheConstants::getUserPayTimeCacheKey($user_id);
- $redis->set($cacheTime, time());
- $redis->expire($cacheTime, 86400);
- //用户完成订单数,KL使用
- $cacheCount = CacheConstants::getUserPayTimeCacheKey($user_id);
- if ($redis->exists($cacheCount)) {
- $redis->incr($cacheCount);
- } else {
- $count = OrderService::instance()->getOrderModel()
- ->where('user_id', $user_id)
- ->where('state', OrderContents::ORDER_STATE_PAID)
- ->count();
- $redis->set($cacheCount, $count + 1);
- }
- }catch (\Exception $e){
- Log::error('添加用户充值完成时间失败,Error:'.$e->getMessage());
- }
- }
- /**
- * 添加用户ID白名单
- * @param $user_id int 用户ID
- */
- static function addUserIdWhite($user_id){
- try{
- // $redis = Redis::instance();
- UserService::instance()->update(['is_white' => 1], ['id' => $user_id]);
- // model('User')->setConnect($user_id)->where('id',$user_id)->update(['is_white'=>1]);
- // $redis->del("UN:{$user_id}");
- }catch (\Exception $e){
- Log::error('添加用户ID白名单失败,Error:'.$e->getMessage());
- }
- }
- /**
- * 添加推广链接用户白名单
- * @param $referral_id int 推广链接ID
- * @param $user_id int 用户ID
- */
- static function addReferralUser($referral_id,$user_id){
- try{
- $redis = Redis::instance();
- //获取推广链接用户计数
- $sys_ref_num = intval(Config::get('site.referral_num'));
- $ref_index = intval($redis->get('KCR:'.$referral_id));
- if($sys_ref_num && ($ref_index < $sys_ref_num)){
- //未到阈值时加入用户白名单
- WhiteList::addUserIdWhite($user_id);
- Log::info("白名单:" . ' 用户ID:' . $user_id . '添加时间: '.date('Y-m-d H:i:s').' 原因:推广链接未到阈值时加入用户白名单');
- //添加推广链接计数
- $redis->incrBy('KCR:'.$referral_id,1);
- }
- }catch (\Exception $e){
- Log::error('添加推广链接用户白名单失败,Error:'.$e->getMessage());
- }
- }
- /**
- * 添加渠道城市白名单
- * @param $channel_id int 渠道ID
- */
- static function addChannelCity($channel_id){
- try{
- $redis = Redis::instance();
- $redis->sAdd("ALI:{$channel_id}",Ip::province());
- $real_channel_id = \app\main\service\AdminService::instance()->getAdminExtendModel()->getChannelId($channel_id);
- $redis->sAdd(CacheConstants::getChannelWhiteCityCacheKey($real_channel_id), Ip::citycode());
- }catch (\Exception $e){
- Log::error('添加渠道城市白名单失败,Error:'.$e->getMessage());
- }
- }
- /**
- * 检查KL城市
- * @param $admin_id
- * @param $province
- * @param $country
- * @return bool
- */
- static function checkedCityWhite($admin_id,$province,$country = null){
- try{
- if(is_null($country)){
- $country = Ip::country();
- }
- //不在中国都被指定为白名单
- if($country != '中国'){
- return true;
- }
- $redis = Redis::instance();
- return $redis->sIsMember("ALI:{$admin_id}",$province);
- }catch (\Exception $e){
- return true;
- }
- }
- /**
- * 添加渠道IP白名单
- * @param $ip string IP地址
- */
- static function addChannelIp($ip){
- try{
- $aIp = explode('.', $ip);
- $redis = Redis::instance();
- if (!$redis->sIsMember("KIP:{$aIp[0]}.{$aIp[1]}", $ip)) {
- $redis->sAdd("KIP:{$aIp[0]}.{$aIp[1]}",$ip);
- }
- }catch (\Exception $e){
- Log::error('添加渠道IP白名单出错,Error:'.$e->getMessage());
- }
- }
- /**
- * 检查IP是否在白名单
- * @param $ip
- * @return bool
- */
- static function checkedIpWhite($ip){
- $aIp = explode('.', $ip);
- $redis = Redis::instance();
- return $redis->sIsMember("KIP:{$aIp[0]}.{$aIp[1]}",$ip);
- }
- }
|