Area.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 地区接口
  7. */
  8. class Area extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function city_area(){
  13. $zimuarr = ['A'=>[],'B'=>[],'C'=>[],'D'=>[],'E'=>[],'F'=>[],'G'=>[],'H'=>[],'J'=>[],'K'=>[],'L'=>[],'M'=>[],'N'=>[],'P'=>[],'Q'=>[],'R'=>[],'S'=>[],'T'=>[],'W'=>[],'X'=>[],'Y'=>[],'Z'=>[]];
  14. $list = Db::name('area')->field('id,name,first')->where('level',2)->select();
  15. foreach($list as $key => $val){
  16. if(isset($zimuarr[$val['first']])){
  17. $zimuarr[$val['first']][] = $val;
  18. }
  19. }
  20. $result = [];
  21. foreach($zimuarr as $k => $v){
  22. $result[] = [
  23. 'zimu'=>$k,
  24. 'child' => $v,
  25. ];
  26. }
  27. $this->success(1,$result);
  28. }
  29. public function area_list(){
  30. $pid = input_post('pid',0);
  31. $list = Db::name('area')->field('id,pid,name')->where('pid',$pid)->order('id asc')->select();
  32. $this->success('success',$list);
  33. }
  34. //给ios用的
  35. //一个接口全部数据都给到
  36. public function area_json(){
  37. $list = Db::name('area')->field('id,pid,name,level')->order('id asc')->select();
  38. //按级拆分
  39. $list_1 = [];
  40. $list_2 = [];
  41. $list_3 = [];
  42. foreach($list as $key => $value){
  43. if($value['level'] == 1){
  44. $list_1[] = $value;
  45. }
  46. if($value['level'] == 2){
  47. $list_2[] = $value;
  48. }
  49. if($value['level'] == 3){
  50. $list_3[] = $value;
  51. }
  52. }
  53. //三级并到市级
  54. foreach($list_2 as $k2 => $v2){
  55. foreach($list_3 as $k3 => $v3){
  56. if($v2['id'] == $v3['pid']){
  57. $list_2[$k2]['child'][] = $v3;
  58. }
  59. }
  60. }
  61. //市级并到省级
  62. foreach($list_1 as $k1 => $v1){
  63. foreach($list_2 as $k2 => $v2){
  64. if($v1['id'] == $v2['pid']){
  65. $list_1[$k1]['child'][] = $v2;
  66. }
  67. }
  68. }
  69. $this->success('success',$list_1);
  70. }
  71. }