ipaddress.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. class ModelIpaddress{
  3. public $countryMaster =['RO','US','DE','GB','ES','FR','IN'];//主要国家:罗马尼亚,美国,德国,英国,西班牙,法国,印度
  4. public function getUserCountry($uid,$db=false){
  5. $cache =[];
  6. $key = okeys::userCountryInfo($uid);
  7. if(!$db){
  8. $cache = oo::commonOprRedis('userinfo')->hGetAll($key);
  9. oo::commonOprRedis('userinfo')->expire($key,oo::redisRandomExpire(7*86400));
  10. }
  11. if(empty($cache)){
  12. $tb = otable::useraddress();
  13. $sql = "SELECT * FROM {$tb} where uid={$uid}";
  14. $info = oo::commonOprDb('userinfo')->getOne($sql,1);
  15. $info['showCode'] = !empty($info['country_code'])&&in_array($info['country_code'],$this->countryMaster)?$info['country_code']:'OTHER';
  16. if(isset($info['uid'])&&!empty($info['uid'])){
  17. oo::commonOprRedis('userinfo')->hMset($key,$info);
  18. oo::commonOprRedis('userinfo')->expire($key,oo::redisRandomExpire(7*86400));
  19. $cache = $info;
  20. }
  21. }
  22. return $cache;
  23. }
  24. public function updateUserCountry($data){
  25. }
  26. /**
  27. * Notes:新写用户ip缓存队列
  28. * User: wsc
  29. * Time: 2020/5/19 16:01
  30. * @param $uid
  31. * @param $ip
  32. * @param int $type 1写入 2更新 3失败的
  33. */
  34. public function intoIpList($uid,$ip,$type=1){
  35. $arr = ["uid"=>$uid,"ip"=>$ip];
  36. $res = 0;
  37. if(!empty($arr['uid'])){//&&!empty($arr['ip'])
  38. $key = okeys::newUserIpList($type);
  39. $json = json_encode($arr);
  40. $res = oo::commonOprRedis('common')->rPush($key,$json);
  41. if(!$res){
  42. oo::logs()->debug3(["type"=>4,"data"=>'入队失败,data:'.$json,"res"=>$res],'getIpErr');
  43. }
  44. }
  45. return $res;
  46. }
  47. /**
  48. * Notes:定时消费更新用户地址信息
  49. * User: wsc
  50. * Time: 2020/5/29 15:05
  51. * @return bool
  52. */
  53. public function updateUserAddressIntoDb(){
  54. $key = okeys::newUserIpList(2);
  55. $listJsonArr = oo::commonOprRedis('common')->lGetRange($key,0,99);
  56. $list = funs::getArrFromJsonArr($listJsonArr);
  57. $tb = otable::useraddress();
  58. if(!empty($list)){
  59. foreach ($list as $k=>$v){
  60. $arr =[];
  61. $setdata='';
  62. $arr = $this->getAddressByIp($v['uid'],$v['ip']);
  63. if(isset($arr['country_code'])&&!empty($arr['country_code'])){
  64. $setdata = "country_code='{$arr['country_code']}',";
  65. }
  66. if(isset($arr['continent'])&&!empty($arr['continent'])){
  67. $setdata .= "continent='{$arr['continent']}',";
  68. }
  69. if(isset($arr['country'])&&!empty($arr['country'])){
  70. $setdata .= "country='{$arr['country']}',";
  71. }
  72. if(!empty($setdata)){
  73. $setdata = rtrim($setdata, ',');
  74. $sql = "update {$tb} SET {$setdata} where uid={$v['uid']}";
  75. oo::commonOprDb('userinfo')->query($sql,false);
  76. }
  77. oo::commonOprRedis('common')->lRemove($key,$listJsonArr[$k],1);
  78. }
  79. }else{
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Notes:定时消费队列入库
  86. * User: wsc
  87. * Time: 2020/5/19 16:01
  88. */
  89. public function writeUserAddressIntoDB(){
  90. $key = okeys::newUserIpList(1);
  91. $listJsonArr = oo::commonOprRedis('common')->lGetRange($key,0,99);
  92. $list = funs::getArrFromJsonArr($listJsonArr);
  93. $errData=$res=[];
  94. if(!empty($list)){
  95. $value ='';
  96. foreach ($list as $k=>$v){
  97. $arr = $this->getAddressByIp($v['uid'],$v['ip']);
  98. $arr_values = array_values($arr);
  99. $value =" ('" . implode("','" ,$arr_values) . "')";
  100. oo::commonOprRedis('common')->lRemove($key,$listJsonArr[$k],1);
  101. $sql = "INSERT INTO crazygod_api_user.t_useraddress(uid,ip,continent,country,country_code)values $value";
  102. $res[$v['uid']] =oo::commonOprDb('common')->query($sql);
  103. }
  104. // oo::logs()->debug3(["type"=>3,"data"=>$list,"res"=>$res],'getIp');
  105. }
  106. return true;
  107. }
  108. /**
  109. * Notes:获取ip地址信息
  110. * User: wsc
  111. * Time: 2020/5/19 16:02
  112. * @param $uid
  113. * @param $ip
  114. * @return array
  115. */
  116. public function getAddressByIp($uid,$ip,$t=0){
  117. $data =[
  118. 'uid' =>$uid,
  119. 'ip' =>$ip,
  120. 'continent' =>"",
  121. 'country' =>"",
  122. 'country_code'=>""
  123. ];
  124. if(!empty($ip)&&$ip!='127.0.0.1') {
  125. $res = funs::ip_info($ip);
  126. if (!empty($res)) {
  127. $data['continent'] = isset($res['continent']) ? $res['continent'] : '';
  128. $data['country'] = isset($res['country']) ? $res['country'] : '';
  129. $data['country_code'] = isset($res['country_code']) ? $res['country_code'] : '';
  130. } else if(!$t) {//失败的ip入队到备份ip列表
  131. $this->intoIpList($uid, $ip, 3);
  132. }
  133. }
  134. return $data;
  135. }
  136. public function setUserAddress($uid,$ip){
  137. $ret = [];
  138. $data = $this->getAddressByIp($uid,$ip);
  139. if(!empty($data)){
  140. $sql = funs::db_insertSQL(otable::useraddress(),$data);
  141. $res =oo::commonOprDb('common')->query($sql);
  142. if($res){
  143. $ret = $this->getUserCountry($uid);
  144. }
  145. }
  146. return $ret;
  147. }
  148. }