Area.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 area_list(){
  13. $pid = input_post('pid',0);
  14. $list = Db::name('area')->field('id,pid,name')->where('pid',$pid)->order('id asc')->select();
  15. $this->success('success',$list);
  16. }
  17. //给ios用的
  18. //一个接口全部数据都给到
  19. public function area_json(){
  20. $list = Db::name('area')->field('id,pid,name,level')->order('id asc')->select();
  21. //按级拆分
  22. $list_1 = [];
  23. $list_2 = [];
  24. $list_3 = [];
  25. foreach($list as $key => $value){
  26. if($value['level'] == 1){
  27. $list_1[] = $value;
  28. }
  29. if($value['level'] == 2){
  30. $list_2[] = $value;
  31. }
  32. if($value['level'] == 3){
  33. $list_3[] = $value;
  34. }
  35. }
  36. //三级并到市级
  37. foreach($list_2 as $k2 => $v2){
  38. foreach($list_3 as $k3 => $v3){
  39. if($v2['id'] == $v3['pid']){
  40. $list_2[$k2]['child'][] = $v3;
  41. }
  42. }
  43. }
  44. //市级并到省级
  45. foreach($list_1 as $k1 => $v1){
  46. foreach($list_2 as $k2 => $v2){
  47. if($v1['id'] == $v2['pid']){
  48. $list_1[$k1]['child'][] = $v2;
  49. }
  50. }
  51. }
  52. $this->success('success',$list_1);
  53. }
  54. }