ExpressDeliveryService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\ExpressDeliveryModel;
  13. use utils\RedisCache;
  14. /**
  15. * 配送费模板服务 by wes
  16. * Class ExpressDeliveryService
  17. * @package app\common\service
  18. */
  19. class ExpressDeliveryService
  20. {
  21. protected static $instance = null;
  22. protected $model = null;
  23. public function __construct()
  24. {
  25. $this->model = new ExpressDeliveryModel();
  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 $templateId 模板ID
  41. * @param bool $cache 是否缓存
  42. * @return array|mixed
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getDataByTemplate($templateId, $cache=true)
  48. {
  49. if($templateId<=0){
  50. return false;
  51. }
  52. $cacheKey = "caches:express:info_{$templateId}";
  53. $data = RedisCache::get($cacheKey);
  54. if($data && $cache){
  55. return $data;
  56. }
  57. $where = ['id'=> $templateId];
  58. $data = $this->model->where($where)->findOrEmpty();
  59. $data = $data? $data->toArray():[];
  60. if($data && $cache){
  61. RedisCache::set($cacheKey, $data, rand(10,20));
  62. }
  63. return $data;
  64. }
  65. }