浏览代码

小程序

shish 6 年之前
父节点
当前提交
4020de7740
共有 2 个文件被更改,包括 112 次插入11 次删除
  1. 34 4
      app/client/controllers/AuthController.php
  2. 78 7
      app/client/controllers/UserController.php

+ 34 - 4
app/client/controllers/AuthController.php

@@ -16,7 +16,7 @@ use Lcobucci\JWT\Builder;
 use Lcobucci\JWT\Signer\Hmac\Sha256;
 use Lcobucci\JWT\Parser;
 use Lcobucci\JWT\ValidationData;
-
+use linslin\yii2\curl;
 
 /**
  * 微信 oauth 授权相关
@@ -25,8 +25,6 @@ use Lcobucci\JWT\ValidationData;
 class AuthController extends PublicController
 {
 	
-	
-	
 	//准备授权 shish 2019.11.19
 	public function actionPrepare()
 	{
@@ -57,7 +55,7 @@ class AuthController extends PublicController
 		util::end();
 	}
 	
-	//授权获取用户信息 shish 2019.11.20
+	//微信授权获取用户信息 shish 2019.11.20
 	public function actionGetUserInfo()
 	{
 		$get = Yii::$app->request->get();
@@ -138,4 +136,36 @@ class AuthController extends PublicController
 		$this->redirect($url);
 	}
 	
+	//静默获取小程序用户信息
+	public function actionMiniInfo()
+	{
+		$code = Yii::$app->request->get('code', '');
+		if (empty($code)) {
+			util::fail('没有CODE信息');
+		}
+		$merchant = $this->merchant;
+		$account = $this->merchantId;
+		$appId = $merchant['miniAppId'];
+		$appSecret = $merchant['miniAppSecret'];
+		$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
+		
+		$curl = new curl\Curl();
+		$result = $curl->get($url);
+		$arr = Json::decode($result);
+		$sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
+		$openid = isset($arr['openid']) ? $arr['openid'] : '';
+		//用户来源
+		$userSource = UserService::$userSourceId['mini']['name'];
+		$user = UserService::getByMiniOpenId($openid, $account);
+		if (empty($user)) {
+			$userInfo = ['miniOpenId' => $openid, 'merchantId' => $account];
+			$user = UserService::replaceUser($userInfo, $userSource, $account);
+		}
+		$cacheKey = 'MINI_SESSION_KEY_' . $openid;
+		Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
+		$userId = $user['id'];
+		$token = jwt::getNewToken($userId);
+		util::success(['token' => $token, 'user' => $user]);
+	}
+
 }

+ 78 - 7
app/client/controllers/UserController.php

@@ -1,20 +1,23 @@
 <?php
+
 namespace client\controllers;
+
 use biz\user\services\UserAssetService;
+use biz\user\services\UserService;
 use Yii;
-use yii\web\Controller;
 use common\components\util;
+use yii\helpers\Json;
 
-
-class UserController extends BaseController {
-
+class UserController extends BaseController
+{
+	
 	//获取登陆客户的资产 shish 2019.11.22
 	public function actionAsset()
 	{
 		$asset = UserAssetService::getByUserId($this->userId);
 		util::success($asset);
 	}
-
+	
 	//获取登陆客户的优惠情况  shish 2019.12.3
 	public function actionDiscount()
 	{
@@ -22,6 +25,74 @@ class UserController extends BaseController {
 		$discount = UserAssetService::getDiscount($userId);
 		util::success($discount);
 	}
-
-
+	
+	//小程序用户完整信息 shish 2019.12.12
+	public function actionMiniFullInfo()
+	{
+		$post = Yii::$app->request->post();
+		$iv = isset($post['iv']) ? $post['iv'] : '';
+		$encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
+		$appId = $this->merchant['miniAppId'];
+		$merchantId = $this->merchantId;
+		$miniOpenId = $this->user['miniOpenId'];
+		if (empty($miniOpenId)) {
+			util::fail('mini_open_id empty');
+		}
+		$cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
+		$sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+		$wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
+		require_once($wxMiniSecret . '/wxBizDataCrypt.php');
+		$pc = new \WXBizDataCrypt($appId, $sessionKey);
+		$errCode = $pc->decryptData($encryptedData, $iv, $result);
+		if ($errCode != 0) {
+			Yii::info($result . ' ' . $errCode);
+			util::fail('获取用户信息失败');
+		}
+		Yii::info('小程序获取用户信息:' . $result);
+		$originalInfo = Json::decode($result);
+		$source = UserService::$userSourceId['mini']['name'];
+		$user = UserService::replaceUser($originalInfo, $source, $merchantId);
+		$userId = $user['id'];
+		$userInfo = UserService::getUserInfo($userId);
+		util::success($userInfo);
+	}
+	
+	//小程序用户手机号
+	public function actionMiniMobile()
+	{
+		$post = Yii::$app->request->post();
+		$iv = $post['iv'];
+		$encryptedData = $post['encryptedData'];
+		$merchantId = $this->merchantId;
+		$merchant = $this->merchant;
+		$appId = $merchant['miniAppId'];
+		$miniOpenId = $this->user['miniOpenId'];
+		$user = $this->user;
+		if (!empty($user) && !empty($user['mobile'])) {
+			$userId = $user['id'];
+			UserService::updateById($userId, ['isMember' => 1]);
+			UserAssetService::updateByUserId($userId, ['isMember' => 1]);
+			util::successInfo('操作成功', 'A0001', ['mobile' => $user['mobile']]);
+		}
+		$cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
+		$sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+		if (empty($sessionKey)) {
+			util::fail('sesstion key empty');
+		}
+		$wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
+		require_once($wxMiniSecret . '/wxBizDataCrypt.php');
+		
+		$pc = new \WXBizDataCrypt($appId, $sessionKey);
+		$errCode = $pc->decryptData($encryptedData, $iv, $result);
+		if ($errCode != 0) {
+			util::fail('解密失败');
+		}
+		$arr = Json::decode($result);
+		$mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
+		$userId = $user['id'];
+		/**升级成为会员**/
+		UserService::upgradeToMember($userId, $merchantId, ['mobile' => $mobile]);
+		util::success(['mobile' => $mobile]);
+	}
+	
 }