diff --git a/src/views/WelcomeView.vue b/src/views/WelcomeView.vue index be6641a..d3d7ded 100644 --- a/src/views/WelcomeView.vue +++ b/src/views/WelcomeView.vue @@ -135,299 +135,201 @@ export default { isLoading: false // 添加加载状态 } }, - // 在mounted方法中添加详细日志 mounted () { - console.log('======= WelcomeView 组件挂载开始 =======') - console.log('当前时间:', new Date().toLocaleString()) - - // 先打印electronAPI对象的可用性 - console.log('window.electronAPI 对象:', window.electronAPI ? '可用' : '不可用') - if (window.electronAPI) { - console.log('electronAPI 方法列表:', Object.keys(window.electronAPI).join(', ')) - console.log('checkDatabaseInitialized 方法:', typeof window.electronAPI.checkDatabaseInitialized) - console.log('initializeDatabase 方法:', typeof window.electronAPI.initializeDatabase) - console.log('checkUserDbExists 方法:', typeof window.electronAPI.checkUserDbExists) - console.log('initializeUserDatabaseSilently 方法:', typeof window.electronAPI.initializeUserDatabaseSilently) - } - + console.log('mounted: 调用this.checkDatabaseStatus方法') this.checkDatabaseStatus() + console.log('mounted: 调用this.checkDatabaseStatus方法完成') this.checkAndInitializeUserDb() - - console.log('======= WelcomeView 组件挂载完成 =======') }, + methods: { + // 检查并静默初始化用户数据库 + async checkAndInitializeUserDb() { + try { + console.log('检查user.db是否存在...') + const userDbExists = await window.electronAPI.checkUserDbExists() + console.log('user.db存在状态:', userDbExists) - // 更新checkAndInitializeUserDb方法,添加详细日志 - async checkAndInitializeUserDb () { - try { - console.log('\n----- 开始检查和初始化用户数据库 -----') + if (!userDbExists) { + console.log('user.db不存在,开始静默初始化...') + await window.electronAPI.initializeUserDatabaseSilently() + console.log('user.db静默初始化完成') + } + } catch (error) { + console.error('检查或初始化user.db失败:', error) + // 这里不显示错误信息,因为是静默初始化 + } + }, - // 检查electronAPI是否存在 - if (!window.electronAPI) { - console.error('错误: window.electronAPI 不存在') + async checkDatabaseStatus () { + try { + console.log('组件挂载 - 开始检查数据库初始化状态') + const initialized = await window.electronAPI.checkDatabaseInitialized() + console.log('组件挂载 - 数据库初始化状态检查完成:', initialized) + this.isDatabaseInitialized = initialized + } catch (error) { + console.error('检查数据库初始化状态失败:', error) + Message.error('检查数据库初始化状态失败,请重试') + } + }, + + async initializeDatabase () { + try { + console.log('初始化数据库 - 开始') + this.isInitializing = true + Message.info('开始初始化数据库...') + + const result = await window.electronAPI.initializeDatabase() + console.log('初始化数据库 - 结果:', result) + + // 修复:同时处理布尔值true和带success属性的对象 + if (result === true || (result && result.success)) { + Message.success('数据库初始化成功!') + console.log('初始化数据库 - 成功,更新初始化状态') + this.isDatabaseInitialized = true + } else { + const errorMessage = result && result.error ? result.error : '未知错误' + Message.error(`数据库初始化失败: ${errorMessage}`) + console.error('初始化数据库 - 失败:', errorMessage) + } + } catch (error) { + console.error('数据库初始化失败:', error) + Message.error(`数据库初始化失败: ${error.message || '未知错误'}`) + } finally { + console.log('初始化数据库 - 结束') + this.isInitializing = false + } + }, + + async handleExamineeLogin () { + console.log('考生登录 - 开始', { + examineeIdCard: this.examineeIdCard, + examineeAdmissionTicket: this.examineeAdmissionTicket + }) + + // 清除首尾空格 + const idCard = this.examineeIdCard.trim() + const admissionTicket = this.examineeAdmissionTicket.trim() + + // 前端验证 + if (!idCard || !admissionTicket) { + console.warn('考生登录 - 验证失败: 身份证号和准考证号不能为空') + Message.error('请输入身份证号和准考证号') return } - console.log('检查user.db是否存在...') - const startTime = Date.now() + // 设置加载状态 + this.isLoading = true - // 添加Promise超时处理 - const userDbExists = await Promise.race([ - window.electronAPI.checkUserDbExists(), - new Promise((_, reject) => setTimeout(() => reject(new Error('checkUserDbExists 超时')), 5000)) - ]) + try { + // 调用登录API - 使用正确的参数格式 + const result = await window.electronAPI.userLogin({ idCard, admissionTicket }) + console.log('考生登录 - 结果:', result) - const duration = Date.now() - startTime - console.log(`user.db存在状态: ${userDbExists} (耗时: ${duration}ms)`) - - if (!userDbExists) { - console.log('user.db不存在,开始静默初始化...') - const initStartTime = Date.now() - - // 添加Promise超时处理 - await Promise.race([ - window.electronAPI.initializeUserDatabaseSilently(), - new Promise((_, reject) => setTimeout(() => reject(new Error('initializeUserDatabaseSilently 超时')), 10000)) - ]) - - const initDuration = Date.now() - initStartTime - console.log(`user.db静默初始化完成 (耗时: ${initDuration}ms)`) + if (result && result.id) { + console.log('考生登录 - 成功', result) + // 保存用户信息到store + this.$store.commit('setExaminee', result) + Message.success('登录成功') + // 跳转到考生首页 + this.$router.push('/examinee/home') + } else { + console.warn('考生登录 - 失败:', result) + Message.error(result && result.error ? result.error : '登录失败,请检查身份证号和准考证号') + } + } catch (error) { + console.error('考生登录 - 异常:', error) + Message.error(`登录失败: ${error.message || '未知错误'}`) + } finally { + // 无论成功失败,都关闭加载状态 + this.isLoading = false } + }, - console.log('----- 检查和初始化用户数据库完成 -----') - } catch (error) { - console.error('检查或初始化user.db失败:', error) - console.error('错误详情:', error.stack || error.message) - // 这里不显示错误信息,因为是静默初始化 - } - }, + // 修改handleAdminLogin方法 + async handleAdminLogin () { + console.log('管理员登录 - 开始', { passwordLength: this.adminPassword.length }) - // 更新checkDatabaseStatus方法,添加详细日志 - async checkDatabaseStatus () { - try { - console.log('\n----- 开始检查数据库初始化状态 -----') - - // 检查electronAPI是否存在 - if (!window.electronAPI) { - console.error('错误: window.electronAPI 不存在') + // 前端密码验证 + const passwordError = this.validateAdminPassword(this.adminPassword) + if (passwordError) { + console.warn('管理员登录 - 验证失败:', passwordError) + const errorElement = document.getElementById('admin-error-message') + if (errorElement) { + errorElement.textContent = passwordError + errorElement.style.display = 'block' + } return } - console.log('组件挂载 - 开始检查数据库初始化状态') - const startTime = Date.now() - - // 添加Promise超时处理 - const initialized = await Promise.race([ - window.electronAPI.checkDatabaseInitialized(), - new Promise((_, reject) => setTimeout(() => reject(new Error('checkDatabaseInitialized 超时')), 5000)) - ]) - - const duration = Date.now() - startTime - console.log(`组件挂载 - 数据库初始化状态检查完成: ${initialized} (耗时: ${duration}ms)`) - - // 详细记录返回值类型 - console.log('初始化状态返回值类型:', typeof initialized) - if (initialized === undefined) { - console.warn('警告: 初始化状态返回undefined,可能是IPC调用失败') - } - - this.isDatabaseInitialized = initialized === true - console.log(`更新isDatabaseInitialized状态为: ${this.isDatabaseInitialized}`) - - console.log('----- 检查数据库初始化状态完成 -----') - } catch (error) { - console.error('检查数据库初始化状态失败:', error) - console.error('错误详情:', error.stack || error.message) - Message.error('检查数据库初始化状态失败,请重试') - } - }, - - // 更新initializeDatabase方法,添加详细日志 - async initializeDatabase () { - try { - console.log('\n----- 开始数据库初始化流程 -----') - - // 检查electronAPI是否存在 - if (!window.electronAPI) { - console.error('错误: window.electronAPI 不存在') - Message.error('初始化失败: 无法连接到后端服务') - return - } - - console.log('初始化数据库 - 开始') - this.isInitializing = true - console.log('设置isInitializing状态为:', this.isInitializing) - Message.info('开始初始化数据库...') - - const startTime = Date.now() - - // 添加Promise超时处理 - const result = await Promise.race([ - window.electronAPI.initializeDatabase(), - new Promise((_, reject) => setTimeout(() => reject(new Error('initializeDatabase 超时')), 30000)) - ]) - - const duration = Date.now() - startTime - console.log(`初始化数据库 - 结果:`, result, `(耗时: ${duration}ms)`) - - // 详细记录返回值类型和结构 - console.log('初始化结果类型:', typeof result) - if (typeof result === 'object') { - console.log('初始化结果结构:', Object.keys(result || {}).join(', ')) - } - - // 修复:同时处理布尔值true和带success属性的对象 - if (result === true || (result && result.success)) { - Message.success('数据库初始化成功!') - console.log('初始化数据库 - 成功,更新初始化状态') - this.isDatabaseInitialized = true - console.log('更新isDatabaseInitialized状态为:', this.isDatabaseInitialized) - } else { - const errorMessage = result && result.error ? result.error : '未知错误' - Message.error(`数据库初始化失败: ${errorMessage}`) - console.error('初始化数据库 - 失败:', errorMessage) - } - - console.log('----- 数据库初始化流程完成 -----') - } catch (error) { - console.error('数据库初始化失败:', error) - console.error('错误详情:', error.stack || error.message) - Message.error(`数据库初始化失败: ${error.message || '未知错误'}`) - } finally { - console.log('初始化数据库 - 结束') - this.isInitializing = false - console.log('设置isInitializing状态为:', this.isInitializing) - } - }, - - async handleExamineeLogin () { - console.log('考生登录 - 开始', { - examineeIdCard: this.examineeIdCard, - examineeAdmissionTicket: this.examineeAdmissionTicket - }) - - // 清除首尾空格 - const idCard = this.examineeIdCard.trim() - const admissionTicket = this.examineeAdmissionTicket.trim() - - // 前端验证 - if (!idCard || !admissionTicket) { - console.warn('考生登录 - 验证失败: 身份证号和准考证号不能为空') - Message.error('请输入身份证号和准考证号') - return - } - - // 设置加载状态 - this.isLoading = true - - try { - // 调用登录API - 使用正确的参数格式 - const result = await window.electronAPI.userLogin({ idCard, admissionTicket }) - console.log('考生登录 - 结果:', result) - - if (result && result.id) { - console.log('考生登录 - 成功', result) - // 保存用户信息到store - this.$store.commit('setExaminee', result) - Message.success('登录成功') - // 跳转到考生首页 - this.$router.push('/examinee/home') - } else { - console.warn('考生登录 - 失败:', result) - Message.error(result && result.error ? result.error : '登录失败,请检查身份证号和准考证号') - } - } catch (error) { - console.error('考生登录 - 异常:', error) - Message.error(`登录失败: ${error.message || '未知错误'}`) - } finally { - // 无论成功失败,都关闭加载状态 - this.isLoading = false - } - }, - - // 修改handleAdminLogin方法 - async handleAdminLogin () { - console.log('管理员登录 - 开始', { passwordLength: this.adminPassword.length }) - - // 前端密码验证 - const passwordError = this.validateAdminPassword(this.adminPassword) - if (passwordError) { - console.warn('管理员登录 - 验证失败:', passwordError) + // 清除之前的错误信息 const errorElement = document.getElementById('admin-error-message') if (errorElement) { - errorElement.textContent = passwordError - errorElement.style.display = 'block' + errorElement.style.display = 'none' } - return - } - // 清除之前的错误信息 - const errorElement = document.getElementById('admin-error-message') - if (errorElement) { - errorElement.style.display = 'none' - } + try { + console.log('管理员登录 - 调用主进程登录方法') + // 使用新的adminLogin方法 + const result = await window.electronAPI.adminLogin({ + username: 'admin', + password: this.adminPassword + }) + console.log('管理员登录 - 登录结果:', result) - try { - console.log('管理员登录 - 调用主进程登录方法') - // 使用新的adminLogin方法 - const result = await window.electronAPI.adminLogin({ - username: 'admin', - password: this.adminPassword + if (result && result.success) { + console.log('管理员登录 - 成功,更新store状态并跳转') + // 使用新的setAdmin mutation + this.$store.commit('setAdmin', { username: 'admin' }) + Message.success('登录成功') + this.$router.push('/admin/home') + } else { + const errorMessage = result && result.message ? result.message : '登录失败' + console.warn('管理员登录 - 失败:', errorMessage) + Message.error(errorMessage) + } + } catch (error) { + console.error('管理员登录 - 异常:', error) + Message.error(`登录异常: ${error.message || '未知错误'}`) + } + }, + + validateAdminPassword (password) { + // 检查密码是否为空 + if (!password) { + return '请输入管理员密码' + } + // 检查密码长度 + if (password.length < 4 || password.length > 32) { + return '密码长度必须在4-32个字符之间' + } + // 检查密码是否只包含英文大小写和数字 + const regex = /^[A-Za-z0-9]+$/ + if (!regex.test(password)) { + return '密码只能包含英文大小写字母和数字' + } + return null + }, + // 修改exitExam方法 + async exitExam () { + this.$confirm( + '确定要退出考试吗?', + '退出确认', + { + confirmButtonText: '确定', + cancelButtonText: '取消', + type: 'warning' + } + ).then(() => { + // 完全清除store中的数据 + this.$store.commit('clearUser') + // 重定向到欢迎页 + this.$router.push('/') + }).catch(() => { + // 用户取消操作 + console.log('用户取消退出') }) - console.log('管理员登录 - 登录结果:', result) - - if (result && result.success) { - console.log('管理员登录 - 成功,更新store状态并跳转') - // 使用新的setAdmin mutation - this.$store.commit('setAdmin', { username: 'admin' }) - Message.success('登录成功') - this.$router.push('/admin/home') - } else { - const errorMessage = result && result.message ? result.message : '登录失败' - console.warn('管理员登录 - 失败:', errorMessage) - Message.error(errorMessage) - } - } catch (error) { - console.error('管理员登录 - 异常:', error) - Message.error(`登录异常: ${error.message || '未知错误'}`) - } - }, - - validateAdminPassword (password) { - // 检查密码是否为空 - if (!password) { - return '请输入管理员密码' - } - // 检查密码长度 - if (password.length < 4 || password.length > 32) { - return '密码长度必须在4-32个字符之间' - } - // 检查密码是否只包含英文大小写和数字 - const regex = /^[A-Za-z0-9]+$/ - if (!regex.test(password)) { - return '密码只能包含英文大小写字母和数字' - } - return null - }, - // 修改exitExam方法 - async exitExam () { - this.$confirm( - '确定要退出考试吗?', - '退出确认', - { - confirmButtonText: '确定', - cancelButtonText: '取消', - type: 'warning' - } - ).then(() => { - // 完全清除store中的数据 - this.$store.commit('clearUser') - // 重定向到欢迎页 - this.$router.push('/') - }).catch(() => { - // 用户取消操作 - console.log('用户取消退出') - }) - }, + }, + } }