Area.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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('pid',0);
  14. $list = Db::name('shopro_area')->field('id,pid,name,level')->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('shopro_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. }