payload = $payload; $this->request = request(); $this->wechat = Wechat::openPlatform(); } public function login() { $payload = $this->payload; if (empty($payload['code'])) { throw new BusinessException('登陆失败'); } $config = shop_config('shop.platform.App'); echo "
";
print_r($this->wechat);
echo "";
exit;
// 获取accessToken & openid
$res = Http::get('https://api.weixin.qq.com/sns/oauth2/access_token', [
'appid' => $config['app_id'],
'secret' => $config['secret'],
'code' => $payload['code'],
'grant_type' => 'authorization_code'
]);
$decryptedData = json_decode($res, true);
if (isset($decryptedData['errmsg'])) {
throw new BusinessException($decryptedData['errmsg']);
}
// 获取userInfo
$res = Http::get('https://api.weixin.qq.com/sns/userinfo', ['access_token' => $decryptedData['access_token'], 'openid' => $decryptedData['openid']]);
$userInfo = is_string($res) ? json_decode($res, true) : $res;
if (isset($userInfo['errmsg'])) {
throw new BusinessException($userInfo['errmsg']);
}
$wechatUser = [
'openid' => $userInfo['openid'],
'unionid' => $userInfo['unionid'] ?? '',
'avatar' => $userInfo['headimgurl'],
'nickname' => $userInfo['nickname'],
];
return $wechatUser;
}
public function bind()
{
return $this->login();
}
}