AddressService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\service;
  12. use app\common\model\AddressModel;
  13. use utils\RedisCache;
  14. /**
  15. * 地址管理 by wes
  16. * Class AddressService
  17. * @package app\common\service
  18. */
  19. class AddressService
  20. {
  21. protected static $instance = null;
  22. protected $model = null;
  23. public function __construct()
  24. {
  25. $this->model = new AddressModel();
  26. }
  27. /**
  28. * 静态化入口
  29. * @return static|null
  30. */
  31. public static function make()
  32. {
  33. if(!self::$instance){
  34. self::$instance = new static();
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * 获取列表
  40. * @param $sid 上级ID
  41. * @return array|mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getListByParent($sid)
  47. {
  48. $cacheKey = "caches:address:list_{$sid}";
  49. $data = RedisCache::get($cacheKey);
  50. if($data){
  51. return $data;
  52. }
  53. $data = $this->model->where(['pid'=> $sid])->field('id,name,first,pinyin')->select();
  54. $data = $data? $data->toArray():[];
  55. if($data){
  56. RedisCache::set($cacheKey, $data, rand(5,10));
  57. }
  58. return $data;
  59. }
  60. }