欢迎光临
我们一直在努力

ECOS体系 地区字符串格式验证,主要用于统一其他格式地区为ECOS标准地区,主要用于系统地区数据对接

当我们做其他系统地区导入的时候,比如从其他系统导入订单.一定会包含地区数据.,这时候我们怎么匹配上ECOS本地地区数据呢

我们下边提供,ECOS地区本地化类.直接引用即可.代码如下

<?php
class test_service_regions
{
    /**
     * 地区字符串格式验证
     * 正则匹配地区是否为本系统的标准地区格式,标准格式原样返回,非标准格式试图转换标准格式返回,否则
     * @access static
     * @param string $area 待验证地区字符串
     * @return string 转换后的本系统标准格式地区
     */
    public function region_validate(&$area){
        $is_correct_area = $this->is_correct_region($area);
        if (!$is_correct_area){
            //非标准格式进行转换
            $this->local_region($area);
        }
    }

    /**
     * ECOS本地标准地区格式判断
     * @access public
     * @param string $area 地区字符串,如:malind:上海/徐汇区:22
     * @return boolean
     */
    public function is_correct_region($area){
        $pattrn = "/^([a-zA-Z]+)\:(\S+)\:(\d+)$/";
        if (preg_match($pattrn, $area)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 本系统标准地区格式转换
     * 正则匹配地区是否为本系统的标准地区格式,转换成功返回标准地区格式,转换失败原地区字符串返回
     * @access static
     * @param string $area 待转换地区字符串
     * @return string  转换后的本系统标准格式地区
    */
    public function local_region(&$area){

        $tmp_area = explode("/",$area);
        //地区初始值临时存储
        $ini_first_name = trim($tmp_area[0]);
        $ini_second_name = trim($tmp_area[1]);
        $ini_third_name = trim($tmp_area[2]);

        $tmp_area2 = preg_replace("/省|市|县|区/","",$tmp_area);
        $first_name = trim($tmp_area2[0]);
        //自治区兼容
        $tmp_first_name = $this->area_format($first_name);
        if ($tmp_first_name) $first_name = $tmp_first_name;
        $second_name = trim($tmp_area2[1]);
        $third_name = trim($tmp_area2[2]);
        $regionObj = &app::get('ectools')->model('regions');

        $region_first = $region_second = $region_third = "";
        if ($first_name){
            //获取省------region_id
            //TODO:针对北京省份数据存在BOM头进行兼容
            if (strstr($first_name, '北京')){
                $bom_first_name = chr(239).chr(187).chr(191).$first_name;
                $region_first = $regionObj->dump(array('local_name|head'=>$bom_first_name,'region_grade'=>'1'), 'package,region_id,local_name');
                if (empty($region_first)){
                  $region_first = $regionObj->dump(array('local_name|head'=>$first_name,'region_grade'=>'1'), 'package,region_id,local_name');
                }
            }else{
                $region_first = $regionObj->dump(array('local_name|head'=>$first_name,'region_grade'=>'1'), 'package,region_id,local_name');
            }
            $first_name = $region_first['local_name'];
            
            if (!$first_name){
                $region_first = array(
                    'local_name' =>$ini_first_name,
                    'package' =>'mainland',
                    'region_grade' =>'1',
                );
                $regionObj->save($region_first);
                $first_name = $region_first['local_name'];
                $region_path = ",".$region_first['region_id'].",";
                //更新region_path字段
                $regionObj->update(array('region_path'=>$region_path), array('region_id'=>$region_first['region_id']));
            }
            
        }
        
        if ($second_name){//获取市------region_id
            //精确查找
            $second_filter = array('local_name'=>trim($tmp_area[1]),'region_grade'=>'2','p_region_id'=>$region_first['region_id']);
            $region_second = $regionObj->dump($second_filter, 'package,region_id,p_region_id,local_name');
            
            if (empty($region_second['local_name'])){
                //模糊查找
                $second_filter = array('local_name|head'=>$second_name,'region_grade'=>'2','p_region_id'=>$region_first['region_id']);
                $region_second = $regionObj->dump($second_filter, 'package,region_id,p_region_id,local_name');
            }
            $second_name = $region_second['local_name'];
            if (!$second_name){
                $region_second = array(
                    'local_name' =>$ini_second_name,
                    'p_region_id' =>$region_first['region_id'],
                    'package' =>'mainland',
                    'region_grade' =>'2',
                );
                $regionObj->save($region_second);
                $second_name = $region_second['local_name'];
                $region_path = ",".$region_first['region_id'].",".$region_second['region_id'].",";
                //更新region_path字段
                $regionObj->update(array('region_path'=>$region_path), array('region_id'=>$region_second['region_id']));

                //添加二级地区后更新一级地区的
                $regionObj->update(array('haschild'=>1), array('region_id'=>$region_first['region_id']));
            }
        }
        if ($third_name){
            //获取县region_id
            if (!$region_second['region_id']){
                
                //先根据第三级查出所有第二级
                $filter = array('local_name|head'=>$third_name);
                
                $regions = $regionObj->getList('p_region_id', $filter, 0, -1);
                
                if ($regions){
                    foreach ($regions as $k=>$v){
                        $region_second_tmp = $regionObj->dump(array('region_id'=>$v['p_region_id'],'region_grade'=>'2'), 'region_path,package,region_id,p_region_id,local_name');
                        
                        $tmp = explode(",",$region_second_tmp['region_path']);
                        if (in_array($region_first['region_id'],$tmp)){
                            $region_second = $region_second_tmp;
                            $second_name = $region_second['local_name'];
                            break;
                        }
                    }
                }
            }
            
            //精确查找
            $third_filter = array('local_name'=>trim($tmp_area[2]),'region_grade'=>'3','p_region_id'=>$region_second['region_id']);
            $region_third = $regionObj->dump($third_filter, 'package,region_id,p_region_id,local_name');
            if (empty($region_third['local_name'])){
                //模糊查找
                $third_filter = array('local_name|head'=>$third_name,'region_grade'=>'3','p_region_id'=>$region_second['region_id']);
                
                $region_third = $regionObj->dump($third_filter, 'package,region_id,p_region_id,local_name');
            }
            $third_name = $region_third['local_name'];
            if (!$third_name){
                if ($region_second['region_id']) {
                    
                
                    $region_third = array(
                        'local_name' =>$ini_third_name,
                        'p_region_id' =>$region_second['region_id'],
                        'package' =>'mainland',
                        'region_grade' =>'3',
                    );

                    $regionObj->save($region_third);
                
                    $third_name = $region_third['local_name'];
                    $region_path = ",".$region_first['region_id'].",".$region_second['region_id'].",".$region_third['region_id'].",";
                    //更新region_path字段
                    $regionObj->update(array('region_path'=>$region_path), array('region_id'=>$region_third['region_id']));

                    //添加三级地区后更新二级地区的
                    $regionObj->update(array('haschild'=>1), array('region_id'=>$region_second['region_id']));
                }else{
                    
                    $region_third = $regionObj->dump(array('local_name|head'=>$tmp_area[2],'p_region_id'=>$region_first['region_id']), 'package,region_id,p_region_id,local_name');
                    if ($region_third) {
                        $third_name = $tmp_area[2];
                    }
                    
                }
            }
        }
        $return = false;
        
        if ($region_third['region_id']){
            $region_id = $region_third['region_id'];
            $package = $region_third['package'];
        }elseif ($region_second['region_id']){
            $region_id = $region_second['region_id'];
            $package = $region_second['package'];
        }
        $region_area = array_filter(array($first_name,$second_name,$third_name));
        $region_area = implode("/", $region_area);
      
        if ($region_area || $region_id){
            $area = $package.":".$region_area.":".$region_id;
            $return = true;
        }

        //去除多余分隔符“/”
        if ($return==false){
            $area = implode("/", array_filter($tmp_area));
        }

    }

    /**
     * 前端店铺三级地区本地临时转换
     * @param $area
     */
    public function area_format($area){
        $area_format = array(
            '内蒙古自治' => '内蒙古',
            '广西壮族自治' => '广西',
            '西藏自治' => '西藏',
            '宁夏回族自治' => '宁夏',
            '新疆维吾尔自治' => '新疆',
            '香港特别行政' => '香港',
            '澳门特别行政' => '澳门',
        );
        if ($area_format[$area]){
            return $area_format[$area];
        }else{
            return false;
        }
    }

    /**
     * 拆分标准格式为:省市县
     * @param string $area
     * @return array 下标从0开始,依次代表:省、市、县
     */
    public function split_area(&$area){
       preg_match("/:(.*):/", $area,$tmp_area);
       if($tmp_area[1]){
           $tmp_area = explode('/', $tmp_area[1]);
           $area = $tmp_area;
       }
    }   
    
    
}

下边是使用方法

(正则匹配地区是否为本系统的标准地区格式,标准格式原样返回,非标准格式试图转换标准格式返回,直接存入数据库即可,若数据库没有该地区的数据.则会自动新建.)

    ecstore地区本地化
        $area = '广东省/深圳市/测试区';
        kernel::single('test_service_regions')->region_validate($area);
        $area = str_replace('::','',$area);
        echo $area;die;

输出:mainland:广东/深圳市/测试区:1425

刚刚我们测试的是ecos体系里边地区里边所没有的地区,接下来我们去看后台,这个地区是否新建了.

ecos地区体系本地化

ecos地区体系本地化

 

这个类的便捷性就在于,你只需要传入地区数据.直接转换成本地化地区数据库,,直接存入数据库.就可直接匹配上.系统会自动判断地区库里是否存在,该地区,若没有新建,若有,则直接保存,这对于第三方系统对接用处特别大.

未经允许不得转载:PHP之窗 » ECOS体系 地区字符串格式验证,主要用于统一其他格式地区为ECOS标准地区,主要用于系统地区数据对接

分享到:更多 ()

评论 3

  • (必填)
  • 12 + 5 = ?(必填)
  1. #3

    我又来了,您高兴吗?!

    1812598693年前 (2016-04-16)回复
  2. #2

    不错,不错,看看了!

    易路营销3年前 (2016-04-24)回复
  3. #1

    研究研究,学习学习。

    zengda3年前 (2016-05-16)回复

产品与解决方案,顶尖的服务!!

联系我们