123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/8/7
- * Time: 21:23
- * 清水小说
- */
- namespace app\common\service;
- use app\common\library\Ip;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use think\Config;
- class WaterBookService extends BaseService
- {
- private static $self;
- /**
- * @return $this
- */
- public static function instance()
- {
- if(self::$self == NULL){
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 校验展示给用户小说类型(清水 | 默认)
- * @param $channel_id
- * @param $uid
- * @param string $ip
- * @return bool (TRUE =>清水 FALSE=> 默认)
- */
- public function showBook($channel_id, $uid='', $ip='')
- {
- LogService::info('WaterBookService:params:channel_id:' . $channel_id . ',uid:' . $uid . ',IP:' . $ip);
- $white_uids = array_filter(explode(',', Config::get('site.white_water_uids')));
- if ($white_uids && in_array($uid, $white_uids)) {
- LogService::info('WaterBookService:params:清水书籍白名单用户');
- return false;
- }
-
- $result = false;
- // 读取Hash中的规则
- $waterRule = array_filter(Redis::instance()->hMGet(CacheConstants::LIGHT_RULE, ['*', $channel_id]));
- if($waterRule){
- // 所有城市CODE
- $aim_city = [];
- foreach ($waterRule as $k=>$item)
- {
- $item_arr = json_decode($item, true);
- $waterRule[$k] = $item_arr['c'] ?? '';
- }
- $waterRule = array_filter($waterRule);
- // 判断 * 的情况
- if (!empty($waterRule['*'])) {
- if ($waterRule['*'] == '*') {
- LogService::info('WaterBookService:params:清水,*命中');
- return true;
- } else {
- $aim_city = array_merge($aim_city, explode(',', $waterRule['*']));
- }
- }
- // 校验渠道是否开启了清水
- if (!empty($waterRule[$channel_id])) {
- if ($waterRule[$channel_id] == '*') {
- LogService::info('WaterBookService:params:清水,渠道城市为*命中');
- return true;
- } else {
- $aim_city = array_merge($aim_city, explode(',', $waterRule[$channel_id]));
- }
- }
- $aim_city = array_unique(array_filter($aim_city));
- if (!empty($ip)) {
- // ip 不为空,解析为 城市code
- $cityCode = Ip::citycode($ip);
- // 判断城市是否清水
- if (in_array($cityCode, $aim_city)) {
- LogService::info('WaterBookService:params:清水,根据城市命中');
- return true;
- }
- }else{
- // 没有IP参数,说明是微信回调过来的,前台页面进来都需要传IP
- if(!empty($aim_city)){
- LogService::info('WaterBookService:params:微信回调,根据城市命中');
- // 微信回调,只要有匹配的城市,返回清水 TRUE
- return true;
- }
- }
- }
- if (!empty($uid)) {
- // uid 不为空,判断是否有uid 限制
- $water_uids = Config::get('site.water_uids');
- if (in_array($uid, explode(',', $water_uids))) {
- LogService::info('WaterBookService:params:清水,根据uid命中');
- return true;
- }
- }
- return $result;
- }
- }
|