新增文献接口开发

This commit is contained in:
dww
2025-12-26 10:53:15 +08:00
parent bad425f10c
commit 3b4f009abd
9 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package com.genepioneer.common;
import lombok.Data;
@Data
public class Result<T> {
private Integer code; // 200成功500失败
private String msg;
private T data;
// 成功返回
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>();
result.setCode(200);
result.setMsg("操作成功");
result.setData(data);
return result;
}
// 失败返回
public static <T> Result<T> error(String msg) {
Result<T> result = new Result<>();
result.setCode(500);
result.setMsg(msg);
return result;
}
}

View File

@@ -0,0 +1,7 @@
package com.genepioneer.common.exception;
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,49 @@
package com.genepioneer.common.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class JwtUtil {
// 1. 自动生成符合256位要求的密钥固定密钥重启项目也不会变
private static final SecretKey SECRET_KEY = Keys.secretKeyFor(io.jsonwebtoken.SignatureAlgorithm.HS256);
// 2. JWT过期时间1小时3600000毫秒
private static final Long EXPIRE = 3600000L;
/**
* 生成JWT令牌
*/
public String generateToken(Long userId) {
// 构建负载存储用户ID
Map<String, Object> claims = new HashMap<>();
claims.put("userId", userId);
// 生成令牌使用自动生成的256位密钥
return Jwts.builder()
.setClaims(claims)
.setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
.signWith(SECRET_KEY)
.compact();
}
/**
* 解析JWT令牌获取用户ID
*/
public Long getUserIdFromToken(String token) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(SECRET_KEY)
.build()
.parseClaimsJws(token)
.getBody();
return claims.get("userId", Long.class);
}
}

View File

@@ -0,0 +1,9 @@
package com.genepioneer.common.utils;
import org.apache.commons.codec.digest.DigestUtils;
public class MD5Util {
public static String encrypt(String str) {
return DigestUtils.md5Hex(str);
}
}

View File

@@ -0,0 +1,41 @@
package com.genepioneer.controller;
import com.genepioneer.common.Result;
import com.genepioneer.common.utils.JwtUtil;
import com.genepioneer.dto.LoginDTO;
import com.genepioneer.entity.User;
import com.genepioneer.service.impl.UserService;
import com.genepioneer.vo.LoginVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@Resource
private UserService userService;
@Resource
private JwtUtil jwtUtil;
@PostMapping("/login")
public Result<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
// 调用service登录自动适配用户名/手机号)
User user = userService.login(loginDTO);
// 生成JWT令牌
String token = jwtUtil.generateToken(user.getId());
// 封装返回结果
LoginVO loginVO = new LoginVO();
loginVO.setUserId(user.getId());
loginVO.setUsername(user.getUsername()); // 仍返回用户名(前端展示用)
loginVO.setToken(token);
return Result.success(loginVO);
}
}

View File

@@ -0,0 +1,13 @@
package com.genepioneer.dto;
import lombok.Data;
import jakarta.validation.constraints.NotBlank;
@Data
public class LoginDTO {
@NotBlank(message = "登录账号不能为空(用户名/手机号)")
private String loginAccount; // 统一接收:用户名 或 手机号
@NotBlank(message = "密码不能为空")
private String password;
}

View File

@@ -0,0 +1,14 @@
package com.genepioneer.entity;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private Long id; // 主键ID
private String username;// 账号
private String password;// 加密后的密码
private String role; // 角色如admin
private String phone; // 新增:手机号码
private Date createTime;// 创建时间
}

View File

@@ -0,0 +1,13 @@
package com.genepioneer.mapper;
import com.genepioneer.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
// 新增:根据用户名/手机号查询用户
@Select("SELECT id, username, password, role, phone, create_time AS createTime " +
"FROM user WHERE username = #{loginAccount} OR phone = #{loginAccount}")
User selectByUsernameOrPhone(String loginAccount);
}

View File

@@ -0,0 +1,10 @@
package com.genepioneer.vo;
import lombok.Data;
@Data
public class LoginVO {
private Long userId;
private String username;
private String token;
}