Преглед изворни кода

获取平台id方式变更

shish пре 6 година
родитељ
комит
fbd405eae3

+ 2 - 0
app/admin/controllers/AuthController.php

@@ -3,6 +3,7 @@
 namespace admin\controllers;
 
 use biz\admin\services\AdminService;
+use common\components\httpUtil;
 use common\components\jwt;
 use common\components\util;
 use Yii;
@@ -18,6 +19,7 @@ class AuthController extends PublicController
 	//登陆并拿到token shish 2019.11.23
 	public function actionLogin()
 	{
+		httpUtil::getSourceId();die;
 		$get = Yii::$app->request->get();
 		$mobile = isset($get['mobile']) ? $get['mobile'] : 0;
 		if (stringUtil::isMobieNumber($mobile) == false) {

+ 0 - 1
app/mobile/controllers/AuthController.php

@@ -3,7 +3,6 @@
 namespace mobile\controllers;
 
 use biz\merchant\services\MerchantService;
-use biz\platform\services\WxOpenService;
 use biz\user\services\UserService;
 use common\components\httpUtil;
 use common\components\jwt;

+ 0 - 20
biz/weixin/classes/WxOpenClass.php

@@ -10,24 +10,4 @@ class WxOpenClass extends BaseClass
 	
 	public static $baseFile = '\biz\weixin\models\WxOpen';
 	
-	//各来源ID
-	public static $sourceList = [
-		//商城移动端
-		'mobile' => 1,
-		//商城小程序
-		'mini' => 2,
-		//商家后台PC端
-		'admin' => 3,
-		//商家后台移动端
-		'back' => 4,
-		//总后台
-		'platform' => 5,
-	];
-
-	//获取各端列表
-	public static function getSourceList()
-	{
-		return self::$sourceList;
-	}
-	
 }

+ 1 - 11
biz/weixin/services/WxOpenService.php

@@ -3,22 +3,12 @@
 namespace biz\weixin\services;
 
 use biz\base\services\BaseService;
-use biz\platform\classes\WxOpenClass;
+use biz\weixin\classes\WxOpenClass;
 use common\components\httpUtil;
 use Yii;
 
 class WxOpenService extends BaseService
 {
 	public static $baseFile = '\biz\weixin\classes\WxOpenClass';
-	
-	//获取来源id shish 2019.11.22
-	public static function getSourceId()
-	{
-		$header = httpUtil::getHeader();
-		$source = isset($header['source']) ? $header['source'] : 'mobile';
-		$sourceList = WxOpenClass::getSourceList();
-		$id = isset($sourceList[$source]) ? $sourceList[$source] : 1;
-		return $id;
-	}
 
 }

+ 19 - 2
common/components/httpUtil.php

@@ -52,14 +52,14 @@ class httpUtil
 		//$token = isset($headers['token']) ? $headers['token'] : '';
 		return $headers;
 	}
-
+	
 	public static function getToken()
 	{
 		$header = self::getHeader();
 		$token = isset($header['token']) ? $header['token'] : '';
 		return $token;
 	}
-
+	
 	//获取地址里的account shish 2019.11.24
 	public static function getAccount($url)
 	{
@@ -79,4 +79,21 @@ class httpUtil
 		return $account;
 	}
 	
+	//获取平台id shish 2019.11.28
+	public static function getSourceId()
+	{
+		$ptSource = Yii::$app->params['ptSource'];
+		$host = self::getHost();
+		if (empty($host)) {
+			return 0;
+		}
+		$arr = parse_url($host);
+		if (isset($arr['host']) == false || empty($arr['host'])) {
+			return 0;
+		}
+		$pos = strpos($arr['host'], '.');
+		$ptName = substr($arr['host'], 0, $pos);
+		return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
+	}
+	
 }

+ 3 - 51
common/components/jwt.php

@@ -7,7 +7,7 @@
 
 namespace common\components;
 
-use biz\platform\services\WxOpenService;
+use biz\weixin\services\WxOpenService;
 use Lcobucci\JWT\Builder;
 use Lcobucci\JWT\Parser;
 use Lcobucci\JWT\ValidationData;
@@ -26,7 +26,7 @@ class jwt
 		$salt = Yii::$app->params['secretKey'];
 		$key = new Key($salt);
 		$host = httpUtil::getHost();
-		$sourceId = WxOpenService::getSourceId();
+		$sourceId = httpUtil::getSourceId();
 		$jwtId = $sourceId . '_' . $uniqueId;
 		$token = (new Builder())->issuedBy($host)//发布者的url地址
 		->canOnlyBeUsedBy($host)//接受者的url地址
@@ -68,7 +68,7 @@ class jwt
 		$token->getClaims(); // 获取JWT的PayLoad(负载)信息
 		$uniqueId = $token->getClaim('uniqueId');
 		$sourceId = $token->getClaim('sourceId');
-		$currentSourceId = WxOpenService::getSourceId();
+		$currentSourceId = httpUtil::getSourceId();
 		if ($sourceId != $currentSourceId) {
 			return 0;
 		}
@@ -86,52 +86,4 @@ class jwt
 		return self::validate($token);
 	}
 	
-	public static function delToken($uniqueId)
-	{
-		$token = httpUtil::getToken();
-		$host = httpUtil::getHost();
-		$signer = new Sha256();//生成JWT时使用的加密方式
-		$salt = Yii::$app->params['secretKey'];
-		$signKey = new Key($salt);
-
-		$token = (new Parser())->parse($token);
-		//数据校验
-		$data = new ValidationData();//使用当前时间来校验数据
-		$data->setIssuer($host);
-		$data->setAudience($host);
-
-		if (!$token->verify($signer, $signKey)) {
-			Yii::info('token解密没有通过。token:' . $token);
-			return false;
-		}
-		
-		if (!$token->validate($data)) {
-			Yii::info('token验证没有通过。token:' . $token);
-			return false;
-		}
-		
-		$token->getHeaders(); // 获取JWT的Header(头部)信息
-		$token->getClaims(); // 获取JWT的PayLoad(负载)信息
-		$returnId = $token->getClaim('uniqueId');
-		$sourceId = $token->getClaim('sourceId');
-		$currentSourceId = WxOpenService::getSourceId();
-
-		echo $currentSourceId.' '.$sourceId.' '.$returnId.' '.$uniqueId;
-		die;
-
-		if($currentSourceId != $sourceId){
-			Yii::info('不是你此端的token。token:' . $token);
-			return false;
-		}
-		if ($returnId != $uniqueId) {
-			Yii::info('token验证出的id不是登陆用户id。token:' . $token);
-			return false;
-		}
-
-		//设置为过期
-		$data->setCurrentTime(time() + 600);
-
-		return true;
-	}
-	
 }

+ 13 - 0
common/config/params.php

@@ -23,4 +23,17 @@ return [
 	'wwwUrl' => $wwwUrl,
 	'mobileUrl' => $mobileUrl,
 	'miniUrl' => $miniUrl,
+	//平台来源
+	'ptSource' => [
+		//商城移动端
+		'mobile' => 1,
+		//商城小程序
+		'mini' => 2,
+		//商家后台PC端
+		'admin' => 3,
+		//商家后台移动端
+		'back' => 4,
+		//总后台
+		'saas' => 5,
+	]
 ];