| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace openend\controllers;
- use common\services\xhMerchantExtendService;
- use common\services\xhWxOpenService;
- use Yii;
- use yii\web\Controller;
- use common\components\jsSDK;
- use common\components\weixinUtil;
- use common\components\configDict;
- use common\components\util;
- use common\services\xhWxOpenAdminService;
- use common\services\xhUserService;
- use common\services\xhMerchantService;
- /**
- * 前台次级基类,次于PublicController
- */
- class OpenendController extends PublicController{
- public $isLogin = false;
- public $withoutLogin = [];//不需要登陆的方法名
- public $userId,$user,$isWeixin,$signPackage,$merchant,$merchantId,$merchantExtend;
- public $wxOpen;
- public function beforeAction($action){
- $this->isWeixin = util::isWeixin();
- $get = Yii::$app->request->get();
- $account = isset($get['account']) ? $get['account'] : '';
- $one = xhMerchantService::getOne();
- $wxOpenId = configDict::getConfig('openId');
- $this->wxOpen = xhWxOpenService::getById($wxOpenId);
- $noNeedLogin = false;
- if(!empty($one)){
- if(empty($account)){
- $merchant = xhWxOpenService::getMerchant();
- }else{
- $merchant = xhMerchantService::getByAccount($account);
- }
- $account = $merchant['id'];
- $this->merchant = $merchant;
- $merchantId = $merchant['id'];
- $this->merchantId = $merchantId;
- $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- if($this->isWeixin){
- $jssdk = new jsSDK();//微信分享
- $this->signPackage = $jssdk->GetSignPackage($merchant);
- }
- }else{
- $noNeedLogin = true;
- }
- if(in_array($action->id, $this->withoutLogin) || $this->isLogin == true){
- $noNeedLogin = true;
- }
- $session = Yii::$app->session;
- if(isset($session['visitingMerchantAccount']) && $session['visitingMerchantAccount'] != $account){
- xhUserService::logout();
- }
- $this->isLogin = xhUserService::isLogin();
- if($this->isLogin){
- $userId = xhUserService::getId();
- $user = xhUserService::getById($userId);
- $this->userId = $userId;
- $this->user = $user;
- $noNeedLogin = true;
- }
- if ($this->isWeixin) {//如果是微信,走微信授权取用户信息
- if($noNeedLogin == false){
- //snsapi_userinfo 需要用户授权获取用户信息,snsapi_base表示自动静默方式
- $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_userinfo';
- $params = empty($_SERVER['REQUEST_URI']) ? '' : base64_encode($_SERVER['REQUEST_URI']);//取主域名后面的参数
- $url = 'http://'.$_SERVER['HTTP_HOST'].'/auth/get-user-info?params='.$params.'&authScope='.$authScope;
- weixinUtil::getToken($url,$merchant,$authScope);
- util::end();
- }
- }else{
- if($noNeedLogin == false){
- $this->redirect(['main/login']);
- util::stop();
- }
- }
- return parent::beforeAction($action);
- }
- }
|