64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
||
|
||
namespace common\core;
|
||
|
||
use common\foundation\Cors;
|
||
use common\models\oldDb\Config;
|
||
use yii\db\Exception;
|
||
use yii\filters\auth\HttpBearerAuth;
|
||
|
||
class BaseServiceController extends BaseController
|
||
{
|
||
//beforeAction 注入
|
||
protected $store;//门店ID
|
||
|
||
public function init()
|
||
{
|
||
parent::init();
|
||
// 多语言,需要在http header中设置 app-language:zh-CN
|
||
//Yii::$app->language = Yii::$app->request->getHeaders()->get('app-language'); //'en-US';
|
||
\Yii::$app->params['web'] = Config::lists();
|
||
}
|
||
|
||
final public function behaviors()
|
||
{
|
||
$behaviors = parent::behaviors();
|
||
$newBehaviors = [];
|
||
$newBehaviors['corsFilter'] = [
|
||
'class' => Cors::class,
|
||
];
|
||
foreach ($behaviors as $k=>$v){
|
||
$newBehaviors[$k]=$v;
|
||
}
|
||
unset($behaviors['authenticator']); //删掉,保持先cors,后authenticator
|
||
//设置认证方式,接口才认证
|
||
$newBehaviors['authenticator'] = [
|
||
'class' => HttpBearerAuth::class,
|
||
'optional' => $this->optional,
|
||
];
|
||
return $newBehaviors;
|
||
}
|
||
|
||
public function beforeAction($action)
|
||
{
|
||
$ret = parent::beforeAction($action); // TODO: Change the autogenerated stub
|
||
//判断是否是游客
|
||
if(!\Yii::$app->user->isGuest){
|
||
if (\Yii::$app->request->isPost){
|
||
$this->store=$this->post('store_id');
|
||
}
|
||
if (\Yii::$app->request->isGet){
|
||
$this->store=$this->get('store_id');
|
||
}
|
||
\Yii::$app->store=$this->store;
|
||
if (empty(\Yii::$app->store)){
|
||
throw new Exception('参数store_id不能为空');
|
||
}
|
||
}else{
|
||
//游客模式
|
||
\Yii::$app->store=$this->store=11001;//默认门店
|
||
}
|
||
return $ret;
|
||
}
|
||
}
|