BaseService.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service;
  13. use think\facade\Request;
  14. use cores\traits\ErrorTrait;
  15. /**
  16. * 系统服务基础类
  17. * Class BaseService
  18. * @package app\common\service
  19. */
  20. class BaseService
  21. {
  22. use ErrorTrait;
  23. // 请求管理类
  24. /* @var $request \cores\Request */
  25. protected $request;
  26. // 当前访问的商城ID
  27. protected ?int $storeId;
  28. /**
  29. * 构造方法
  30. * BaseService constructor.
  31. */
  32. public function __construct()
  33. {
  34. // 请求管理类
  35. $this->request = Request::instance();
  36. // 获取当前操作的商城ID
  37. $this->getStoreId();
  38. // 执行子类的构造方法
  39. $this->initialize();
  40. }
  41. /**
  42. * 手动设置商城ID
  43. * @param int $storeId
  44. * @return static
  45. */
  46. public function setStoreId(int $storeId): self
  47. {
  48. $this->storeId = $storeId;
  49. return $this;
  50. }
  51. /**
  52. * 构造方法 (供继承的子类使用)
  53. */
  54. protected function initialize()
  55. {
  56. }
  57. /**
  58. * 获取当前操作的商城ID
  59. * @return int|null
  60. */
  61. protected function getStoreId(): ?int
  62. {
  63. if (empty($this->storeId)) {
  64. $this->storeId = \getStoreId();
  65. }
  66. return $this->storeId;
  67. }
  68. }