Commit 97b829c1 authored by liuyuzhen's avatar liuyuzhen

省市测试

parent 9c8220ac
......@@ -192,4 +192,24 @@ class HomeController extends \Our\Controller_AbstractIndex{
$otherGoods = $goodsCommonService->getIndexHotSalesGoods($currentAddress);
$this->success($otherGoods);
}
public function testCityAction(){
$cityCode = \Area\AreaUtil::getInstance()->getIpCityInfo();
var_dump($cityCode);
$cityCode1 = \Area\AreaUtil::getInstance()->getIpCityInfo('218.195.219.255');
var_dump($cityCode1);
$cityCode2 = \Area\AreaUtil::getInstance()->getIpCityInfo('61.244.148.166');
var_dump($cityCode2);
$cityCode3 = \Area\AreaUtil::getInstance()->getIpCityInfo('60.246.233.219');
var_dump($cityCode3);
$cityCode4 = \Area\AreaUtil::getInstance()->getIpCityInfo('59.125.39.5');
var_dump($cityCode4);
$cityCode5 = \Area\AreaUtil::getInstance()->getIpCityInfo('169.235.24.133');
var_dump($cityCode5);
}
}
\ No newline at end of file
<?php
namespace Area;
/**
* User: liuyuzhen
* Date: 2018/12/3
* Time: 16:52
* Description:
*/
class AreaUtil {
public function getIpCityInfo($ip = false){
if($ip==false){
$ip =\Our\Common::getIP();
}
$cityData = \Our\Common::getIpCity($ip);
$provinceId = \Our\ApiConst::zero;
$cityId = \Our\ApiConst::zero;
switch($cityData['country']){
case \Our\NameConst::China:
if($cityData['region']){
$province = mb_substr($cityData['region'],0,2);
$provinceObj = \DAO\AreaModel::getInstance()->getOne(array('area_deep'=>1, 'area_name'=>array('like',$province.'%')),'area_id,area_name');
if($provinceObj){
$provinceId=$provinceObj['area_id'];
$city = trim($cityData['city']);
if($city){
$cityObj = \DAO\AreaModel::getInstance()->getOne(array('area_parent_id'=>$provinceId, 'area_name'=>$city),'area_id,area_name');
$cityObj&&$cityId = $cityObj['area_id'];
}
}
}
break;
case \Our\NameConst::Macao:
$provinceId = \Our\ApiConst::MacaoCode;
break;
case \Our\NameConst::Hongkong:
$provinceId = \Our\ApiConst::HongkongCode;
break;
case \Our\NameConst::Taiwan:
$provinceId = \Our\ApiConst::TaiwanCode;
break;
default:
$provinceId = \Our\ApiConst::otherCountryCode;
break;
}
return array('member_provinceid'=>$provinceId,'member_cityid'=>$cityId);
}
/**
* 类实例
*
*/
private static $_instance = null;
/**
* 单例模式获取类实例
*
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
}
\ No newline at end of file
......@@ -462,6 +462,13 @@ class ApiConst
const arrBegin=0;
const TaiwanCode = 32;
const HongkongCode = 33;
const MacaoCode = 34;
const otherCountryCode = 35;
......
......@@ -575,4 +575,46 @@ class Common
return $price_format;
}
/**
* 获取当前访问用户地址
* @return string
*/
public static function getIP(){
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
/**
* 获取用户所在地区
* @param $ip
* @return array|bool
*/
public static function getIpCity($ip){
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
return $data;
}
}
......@@ -185,6 +185,15 @@ class NameConst {
const bySeller='bySeller';
const bySelf='bySelf';
const China = '中国';
const Hongkong = '香港';
const Taiwan = '台湾';
const Macao = '澳门';
const otherCountry = '海外';
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment