Git Product home page Git Product logo

region's Introduction

zwei/region 包

  1. 提供**城市区域(类似美团地区选择),数据**城市区域数据抓取美团地区数据
  2. 提供自定义层级地区

选择国家城市 选择区域 未选择区域

1 安装(Install)

  1. 通过Composer安装
  2. 创建composer.json文件,并写入以下内容:
{
  "require": {
    "zwei/region": "dev-master",
    "zwei/base": "~v1"
  }
}
  1. 执行composer install
  1. 在项目目录创建config/bao-loan.yml文件,添加一下内容
BASE_TEST_CONFIG: "base_test_config" # 本包测试键(单元测试用)

# 数据库配置
DB_HOST: "localhost" # 主机
DB_PORT: 3306 # 端口
DB_USER: "root" # 用户名
DB_PASS: "root" # 密码
DB_NAME: "demo" # 数据库名
DB_TABLE_PREFIX: "" # 表前缀
DB_CHARSET: "utf8" # 设置字符编码,空字符串不设置
DB_SQLLOG: false # 是否启用sql调试
  1. 导入"dev/sql/city_area.20180403.sql"文件中的sql到数据库中

常用查询语句

-- 查询所有国家列表
select * from city_area c INNER JOIN city_area_translation ct on c.region_id=ct.region_id
where region_pid = 0 and `region_level` = 1 and lang_code = 'zh-cn' order by letter_sort asc;

-- 查询**所有城市列表
select * from city_area c INNER JOIN city_area_translation ct on c.region_id=ct.region_id
where region_pid = 1 and `region_level` = 2 and lang_code = 'zh-cn' order by letter_sort asc;

-- 查询** 安岳城市 所有区域列表
select * from city_area c INNER JOIN city_area_translation ct on c.region_id=ct.region_id
where region_pid = 80 and `region_level` = 3 and lang_code = 'zh-cn';


-- 查询** 成都城市 所有区域列表
select * from city_area c INNER JOIN city_area_translation ct on c.region_id=ct.region_id
where region_pid = 554 and `region_level` = 3 and lang_code = 'zh-cn';

select count(*) /*10467条数据*/ from city_area;

多国家城市地区使用示例(use)

  1. 例如项目目录在"E:\web\php7\test"
  2. 创建index.php,并加入以下内容
<?php
include_once 'vendor/autoload.php';

use Overtrue\Pinyin\Pinyin;
use Zwei\Region\Custom\CityArea\CityArea;

$pinyin = new Pinyin(); // 默认
        
$cityArea   = new CityArea();
// 添加国家
$countryId  = $cityArea->addCountry("国家名");
$lang       = 'zh-cn';// 语言code
$countryLangName = "**";// 中文国家名

// 中文转拼音
$nameArr    = $pinyin->convert($countryLangName);
$spell      = implode('', $nameArr);// 拼写(中文语言是中文拼音, 英语语言就是英语)
$letterSort = strtoupper($spell[0]);// 拼写首字母
// 添加国家翻译
$cityArea->getRegionTranslation()->t($countryId, $lang, $countryLangName, $spell, $letterSort);

// 添加城市
$cityId = $cityArea->addCity($countryId, "城市名");
// 城市翻译
$cityArea->getRegionTranslation()->t($cityId, 'zh-cn', '成都', 'chengdu', 'C');

// 添加城市地区
$areaId = $cityArea->addCityArea($countryId, $cityId, '青羊区');
// 城市地区翻译
$cityArea->getRegionTranslation()->t($cityId, 'zh-cn', '青羊区', 'qingyangqu', 'Q');

// 获取中文语言国家列表
$lists = $cityArea->getCountryLists('zh-cn');
// 取**中文语言城市列表
$lists = $cityArea->getCityLists(1,'zh-cn');
// 取** 成都城市中文语言区域列表
$lists = $cityArea->getCityAreaLists(554,'zh-cn');

自定义地区请使用"Region"和"Region"

请参考"Zwei\Region\Custom\CityArea\CityArea"类,该类就是使用的"Region"和"RegionTranslation"封装的

use Zwei\Region\Region;
use Zwei\Region\RegionTranslation;

$region = new Region();
$regionTranslation = new RegionTranslation();

单元测试使用

--bootstrap 在测试前先运行一个 "bootstrap" PHP 文件

  • --bootstrap引导测试: phpunit --bootstrap tests/TestInit.php tests/

  • --bootstrap美团爬取城市字母测试: phpunit --bootstrap tests/TestInit.php tests/MeiTuanSpiderTest.php --filter testGetLetterLists

  • --bootstrap美团爬取城市列表测试: phpunit --bootstrap tests/TestInit.php tests/MeiTuanSpiderTest.php --filter testGetCityLists

  • --bootstrap根据美团城市信息生成多国家多语言城市区域地区信息测试: phpunit --bootstrap tests/TestInit.php tests/MeiTuanSpiderTest.php --filter testGeneratorCountryCityArea

  • --bootstrap国家城市地区列表查询测试: phpunit --bootstrap tests/TestInit.php tests/CityAreaTest.php

D:\phpStudy\php\php-7.1.13-nts\php.exe D:\phpStudy\php\php-5.6.27-nts\composer.phar update

D:\phpStudy\php\php-7.1.13-nts\php.exe vendor\phpunit\phpunit\phpunit --bootstrap tests/TestInit.php tests/

region's People

Contributors

qq1060656096 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.