| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * User: shish <479439056@qq.com>
- * 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);
- }
-
- }
|