From 8128f76a4e582e5487e9179feba4009b0a482bb7 Mon Sep 17 00:00:00 2001
From: flying_pig <1839976096@qq.com>
Date: Mon, 14 Oct 2024 16:41:23 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A8=8D=E5=BE=AE=E4=BF=AE=E6=94=B9?=
=?UTF-8?q?=E4=B8=80=E4=B8=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/misc.xml | 4 ++
.idea/workspace.xml | 12 ++++
backend/pom.xml | 6 +-
.../init/InitializeController.java | 29 ++++++++
...InitializeDispatcherServletController.java | 17 -----
.../service/impl/StudentServiceImpl.java | 38 +++-------
.../service/impl/TeacherServiceImpl.java | 12 ++--
.../StudentServiceImplTest.java | 72 -------------------
frontend/package-lock.json | 4 +-
9 files changed, 69 insertions(+), 125 deletions(-)
create mode 100644 .idea/misc.xml
create mode 100644 backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeController.java
delete mode 100644 backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeDispatcherServletController.java
delete mode 100644 backend/src/test/java/com/flyingpig/kclassrollcall/StudentServiceImplTest.java
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..c1c2a99
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 42e64c7..f365e64 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,5 +1,8 @@
+
+
+
@@ -17,6 +20,11 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "node.js.selected.package.tslint": "(autodetect)",
+ "nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true"
}
}]]>
@@ -29,7 +37,11 @@
1727955603618
+
+
+
+
\ No newline at end of file
diff --git a/backend/pom.xml b/backend/pom.xml
index 6ac14a8..db6b7d8 100644
--- a/backend/pom.xml
+++ b/backend/pom.xml
@@ -21,7 +21,11 @@
org.springframework.boot
spring-boot-starter-web
-
+
+ org.springframework.security
+ spring-security-core
+ 5.8.0
+
org.springframework.boot
spring-boot-starter-mail
diff --git a/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeController.java b/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeController.java
new file mode 100644
index 0000000..2a59c16
--- /dev/null
+++ b/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeController.java
@@ -0,0 +1,29 @@
+package com.flyingpig.kclassrollcall.init;
+
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.sql.DataSource;
+
+
+@Slf4j(topic = "Initialize")
+@RestController
+@RequiredArgsConstructor
+public class InitializeController {
+
+ private final DataSource dataSource;
+
+ @GetMapping("/initialize/application")
+ public void initializeDispatcherServletAndDataSource() {
+ log.info("开始初始化DispatcherServlet和数据源");
+ try {
+ log.info("初始化DispatcherServlet和数据源成功");
+ dataSource.getConnection().createStatement().execute("SELECT 1");
+ } catch (Exception e) {
+ log.error("初始化DispatcherServlet和数据源失败", e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeDispatcherServletController.java b/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeDispatcherServletController.java
deleted file mode 100644
index 8279b2b..0000000
--- a/backend/src/main/java/com/flyingpig/kclassrollcall/init/InitializeDispatcherServletController.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.flyingpig.kclassrollcall.init;
-
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-
-@Slf4j(topic = "Initialize DispatcherServlet")
-@RestController
-public final class InitializeDispatcherServletController {
-
- @GetMapping("/initialize/dispatcher-servlet")
- public void initializeDispatcherServlet() {
- log.info("Initialized the dispatcherServlet to improve the first response time of the interface...");
- }
-}
\ No newline at end of file
diff --git a/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/StudentServiceImpl.java b/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/StudentServiceImpl.java
index 3c00f31..cefa9fd 100644
--- a/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/StudentServiceImpl.java
+++ b/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/StudentServiceImpl.java
@@ -52,48 +52,32 @@ public class StudentServiceImpl extends ServiceImpl impl
if (!mode.equals(RollCallMode.EQUAL)) {
return Result.success(rollBackStudentBaseScore());
} else {
- return Result.success(selectStudentByTeacherId().get(new Random().nextInt(count()) - 1));
+ List students = this.baseMapper.selectList(new LambdaQueryWrapper()
+ .eq(Student::getTeacherId, UserContext.getUser()));
+ return Result.success(students.get(new Random().nextInt(students.size())));
}
}
private List selectStudentByTeacherId() {
- List cachedList = listCacheUtil.safeGetWithLock(
- RedisConstant.STUDENT_LIST_KEY + UserContext.getUser(),
- StudentInfoInCache.class, // 使用具体的 StudentInfoInCache.class 作为类型
+ return listCacheUtil.safeGetWithLock(
+ RedisConstant.STUDENT_LIST_KEY + UserContext.getUser(), // Redis Key
+ Student.class,
() -> {
- // 查询学生的逻辑,转换为 StudentInfoInCache 列表
+ // 查询学生ID的逻辑
return this.baseMapper.selectList(new LambdaQueryWrapper()
- .eq(Student::getTeacherId, UserContext.getUser())).stream()
- .map(student -> new StudentInfoInCache(student.getId(), student.getName(), student.getNo(), student.getMajor()))
- .collect(Collectors.toList());
+ .eq(Student::getTeacherId, UserContext.getUser()));
},
- 30L,
+ 30L, // 缓存时间
TimeUnit.MINUTES
);
- List students = new ArrayList<>();
- for (StudentInfoInCache studentInfo : cachedList) {
- Student newStudent = new Student();
- BeanUtil.copyProperties(studentInfo, newStudent);
- newStudent.setScore(stringCacheUtil.safeGetWithLock(
- RedisConstant.STUDENT_SCORE_KEY + studentInfo.getId(),
- Double.class, // 传入正确的类型
- () -> {
- return this.getBaseMapper().selectById(studentInfo.getId()).getScore();
- },
- 30L,
- TimeUnit.MINUTES
- ));
- newStudent.setTeacherId(Long.parseLong(UserContext.getUser()));
- students.add(newStudent);
- }
- return students;
}
private Student rollBackStudentBaseScore() {
// 获取符合条件的学生列表
- List students = selectStudentByTeacherId();
+ List students = this.baseMapper.selectList(new LambdaQueryWrapper()
+ .eq(Student::getTeacherId, UserContext.getUser()));
// 计算权重
double totalWeight = 0;
Map weightMap = new HashMap<>();
diff --git a/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/TeacherServiceImpl.java b/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/TeacherServiceImpl.java
index 47bf1c0..5f2d07a 100644
--- a/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/TeacherServiceImpl.java
+++ b/backend/src/main/java/com/flyingpig/kclassrollcall/service/impl/TeacherServiceImpl.java
@@ -11,6 +11,7 @@ import com.flyingpig.kclassrollcall.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyingpig.kclassrollcall.util.JwtUtil;
import org.apache.catalina.User;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
/**
@@ -28,11 +29,10 @@ public class TeacherServiceImpl extends ServiceImpl impl
@Override
public Result login(LoginReq loginReq) {
Teacher teacher = this.getOne(new LambdaQueryWrapper()
- .eq(Teacher::getUsername, loginReq.getUsername())
- .eq(Teacher::getPassword, loginReq.getPassword()));
- if(teacher == null){
+ .eq(Teacher::getUsername, loginReq.getUsername()));
+ if (teacher == null || !new BCryptPasswordEncoder().matches(loginReq.getPassword(), teacher.getPassword())) {
return Result.error("账号或密码错误");
- }else {
+ } else {
return Result.success(new LoginResp(teacher.getName(), JwtUtil.createJWT(teacher.getId().toString())));
}
}
@@ -40,9 +40,9 @@ public class TeacherServiceImpl extends ServiceImpl impl
@Override
public Result register(Teacher teacher) {
try {
- save(teacher);
+ save(teacher.setPassword(new BCryptPasswordEncoder().encode(teacher.getPassword())));
return Result.success("注册成功");
- }catch (Exception e){
+ } catch (Exception e) {
log.error(e.getMessage());
return Result.error("注册失败,可能存在用户名已被注册等问题");
}
diff --git a/backend/src/test/java/com/flyingpig/kclassrollcall/StudentServiceImplTest.java b/backend/src/test/java/com/flyingpig/kclassrollcall/StudentServiceImplTest.java
deleted file mode 100644
index c85ea3e..0000000
--- a/backend/src/test/java/com/flyingpig/kclassrollcall/StudentServiceImplTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-package com.flyingpig.kclassrollcall;
-
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.flyingpig.kclassrollcall.common.RedisConstant;
-import com.flyingpig.kclassrollcall.common.Result;
-import com.flyingpig.kclassrollcall.common.RollCallMode;
-import com.flyingpig.kclassrollcall.dto.resp.StudentInfoInCache;
-import com.flyingpig.kclassrollcall.entity.Student;
-import com.flyingpig.kclassrollcall.filter.UserContext;
-import com.flyingpig.kclassrollcall.mapper.StudentMapper;
-import com.flyingpig.kclassrollcall.service.impl.StudentServiceImpl;
-import com.flyingpig.kclassrollcall.util.cache.ListCacheUtil;
-import com.flyingpig.kclassrollcall.util.cache.StringCacheUtil;
-import com.google.common.cache.CacheLoader;
-import org.apache.poi.ss.formula.functions.T;
-import org.junit.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
-
-import static org.junit.Assert.*;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.*;
-import static org.mockito.ArgumentMatchers.any;
-
-@ExtendWith(MockitoExtension.class)
-public class StudentServiceImplTest {
-
- @Mock
- private StudentMapper studentMapper;
-
- @Mock
- private ListCacheUtil listCacheUtil;
-
- @Mock
- private StringCacheUtil stringCacheUtil;
-
-
-
- @InjectMocks
- private StudentServiceImpl studentService;
-
- @Test
- public
-
- void testRollCall() {
- // 模拟依赖方法
- // 调用依赖方法方法
- Mockito.doReturn(Result.success()).when(studentService).rollCall(RollCallMode.EQUAL);
- List mockStudents = Arrays.asList(
- new Student(1L, "Alice", 1, "Math", 50.0, 100L),
- new Student(2L, "Bob", 2, "Science", 0.0, 100L)
- );
- when(studentMapper.selectList(new LambdaQueryWrapper().eq(Student::getTeacherId, any()))).thenReturn(mockStudents);
- // 模拟 rollCall 调用
- Result result = studentService.rollCall(RollCallMode.EQUAL);
-
- // 验证返回结果
- assertNotNull(result);
- assertTrue(mockStudents.contains(result.getData())); // 随机选择一个学生
- }
-}
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 26364b7..9971929 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -1,11 +1,11 @@
{
- "name": "k-class-roll-call",
+ "name": "k-calss-rall-call",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
- "name": "k-class-roll-call",
+ "name": "k-calss-rall-call",
"version": "0.1.0",
"dependencies": {
"axios": "^1.5.1",