我听有人说微信开放平台可以实现网页的微信登录,电脑网页可以,但手机网页就不行,生成的二维码只能摄像头扫码,不能截屏扫码。有没有大佬可以帮帮我,搞不定了,这东西好难搞
1 个赞
花300快开通微信开放平台的认证就行了 或者认证一个公众号
1 个赞
开通了大佬,微信开发平台的网站应用,但是生成的二维码只能摄像头扫码,不能截屏扫码。导致手机网站没法微信登录。
registerApi(‘get’, ‘/wechat/login’, async (req, res) => {
try {
const appid = process.env.WECHAT_OPEN_APPID;
const redirectUri = process.env.WECHAT_REDIRECT_URI;
const state = crypto.randomBytes(16).toString('hex');
// 将 state 存储到 Redis 中,用于后续验证(替代 session,支持分布式环境)
const storeResult = await redisService.setWechatState(state, 5 \* 60 \* 1000); // 5分钟过期
if (!storeResult) {
console.error('Failed to store wechat state to Redis');
return res.status(500).json({ status: 'error', message: 'Failed to store state' });
}
console.log('Wechat login parameters generated:', { appid, redirectUri, state, storeResult });
res.json({
status: 'ok',
appid: appid,
redirect_uri: redirectUri,
state: state
});
} catch (error) {
console.error('Wechat login error:', error);
res.status(500).json({ status: 'error', message: 'Failed to generate wechat login parameters: ' + error.message });
}
});
// 处理微信授权回调(使用开放平台参数)
registerApi(‘get’, ‘/wechat/callback’, async (req, res) => {
try {
const { code, state } = req.query;
if (!code) {
return res.status(400).json({ status: 'error', message: 'Code is required' });
}
// 验证 state 参数,防止 CSRF 攻击(从 Redis 获取)
console.log('Wechat callback received - checking state:', { code, state });
const storedState = await redisService.getWechatState(state);
console.log('Stored state from Redis:', storedState);
if (!state) {
console.error('Wechat callback: state parameter is empty');
return res.status(400).json({ status: 'error', message: 'State parameter is empty' });
}
if (!storedState) {
console.error('Wechat callback: state not found in Redis:', { state });
return res.status(400).json({ status: 'error', message: 'State parameter not found or expired' });
}
1 个赞