Controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\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. use think\db\exception\DbException;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * API控制器基类
  23. * Class BaseController
  24. * @package app\store\controller
  25. */
  26. class Controller extends BaseController
  27. {
  28. // 当前商城ID
  29. protected int $storeId;
  30. /**
  31. * API基类初始化
  32. * @throws BaseException
  33. * @throws DataNotFoundException
  34. * @throws DbException
  35. * @throws ModelNotFoundException
  36. */
  37. protected function initialize()
  38. {
  39. // 当前的商城ID
  40. $this->getStoreId();
  41. // 验证当前商城状态
  42. $this->checkStore();
  43. // 验证当前客户端状态
  44. $this->checkClient();
  45. }
  46. /**
  47. * 获取当前的商城ID
  48. * @throws BaseException
  49. */
  50. protected function getStoreId()
  51. {
  52. if (!$this->storeId = \getStoreId()) {
  53. throwError('很抱歉,未找到必要的参数storeId');
  54. }
  55. }
  56. /**
  57. * 验证当前商城状态
  58. * @return void
  59. * @throws BaseException
  60. */
  61. private function checkStore(): void
  62. {
  63. // 获取当前商城信息
  64. $store = StoreModel::detail($this->storeId);
  65. if (empty($store)) {
  66. throwError('很抱歉,当前商城信息不存在');
  67. }
  68. if ($store['is_recycle'] || $store['is_delete']) {
  69. throwError('很抱歉,当前商城已删除');
  70. }
  71. }
  72. /**
  73. * 验证当前客户端是否允许访问
  74. * @return void
  75. * @throws BaseException
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. private function checkClient()
  81. {
  82. $client = getPlatform();
  83. $settingClass = [
  84. 'MP-WEIXIN' => [
  85. 'name' => '微信小程序端',
  86. 'class' => '\app\api\model\wxapp\Setting',
  87. 'method' => 'checkStatus',
  88. ],
  89. 'H5' => [
  90. 'name' => 'H5端',
  91. 'class' => '\app\api\model\h5\Setting',
  92. 'method' => 'checkStatus',
  93. ],
  94. ];
  95. if (!isset($settingClass[$client])) {
  96. return;
  97. }
  98. $item = $settingClass[$client];
  99. if (!class_exists($item['class'])) {
  100. throwError("很抱歉,当前{$item['name']}不存在");
  101. }
  102. if (!call_user_func([$item['class'], $item['method']])) {
  103. throwError("很抱歉,当前{$item['name']}暂未开启访问");
  104. }
  105. }
  106. /**
  107. * 获取当前用户信息
  108. * @param bool $isForce 强制验证登录
  109. * @return UserModel|bool|null
  110. * @throws BaseException
  111. */
  112. protected function getLoginUser(bool $isForce = true)
  113. {
  114. return UserService::getCurrentLoginUser($isForce);
  115. }
  116. }