CityService.php 2.8 KB

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