Area.php 2.2 KB

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