WaterBookService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/8/7
  6. * Time: 21:23
  7. * 清水小说
  8. */
  9. namespace app\common\service;
  10. use app\common\library\Ip;
  11. use app\common\library\Redis;
  12. use app\main\constants\CacheConstants;
  13. use think\Config;
  14. class WaterBookService extends BaseService
  15. {
  16. private static $self;
  17. /**
  18. * @return $this
  19. */
  20. public static function instance()
  21. {
  22. if(self::$self == NULL){
  23. self::$self = new self();
  24. }
  25. return self::$self;
  26. }
  27. /**
  28. * 校验展示给用户小说类型(清水 | 默认)
  29. * @param $channel_id
  30. * @param $uid
  31. * @param string $ip
  32. * @return bool (TRUE =>清水 FALSE=> 默认)
  33. */
  34. public function showBook($channel_id, $uid='', $ip='')
  35. {
  36. LogService::info('WaterBookService:params:channel_id:' . $channel_id . ',uid:' . $uid . ',IP:' . $ip);
  37. $white_uids = array_filter(explode(',', Config::get('site.white_water_uids')));
  38. if ($white_uids && in_array($uid, $white_uids)) {
  39. LogService::info('WaterBookService:params:清水书籍白名单用户');
  40. return false;
  41. }
  42. $result = false;
  43. // 读取Hash中的规则
  44. $waterRule = array_filter(Redis::instance()->hMGet(CacheConstants::LIGHT_RULE, ['*', $channel_id]));
  45. if($waterRule){
  46. // 所有城市CODE
  47. $aim_city = [];
  48. foreach ($waterRule as $k=>$item)
  49. {
  50. $item_arr = json_decode($item, true);
  51. $waterRule[$k] = $item_arr['c'] ?? '';
  52. }
  53. $waterRule = array_filter($waterRule);
  54. // 判断 * 的情况
  55. if (!empty($waterRule['*'])) {
  56. if ($waterRule['*'] == '*') {
  57. LogService::info('WaterBookService:params:清水,*命中');
  58. return true;
  59. } else {
  60. $aim_city = array_merge($aim_city, explode(',', $waterRule['*']));
  61. }
  62. }
  63. // 校验渠道是否开启了清水
  64. if (!empty($waterRule[$channel_id])) {
  65. if ($waterRule[$channel_id] == '*') {
  66. LogService::info('WaterBookService:params:清水,渠道城市为*命中');
  67. return true;
  68. } else {
  69. $aim_city = array_merge($aim_city, explode(',', $waterRule[$channel_id]));
  70. }
  71. }
  72. $aim_city = array_unique(array_filter($aim_city));
  73. if (!empty($ip)) {
  74. // ip 不为空,解析为 城市code
  75. $cityCode = Ip::citycode($ip);
  76. // 判断城市是否清水
  77. if (in_array($cityCode, $aim_city)) {
  78. LogService::info('WaterBookService:params:清水,根据城市命中');
  79. return true;
  80. }
  81. }else{
  82. // 没有IP参数,说明是微信回调过来的,前台页面进来都需要传IP
  83. if(!empty($aim_city)){
  84. LogService::info('WaterBookService:params:微信回调,根据城市命中');
  85. // 微信回调,只要有匹配的城市,返回清水 TRUE
  86. return true;
  87. }
  88. }
  89. }
  90. if (!empty($uid)) {
  91. // uid 不为空,判断是否有uid 限制
  92. $water_uids = Config::get('site.water_uids');
  93. if (in_array($uid, explode(',', $water_uids))) {
  94. LogService::info('WaterBookService:params:清水,根据uid命中');
  95. return true;
  96. }
  97. }
  98. return $result;
  99. }
  100. }