12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 地区接口
- */
- class Area extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- //首字母
- public function city_area(){
- $zimuarr = ['A'=>[],'B'=>[],'C'=>[],'D'=>[],'E'=>[],'F'=>[],'G'=>[],'H'=>[],'J'=>[],'K'=>[],'L'=>[],'M'=>[],'N'=>[],'P'=>[],'Q'=>[],'R'=>[],'S'=>[],'T'=>[],'W'=>[],'X'=>[],'Y'=>[],'Z'=>[]];
- $list = Db::name('area')->field('id,name,first')->where('level',2)->select();
- foreach($list as $key => $val){
- if(isset($zimuarr[$val['first']])){
- $zimuarr[$val['first']][] = $val;
- }
- }
- $result = [];
- foreach($zimuarr as $k => $v){
- $result[] = [
- 'zimu'=>$k,
- 'child' => $v,
- ];
- }
- $this->success(1,$result);
- }
- public function area_list(){
- $pid = input('pid',0);
- $list = Db::name('shopro_area')->field('id,pid,name')->where('pid',$pid)->order('id asc')->select();
- $this->success('success',$list);
- }
- //给ios用的
- //一个接口全部数据都给到
- public function area_json(){
- $list = Db::name('shopro_area')->field('id,pid,name,level')->order('id asc')->select();
- //按级拆分
- $list_1 = [];
- $list_2 = [];
- $list_3 = [];
- foreach($list as $key => $value){
- if($value['level'] == 1){
- $list_1[] = $value;
- }
- if($value['level'] == 2){
- $list_2[] = $value;
- }
- if($value['level'] == 3){
- $list_3[] = $value;
- }
- }
- //三级并到市级
- foreach($list_2 as $k2 => $v2){
- foreach($list_3 as $k3 => $v3){
- if($v2['id'] == $v3['pid']){
- $list_2[$k2]['child'][] = $v3;
- }
- }
- }
- //市级并到省级
- foreach($list_1 as $k1 => $v1){
- foreach($list_2 as $k2 => $v2){
- if($v1['id'] == $v2['pid']){
- $list_1[$k1]['child'][] = $v2;
- }
- }
- }
- $this->success('success',$list_1);
- }
- }
|