|
|
@@ -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]);
|
|
|
+ }
|
|
|
+
|
|
|
}
|