electron-vue-exam-single/electron/service/configService.js

162 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 导入数据库操作方法
import { getConfig, setConfig, getAllConfigs, getConfigById, deleteConfig } from '../db/config.js';
import argon2 from 'argon2';
// 原有接口保持不变
/**
* 服务层:获取配置项
* @param {string} key - 配置项键名
* @returns {Promise<{key: string, value: string} | null>}
*/
export async function fetchConfig(key) {
try {
return await getConfig(key);
} catch (error) {
console.error('服务层: 获取配置项失败', error);
throw error;
}
}
/**
* 服务层:获取所有配置项
* @returns {Promise<Array<{id: number, key: string, value: string, protected: number}>>}
*/
export async function fetchAllConfigs() {
try {
return await getAllConfigs();
} catch (error) {
console.error('服务层: 获取所有配置项失败', error);
throw error;
}
}
/**
* 服务层通过ID获取配置项
* @param {number} id - 配置项ID
* @returns {Promise<{id: number, key: string, value: string, protected: number} | null>}
*/
export async function fetchConfigById(id) {
try {
return await getConfigById(id);
} catch (error) {
console.error('服务层: 通过ID获取配置项失败', error);
throw error;
}
}
/**
* 服务层:保存配置项
* @param {string} key - 配置项键名
* @param {string} value - 配置项值
* @returns {Promise<void>}
*/
export async function saveConfig(key, value) {
try {
await setConfig(key, value);
} catch (error) {
console.error('服务层: 保存配置项失败', error);
throw error;
}
}
/**
* 服务层:删除配置项
* @param {number} id - 配置项ID
* @returns {Promise<void>}
*/
export async function removeConfig(id) {
try {
await deleteConfig(id);
} catch (error) {
console.error('服务层: 删除配置项失败', error);
throw error;
}
}
// 从 system.js 整合的接口
/**
* 获取系统配置并转为Map
* @returns {Promise<{[key: string]: string}>}
*/
export async function getSystemConfig() {
try {
const configs = await getAllConfigs();
const configMap = {};
configs.forEach(config => {
configMap[config.key] = config.value;
});
return configMap;
} catch (error) {
console.error('获取系统配置失败:', error);
throw error;
}
}
/**
* 批量更新系统配置
* @param {{[key: string]: string}} config - 配置对象
* @returns {Promise<boolean>}
*/
export async function updateSystemConfig(config) {
try {
for (const [key, value] of Object.entries(config)) {
await setConfig(key, value);
}
return true;
} catch (error) {
console.error('更新系统配置失败:', error);
throw error;
}
}
/**
* 增加题库版本号
* @returns {Promise<boolean>}
*/
export async function increaseQuestionBandVersion() {
try {
const currentVersion = await getConfig('question_bank_version');
const newVersion = currentVersion ? parseInt(currentVersion.value) + 1 : 1;
await setConfig('question_bank_version', newVersion.toString());
return true;
} catch (error) {
console.error('增加题库版本号失败:', error);
throw error;
}
}
// 从 auth.service.js 整合的接口
/**
* 管理员登录验证
* @param {string} password - 用户输入的密码
* @returns {Promise<{success: boolean, message: string}>}
*/
export async function verifyAdminPassword(password) {
try {
const config = await getConfig('admin_password');
if (!config || !config.value) {
return { success: false, message: '管理员密码未设置' };
}
const isMatch = await argon2.verify(config.value, password);
if (isMatch) {
return { success: true, message: '登录成功' };
} else {
return { success: false, message: '密码错误' };
}
} catch (error) {
console.error('验证管理员密码失败:', error);
return { success: false, message: '验证过程发生错误' };
}
}
/**
* 初始化认证相关的IPC处理程序
* @param {import('electron').IpcMain} ipcMain - IPC主进程实例
*/
export function initAuthIpc(ipcMain) {
// 管理员登录验证
ipcMain.handle('admin-login', async (event, credentials) => {
return await verifyAdminPassword(credentials.password);
});
}