shish 6 жил өмнө
parent
commit
594a740c4c

+ 9 - 6
app/mobile/controllers/AuthController.php

@@ -3,7 +3,9 @@
 namespace mobile\controllers;
 
 use biz\merchant\services\MerchantService;
+use biz\platform\services\WxOpenService;
 use biz\user\services\UserService;
+use common\components\jwt;
 use common\components\stringUtil;
 use Yii;
 use common\components\util;
@@ -16,7 +18,6 @@ use Lcobucci\JWT\Parser;
 use Lcobucci\JWT\ValidationData;
 
 
-
 /**
  * 微信 oauth 授权相关
  *
@@ -140,12 +141,14 @@ class AuthController extends PublicController
 		Yii::$app->params['merchantId'] = $merchantId;
 		Yii::$app->params['merchant'] = $merchant;
 		$host = Yii::$app->request->getHostInfo();
-		//模拟生成token
-		$token = rand(111,9999);
-		$suffixUrl = strpos($suffixUrl,'?') === false ? $suffixUrl.'?token='.$token :  $suffixUrl.'&token='.$token;
+		//来源
+		$sourceId = WxOpenService::getSourceId();
+		$userId = $user['id'];
+		//获取token
+		$token = jwt::get($sourceId, $userId);
+		$suffixUrl = strpos($suffixUrl, '?') === false ? $suffixUrl . '?token=' . $token : $suffixUrl . '&token=' . $token;
 		$url = $host . '/' . $suffixUrl;
-		echo $url;die;
 		$this->redirect($url);
 	}
-
+	
 }

+ 33 - 0
biz/platform/classses/WxOpenClass.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace biz\platform\classes;
+
+use Yii;
+use biz\base\classes\BaseClass;
+
+class WxOpenClass extends BaseClass
+{
+	
+	public static $baseFile = '\biz\platform\models\WxOpen';
+	
+	//各来源ID
+	public static $sourceList = [
+		//商城H5
+		'h5' => 1,
+		//商城小程序
+		'mini' => 2,
+		//商家后台PC
+		'admin' => 3,
+		//商家后台H5
+		'backend' => 4,
+		//总后台
+		'platform' => 5,
+	];
+	
+	//获取各端列表
+	public static function getSourceList()
+	{
+		return self::$sourceList;
+	}
+	
+}

+ 13 - 0
biz/platform/models/WxOpen.php

@@ -0,0 +1,13 @@
+<?php
+namespace biz\platform\models;
+use biz\base\models\Base;
+
+class WxOpen extends Base
+{
+
+	public static function tableName()
+	{
+		return 'xhWxOpen';
+	}
+
+}

+ 24 - 0
biz/platform/services/WxOpenService.php

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

+ 20 - 0
common/components/httpUtil.php

@@ -31,4 +31,24 @@ class httpUtil
 		return Yii::$app->request->getHostInfo();
 	}
 	
+	//获取header里的数据 shish 2019.11.22
+	public static function getHeader()
+	{
+		$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'] : '';
+		return $headers;
+	}
+	
 }

+ 19 - 19
common/components/jwt.php

@@ -16,16 +16,16 @@ use Yii;
 
 class jwt
 {
-
+	
 	//生成token
-	public static function create($platId, $userId)
+	public static function create($sourceId, $userId)
 	{
 		$time = time();
 		$signer = new Sha256();
 		$salt = Yii::$app->params['secretKey'];
 		$key = new Key($salt);
 		$host = httpUtil::getHost();
-		$uniqueId = $platId . '_' . $userId;
+		$uniqueId = $sourceId . '_' . $userId;
 		$token = (new Builder())->issuedBy($host)//发布者的url地址
 		->canOnlyBeUsedBy($host)//接受者的url地址
 		->identifiedBy($uniqueId, true)//该jwt的唯一ID编号
@@ -35,36 +35,36 @@ class jwt
 		->set('userId', $userId)//设置一个变量
 		->sign($signer, $key)->getToken();
 		
-		$keyName = self::getKeyName($platId,$userId);
-		Yii::$app->redis->executeCommand('SET', [$keyName,$token]);
+		$keyName = self::getKeyName($sourceId, $userId);
+		Yii::$app->redis->executeCommand('SET', [$keyName, $token]);
 		
 		return $token;
 	}
-
-	public static function getKeyName($platId,$userId)
+	
+	public static function getKeyName($sourceId, $userId)
 	{
-		return 'JWT_'.$platId.'_'.$userId;
+		return 'JWT_' . $sourceId . '_' . $userId;
 	}
-
+	
 	//返回一个可用的token shish 2019.11.22
-	public static function get($platId, $userId)
+	public static function get($sourceId, $userId)
 	{
-		$keyName = self::getKeyName($platId,$userId);
+		$keyName = self::getKeyName($sourceId, $userId);
 		$cacheToken = Yii::$app->redis->executeCommand('GET', [$keyName]);
 		$token = '';
 		//从缓存中获取
-		if(!empty($cacheToken)){
+		if (!empty($cacheToken)) {
 			$tokenResource = (new Parser())->parse((string)$cacheToken);
 			$data = new ValidationData();
 			$r = $tokenResource->validate($data);
 			$token = $r == false ? '' : $cacheToken;
 		}
-		if(empty($token)){
-			$token = self::create($platId,$userId);
+		if (empty($token)) {
+			$token = self::create($sourceId, $userId);
 		}
 		return $token;
 	}
-
+	
 	//验证token
 	public static function validate($token)
 	{
@@ -72,18 +72,18 @@ class jwt
 		//数据校验
 		$data = new ValidationData(); // 使用当前时间来校验数据
 		if (!$token->validate($data)) {
-			return ['code'=>1];
+			return ['code' => 1];
 		}
 		//token校验
 		$signer = new Sha256();//生成JWT时使用的加密方式
 		$salt = Yii::$app->params['secretKey'];
 		if (!$token->verify($signer, new Key($salt))) {
-			return ['code'=>1];
+			return ['code' => 1];
 		}
 		$token->getHeaders(); // 获取JWT的Header(头部)信息
 		$token->getClaims(); // 获取JWT的PayLoad(负载)信息
 		$userId = $token->getClaim('userId');
-		return ['code'=>0,'data'=>['userId'=>$userId]];
+		return ['code' => 0, 'data' => ['userId' => $userId]];
 	}
-
+	
 }