* Date: 2019/11/18 0018 * Time: 14:58 */ class BaseController extends PublicController { public $validate = true; public $withoutLogin = [];//不需要登陆可以访问的方法 public $userId = 0, $user, $shopId; public $merchantId, $merchant, $merchantExtend; public $isWx; public function beforeAction($action) { //token验证 $userId = jwt::getLoginId(); $header = httpUtil::getHeader(); $merchantId = isset($header['account']) ? $header['account'] : 0; $shopId = isset($header['shopId']) ? $header['shopId'] : 0; if (empty($merchantId)) { util::fail('商家无效'); } $merchant = MerchantService::getMerchantById($merchantId); if (empty($merchant)) { util::fail('商家信息无效'); } $this->merchantId = $merchantId; $this->merchant = $merchant; $this->shopId = $shopId; $this->merchantExtend = MerchantExtendService::getByMerchantId($merchantId); $this->isWx = util::isWx(); //收集访问量 StatVisitService::gatherVisit($merchantId); //本地环境没有token时模拟一个可以用的帐号 if (getenv('YII_ENV') == 'local') { $one = UserService::getByCondition(['merchantId'=>$merchantId]); if (!empty($one)) { $userId = $one['id']; } } if (empty($userId)) { $this->validate = false; } else { $this->validate = true; $this->userId = $userId; $userInfo = UserService::getUserInfo($userId); $this->user = $userInfo; //更新用户访问时间 UserService::updateVisitTime($merchantId, $userId); //客户短时间访问二家商家的商城,登陆信息一致性问题 if ($userInfo['merchantId'] != $merchantId) { util::logout(); } } //游客可以访问的方法 if (in_array($action->id, $this->withoutLogin)) { $this->validate = true; } if ($this->validate == false) { util::notLogin(); } return parent::beforeAction($action); } }