CityService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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;
  12. use App\Models\CityModel;
  13. use App\Models\ConfigModel;
  14. /**
  15. * 地址管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * @package App\Services
  19. */
  20. class CityService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * ConfigService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new CityModel();
  33. }
  34. /**
  35. * 静态入口
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new static();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取
  45. * @param $name
  46. * @return array|int|mixed
  47. */
  48. public function getFieldByName($name, $field='id')
  49. {
  50. if(empty($name)){
  51. return 0;
  52. }
  53. $cacheKey = "caches:address:city_{$field}_".md5($name);
  54. $data = RedisService::get($cacheKey);
  55. if($data){
  56. return isset($data[$field])? $data[$field] : 0;
  57. }
  58. $data = $provinceId = $this->model->where('name','like',"%{$name}%")->select(['id','pid','name','level','citycode'])->first();
  59. $data = $data? $data->toArray() : [];
  60. if($data){
  61. RedisService::set($cacheKey, [], 3600);
  62. }
  63. return $data;
  64. }
  65. }