Controller.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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\api\controller;
  13. use cores\BaseController;
  14. use app\api\model\User as UserModel;
  15. use app\api\model\Store as StoreModel;
  16. use app\api\service\User as UserService;
  17. use cores\exception\BaseException;
  18. /**
  19. * API控制器基类
  20. * Class Controller
  21. * @package app\store\controller
  22. */
  23. class Controller extends BaseController
  24. {
  25. // 当前商城ID
  26. protected $storeId;
  27. /**
  28. * API基类初始化
  29. * @throws BaseException
  30. */
  31. public function initialize()
  32. {
  33. // 当前商城id
  34. $this->storeId = $this->getStoreId();
  35. // 验证当前商城状态
  36. $this->checkStore();
  37. // 验证当前客户端状态
  38. $this->checkClient();
  39. }
  40. /**
  41. * 获取当前商城ID
  42. * @return int|null
  43. * @throws BaseException
  44. */
  45. protected function getStoreId(): ?int
  46. {
  47. $storeId = getStoreId(); // app/api/common.php
  48. empty($storeId) && throwError('缺少必要的参数:storeId');
  49. return $storeId;
  50. }
  51. /**
  52. * 验证当前商城状态
  53. * @return void
  54. * @throws BaseException
  55. */
  56. private function checkStore(): void
  57. {
  58. // 获取当前商城信息
  59. $store = StoreModel::detail($this->storeId);
  60. if (empty($store)) {
  61. throwError('当前商城信息不存在');
  62. }
  63. if ($store['is_recycle'] || $store['is_delete']) {
  64. throwError('当前商城已删除');
  65. }
  66. }
  67. /**
  68. * 验证当前客户端是否允许访问
  69. * @throws BaseException
  70. */
  71. private function checkClient()
  72. {
  73. $client = getPlatform();
  74. $settingClass = [
  75. 'H5' => [\app\api\model\h5\Setting::class, 'checkStatus', 'H5端']
  76. ];
  77. if (!isset($settingClass[$client])) {
  78. return;
  79. }
  80. $status = call_user_func([$settingClass[$client][0], $settingClass[$client][1]]);
  81. $status === false && throwError('很抱歉,当前' . $settingClass[$client][2] . '端暂不支持访问');
  82. }
  83. /**
  84. * 获取当前用户信息
  85. * @param bool $isForce 强制验证登录
  86. * @return UserModel|bool|null
  87. * @throws BaseException
  88. */
  89. protected function getLoginUser(bool $isForce = true)
  90. {
  91. return UserService::getCurrentLoginUser($isForce);
  92. }
  93. }