|
@@ -0,0 +1,112 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\data\controller\api;
|
|
|
+
|
|
|
+use app\data\model\BasePostageRegion;
|
|
|
+use hg\apidoc\annotation\Param;
|
|
|
+use hg\apidoc\annotation\Title;
|
|
|
+use think\admin\Controller;
|
|
|
+use think\Cache;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Title("地区模块")
|
|
|
+ */
|
|
|
+class Area extends Controller
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @Title("获取地区信息")
|
|
|
+ * @Param("level",desc="1省2市3区")
|
|
|
+ * @Param("pid",desc="上级区域ID")
|
|
|
+ */
|
|
|
+ public function area(){
|
|
|
+ $data=$this->_vali([
|
|
|
+ 'level'=>['require'],
|
|
|
+ 'pid'=>['require'],
|
|
|
+ ]);
|
|
|
+ $this->success(
|
|
|
+ '',
|
|
|
+ BasePostageRegion::where('level',$data['level'])
|
|
|
+ ->where('pid',$data['pid'])
|
|
|
+ ->order('pinyin')
|
|
|
+ ->select()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Title("获取地区信息(按字母排序)")
|
|
|
+ */
|
|
|
+ public function sort(){
|
|
|
+ $citys=BasePostageRegion::field(['id','name','first'])->select();
|
|
|
+ $a=[];
|
|
|
+ for ($i=65;$i<91;$i++) {
|
|
|
+ $one=[
|
|
|
+ 'index'=>chr($i),
|
|
|
+ 'child'=>[]
|
|
|
+ ];
|
|
|
+ foreach ($citys as $key=>&$city) {
|
|
|
+ if($city['first']==$one['index']){
|
|
|
+ $one['child'][]=[
|
|
|
+ 'title'=>$city['name'],
|
|
|
+ 'weight'=>$city['id'],
|
|
|
+ 'id' =>$city['id'],
|
|
|
+ ];
|
|
|
+ unset($citys[$key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $a[]=$one;
|
|
|
+ }
|
|
|
+ $this->success('',$a);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Title("地区递归三级联动")
|
|
|
+ */
|
|
|
+ public function tree(){
|
|
|
+ function area(&$data,$areas,$first=false){
|
|
|
+ foreach ($areas as $k=>$area){
|
|
|
+ if($first){
|
|
|
+ if($area['pid']==0) {
|
|
|
+ area($area, $areas);
|
|
|
+ unset($area['pid']);
|
|
|
+ $data[] = $area;
|
|
|
+ unset($areas[$k]);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if($data['value']==$area['pid']){
|
|
|
+ area($area, $areas);
|
|
|
+ unset($area['pid']);
|
|
|
+ $data['children'][]=$area;
|
|
|
+ unset($areas[$k]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $cache=$this->app->cache;
|
|
|
+ $fromCache=$cache->get('app_area');
|
|
|
+ if(!$fromCache){
|
|
|
+ $fina=[];
|
|
|
+ $areas=BasePostageRegion::where('level','<=',3)->field('id as value,name as label,pid')->select()->toArray();
|
|
|
+ area($fina,$areas,true);
|
|
|
+ $fromCache=$fina;
|
|
|
+ $cache->remember('app_area',$fina,0);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->success('',$fromCache);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Title("根据名称获取信息")
|
|
|
+ * @Param("name",desc="城市名")
|
|
|
+ * @ApiReturnParams (name="id",description="城市ID")
|
|
|
+ */
|
|
|
+ public function name(){
|
|
|
+ $rs=null;
|
|
|
+ $cache=$this->app->cache;
|
|
|
+ if($name=input('name')){
|
|
|
+ $cacheName="area_name_".$name;
|
|
|
+ $rs=$cache->get($cacheName,null);
|
|
|
+ if(!$rs){
|
|
|
+ $rs=BasePostageRegion::where('name|short',$name)->find();
|
|
|
+ $cache->set($cacheName,$rs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->success('',$rs);
|
|
|
+ }
|
|
|
+}
|