City.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store\shipping;
  12. use app\common\repositories\store\CityAreaRepository;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\store\shipping\CityRepository as repository;
  16. use think\facade\Log;
  17. class City extends BaseController
  18. {
  19. protected $repository;
  20. /**
  21. * City constructor.
  22. * @param App $app
  23. * @param repository $repository
  24. */
  25. public function __construct(App $app, repository $repository)
  26. {
  27. parent::__construct($app);
  28. $this->repository = $repository;
  29. }
  30. /**
  31. * 获取列表
  32. *
  33. * @return \think\response\Json
  34. */
  35. public function lst()
  36. {
  37. // 调用 repository 类的 getFormatList 方法获取列表数据,并通过 json 组件返回成功状态和数据
  38. return app('json')->success($this->repository->getFormatList());
  39. }
  40. /**
  41. * 获取指定 pid 下的子级列表
  42. *
  43. * @param int $pid 父级 ID
  44. * @return \think\response\Json
  45. */
  46. public function lstV2($pid)
  47. {
  48. // 调用 CityAreaRepository 类的 getChildren 方法获取子级列表数据,并通过 json 组件返回成功状态和数据
  49. return app('json')->success(app()->make(CityAreaRepository::class)->getChildren(intval($pid)));
  50. }
  51. /**
  52. * 根据地址查询城市列表
  53. *
  54. * @return \think\response\Json
  55. */
  56. public function cityList()
  57. {
  58. // 从请求参数中获取地址
  59. $address = $this->request->param('address');
  60. // 如果地址不存在,则返回失败状态和错误信息
  61. if (!$address){
  62. Log::info('用户定位对比失败,地址不存在:' . var_export($address, true));
  63. return app('json')->fail('地址不存在');
  64. }
  65. // 创建 CityAreaRepository 实例
  66. $make = app()->make(CityAreaRepository::class);
  67. // 根据地址查询城市信息
  68. $city = $make->search(compact('address'))->order('id DESC')->find();
  69. // 如果城市信息不存在,则记录日志并返回失败状态和错误信息
  70. if (!$city) {
  71. Log::info('用户定位对比失败,请在城市数据中增加:' . var_export($address, true));
  72. return app('json')->fail('地址不存在');
  73. }
  74. // 调用 CityAreaRepository 类的 getCityList 方法获取城市列表数据,并通过 json 组件返回成功状态和数据
  75. return app('json')->success($make->getCityList($city));
  76. }
  77. /**
  78. * 获取列表数据
  79. *
  80. * @return \Illuminate\Http\JsonResponse
  81. */
  82. public function getlist()
  83. {
  84. // 调用 repository 类的 getFormatList 方法获取 is_show 字段为 1 的列表数据,并通过 json 组件返回成功状态和数据
  85. return app('json')->success($this->repository->getFormatList(['is_show' => 1]));
  86. }
  87. }