Jelajahi Sumber

授权开放平台创建首个商家

shish 6 tahun lalu
induk
melakukan
5a225af693
1 mengubah file dengan 97 tambahan dan 103 penghapusan
  1. 97 103
      app/www/controllers/WxOpenController.php

+ 97 - 103
app/www/controllers/WxOpenController.php

@@ -21,9 +21,9 @@ use common\services\xhSetMealService;
  */
 class WxOpenController extends PublicController
 {
-
+	
 	public $withoutLogin = ['callback-create-first-merchant'];
-
+	
 	//开放平台开始授权 shish 2020.1.16
 	public function actionBeginAuth()
 	{
@@ -34,14 +34,14 @@ class WxOpenController extends PublicController
 		$oData = [];
 		$oData['preAuthCodeTime'] = date("Y-m-d H:i:s");//设置预授码过期,让下个用户可以获取新的预授码
 		xhWxOpenService::updateById($id, $oData);
-		$url = Yii::$app->params['w'] . '/join/callback';
+		$url = Yii::$app->params['w'] . '/wx-open/callback';
 		$merchantId = $open['merchantId'];//如果平台没有绑定商家,先去创建第一个商家
 		if (empty($merchantId)) {
-			$url = Yii::$app->params['w'] . '/join/callback-create-first-merchant';
+			$url = Yii::$app->params['w'] . '/wx-open/callback-create-first-merchant';
 		}
 		echo "<a href='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}'>授权</a>";
 	}
-
+	
 	public function actionCheckMerchantName()
 	{
 		$get = Yii::$app->request->get();
@@ -74,58 +74,58 @@ class WxOpenController extends PublicController
 	{
 		$get = Yii::$app->request->get();
 		if (isset($get['auth_code']) == false) {
-			return $this->renderPartial('hint', ['msg' => '公众号授权回调未成功']);
+			util::stop('公众号授权回调未成功');
 		}
 		$code = $get['auth_code'];
 		//使用授权码换取公众号的接口调用凭据和授权信息
-		$authorizer = weixinOpenUtil::getAuthorizer($code);
-		if (isset($authorizer['authorization_info']) == false) {
-			return $this->renderPartial('hint', ['msg' => '未换取公众号的接口调用凭据和授权信息']);
+		$authorize = weixinOpenUtil::getAuthorizer($code);
+		if (isset($authorize['authorization_info']) == false) {
+			util::stop('未换取公众号的接口调用凭据和授权信息');
 		}
-		$info = $authorizer['authorization_info'];
-		$authorizer_appid = $info['authorizer_appid'];
-		$authorizer_access_token = $info['authorizer_access_token'];
+		$info = $authorize['authorization_info'];
+		$authorizeAppId = $info['authorizer_appid'];
+		$authorize_access_token = $info['authorizer_access_token'];
 		$expires_in = $info['expires_in'];//7200
-		$authorizer_refresh_token = $info['authorizer_refresh_token'];
+		$authorize_refresh_token = $info['authorizer_refresh_token'];
 		//获取授权方的公众号帐号基本信息
-		$authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid);
-		if (isset($authorizerDetailInfo['authorizer_info']) == false) {
-			return $this->renderPartial('hint', ['msg' => '没有获取到公众号帐号基本信息']);
-		}
-		$authorizer_info = $authorizerDetailInfo['authorizer_info'];
-		$nick_name = $authorizer_info['nick_name'];
-		$head_img = $authorizer_info['head_img'];
-		$service_type_info = $authorizer_info['service_type_info'];
+		$authorizeDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizeAppId);
+		if (isset($authorizeDetailInfo['authorizer_info']) == false) {
+			util::stop('没有获取到公众号帐号基本信息');
+		}
+		$authorize_info = $authorizeDetailInfo['authorizer_info'];
+		$nick_name = $authorize_info['nick_name'];
+		$head_img = $authorize_info['head_img'];
+		$service_type_info = $authorize_info['service_type_info'];
 		if ($service_type_info['id'] != 2) {
-			return $this->renderPartial('hint', ['msg' => '公众号不是服务号']);
+			util::stop('公众号不是服务号');
 		}
-		$verify_type_info = $authorizer_info['verify_type_info'];
+		$verify_type_info = $authorize_info['verify_type_info'];
 		if ($verify_type_info['id'] != 0) {
-			return $this->renderPartial('hint', ['msg' => '公众号还没有认证哦']);
+			util::stop('公众号还没有认证哦');
 		}
-		$user_name = $authorizer_info['user_name'];
-		$alias = $authorizer_info['alias'];
-		$qrcode_url = $authorizer_info['qrcode_url'];
-		$business_info = $authorizer_info['business_info'];
+		$user_name = $authorize_info['user_name'];
+		$alias = $authorize_info['alias'];
+		$qrCodeUrl = $authorize_info['qrcode_url'];
+		$business_info = $authorize_info['business_info'];
 		$open_pay = $business_info['open_pay'];
-		$merchant = xhMerchantService::getByAppId($authorizer_appid);
+		$merchant = xhMerchantService::getByAppId($authorizeAppId);
 		$wxAccessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
 		if (!empty($merchant)) {
-			return $this->renderPartial('hint', ['msg' => '平台已经关联公众号了']);
+			util::stop('平台已经关联公众号了');
 		}
 		//引导生成商家
 		$date = date("Y-m-d H:i:s");
 		$trainDemo = configDict::getConfig('trainDemo');
-		$logo = xhMerchantService::logoSave($head_img, $authorizer_appid);
+		$logo = xhMerchantService::logoSave($head_img, $authorizeAppId);
 		$appData['logo'] = $logo;
-		$wxQrcodeUrl = xhMerchantService::qrCodeSave($qrcode_url, $authorizer_appid);
-		$appData['wxQrcodeUrl'] = $wxQrcodeUrl;
-		$appData['wxAppId'] = $authorizer_appid;
+		$wxQrCodeUrl = xhMerchantService::qrCodeSave($qrCodeUrl, $authorizeAppId);
+		$appData['wxQrcodeUrl'] = $wxQrCodeUrl;
+		$appData['wxAppId'] = $authorizeAppId;
 		$appData['wxUserName'] = $user_name;
 		$appData['wxAliasName'] = $alias;
-		$appData['wxAccessToken'] = $authorizer_access_token;
+		$appData['wxAccessToken'] = $authorize_access_token;
 		$appData['wxAccessTokenTime'] = $wxAccessTokenTime;
-		$appData['wxRefreshToken'] = $authorizer_refresh_token;
+		$appData['wxRefreshToken'] = $authorize_refresh_token;
 		$appData['accountStyle'] = 0;//认证服务号
 		$appData['status'] = 0;//待审核
 		$appData['createTime'] = $date;
@@ -158,107 +158,101 @@ class WxOpenController extends PublicController
 		xhWxOpenService::updateById($wxOpenId, ['merchantId' => $merchantId]);
 		xhWxMenuService::applyInit($merchant);//初始化微信菜单
 		$openMerchant = xhWxOpenService::getMerchant();
-		$openMerchantAccount = $openmerchant['id'];
+		$openMerchantAccount = $openMerchant['id'];
 		//将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟
 		$newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
-		$this->redirect(Yii::$app->params['adminUrl'] . '/admin/generate?account=' . $openMerchantAccount . '&newAccount=' . $newAccount);
+		echo 'ok';
 	}
 	
 	public function actionCallback()
 	{
 		$get = Yii::$app->request->get();
 		if (isset($get['auth_code']) == false) {
-			return $this->renderPartial('hint', ['msg' => '公众号授权回调未成功']);
+			util::stop('公众号授权回调未成功');
 		}
 		$code = $get['auth_code'];
 		//使用授权码换取公众号的接口调用凭据和授权信息
-		$authorizer = weixinOpenUtil::getAuthorizer($code);
-		if (isset($authorizer['authorization_info']) == false) {
-			return $this->renderPartial('hint', ['msg' => '未换取公众号的接口调用凭据和授权信息']);
+		$authorize = weixinOpenUtil::getAuthorizer($code);
+		if (isset($authorize['authorization_info']) == false) {
+			util::stop('未换取公众号的接口调用凭据和授权信息');
 		}
-		$info = $authorizer['authorization_info'];
-		$authorizer_appid = $info['authorizer_appid'];
-		$authorizer_access_token = $info['authorizer_access_token'];
+		$info = $authorize['authorization_info'];
+		$authorizeAppId = $info['authorizer_appid'];
+		$authorize_access_token = $info['authorizer_access_token'];
 		$expires_in = $info['expires_in'];//7200
-		$authorizer_refresh_token = $info['authorizer_refresh_token'];
+		$authorize_refresh_token = $info['authorizer_refresh_token'];
 		//获取授权方的基本信息
-		$authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid);
-		print_r($authorizerDetailInfo);
-		if (isset($authorizerDetailInfo['authorizer_info']) == false) {
-			return $this->renderPartial('hint', ['msg' => '没有获取到公众号帐号基本信息']);
-		}
-		$authorizer_info = $authorizerDetailInfo['authorizer_info'];
-		$nick_name = $authorizer_info['nick_name'];
-		$head_img = $authorizer_info['head_img'];
-		$service_type_info = $authorizer_info['service_type_info'];
+		$authorizeDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizeAppId);
+		print_r($authorizeDetailInfo);
+		if (isset($authorizeDetailInfo['authorizer_info']) == false) {
+			util::stop('没有获取到公众号帐号基本信息');
+		}
+		$authorize_info = $authorizeDetailInfo['authorizer_info'];
+		$nick_name = $authorize_info['nick_name'];
+		$head_img = $authorize_info['head_img'];
+		$service_type_info = $authorize_info['service_type_info'];
 		if ($service_type_info['id'] != 2) {
-			//return $this->renderPartial('hint',['msg' => '公众号不是服务号,appid:'.$authorizer_appid.' refresh:'.$authorizer_refresh_token]);
+			util::stop('公众号不是服务号,appId:'.$authorizeAppId.' refresh:'.$authorize_refresh_token);
 		}
-		$verify_type_info = $authorizer_info['verify_type_info'];
+		$verify_type_info = $authorize_info['verify_type_info'];
 		if ($verify_type_info['id'] != 0) {
-			//return $this->renderPartial('hint',['msg' => '公众号还没有认证哦']);
+			util::stop('公众号还没有认证哦');
 		}
-		$user_name = $authorizer_info['user_name'];
-		$alias = $authorizer_info['alias'];
-		$qrcode_url = $authorizer_info['qrcode_url'];
-		$business_info = $authorizer_info['business_info'];
+		$user_name = $authorize_info['user_name'];
+		$alias = $authorize_info['alias'];
+		$qrCodeUrl = $authorize_info['qrcode_url'];
+		$business_info = $authorize_info['business_info'];
 		$open_pay = $business_info['open_pay'];
 		$accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
 		//小程序
-		
-		if (isset($authorizer_info['MiniProgramInfo']) && !empty($authorizer_info['MiniProgramInfo'])) {
-			$merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizer_appid]);
+		if (isset($authorize_info['MiniProgramInfo']) && !empty($authorize_info['MiniProgramInfo'])) {
+			$merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizeAppId]);
 			if (empty($merchant)) {
-				Yii::info('没有小程序信息 appid:' . $authorizer_appid);
-				echo '没有小程序信息 appid:' . $authorizer_appid;
-				Yii::$app->end();
+				Yii::info('没有小程序信息 appId:' . $authorizeAppId);
+				util::stop('没有小程序信息 appId:' . $authorizeAppId);
 			}
 			$merchantId = $merchant['id'];
-			xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorizer_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorizer_refresh_token]);
-			Yii::info('小程序信息更新成功:' . json_encode($authorizer_info));
-			echo "<pre>";
-			echo "小程序信息更新成功";
+			xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorize_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorize_refresh_token]);
+			Yii::info('小程序信息更新成功:' . json_encode($authorize_info));
+			util::stop("小程序信息更新成功");
 		} else {
-			$merchant = xhMerchantService::getByAppId($authorizer_appid);
-			
-			Yii::info('appId:' . $authorizer_appid);
-			
+			$merchant = xhMerchantService::getByAppId($authorizeAppId);
+			Yii::info('appId:' . $authorizeAppId);
 			$set = xhSetMealService::getRandOne();
 			if (empty($set)) {
-				return $this->renderPartial('hint', ['msg' => '没有套餐信息']);
+				util::stop('没有套餐信息');
 			}
 			if (empty($merchant)) {
-				echo '这个商家没有生成过基本信息。appid:' . $authorizer_appid;
-				Yii::$app->end();
+				util::stop('这个商家没有生成过基本信息。appid:' . $authorizeAppId);
 			}
 			if (empty($merchant)) {
 				echo '新建商家能力暂时关闭';
 				Yii::$app->end();
 				//没有生成商家,引导进去设置
 				$session = Yii::$app->session;
-				$session['wxAppId'] = $authorizer_appid;
+				$session['wxAppId'] = $authorizeAppId;
 				$session['wxUserName'] = $user_name;
 				$session['wxAliasName'] = $alias;
-				$session['wxAccessToken'] = $authorizer_access_token;
+				$session['wxAccessToken'] = $authorize_access_token;
 				$session['wxAccessTokenTime'] = $accessTokenTime;
-				$session['wxRefreshToken'] = $authorizer_refresh_token;
+				$session['wxRefreshToken'] = $authorize_refresh_token;
 				$session['logo'] = $head_img;
-				$session['wxQrcodeUrl'] = $qrcode_url;
+				$session['wxQrcodeUrl'] = $qrCodeUrl;
 				$session['allowGenerateMerchant'] = 'ok';//经过授权流程,允许允许生成商家
 				$trainDemo = configDict::getConfig('trainDemo');
 				$demoName = $trainDemo['merchant']['name'];//培训演示的公众号
 				return $this->renderPartial('callback', ['merchantName' => $nick_name, 'set' => $set, 'demoName' => $demoName]);
 			}
 			$cData = [];
-			$cData['wxAccessToken'] = $authorizer_access_token;
+			$cData['wxAccessToken'] = $authorize_access_token;
 			$cData['wxAccessTokenTime'] = $accessTokenTime;
-			$cData['wxRefreshToken'] = $authorizer_refresh_token;
+			$cData['wxRefreshToken'] = $authorize_refresh_token;
 			//审核通过
 			$cData['status'] = 1;
 			$merchantId = $merchant['id'];
 			xhMerchantService::updateById($merchantId, $cData);
 		}
-		echo $authorizer_appid . ' ';
+		echo $authorizeAppId . ' ';
 		exit('ok');
 		$price = $set['price'];
 		$now = time();
@@ -269,7 +263,7 @@ class WxOpenController extends PublicController
 		$orderId = $apply['id'];
 		$price = $apply['actPrice'];
 		$openMerchant = xhWxOpenService::getMerchant();
-		$openMerchantAccount = $openmerchant['id'];
+		$openMerchantAccount = $openMerchant['id'];
 		$account = $merchant['id'];
 		//将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟
 		$newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
@@ -338,8 +332,8 @@ class WxOpenController extends PublicController
 			$post['logo'] = $logo;
 		}
 		if (isset($session['wxQrcodeUrl'])) {
-			$wxQrcodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'], $wxAppId);
-			$post['wxQrcodeUrl'] = $wxQrcodeUrl;
+			$wxQrCodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'], $wxAppId);
+			$post['wxQrcodeUrl'] = $wxQrCodeUrl;
 		}
 		$date = date("Y-m-d H:i:s");
 		$inviteCode = isset($post['inviteCode']) ? $post['inviteCode'] : '';
@@ -416,7 +410,7 @@ class WxOpenController extends PublicController
 			xhInviteService::updateByCode($inviteCode, ['targetId' => $orderId, 'takeStatus' => 1, 'invitedMerchantId' => $id, 'inviteUseTo' => 0]);
 		}
 		$openMerchant = xhWxOpenService::getMerchant();
-		$openMerchantAccount = $openmerchant['id'];
+		$openMerchantAccount = $openMerchant['id'];
 		$newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
 		util::successInfo('提交成功', 'A0001', ['account' => $openMerchantAccount, 'newAccount' => $newAccount, 'toPay' => $toPay, 'orderId' => $orderId, 'actPrice' => $actPrice]);
 	}
@@ -504,8 +498,8 @@ class WxOpenController extends PublicController
 			//取消授权通知
 			case 'unauthorized':
 				$AppId = $postObj->AppId;
-				$AuthorizerAppid = $postObj->AuthorizerAppid;
-				$merchant = xhMerchantService::getByAppId($AuthorizerAppid);
+				$authorizeAppid = $postObj->AuthorizerAppid;
+				$merchant = xhMerchantService::getByAppId($authorizeAppid);
 				if (!empty($merchant)) {
 					$merchantId = $merchant['id'];
 					xhMerchantService::updateById($merchantId, ['status' => 5]);//自主冻结
@@ -515,7 +509,7 @@ class WxOpenController extends PublicController
 			case 'authorized':
 				$AppId = $postObj->AppId;//第三方平台appid
 				$CreateTime = $postObj->CreateTime;//公众号
-				$AuthorizerAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
+				$authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
 				$AuthorizationCode = $postObj->AuthorizationCode;
 				$AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
 				break;
@@ -523,28 +517,28 @@ class WxOpenController extends PublicController
 			case 'updateauthorized':
 				$AppId = $postObj->AppId;
 				$CreateTime = $postObj->CreateTime;
-				$AuthorizerAppid = $postObj->AuthorizerAppid;
+				$authorizeAppid = $postObj->AuthorizerAppid;
 				$AuthorizationCode = $postObj->AuthorizationCode;
 				$AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
-				$authorizer = weixinOpenUtil::getAuthorizer($AuthorizationCode);
-				if (isset($authorizer['authorization_info']) == false) {
+				$authorize = weixinOpenUtil::getAuthorizer($AuthorizationCode);
+				if (isset($authorize['authorization_info']) == false) {
 					$msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
 					Yii::warning($msg, __METHOD__);
 					Yii::$app->end();
 				}
-				$info = $authorizer['authorization_info'];
-				$authorizer_appid = $info['authorizer_appid'];
-				$authorizer_access_token = $info['authorizer_access_token'];
+				$info = $authorize['authorization_info'];
+				$authorizeAppId = $info['authorizer_appid'];
+				$authorize_access_token = $info['authorizer_access_token'];
 				$expires_in = $info['expires_in'];//7200
-				$authorizer_refresh_token = $info['authorizer_refresh_token'];
-				$merchant = xhMerchantService::getByAppId($authorizer_appid);
+				$authorize_refresh_token = $info['authorizer_refresh_token'];
+				$merchant = xhMerchantService::getByAppId($authorizeAppId);
 				if (empty($merchant)) {
 					return false;
 				}
 				$merchantId = $merchant['id'];
 				$wxData = [];
-				$wxData['wxAccessToken'] = $authorizer_access_token;
-				$wxData['wxRefreshToken'] = $authorizer_refresh_token;
+				$wxData['wxAccessToken'] = $authorize_access_token;
+				$wxData['wxRefreshToken'] = $authorize_refresh_token;
 				$wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
 				$wxData['status'] = 1;
 				xhMerchantService::updateById($merchantId, $wxData);