Files
xk-api-yii/common/core/BaseServiceController.php

64 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace common\core;
use common\foundation\Cors;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Config;
2024-09-19 11:32:39 +08:00
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;
}
}