Area.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. $isArea = $this->request->param('is_area',0);
  21. $list = Db::name('shopro_area')->field('id,pid,name,level')->order('id asc')->select();
  22. //按级拆分
  23. $list_1 = [];
  24. $list_2 = [];
  25. if ($isArea == 1) {
  26. $list_3 = [];
  27. }
  28. foreach($list as $key => $value){
  29. if($value['level'] == 1){
  30. $list_1[] = $value;
  31. }
  32. if($value['level'] == 2){
  33. $list_2[] = $value;
  34. }
  35. if ($isArea == 1) {
  36. if($value['level'] == 3){
  37. $list_3[] = $value;
  38. }
  39. }
  40. }
  41. if ($isArea == 1) {
  42. //三级并到市级
  43. foreach ($list_2 as $k2 => $v2) {
  44. foreach ($list_3 as $k3 => $v3) {
  45. if ($v2['id'] == $v3['pid']) {
  46. $list_2[$k2]['child'][] = $v3;
  47. }
  48. }
  49. }
  50. }
  51. //市级并到省级
  52. foreach($list_1 as $k1 => $v1){
  53. foreach($list_2 as $k2 => $v2){
  54. if($v1['id'] == $v2['pid']){
  55. $list_1[$k1]['child'][] = $v2;
  56. }
  57. }
  58. }
  59. $this->success('success',$list_1);
  60. }
  61. }