Skip to content

Commit

Permalink
功能增强与bug修复:
Browse files Browse the repository at this point in the history
- 新增了管理员权限运行的功能,以确保应用在Windows上作为管理员启动,提升应用的安全性和稳定性。
- 修复了天气数据获取逻辑,改进了高德地图天气接口的调用方式,提高了天气数据获取的效率和准确性。
- 优化了时间同步逻辑,集成了NTP时间同步功能,确保系统时间的准确性,这对于需要高时间精度的应用场景尤为重要。
- 增加了版本号自动生成逻辑,让用户随时了解应用的当前版本,提升了用户体验。
- 在设置界面增加了同步时间的按钮,方便用户手动同步系统时间,增强了易用性。
- 修复了设置界面的视频音量调节范围,从0.1精确到0.01,提升了用户调节音量的灵活性。

本次更新涉及了主功能窗口的启动逻辑、时间同步、天气数据获取、版本管理以及用户界面的多个方面,旨在提升应用的综合性能和用户体验。
  • Loading branch information
Yunmoan committed Sep 13, 2024
1 parent 4ea3e6f commit fd0db31
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 97 deletions.
49 changes: 47 additions & 2 deletions core/systemInfo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const os = require('os');
const axios = require('axios');

const sudo = require('sudo-prompt'); // 新增
let networkCurrentTime = null;
let networkTime = false;
let timeServer = 'https://api.zyghit.cn/time-aligned?type=unix'; // 使用的时间服务接口


const { exec } = require('child_process');
const options = {
name: 'SchoolClock'
};



// 通过HTTP GET请求同步网络时间的函数
async function syncNetworkTime() {
try {
Expand Down Expand Up @@ -37,6 +44,44 @@ function initializeTimeSync(config) {
}
}

function initializeNTPTimeSync(config) {
const ntpServer = config.ntpServer || 'pool.ntp.org'; // 默认 NTP 服务器

if (process.platform === 'win32') {
// Windows 系统时间同步
const command = `w32tm /config /manualpeerlist:"${ntpServer}" /syncfromflags:manual /reliable:YES /update && w32tm /resync`;
sudo.exec(command, options, (error, stdout, stderr) => {
if (error) {
console.error(`Windows NTP Time synchronization failed: ${error}`);
return;
}
console.log('Windows Time synchronization successful:', stdout);
});
} else if (process.platform === 'linux') {
// Linux 系统时间同步
const command = `timedatectl set-ntp true && ntpdate ${ntpServer}`;
sudo.exec(command, options, (error, stdout, stderr) => {
if (error) {
console.error(`Linux NTP Time synchronization failed: ${error}`);
return;
}
console.log('Linux Time synchronization successful:', stdout);
});
} else if (process.platform === 'darwin') {
// macOS 系统时间同步
const command = `sntp -sS ${ntpServer}`;
sudo.exec(command, options, (error, stdout, stderr) => {
if (error) {
console.error(`macOS NTP Time synchronization failed: ${error}`);
return;
}
console.log('macOS Time synchronization successful:', stdout);
});
} else {
console.log('Unsupported operating system');
}
}

function getSystemInfo() {
let currentTime = networkCurrentTime ? networkCurrentTime.toLocaleString() : new Date().toLocaleString();
let timeStatus = networkCurrentTime ? 'Network' : 'Local';
Expand All @@ -57,4 +102,4 @@ function getSystemInfo() {
};
}

module.exports = { getSystemInfo, initializeTimeSync };
module.exports = { getSystemInfo, initializeTimeSync,initializeNTPTimeSync };
6 changes: 0 additions & 6 deletions core/version.json

This file was deleted.

5 changes: 5 additions & 0 deletions core/versionConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"major": 1,
"minor": 0,
"patch": 5
}
39 changes: 39 additions & 0 deletions core/versionGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

function getCommitHash() {
try {
return execSync('git rev-parse --short HEAD').toString().trim();
} catch (error) {
console.error('无法获取 Git 提交号', error);
return 'unknown';
}
}

function getCurrentTime() {
const now = new Date();
const year = String(now.getFullYear()).slice(-2);
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
return `${year}${month}${day}_${hours}${minutes}`;
}

function generateVersion() {
const versionConfigPath = path.join(__dirname, 'versionConfig.json');
if (!fs.existsSync(versionConfigPath)) {
console.error('versionConfig.json 文件不存在');
return null;
}

const versionConfig = JSON.parse(fs.readFileSync(versionConfigPath, 'utf-8'));
const { major, minor, patch } = versionConfig;
const commitHash = getCommitHash();
const timestamp = getCurrentTime();

return `build.${major}.${minor}.${patch}_${timestamp}_${commitHash}`;
}

module.exports = { generateVersion };
22 changes: 21 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const path = require('path');
const configManager = require('./core/configManager');
const { createWindow, closeWindow } = require('./core/windowManager');
const { convertWebmToMp4, deleteFile } = require('./core/videoConverter');
const { getSystemInfo, initializeTimeSync } = require('./core/systemInfo');
const { getSystemInfo, initializeTimeSync,initializeNTPTimeSync } = require('./core/systemInfo');
const { checkNetworkStatus, checkURLStatus } = require('./core/networkStatus');
const { generateVersion } = require('./core/versionGenerator');
const { exec } = require('child_process');

// 获取用户数据目录
const userDataDir = app.getPath('userData');
Expand All @@ -18,13 +20,27 @@ if (!fs.existsSync(dataDir)) {

app.on('ready', () => {
global.global_config = configManager.readConfig();
const version = generateVersion();
global.appVersion = version; // 将版本号存储在全局变量中

// 输出版本号(可选)
console.log(`Version: ${version}`);

// 初始化并同步时间
initializeTimeSync(global.global_config);

initializeNTPTimeSync(global.global_config);
console.log('sync ntp time')

// 配置文件路径
const configFilePath = path.join(app.getPath('userData'), 'data', 'config.json');

ipcMain.handle('get-app-version', () => {
return global.appVersion;
});

setInterval(()=>initializeNTPTimeSync(global.global_config), 3600000);

// 处理打开配置文件目录的请求
ipcMain.on('open-config-folder', (event) => {
const configDir = path.dirname(configFilePath);
Expand All @@ -49,6 +65,10 @@ app.on('ready', () => {
shell.openExternal(url);
});

ipcMain.on('sync-time', () => {
initializeNTPTimeSync(global.global_config)
});

ipcMain.on('upload-webm', (event, { data }) => {
const inputPath = path.join(dataDir, 'input.webm');
const outputPath = path.join(dataDir, 'play.mp4');
Expand Down
Loading

0 comments on commit fd0db31

Please sign in to comment.