151 lines
3.2 KiB
JavaScript
151 lines
3.2 KiB
JavaScript
// pages/gpa/gpa.js
|
||
const { calculateGPA, showSuccess, showError } = require('../../utils/util.js')
|
||
const learningTracker = require('../../utils/learningTracker.js')
|
||
|
||
Page({
|
||
data: {
|
||
courses: [],
|
||
courseName: '',
|
||
courseScore: '',
|
||
courseCredit: '',
|
||
totalGPA: 0,
|
||
totalCredits: 0
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadCourses()
|
||
},
|
||
|
||
onShow() {
|
||
// 开始跟踪学习时间
|
||
learningTracker.onPageShow('tools')
|
||
},
|
||
|
||
onHide() {
|
||
// 停止跟踪学习时间
|
||
learningTracker.onPageHide()
|
||
},
|
||
|
||
onUnload() {
|
||
// 记录学习时长
|
||
learningTracker.onPageUnload()
|
||
},
|
||
|
||
// 加载已保存的课程
|
||
loadCourses() {
|
||
const courses = wx.getStorageSync('gpaCourses') || []
|
||
this.setData({ courses })
|
||
this.calculateTotal()
|
||
},
|
||
|
||
// 课程名称输入
|
||
onNameInput(e) {
|
||
this.setData({ courseName: e.detail.value })
|
||
},
|
||
|
||
// 成绩输入
|
||
onScoreInput(e) {
|
||
this.setData({ courseScore: e.detail.value })
|
||
},
|
||
|
||
// 学分输入
|
||
onCreditInput(e) {
|
||
this.setData({ courseCredit: e.detail.value })
|
||
},
|
||
|
||
// 添加课程
|
||
onAddCourse() {
|
||
const { courseName, courseScore, courseCredit, courses } = this.data
|
||
|
||
if (!courseName.trim()) {
|
||
showError('请输入课程名称')
|
||
return
|
||
}
|
||
|
||
const score = parseFloat(courseScore)
|
||
const credit = parseFloat(courseCredit)
|
||
|
||
if (isNaN(score) || score < 0 || score > 100) {
|
||
showError('请输入有效的成绩(0-100)')
|
||
return
|
||
}
|
||
|
||
if (isNaN(credit) || credit <= 0) {
|
||
showError('请输入有效的学分')
|
||
return
|
||
}
|
||
|
||
const newCourse = {
|
||
id: Date.now(),
|
||
name: courseName.trim(),
|
||
score: score,
|
||
credit: credit
|
||
}
|
||
|
||
courses.push(newCourse)
|
||
wx.setStorageSync('gpaCourses', courses)
|
||
|
||
this.setData({
|
||
courses,
|
||
courseName: '',
|
||
courseScore: '',
|
||
courseCredit: ''
|
||
})
|
||
|
||
this.calculateTotal()
|
||
showSuccess('添加成功')
|
||
},
|
||
|
||
// 删除课程
|
||
onDeleteCourse(e) {
|
||
const { id } = e.currentTarget.dataset
|
||
|
||
wx.showModal({
|
||
title: '确认删除',
|
||
content: '确定要删除这门课程吗?',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
let { courses } = this.data
|
||
courses = courses.filter(course => course.id !== id)
|
||
wx.setStorageSync('gpaCourses', courses)
|
||
|
||
this.setData({ courses })
|
||
this.calculateTotal()
|
||
showSuccess('删除成功')
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 计算总GPA
|
||
calculateTotal() {
|
||
const { courses } = this.data
|
||
const gpa = calculateGPA(courses)
|
||
const totalCredits = courses.reduce((sum, course) => sum + course.credit, 0)
|
||
|
||
this.setData({
|
||
totalGPA: gpa,
|
||
totalCredits: totalCredits
|
||
})
|
||
},
|
||
|
||
// 清空所有
|
||
onClearAll() {
|
||
wx.showModal({
|
||
title: '确认清空',
|
||
content: '确定要清空所有课程吗?',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.removeStorageSync('gpaCourses')
|
||
this.setData({
|
||
courses: [],
|
||
totalGPA: 0,
|
||
totalCredits: 0
|
||
})
|
||
showSuccess('已清空')
|
||
}
|
||
}
|
||
})
|
||
}
|
||
})
|