BaseService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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. /**
  15. * 系统服务基础类
  16. * Class BaseService
  17. * @package app\common\service
  18. */
  19. class BaseService
  20. {
  21. // 请求管理类
  22. /* @var $request \think\Request */
  23. protected $request;
  24. // 错误信息
  25. protected $error;
  26. // 当前访问的商城ID
  27. protected $storeId;
  28. /**
  29. * 构造方法
  30. * BaseService constructor.
  31. */
  32. public function __construct()
  33. {
  34. // 请求管理类
  35. $this->request = Request::instance();
  36. // 执行构造方法
  37. $this->initialize();
  38. }
  39. /**
  40. * 构造方法 (供继承的子类使用)
  41. */
  42. protected function initialize()
  43. {
  44. // 获取当前操作的商城ID
  45. $this->getStoreId();
  46. }
  47. /**
  48. * 获取当前操作的商城ID
  49. * @return int|null
  50. */
  51. protected function getStoreId()
  52. {
  53. if (empty($this->storeId) && in_array(app_name(), ['store', 'api'])) {
  54. $this->storeId = getStoreId();
  55. }
  56. return $this->storeId;
  57. }
  58. /**
  59. * 获取错误信息
  60. * @return mixed
  61. */
  62. public function getError()
  63. {
  64. return empty($this->error) ? false : $this->error;
  65. }
  66. /**
  67. * 是否存在错误
  68. * @return bool
  69. */
  70. public function hasError()
  71. {
  72. return !empty($this->error);
  73. }
  74. }