* Date: 2019/6/29 * Time: 13:07 */ namespace mini\controllers; use biz\user\services\UserService; use common\components\util; use common\services\xhMerchantService; use Yii; use yii\web\Controller; class BaseController extends Controller { public $merchantId, $merchant, $userId = 0, $userInfo = [], $isLogin = false; public $withoutLogin = [];//不需要登陆的方法名 public function beforeAction($action) { //获取token $ignore = ['host', 'accept', 'content-length', 'content-type']; $headers = []; foreach ($_SERVER as $key => $value) { if (substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); $key = str_replace('_', ' ', $key); $key = str_replace(' ', '-', $key); $key = strtolower($key); if (!in_array($key, $ignore)) { $headers[$key] = $value; } } } $token = isset($headers['token']) ? $headers['token'] : ''; $account = isset($headers['account']) && !empty($headers['account']) ? $headers['account'] : 0; $userId = Yii::$app->redis->executeCommand('GET', [$token]); if (empty($account)) { $account = Yii::$app->request->get('account', 0); } if (empty($account)) { util::sendFail('没有店铺信息'); } $merchant = xhMerchantService::getByAccount($account); if (empty($merchant)) { util::sendFail('没有找到店铺信息'); } if (in_array($action->id, $this->withoutLogin) == false) { if (empty($userId)) { util::sendFail('您没有权限访问'); } } if (!empty($userId)) { $userInfo = UserService::getById($userId); $this->userInfo = $userInfo; Yii::$app->params['userInfo'] = $userInfo; } $this->userId = $userId; Yii::$app->params['userId'] = $userId; //全局变量设置 Yii::$app->params['merchantId'] = $account; Yii::$app->params['merchant'] = $merchant; $page = Yii::$app->request->get('page', 1); Yii::$app->params['page'] = $page; //获取头信息里的token $ignore = ['host', 'accept', 'content-length', 'content-type']; $headers = []; foreach ($_SERVER as $key => $value) { if (substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); $key = str_replace('_', ' ', $key); $key = str_replace(' ', '-', $key); $key = strtolower($key); if (!in_array($key, $ignore)) { $headers[$key] = $value; } } } $token = isset($headers['token']) ? $headers['token'] : ''; //小程序端默认的输出方式是json Yii::$app->params['exportStyle'] = 'json'; $this->merchant = $merchant; $this->merchantId = $account; return parent::beforeAction($action); } }