修复Mac下的虾米验证、托盘问题
parent
edff998be9
commit
36d140d4bd
|
@ -10,7 +10,7 @@ global.lx_event.tray.on(TRAY_EVENT_NAME.destroy, () => {
|
|||
})
|
||||
|
||||
let tray
|
||||
function createTray() {
|
||||
const createTray = () => {
|
||||
if ((tray && !tray.isDestroyed()) || !global.appSetting.tray || !global.appSetting.tray.isShow) return
|
||||
|
||||
const iconPath = path.join(global.__static, 'images/tray', isWin ? 'trayTemplate.ico' : 'trayTemplate.png')
|
||||
|
@ -18,6 +18,25 @@ function createTray() {
|
|||
// 托盘
|
||||
tray = new Tray(iconPath)
|
||||
|
||||
tray.setToolTip('洛雪音乐助手')
|
||||
if (isWin) createMenu(tray)
|
||||
tray.setIgnoreDoubleClickEvents(true)
|
||||
tray.on('click', () => {
|
||||
const mainWindow = global.mainWindow
|
||||
if (!mainWindow) return
|
||||
mainWindow.isVisible()
|
||||
? mainWindow.focus()
|
||||
: mainWindow.show()
|
||||
})
|
||||
}
|
||||
|
||||
const destroyTray = () => {
|
||||
if (!tray) return
|
||||
tray.destroy()
|
||||
tray = null
|
||||
}
|
||||
|
||||
const createMenu = tray => {
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '退出',
|
||||
|
@ -27,26 +46,6 @@ function createTray() {
|
|||
},
|
||||
},
|
||||
])
|
||||
tray.setToolTip('洛雪音乐助手')
|
||||
tray.setContextMenu(contextMenu)
|
||||
tray.on('click', () => {
|
||||
const mainWindow = global.mainWindow
|
||||
if (!mainWindow) return
|
||||
mainWindow.isVisible()
|
||||
? mainWindow.focus()
|
||||
: mainWindow.show()
|
||||
})
|
||||
tray.on('double-click', () => {
|
||||
const mainWindow = global.mainWindow
|
||||
if (!mainWindow) return
|
||||
mainWindow.isVisible()
|
||||
? mainWindow.focus()
|
||||
: mainWindow.show()
|
||||
})
|
||||
}
|
||||
|
||||
function destroyTray() {
|
||||
if (!tray) return
|
||||
tray.destroy()
|
||||
tray = null
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
const { isMac } = require('../../../common/utils')
|
||||
|
||||
// mac下的 BrowserView 无法拖动验证栏,改用 BrowserWindow
|
||||
require(isMac ? './xm_verify_win' : './xm_verify_view')
|
|
@ -0,0 +1,4 @@
|
|||
const { isMac } = require('../../../common/utils')
|
||||
|
||||
// mac下的 BrowserView 无法拖动验证栏,改用 BrowserWindow
|
||||
require(isMac ? './xm_verify_win' : './xm_verify_view')
|
|
@ -1,9 +1,10 @@
|
|||
const { BrowserView } = require('electron')
|
||||
const { mainHandle } = require('../../common/ipc')
|
||||
const { getWindowSizeInfo } = require('../utils')
|
||||
const { isMac } = require('../../common/utils')
|
||||
const { mainHandle } = require('../../../common/ipc')
|
||||
const { getWindowSizeInfo } = require('../../utils')
|
||||
|
||||
let view
|
||||
let isActioned = false
|
||||
let rejectFn
|
||||
|
||||
const closeView = async() => {
|
||||
if (!view) return
|
||||
|
@ -21,44 +22,45 @@ mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
|
|||
view.destroy()
|
||||
}
|
||||
|
||||
let firstLoad = true
|
||||
rejectFn = reject
|
||||
|
||||
isActioned = false
|
||||
|
||||
view = new BrowserView({
|
||||
webPreferences: {
|
||||
enableRemoteModule: false,
|
||||
disableHtmlFullscreenWindowResize: true,
|
||||
},
|
||||
movable: false,
|
||||
})
|
||||
view.webContents.on('did-finish-load', () => {
|
||||
if (/punish\?/.test(view.webContents.getURL())) {
|
||||
if (isMac) { // 解决 MAC 首次加载页面无法拖动滑块的问题
|
||||
if (firstLoad) view.webContents.reload()
|
||||
firstLoad = false
|
||||
}
|
||||
return
|
||||
}
|
||||
if (/punish\?/.test(view.webContents.getURL())) return
|
||||
let ses = view.webContents.session
|
||||
ses.cookies.get({ name: 'x5sec' })
|
||||
.then(async([x5sec]) => {
|
||||
isActioned = true
|
||||
await closeView()
|
||||
if (!x5sec) return reject(new Error('get x5sec failed'))
|
||||
resolve(x5sec.value)
|
||||
}).catch(async err => {
|
||||
isActioned = true
|
||||
await closeView()
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
|
||||
global.mainWindow.setBrowserView(view)
|
||||
const windowSizeInfo = getWindowSizeInfo(global.appSetting)
|
||||
view.setBounds({ x: (windowSizeInfo.width - 360) / 2, y: ((windowSizeInfo.height - 320 + 52) / 2), width: 360, height: 320 })
|
||||
view.setBounds({ x: (windowSizeInfo.width - 380) / 2, y: ((windowSizeInfo.height - 320 + 52) / 2), width: 380, height: 320 })
|
||||
view.webContents.loadURL(url, {
|
||||
httpReferrer: 'https://www.xiami.com/',
|
||||
})
|
||||
view.webContents.openDevTools({ mode: 'undocked' })
|
||||
// view.webContents.openDevTools()
|
||||
}))
|
||||
|
||||
mainHandle('xm_verify_close', async() => {
|
||||
await closeView()
|
||||
if (!rejectFn) return
|
||||
if (!isActioned) rejectFn(new Error('canceled verify'))
|
||||
rejectFn = null
|
||||
})
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
const { BrowserWindow } = require('electron')
|
||||
const { mainHandle } = require('../../../common/ipc')
|
||||
const { getWindowSizeInfo } = require('../../utils')
|
||||
|
||||
let win
|
||||
|
||||
const closeWin = async() => {
|
||||
if (!win) return
|
||||
// await win.webContents.session.clearCache()
|
||||
// if (global.mainWindow) global.mainWindow.removeBrowserView(win)
|
||||
if (win.isDestroyed()) {
|
||||
win = null
|
||||
return
|
||||
}
|
||||
await win.webContents.session.clearStorageData()
|
||||
win.destroy()
|
||||
win = null
|
||||
}
|
||||
|
||||
mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
|
||||
if (!global.mainWindow) return reject(new Error('mainwindow is undefined'))
|
||||
if (win) win.destroy()
|
||||
|
||||
let isActioned = false
|
||||
|
||||
const mainWindowSizeInfo = global.mainWindow.getBounds()
|
||||
const windowSizeInfo = getWindowSizeInfo(global.appSetting)
|
||||
win = new BrowserWindow({
|
||||
parent: global.mainWindow,
|
||||
width: 460,
|
||||
height: 370,
|
||||
resizable: false,
|
||||
// transparent: true,
|
||||
x: mainWindowSizeInfo.x + (windowSizeInfo.width - 460) / 2,
|
||||
y: mainWindowSizeInfo.y + (windowSizeInfo.height - 320 + 52) / 2,
|
||||
minimizable: false,
|
||||
maximizable: false,
|
||||
// movable: false,
|
||||
// frame: false,
|
||||
// modal: true,
|
||||
webPreferences: {
|
||||
enableRemoteModule: false,
|
||||
disableHtmlFullscreenWindowResize: true,
|
||||
},
|
||||
})
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
if (/punish\?/.test(win.webContents.getURL())) return
|
||||
let ses = win.webContents.session
|
||||
ses.cookies.get({ name: 'x5sec' })
|
||||
.then(async([x5sec]) => {
|
||||
isActioned = true
|
||||
await closeWin()
|
||||
if (!x5sec) return reject(new Error('get x5sec failed'))
|
||||
resolve(x5sec.value)
|
||||
}).catch(async err => {
|
||||
isActioned = true
|
||||
await closeWin()
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
|
||||
win.webContents.loadURL(url, {
|
||||
httpReferrer: 'https://www.xiami.com/',
|
||||
})
|
||||
|
||||
win.on('closed', async() => {
|
||||
await closeWin()
|
||||
if (isActioned) return
|
||||
reject(new Error('canceled verify'))
|
||||
})
|
||||
|
||||
// win.webContents.openDevTools()
|
||||
}))
|
||||
|
||||
mainHandle('xm_verify_close', async() => {
|
||||
await closeWin()
|
||||
})
|
|
@ -90,7 +90,6 @@ export default {
|
|||
if (limit != null) this.limit = limit
|
||||
// http://newlyric.kuwo.cn/newlyric.lrc?62355680
|
||||
return this.musicSearch(str, page).then(result => {
|
||||
// console.log(result)
|
||||
if (!result) return this.search(str, page, { limit }, retryNum)
|
||||
if (result.code !== 'SUCCESS') return this.search(str, page, { limit }, retryNum)
|
||||
// const songResultData = result.data || { songs: [], total: 0 }
|
||||
|
@ -109,6 +108,6 @@ export default {
|
|||
total: this.total,
|
||||
source: 'xm',
|
||||
})
|
||||
}).catch(() => this.search(str, page, { limit }, retryNum))
|
||||
}).catch(err => err.message.includes('canceled verify') ? Promise.reject(err) : this.search(str, page, { limit }, retryNum))
|
||||
},
|
||||
}
|
||||
|
|
|
@ -98,9 +98,12 @@ export const xmRequest = (path, params = '') => {
|
|||
return wait(300).then(() => {
|
||||
return rendererInvoke('xm_verify_open', 'https:' + resp.body.url).then(x5sec => {
|
||||
handleSaveToken({ cookies: { x5sec } })
|
||||
// console.log(x5sec)
|
||||
console.log(x5sec)
|
||||
window.globalObj.xm.isShowVerify = false
|
||||
return Promise.reject(new Error('获取失败'))
|
||||
return Promise.reject(new Error('获取成功'))
|
||||
}).catch(err => {
|
||||
window.globalObj.xm.isShowVerify = false
|
||||
return Promise.reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -114,6 +117,7 @@ export const xmRequest = (path, params = '') => {
|
|||
|
||||
export const closeVerifyModal = async() => {
|
||||
if (!window.globalObj.xm.isShowVerify) return
|
||||
console.log('object')
|
||||
await rendererInvoke('xm_verify_close')
|
||||
window.globalObj.xm.isShowVerify = false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue