微信授权,扫码后会重定向到回调地址,但是扫码授权后没有跳转。
来源:1-1 学习本课程的收获以及案例展示(内附彩蛋)

zhangweb
2021-07-07
这是本人在使用electron时遇到的坑,查阅了各大网址都没有具体的解决方法,最后通过自己的理解在electron版本6.0.0以上版本中找到个实验性api解决了该问题。
项目要求是这样的,公司将前端项目打包好后,放在服务器上并挂好域名,然后将electron当做一个壳子(可以理解为浏览器)访问该项目,通过electron-forge打包成一个桌面级应用。打包后功能一切正常,唯一不正常的功能就是微信扫码登录,扫码后会重定向到回调地址中,这时改变了electron访问地址,这个项目就会刷新,然后重置所有状态,本人已经确认过当前是跳转后的地址,但是后续操作全部被reload掉了。(tip: 不知道是代码问题还是将微信嵌入项目中的这种方式有问题,正常访问微信官网扫码不会出现这种情况)
贴上当时的代码:
const {app, BrowserWindow} = require(‘electron’);
const electronHotKey = require(‘electron-localshortcut’); // 这里是注册的全局按键,是第三方插件,与问题无关
const path = require(‘path’);
let mainWindow;
function createWindow () {
mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, ‘preload.js’),
// webviewTag: true,
},
show: false,
});
mainWindow.setMenu(null);
mainWindow.maximize();
// mainWindow.loadFile(’./dist/index.html’);
mainWindow.loadURL(‘http://www.****.com/’);
new_win(mainWindow);
// Open the DevTools.
// mainWindow.webContents.openDevTools();
// hot key register
electronHotKey.register(mainWindow, ‘CmdOrCtrl+Shift+I’, () => {
mainWindow.webContents.openDevTools();
});
electronHotKey.register(mainWindow, ‘CmdOrCtrl+R’, () => {
mainWindow.webContents.reload();
});
mainWindow.on(‘closed’, function () {
mainWindow = null
});
}
function new_win(win) {
win.webContents.on(‘new-window’, (event, url) => {
let childWindow;
childWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {nodeIntegration: false}
});
new_win(childWindow);
childWindow.setMenu(null);
childWindow.loadURL(url);
event.preventDefault();
});
}
app.on(‘ready’, createWindow);
app.on(‘window-all-closed’, function () {
if (process.platform !== ‘darwin’) app.quit()
});
app.on(‘activate’, function () {
if (mainWindow === null) createWindow()
});
网上查阅了大量的资料(最好访问官网的文档,国内的文档都是以前的,没有更新过了,少了很多新版本的api),最好在github上翻阅electron源码和issue时,了解到我所使用的就是类iframe的形式,果然在最新版的文档里找到了该属性sanbox沙盒(tip:该属性为实验性属性,可能有未知的错误)。
webPreferences: {
preload: path.join(__dirname, ‘preload.js’),
sandbox: true,
// webviewTag: true,
},
加了上述该属性。mac上正常了。window 个别机器授权后重定向直接跳登录页面了。
1回答
-
同学你好 碰巧我接触过微信登录的一些内容 我记得 微信登录 iframe 有一个选项是在 iframe 中跳转,不直接跳转外侧 url,你可以试一下这种 文档在这里:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
请查看如截图的这一部分
这里有一个 self_redirect 的参数,可以控制在整体跳转还是内部跳转,可以试试。
012021-07-08
相似问题