CityService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\CityModel;
  13. /**
  14. * 城市管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class CityService
  18. * @package App\Services
  19. */
  20. class CityService extends BaseService
  21. {
  22. protected static $instance;
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * CityService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new CityModel();
  32. }
  33. /**
  34. * @return CityService
  35. */
  36. public static function make(){
  37. if(!self::$instance){
  38. self::$instance = new CityService();
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * 获取城市列表
  44. * @return array
  45. * @since 2020/11/11
  46. * @author wesmiler
  47. */
  48. public function getList()
  49. {
  50. $param = request()->all();
  51. // 查询条件
  52. $map = [];
  53. // 上级ID
  54. $pid = intval(getter($param, 'pid', 0));
  55. if (!$pid) {
  56. $map[] = ['pid', '=', 0];
  57. } else {
  58. $map[] = ['pid', '=', $pid];
  59. }
  60. // 城市名称
  61. $name = getter($param, "name");
  62. if ($name) {
  63. $map[] = ['name', 'like', "%{$name}%"];
  64. }
  65. $list = $this->model->getList($map, [['sort', 'asc']]);
  66. if (!empty($list)) {
  67. foreach ($list as &$val) {
  68. if ($val['level'] <= 2) {
  69. $val['hasChildren'] = true;
  70. }
  71. }
  72. }
  73. return message("操作成功", true, $list);
  74. }
  75. /**
  76. * 获取城市名称
  77. * @param $id
  78. * @return mixed
  79. */
  80. public function getName($id){
  81. return $this->model::where(['citycode'=> $id])->value('name');
  82. }
  83. }