Init.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace common\components;
  3. use common\helpers\ArrayHelper;
  4. use Yii;
  5. use yii\base\BootstrapInterface;
  6. use yii\web\UnauthorizedHttpException;
  7. use common\models\backend\Member;
  8. use common\helpers\StringHelper;
  9. use common\enums\AppEnum;
  10. /**
  11. * Class InitConfig
  12. * @package common\components
  13. * @author qimall
  14. */
  15. class Init implements BootstrapInterface
  16. {
  17. /**
  18. * 应用ID
  19. *
  20. * @var
  21. */
  22. protected $id;
  23. /**
  24. * @param \yii\base\Application $application
  25. * @throws UnauthorizedHttpException
  26. * @throws \Exception
  27. */
  28. public function bootstrap($application)
  29. {
  30. Yii::$app->params['uuid'] = StringHelper::uuid('uniqid');
  31. $this->id = $application->id; // 初始化变量
  32. // 商户信息
  33. if (in_array(Yii::$app->id, [AppEnum::CONSOLE, AppEnum::BACKEND])) {
  34. $this->afreshLoad('');
  35. } elseif (in_array(Yii::$app->id, [AppEnum::MERCHANT, AppEnum::MER_API])) {
  36. /** @var Member $identity */
  37. $identity = Yii::$app->user->identity;
  38. $this->afreshLoad($identity->merchant_id ?? '');
  39. } else {
  40. $merchant_id = Yii::$app->request->headers->get('merchant-id', '');
  41. if (empty($merchant_id)) {
  42. $merchant_id = Yii::$app->request->get('merchant_id', '');
  43. }
  44. $this->afreshLoad($merchant_id);
  45. }
  46. }
  47. /**
  48. * @param $merchant_id
  49. * @throws UnauthorizedHttpException
  50. * @throws \yii\base\InvalidConfigException
  51. */
  52. public function afreshLoad($merchant_id)
  53. {
  54. $sys_ip_blacklist_open = false;
  55. try {
  56. // Yii::$app->services->merchant->setId($merchant_id);
  57. // 获取 ip 配置
  58. // $this->id != AppEnum::CONSOLE && $sys_ip_blacklist_open = Yii::$app->debris->backendConfig('sys_ip_blacklist_open');
  59. // 初始化模块
  60. Yii::$app->setModules($this->getModulesByAddons());
  61. } catch (\Exception $e) {
  62. }
  63. // ip黑名单拦截器
  64. if ($sys_ip_blacklist_open == true && !in_array(Yii::$app->id, [AppEnum::CONSOLE])) {
  65. $ips = Yii::$app->services->ipBlacklist->findIps();
  66. if (ArrayHelper::ipInArray(Yii::$app->request->userIP, $ips)) {
  67. throw new UnauthorizedHttpException('你的访问被禁止');
  68. }
  69. }
  70. }
  71. /**
  72. * 获取模块
  73. *
  74. * @throws \yii\base\InvalidConfigException
  75. */
  76. public function getModulesByAddons()
  77. {
  78. $addons = Yii::$app->services->addons->findAllNames();
  79. $modules = [];
  80. $merchant = AppEnum::MERCHANT;
  81. foreach ($addons as $addon) {
  82. $name = $addon['name'];
  83. $app_id = $this->id;
  84. // 模块映射
  85. if ($this->id == AppEnum::BACKEND && $addon['is_merchant_route_map'] == true) {
  86. $app_id = $merchant;
  87. }
  88. $modules[StringHelper::toUnderScore($name)] = [
  89. 'class' => 'common\components\BaseAddonModule',
  90. 'name' => $name,
  91. 'app_id' => $app_id,
  92. ];
  93. // 初始化服务
  94. if (!empty($addon['service'])) {
  95. // 动态注入服务
  96. Yii::$app->set(lcfirst($name) . 'Service', [
  97. 'class' => $addon['service'],
  98. ]);
  99. }
  100. }
  101. return $modules;
  102. }
  103. }