@@ -2,16 +2,136 @@ const shortid = require('shortid')
22const xlsx = require ( 'xlsx' )
33const path = require ( 'path' )
44const electron = require ( 'electron' )
5+ const os = require ( 'os' )
56const app = electron . app
67const BrowserWindow = electron . BrowserWindow
78const dialog = electron . dialog
89const ipcMain = electron . ipcMain
9- let savePath = ''
10-
10+ const shell = electron . shell
11+ let savePath = '' ,
12+ downloadsPath = app . getPath ( 'downloads' ) ,
13+ platform = os . platform ( ) + '_' + os . arch ( ) ,
14+ version = app . getVersion ( ) ,
15+ updateWindow ,
16+ updateItem ,
17+ downloadsFullPath
18+ console . log ( version )
1119shortid . characters ( '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@' )
1220
1321module . exports = function ( mainWindow , backgroundWindow ) {
1422
23+ ipcMain . on ( 'will-download-handler' , ( ipcEvent , arg ) => {
24+ if ( ! updateWindow || updateWindow . isDestroyed ( ) ) {
25+ updateWindow = createUpdateWindow ( )
26+ }
27+ if ( ! updateWindow . isDestroyed ( ) ) {
28+ updateWindow . webContents . session . removeAllListeners ( )
29+ updateWindow . webContents . session . on ( 'will-download' , ( event , item , webContents ) => {
30+ updateItem = item
31+ downloadsFullPath = downloadsPath + item . getFilename ( )
32+ item . setSavePath ( downloadsFullPath )
33+ console . log ( 'getTotalBytes' , item . getTotalBytes ( ) )
34+ item . on ( 'updated' , ( event , state ) => {
35+ if ( state === 'interrupted' ) {
36+ console . log ( 'Download is interrupted but can be resumed' )
37+ } else if ( state === 'progressing' ) {
38+ if ( item . isPaused ( ) ) {
39+ console . log ( 'Download is paused' )
40+ } else {
41+ console . log ( `Received bytes: ${ item . getReceivedBytes ( ) } ` )
42+ }
43+ }
44+ if ( ! updateWindow . isDestroyed ( ) ) {
45+ updateWindow . webContents . send ( 'will-download-response' , {
46+ curReceivedBytes : item . getReceivedBytes ( ) ,
47+ totalBytes : item . getTotalBytes ( ) ,
48+ downloadStatus : state
49+ } )
50+ }
51+ } )
52+
53+ item . once ( 'done' , ( event , state ) => {
54+ if ( state === 'completed' ) {
55+ console . log ( 'Download successfully' )
56+ if ( ! shell . openItem ( downloadsFullPath ) ) {
57+ shell . showItemInFolder ( downloadsPath )
58+ }
59+ if ( ! updateWindow . isDestroyed ( ) ) {
60+ updateWindow . close ( )
61+ }
62+ } else {
63+ console . log ( `Download failed: ${ state } ` )
64+ }
65+ item . removeAllListeners ( )
66+ updateItem . removeAllListeners ( )
67+ updateItem = null
68+ item = null
69+ } )
70+ } )
71+ if ( process . env . NODE_ENV === 'development' ) {
72+ updateWindow . webContents . downloadURL ( arg . url )
73+ console . log ( arg . url )
74+ // updateWindow.webContents.downloadURL('https://jdc.jd.com/lab/xcel/xcel/XCel-darwin-x64.zip')
75+ } else {
76+ // updateWindow.webContents.downloadURL()
77+ }
78+ }
79+ } )
80+ function createUpdateWindow ( ) {
81+ var win = new BrowserWindow ( {
82+ height : 160 ,
83+ width : 550 ,
84+ title : '下载最新版的XCel' ,
85+ backgroundColor : "#f5f5f5"
86+ } )
87+ win . loadURL ( `file://${ __dirname } /dist/update/index.html` )
88+ win . once ( 'closed' , closeUpdateWindow )
89+ return win
90+ }
91+ function closeUpdateWindow ( event ) {
92+ console . log ( updateItem )
93+ if ( updateItem ) {
94+ updateItem . removeAllListeners ( )
95+ updateItem . cancel ( ) // cancel 后,DownloadItem 就是 null 了
96+ updateItem = null
97+ }
98+ }
99+ ipcMain . on ( 'update-switch' , ( event , arg ) => {
100+ if ( updateItem ) {
101+ let status = ''
102+ if ( updateItem . isPaused ( ) ) {
103+ if ( updateItem . canResume ( ) ) {
104+ updateItem . resume ( )
105+ status = '暂停'
106+ }
107+ } else {
108+ updateItem . pause ( )
109+ status = '继续'
110+ }
111+ event . sender . send ( 'update-switch-response' , {
112+ text : status
113+ } )
114+ }
115+ } )
116+
117+ ipcMain . on ( 'update-cancel' , ( event , arg ) => {
118+ if ( updateItem ) {
119+ updateItem . removeAllListeners ( )
120+ updateItem . cancel ( )
121+ updateWindow . close ( )
122+ updateItem = null
123+ }
124+ } )
125+
126+ ipcMain . on ( 'update-checkout' , ( event , arg ) => {
127+ if ( updateItem ) {
128+ updateItem . cancel ( )
129+ shell . openExternal ( 'https://xcel.aotu.io/' )
130+ updateWindow . close ( )
131+ updateItem = null
132+ }
133+ } )
134+
15135 ipcMain . on ( "readFile-response" , ( event , arg ) => {
16136 console . log ( "触发readFile-response" )
17137 mainWindow . webContents . send ( "readFile-response" , arg )
0 commit comments