| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace api\controllers;
- use common\enums\AddonsEnum;
- use common\enums\ApiStatusEnum;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use Yii;
- /**
- * 需要授权登录访问基类
- *
- * Class OnAuthController
- * @package api\controllers
- * @property yii\db\ActiveRecord|yii\base\Model $modelClass
- * @author qimall
- */
- class OnAuthController extends ActiveController
- {
- /**
- * 登录令牌
- *
- * @var string
- */
- public $access_token = '';
- /**
- * 商城ID
- *
- * @var string
- */
- public $mall_id = '';
- /**
- * 绑定用户ID
- *
- * @var string
- */
- public $parent_id = '';
- /**
- * 来源
- *
- * @var string
- */
- public $source = '';
- /**
- * 登录平台
- *
- * @var string
- */
- public $platform = '';
- /**
- * 当前用户ID
- *
- * @var string
- */
- public $user_id = '';
- /**
- * 当前用户
- *
- * @var User|null
- */
- public $user = null;
- /**
- * 商城信息
- *
- * @var array|null
- */
- public $mall = null;
- /**
- * 智能名片ID
- *
- * @var integer
- */
- public $card_id = 0;
- /**
- * 前置操作
- *
- * @param Action $action
- * @return Action
- */
- public function beforeAction($action)
- {
- parent::beforeAction($action);
- $headers = \Yii::$app->request->headers;
- $this->access_token = $headers['x-access-token'];
- $this->source = $headers['x-source'];
- $this->parent_id = $headers['x-parent-id'];
- $this->platform = $headers['x-app-platform'];
- $this->card_id = $headers['x-card-id'] ?? 0;
- $mall_sign = '';
- if (!empty($headers['x-mall-sign'])) $mall_sign = $headers['x-mall-sign'];
- if (!empty(\Yii::$app->request->get('mall-sign'))) $mall_sign = \Yii::$app->request->get('mall-sign');
- if (!empty(\Yii::$app->request->get('state'))) $mall_sign = \Yii::$app->request->get('state');
- if (empty($mall_sign)) {
- \Yii::$app->response->data = ['code' => 1, 'msg' => '商城签名不能为空'];
- \Yii::$app->response->send();
- return;
- }
- $mall = Mall::getMallInfo($mall_sign);
- if (empty($mall)) {
- \Yii::$app->response->data = ['code' => 1, 'msg' => '商城不存在'];
- \Yii::$app->response->send();
- return;
- }
- $this->mall = $mall;
- $this->mall_id = $mall['id'];
- // 是否返回商城维护中
- $isSystemInMaintenance = Yii::$app->services->setting->mall->get($this->mall_id, 'basic.is_system_in_maintenance');
- if ($isSystemInMaintenance && is_numeric($isSystemInMaintenance)) {
- \Yii::$app->response->data = ['code' => 1, 'msg' => '商城维护中'];
- \Yii::$app->response->send();
- return;
- }
- $this->user = \Yii::$app->user->identity->user ?? [];
- if (!empty($this->user) && empty($this->user->status)) {
- \Yii::$app->response->data = ['code' => 1, 'msg' => '您无权访问该商城' . $this->user['mobile']];
- \Yii::$app->response->send();
- return;
- }
- $this->user_id = !empty($this->user) ? $this->user->id : '';
- $this->checkRegisterInfo($action);
- return $action;
- }
- /**
- * @return array
- */
- public function actions()
- {
- $actions = parent::actions();
- // 注销系统自带的实现方法
- unset($actions['index'], $actions['update'], $actions['create'], $actions['view'], $actions['delete']);
- // 自定义数据indexDataProvider覆盖IndexAction中的prepareDataProvider()方法
- // $actions['index']['prepareDataProvider'] = [$this, 'indexDataProvider'];
- return $actions;
- }
- public function checkRegisterInfo($action)
- {
- $is_kangbaolin = (bool) (\Yii::$app->params['is_kangbaolin'] ?? false);
- if ($is_kangbaolin === false) {
- return;
- }
- if (!in_array(Yii::$app->controller->module->id, ['v1', 'v2'])) {
- return;
- }
- $ignore_action = [
- 'login', 'register', 'check', 'get-config', 'register-sms-code', 'user-agree', 'buying-point',
- 'get-img-base64', 'jssdk-config', 'mini-login', 'bind', 'auth-phone', 'app-auth-login', 'auth-login',
- 'address-info', 'config', 'user-info', 'visit', 'get-user', 'get-setting', 'get-cate-list', 'share-redpack-popup',
- 'get-components-data', 'upload', 'form', 'get-user-config', 'get-is-enable',
- ];
- if (in_array($action->id, $ignore_action)) {
- return;
- }
- $install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_REGISTER_INFO);
- if ($install) {
- $config = \addons\RegisterInfo\common\services\BackendService::getSetting($this->mall_id);
- $is_enable = (int) ($config['is_enable'] ?? 0);
- if (empty($is_enable)) {
- return;
- }
- if (!$this->user_id) {
- \Yii::$app->response->data = ['code' => ApiStatusEnum::CODE_FAIL, 'msg' => '注册审核'];
- \Yii::$app->response->send();
- return;
- }
- $register_user_class = \addons\RegisterInfo\common\models\RegisterInfoUser::class;
- if (class_exists($register_user_class)) {
- //用户是否有注册审核信息
- /** @var \addons\RegisterInfo\common\models\RegisterInfoUser $register_user_class */
- $is_required = $register_user_class::checkIsRequired($this->mall_id, $this->user_id);
- if ($is_required) {
- \Yii::$app->response->data = ['code' => ApiStatusEnum::CODE_FAIL, 'msg' => '注册审核'];
- \Yii::$app->response->send();
- return;
- }
- }
- }
- }
- }
|