shish il y a 6 ans
Parent
commit
1b66ca23fa

+ 39 - 41
app/mobile/controllers/AuthController.php

@@ -4,6 +4,7 @@ namespace mobile\controllers;
 
 use biz\merchant\services\MerchantService;
 use biz\user\services\UserService;
+use common\components\stringUtil;
 use Yii;
 use common\components\util;
 use common\services\xhMerchantService;
@@ -19,20 +20,47 @@ class AuthController extends PublicController
 	
 	public $enableCsrfValidation = false;
 	
+	//准备授权 shish 2019.11.19
+	public function actionPrepare()
+	{
+		$post = Yii::$app->request->post();print_r($post);die;
+		$suffixUrl = isset($post['suffixUrl']) && !empty($post['suffixUrl']) ? $post['suffixUrl'] : '';
+		if (empty($suffixUrl)) {
+			util::fail('没有网址');
+		}
+		$account = isset($post['account']) ? $post['account'] : 0;
+		$merchant = MerchantService::getById($account);
+		if (empty($merchant)) {
+			util::fail('商家无效');
+		}
+		/**
+		 * authScope 参数的说明
+		 * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
+		 * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
+		 * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
+		 * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
+		 */
+		$authScope = isset($post['authScope']) ? $post['authScope'] : 'snsapi_base';
+		$host = Yii::$app->request->getHostInfo();
+		$url = $host . '/auth/get-user-info?suffixUrl=' . $suffixUrl . '&authScope=' . $authScope;
+		weixinUtil::getToken($url, $merchant, $authScope);
+		util::success();
+	}
+	
+	//授权获取用户信息 shish 2019.11.20
 	public function actionGetUserInfo()
 	{
 		$get = Yii::$app->request->get();
 		$code = isset($get['code']) ? $get['code'] : '';
 		if (empty($code)) {
-			echo '没有获取用户code';
-			util::end();
+			util::fail('没有获取用户code');
 		}
 		if (isset($get['state']) && $get['state'] == 'authdeny') {
-			util::stop('auth fail');
+			util::fail('auth fail');
 		}
 		$account = '';
-		$params = $get['params'];
-		$suffixUrl = base64_decode($params);
+		$suffixUrlEncode = $get['suffixUrl'];
+		$suffixUrl = stringUtil::urlSafeBase64Decode($suffixUrlEncode);
 		$parse = parse_url($suffixUrl);
 		if (isset($parse['query'])) {
 			parse_str($parse['query'], $queryArray);
@@ -40,7 +68,7 @@ class AuthController extends PublicController
 				$account = $queryArray['account'];
 			}
 		}
-		//获取m.huahb.com/account/12358这类链接的商家id
+		//获取m.xx.com/account/12358这类链接的商家id
 		if (empty($account) && preg_match('/\/account\/(\d+)/', $suffixUrl, $match)) {
 			$account = $match[1];
 		}
@@ -52,7 +80,7 @@ class AuthController extends PublicController
 		}
 		$merchant = xhMerchantService::getByAccount($account);
 		if (empty($merchant)) {
-			util::stop('商店不存在哦!');
+			util::stop('商店无效');
 		}
 		$merchantId = $merchant['id'];
 		$authScope = isset($get['authScope']) ? $get['authScope'] : '';
@@ -62,8 +90,7 @@ class AuthController extends PublicController
 		$data = weixinUtil::getOpenId($code, $merchant);
 		//获取code失败,重新获取
 		if ($data === false) {
-			$params = $_GET['params'];
-			$url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?params=' . $params . '&authScope=' . $authScope;
+			$url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?suffixUrl=' . $suffixUrl . '&authScope=' . $authScope;
 			weixinUtil::getToken($url, $merchant, $authScope);
 			util::end();
 		}
@@ -74,7 +101,7 @@ class AuthController extends PublicController
 		$userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
 		$source = $userSource['name'];
 		if (empty($user)) {
-			//$authScope snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
+			//$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
 			$originalInfo = [];
 			$originalInfo['openId'] = $openId;
 			$originalInfo['merchantId'] = $merchantId;
@@ -111,38 +138,9 @@ class AuthController extends PublicController
 		Yii::$app->params['merchantId'] = $merchantId;
 		Yii::$app->params['merchant'] = $merchant;
 		xhUserService::loginByThird($user);
-		$session = Yii::$app->session;
-		$session['visitingMerchantAccount'] = $merchant['id'];//以此判断是否访问其它商家,如果是用户需要重新登陆
-		$url = 'http://' . $_SERVER['HTTP_HOST'] . $suffixUrl;
+		$host = Yii::$app->request->getHostInfo();
+		$url = $host . '/' . $suffixUrl;
 		$this->redirect($url);
 	}
 	
-	//准备授权 shish 2019.11.19
-	public function actionPrepare()
-	{
-		$post = Yii::$app->request->post();
-		$authScope = isset($post['authScope']) ? $post['authScope'] : 'snsapi_base';
-		$url = isset($post['url']) && !empty($post['url']) ? $post['url'] : '';
-		if (empty($url)) {
-			util::fail('没有网址');
-		}
-		$account = isset($post['account']) ? $post['account'] : 0;
-		$merchant = MerchantService::getById($account);
-		if (empty($merchant)) {
-			util::fail('商家无效');
-		}
-		/**
-		 * authScope 参数的说明
-		 * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
-		 * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
-		 * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
-		 * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
-		 */
-		$authScope = isset($post['authScope']) ? $post['authScope'] : 'snsapi_base';
-		$redirectUrl = base64_encode($url);echo $redirectUrl;die;
-		$url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?params=' . $redirectUrl . '&authScope=' . $authScope;
-		weixinUtil::getToken($url, $merchant, $authScope);
-		util::end();
-	}
-
 }

+ 2 - 4
app/mobile/controllers/MainController.php

@@ -8,10 +8,8 @@ class MainController extends PublicController{
 
 	public function actionIndex()
 	{
-		
-		
-		
-		return $this->renderPartial('index');
+		$host = Yii::$app->request->getHostInfo();
+		return $this->renderPartial('index',['host'=>$host]);
 	}
 
 }

+ 28 - 0
app/mobile/views/main/index.php

@@ -0,0 +1,28 @@
+<body>
+<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
+<script>
+
+    function StandardPost(url,args){
+        var form = $("<form method='post'></form>"),
+            input;
+        form.attr({"action":url});
+
+        //组合成一个个input
+        $.each(args,function(key,value){
+
+            //alert(key);
+            //alert(value);
+            input = $("<input type='hidden'>");
+            input.attr({"name":key});
+            input.val(value);
+            form.append(input);
+        });
+        $(document.body).append(form);
+        form.submit();  //进行提交
+    }
+
+    var args = { suffixUrl: "#main/index?account=12358",authScope: "snsapi_base", _csrf:"<?= Yii::$app->request->csrfToken ?>"};
+    StandardPost("<?= $host ?>/auth/prepare",args)
+
+</script>
+</body>