-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
183 lines (167 loc) · 4.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const electron = require("electron");
const url = require("url");
const path = require("path");
const axios = require("axios");
const api = require("./api.json");
process.env.NODE_ENV = "develop";
const { app, BrowserWindow, Menu, ipcMain } = electron;
let week = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
let mainWindow;
// Listen for the app to be ready
app.on("ready", function () {
//set the google api key to enable the geolocation api
process.env.GOOGLE_API_KEY = api.GOOGLE_API;
//create window
mainWindow = new BrowserWindow({
width: 600,
height: 400,
webPreferences: {
nodeIntegration: true,
},
});
mainWindow.hide();
//load html into window
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "src/mainWindow.html"),
protocol: "file:",
slashes: true,
})
);
//build menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
//insert menu
Menu.setApplicationMenu(mainMenu);
});
function checkTime(i) {
if (i < 10) {
i = "0" + i;
} // add zero in front of numbers < 10
return i;
}
//catch location:get
ipcMain.on("location:get", function (e, lat, lon) {
axios.defaults.headers.post["Content-Type"] = "application/json";
axios
.post(
`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lon}&key=${api.GOOGLE_API}&language=zh-TW`
)
.then((response) => {
//console.log(response.data);
mainWindow.webContents.send("name:put", response.data);
})
.catch((error) => {
console.error(error);
});
33;
axios.defaults.headers.post["Content-Type"] = "application/json";
axios
.post(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${api.WEATHER_API}&units=metric&lang=zh_tw`
)
.then((response) => {
//console.log(response.data);
mainWindow.webContents.send("weather:put", response.data);
timezone = parseInt(response.data.timezone / 3600);
index = Date().indexOf("+");
current_timezone = parseInt(Date().substring(index + 1, index + 5)) / 100;
setInterval(() => {
var today = new Date();
var extra_day = 0;
var time_h = today.getHours() + (timezone - current_timezone);
if (time_h >= 24) {
time_h -= 24;
extra_day++;
} else if (time_h < 0) {
time_h += 24;
extra_day--;
}
var h = ("0" + time_h).slice(-2);
var m = today.getMinutes();
var s = today.getSeconds();
var time_week = today.getDay() + extra_day;
var weekday =
week[
time_week > 6
? time_week - 7
: time_week < 0
? time_week + 7
: time_week
];
m = checkTime(m);
s = checkTime(s);
mainWindow.webContents.send("time:put", `${weekday} ${h}:${m}:${s}`);
}, 500);
})
.catch((error) => {
console.error(error);
});
axios.defaults.headers.post["Content-Type"] = "application/json";
axios
.post(
`https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=fnd&lang=tc`
)
.then((response) => {
//console.log(response.data);
mainWindow.webContents.send("weather_future:put", response.data);
mainWindow.show();
})
.catch((error) => {
console.error(error);
});
});
//menu
const mainMenuTemplate = [
{
label: "File",
submenu: [
{
label: "Add Item",
},
{
label: "Clear Items",
},
{
label: "Quit",
accelerator: process.platform == "darwin" ? "Command+Q" : "Ctrl+Q",
click() {
app.quit();
},
},
],
},
];
if (process.platform == "darwin") {
mainMenuTemplate.unshift({ label: "WEATHER-APP" });
}
if (process.env.NODE_ENV !== "production") {
mainMenuTemplate.push({
label: "Dev Tools",
submenu: [
{
label: "Dev Tools",
accelerator: process.platform == "darwin" ? "Command+I" : "Ctrl+I",
click(item, focusedWindow) {
focusedWindow.openDevTools({ mode: "undocked" });
},
},
{
role: "reload",
},
{
role: "copy",
},
{
role: "paste",
},
],
});
}