CityService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @return array
  78. * @since 2020/11/11
  79. * @author wesmiler
  80. */
  81. public function getPickerList()
  82. {
  83. $cacheKey = "caches:citys";
  84. $cityList = RedisService::get($cacheKey);
  85. if($cityList){
  86. return message("操作成功", true, $cityList);
  87. }
  88. $datas = $this->model::where(['mark'=> 1])
  89. ->select(['id','pid','level','name','citycode as code'])
  90. ->orderBy('sort')
  91. ->get()
  92. ->keyBy('id');
  93. $datas = $datas? $datas->toArray() : [];
  94. if($datas){
  95. $cityList = $this->formatCity($datas);
  96. if($cityList){
  97. RedisService::set($cacheKey, $cityList, 3600);
  98. }
  99. }
  100. return message("操作成功", true, $cityList);
  101. }
  102. /*
  103. * 格式化
  104. */
  105. public function formatCity($datas, $parentId=0){
  106. $cityList = [];
  107. foreach ($datas as $key => $v){
  108. $id = isset($v['id'])? $v['id'] : 0;
  109. $pid = isset($v['pid'])? $v['pid'] : 0;
  110. if($pid == $parentId){
  111. $children = $this->formatCity($datas, $id);
  112. //if($children){
  113. $v['children'] = $children;
  114. //}
  115. $cityList[] = $v;
  116. }
  117. }
  118. return $cityList;
  119. }
  120. /**
  121. * 获取城市名称
  122. * @param $id
  123. * @return mixed
  124. */
  125. public function getName($id){
  126. return $this->model::where(['citycode'=> $id])->value('name');
  127. }
  128. }