Addr.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. class Addr extends BaseController
  6. {
  7. public function province(){
  8. $list=Db::name("province")->select();
  9. return app_show(0,"获取成功",$list);
  10. }
  11. public function area(){
  12. $post=$this->request->post();
  13. $city_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
  14. if($city_code==""){
  15. return error_show(1002,"市级未选择");
  16. }
  17. $city =Db::name("city")->where(['city_code'=>$city_code])->find();
  18. if(empty($city)){
  19. return error_show(1003,"参数code非法数据");
  20. }
  21. $list=Db::name("area")->where(['city_code'=>$city_code])->select();
  22. return app_show(0,"获取成功",$list);
  23. }
  24. public function city(){
  25. $post=$this->request->post();
  26. $province_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
  27. if($province_code==""){
  28. return error_show(1002,"省级未选择");
  29. }
  30. $provice = Db::name("province")->where(['province_code'=>$province_code])->find();
  31. if(empty($provice)){
  32. return error_show(1003,"参数code非法数据");
  33. }
  34. $list=Db::name("city")->where(['province_code'=>$province_code])->select();
  35. return app_show(0,"获取成功",$list);
  36. }
  37. public function all(){
  38. $post=$this->request->post();
  39. $city_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
  40. if($city_code==""){
  41. return error_show(1002,"区级Code未选择");
  42. }
  43. $data=[];
  44. $area =Db::name("area")->where(['area_code'=>$city_code])->field("name,area_code as code,city_code")->find();
  45. if($area==false){
  46. return error_show(1003,"未找到区级数据");
  47. }
  48. $data["area"]=$area;
  49. $city=Db::name("city")->where(['city_code'=>$area['city_code']])->field("name,city_code as code,province_code")->find();
  50. if($city==false){
  51. $data["city"]=[];
  52. }
  53. $data['city']=$city??[];
  54. $province=Db::name("province")->where(['province_code'=>$city['province_code']])->field("name,province_code as code")->find();
  55. $data['province']=$province??[];
  56. return app_show(0,"获取成功",$data);
  57. }
  58. }