QappSendOrderContentShieldConfigService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Modules\Channel\Services;
  3. use App\Libs\Ip2Region;
  4. use App\Modules\Book\Services\ChapterService;
  5. use App\Modules\Channel\Models\QappSendOrderContentShieldConfig;
  6. class QappSendOrderContentShieldConfigService
  7. {
  8. public static function doShied($sen_order_id,$ip)
  9. {
  10. $config = self::getShieldConfig($sen_order_id);
  11. if(!$config || (!$config->area && !$config->ip_text) || !$config->bid){
  12. return [];
  13. }
  14. $chapter_info = ChapterService::getChapterInfoByBidAndSeq($config->bid,1);
  15. if(!$chapter_info){
  16. return [];
  17. }
  18. if($config->area && !self::doShiedArea($config,$ip)){
  19. return [
  20. 'bid'=>$config->bid,
  21. 'cid'=>$chapter_info->id
  22. ];
  23. }
  24. if($config->ip_text && !self::doShiedAreaIp($config,$ip)){
  25. return [
  26. 'bid'=>$config->bid,
  27. 'cid'=>$chapter_info->id
  28. ];
  29. }
  30. return [];
  31. }
  32. private static function doShiedArea($config,$ip)
  33. {
  34. $area_list = explode(',',$config->area);
  35. $ip2Region = new Ip2Region();
  36. $area = $ip2Region->binarySearch($ip);
  37. $area_info = explode('|',$area['region']);
  38. if(!isset($area_info[2]) || !$area_info[2] || !isset($area_info[3]) || !$area_info[3]){
  39. return '';
  40. }
  41. $province = $area_info[2];
  42. $city = $area_info[3];
  43. $access = true;
  44. foreach ($area_list as $area_item){
  45. $area_item_list = explode('/',$area_item);
  46. $item = [
  47. 'province'=>$area_item_list[0],
  48. 'city'=>isset($area_item_list[1]) ? $area_item_list[1]:''
  49. ];
  50. $item_province = str_replace('省','',$item['province']);
  51. $item_province = str_replace('市','',$item_province);
  52. if($item_province == str_replace('省','',$province)){
  53. if(!$item['city']){$access = false;break;}
  54. if(str_replace('市','',$item['city']) == str_replace('市','',$city)){
  55. $access = false;break;
  56. }
  57. }
  58. }
  59. return $access;
  60. }
  61. private static function doShiedAreaIp($config, $ip)
  62. {
  63. $ip_list = explode(',',$config->ip_text);
  64. $access = true;
  65. foreach ($ip_list as $item_ip){
  66. if(strstr($item_ip, '*') == "*"){
  67. $ip_format = trim(strstr($item_ip, '*',true),'.');
  68. if(stripos($ip,$ip_format) === 0){
  69. $access = false;break;
  70. }
  71. }else{
  72. if($item_ip == $ip){
  73. $access = false;
  74. break;
  75. }
  76. }
  77. }
  78. return $access;
  79. }
  80. public static function getShieldConfig($send_order_id)
  81. {
  82. return QappSendOrderContentShieldConfig::join('qapp_send_orders','qapp_send_orders.account','=','qapp_send_order_content_shield_config.qapp_account')
  83. ->where('qapp_send_orders.send_order_id',$send_order_id)
  84. ->where('qapp_send_order_content_shield_config.is_enable',1)
  85. ->select('qapp_send_order_content_shield_config.area',
  86. 'qapp_send_order_content_shield_config.ip_text',
  87. 'qapp_send_order_content_shield_config.bid'
  88. )
  89. ->first();
  90. }
  91. }