“一点一扫,秒回畅登!
在A网站点击登录 → 秒跳安全验证页 → 手机轻松扫码 → 瞬间自动返回A站!
全程免输密码,登录如丝般顺滑,安全又省心~ ”
创建bottom的link
https://auth.sunyee.site/wechat-login.php
B站点带php创建weixin-login.php
<?php
// wechat-login.php
$appid = 'wxxxxxx';
$redirect_uri = urlencode('https://A站点/wechat-callback.php');
$scope = 'snsapi_login'; // 网站应用必须是 snsapi_login
$state = 'state123'; // 可自定义
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=$state#wechat_redirect";
header("Location: $url");
exit;
A站是Wordpress根目录下创建wechat-callback.php
<?php
require_once('wp-load.php');
$appid = 'wxxxxx';
$appsecret = 'aexxxxx';
$code = $_GET['code'];
if (!$code) exit('未获取 code');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
$response = file_get_contents($url);
$data = json_decode($response, true);
if (!isset($data['openid'])) {
exit('微信授权失败:' . $response);
}
$openid = $data['openid'];
$unionid = isset($data['unionid']) ? $data['unionid'] : null;
// 查询 WordPress 用户是否存在
$user = get_users([
'meta_key' => 'wechat_openid',
'meta_value' => $openid,
'number' => 1
]);
if ($user) {
// 登录已有用户
wp_set_current_user($user[0]->ID);
wp_set_auth_cookie($user[0]->ID);
wp_redirect(home_url());
exit;
} else {
// 创建新用户
$username = 'wx_' . wp_generate_password(6, false);
$password = wp_generate_password();
$user_id = wp_create_user($username, $password);
update_user_meta($user_id, 'wechat_openid', $openid);
if ($unionid) update_user_meta($user_id, 'wechat_unionid', $unionid);
// 登录
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect(home_url());
exit;
}
发表回复