OnAuthController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace api\controllers;
  3. use common\enums\AddonsEnum;
  4. use common\enums\ApiStatusEnum;
  5. use common\models\mall\Mall;
  6. use common\models\mall\MallAddons;
  7. use Yii;
  8. /**
  9. * 需要授权登录访问基类
  10. *
  11. * Class OnAuthController
  12. * @package api\controllers
  13. * @property yii\db\ActiveRecord|yii\base\Model $modelClass
  14. * @author qimall
  15. */
  16. class OnAuthController extends ActiveController
  17. {
  18. /**
  19. * 登录令牌
  20. *
  21. * @var string
  22. */
  23. public $access_token = '';
  24. /**
  25. * 商城ID
  26. *
  27. * @var string
  28. */
  29. public $mall_id = '';
  30. /**
  31. * 绑定用户ID
  32. *
  33. * @var string
  34. */
  35. public $parent_id = '';
  36. /**
  37. * 来源
  38. *
  39. * @var string
  40. */
  41. public $source = '';
  42. /**
  43. * 登录平台
  44. *
  45. * @var string
  46. */
  47. public $platform = '';
  48. /**
  49. * 当前用户ID
  50. *
  51. * @var string
  52. */
  53. public $user_id = '';
  54. /**
  55. * 当前用户
  56. *
  57. * @var User|null
  58. */
  59. public $user = null;
  60. /**
  61. * 商城信息
  62. *
  63. * @var array|null
  64. */
  65. public $mall = null;
  66. /**
  67. * 智能名片ID
  68. *
  69. * @var integer
  70. */
  71. public $card_id = 0;
  72. /**
  73. * 前置操作
  74. *
  75. * @param Action $action
  76. * @return Action
  77. */
  78. public function beforeAction($action)
  79. {
  80. parent::beforeAction($action);
  81. $headers = \Yii::$app->request->headers;
  82. $this->access_token = $headers['x-access-token'];
  83. $this->source = $headers['x-source'];
  84. $this->parent_id = $headers['x-parent-id'];
  85. $this->platform = $headers['x-app-platform'];
  86. $this->card_id = $headers['x-card-id'] ?? 0;
  87. $mall_sign = '';
  88. if (!empty($headers['x-mall-sign'])) $mall_sign = $headers['x-mall-sign'];
  89. if (!empty(\Yii::$app->request->get('mall-sign'))) $mall_sign = \Yii::$app->request->get('mall-sign');
  90. if (!empty(\Yii::$app->request->get('state'))) $mall_sign = \Yii::$app->request->get('state');
  91. if (empty($mall_sign)) {
  92. \Yii::$app->response->data = ['code' => 1, 'msg' => '商城签名不能为空'];
  93. \Yii::$app->response->send();
  94. return;
  95. }
  96. $mall = Mall::getMallInfo($mall_sign);
  97. if (empty($mall)) {
  98. \Yii::$app->response->data = ['code' => 1, 'msg' => '商城不存在'];
  99. \Yii::$app->response->send();
  100. return;
  101. }
  102. $this->mall = $mall;
  103. $this->mall_id = $mall['id'];
  104. // 是否返回商城维护中
  105. $isSystemInMaintenance = Yii::$app->services->setting->mall->get($this->mall_id, 'basic.is_system_in_maintenance');
  106. if ($isSystemInMaintenance && is_numeric($isSystemInMaintenance)) {
  107. \Yii::$app->response->data = ['code' => 1, 'msg' => '商城维护中'];
  108. \Yii::$app->response->send();
  109. return;
  110. }
  111. $this->user = \Yii::$app->user->identity->user ?? [];
  112. if (!empty($this->user) && empty($this->user->status)) {
  113. \Yii::$app->response->data = ['code' => 1, 'msg' => '您无权访问该商城' . $this->user['mobile']];
  114. \Yii::$app->response->send();
  115. return;
  116. }
  117. $this->user_id = !empty($this->user) ? $this->user->id : '';
  118. $this->checkRegisterInfo($action);
  119. return $action;
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function actions()
  125. {
  126. $actions = parent::actions();
  127. // 注销系统自带的实现方法
  128. unset($actions['index'], $actions['update'], $actions['create'], $actions['view'], $actions['delete']);
  129. // 自定义数据indexDataProvider覆盖IndexAction中的prepareDataProvider()方法
  130. // $actions['index']['prepareDataProvider'] = [$this, 'indexDataProvider'];
  131. return $actions;
  132. }
  133. public function checkRegisterInfo($action)
  134. {
  135. $is_kangbaolin = (bool) (\Yii::$app->params['is_kangbaolin'] ?? false);
  136. if ($is_kangbaolin === false) {
  137. return;
  138. }
  139. if (!in_array(Yii::$app->controller->module->id, ['v1', 'v2'])) {
  140. return;
  141. }
  142. $ignore_action = [
  143. 'login', 'register', 'check', 'get-config', 'register-sms-code', 'user-agree', 'buying-point',
  144. 'get-img-base64', 'jssdk-config', 'mini-login', 'bind', 'auth-phone', 'app-auth-login', 'auth-login',
  145. 'address-info', 'config', 'user-info', 'visit', 'get-user', 'get-setting', 'get-cate-list', 'share-redpack-popup',
  146. 'get-components-data', 'upload', 'form', 'get-user-config', 'get-is-enable',
  147. ];
  148. if (in_array($action->id, $ignore_action)) {
  149. return;
  150. }
  151. $install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_REGISTER_INFO);
  152. if ($install) {
  153. $config = \addons\RegisterInfo\common\services\BackendService::getSetting($this->mall_id);
  154. $is_enable = (int) ($config['is_enable'] ?? 0);
  155. if (empty($is_enable)) {
  156. return;
  157. }
  158. if (!$this->user_id) {
  159. \Yii::$app->response->data = ['code' => ApiStatusEnum::CODE_FAIL, 'msg' => '注册审核'];
  160. \Yii::$app->response->send();
  161. return;
  162. }
  163. $register_user_class = \addons\RegisterInfo\common\models\RegisterInfoUser::class;
  164. if (class_exists($register_user_class)) {
  165. //用户是否有注册审核信息
  166. /** @var \addons\RegisterInfo\common\models\RegisterInfoUser $register_user_class */
  167. $is_required = $register_user_class::checkIsRequired($this->mall_id, $this->user_id);
  168. if ($is_required) {
  169. \Yii::$app->response->data = ['code' => ApiStatusEnum::CODE_FAIL, 'msg' => '注册审核'];
  170. \Yii::$app->response->send();
  171. return;
  172. }
  173. }
  174. }
  175. }
  176. }