shish hace 6 años
padre
commit
05d34f79fd

+ 1 - 1
app/mall/controllers/AuthController.php

@@ -162,7 +162,7 @@ class AuthController extends PublicController
 			Yii::info(json_encode($userInfo));
 			$user = UserService::replaceUser($userInfo, $userSource, $account);
 		}
-		$cacheKey = 'MINI_SESSION_KEY_' . $openid;
+		$cacheKey = 'MALL_MINI_SESSION_KEY_' . $openid;
 		Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
 		$userId = $user['id'];
 		$token = jwt::getNewToken($userId);

+ 2 - 2
app/mall/controllers/UserController.php

@@ -41,7 +41,7 @@ class UserController extends BaseController
 		if (empty($miniOpenId)) {
 			util::fail('mini_open_id empty');
 		}
-		$cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
+		$cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
 		$sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
 		$wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
 		require_once($wxMiniSecret . '/wxBizDataCrypt.php');
@@ -76,7 +76,7 @@ class UserController extends BaseController
 			UserAssetService::updateByUserId($userId, ['member' => 0]);
 			util::success(['mobile' => $user['mobile']]);
 		}
-		$cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
+		$cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
 		$sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
 		if (empty($sessionKey)) {
 			util::fail('sesstion key empty');

+ 1 - 1
app/shop/config/main.php

@@ -7,7 +7,7 @@ $params = array_merge(
 );
 
 return [
-	'id' => 'app-backend',
+	'id' => 'app-shop',
 	'basePath' => dirname(__DIR__),
 	'bootstrap' => ['log'],
 	'defaultRoute' => 'main/index',//默认控制器

+ 40 - 0
app/shop/controllers/AuthController.php

@@ -154,4 +154,44 @@ class AuthController extends PublicController
 		util::success(['token' => $token, 'account' => $admin['merchantId'], 'merchantId' => $admin['merchantId']]);
 	}
 	
+	//静默获取小程序用户信息
+	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'];
+		if (empty($appSecret)) {
+			util::fail('没有找到小程序的密钥');
+		}
+		$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'] : '';
+		if (empty($openid)) {
+			Yii::info(json_encode($arr));
+			util::fail('没有获取到小程序openId');
+		}
+		//用户来源
+		$userSource = UserClass::$userSourceId['mini']['name'];
+		$user = UserService::getByMiniOpenId($openid, $account);
+		if (empty($user)) {
+			$userInfo = ['miniOpenId' => $openid, 'merchantId' => $account];
+			Yii::info(json_encode($userInfo));
+			$user = UserService::replaceUser($userInfo, $userSource, $account);
+		}
+		$cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
+		Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
+		$userId = $user['id'];
+		$token = jwt::getNewToken($userId);
+		util::success(['token' => $token, 'user' => $user]);
+	}
+
 }

+ 0 - 1
common/config/bootstrap.php

@@ -5,5 +5,4 @@ Yii::setAlias('@shop', dirname(dirname(__DIR__)) . '/app/shop');
 Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
 Yii::setAlias('@saas', dirname(dirname(__DIR__)) . '/app/saas');
 Yii::setAlias('@www', dirname(dirname(__DIR__)) . '/app/www');
-Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/app/api');
 Yii::setAlias('@biz', dirname(dirname(__DIR__)) . '/biz');