AddressService.php 1.3 KB

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