整合作业
This commit is contained in:
@@ -71,24 +71,22 @@ public class WebAppConfig implements WebMvcConfigurer {
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
// 配置静态资源路径
|
||||
@Autowired
|
||||
private AuthInterceptor authInterceptor;
|
||||
|
||||
// 注册拦截器
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authInterceptor)
|
||||
.addPathPatterns("/api/**")
|
||||
.excludePathPatterns("/api/login", "/api/upload", "/api/prodect", "/api/logs");
|
||||
registry.addInterceptor(authInterceptor)// 注册拦截器
|
||||
.addPathPatterns("/api/**")// 拦截所有以/api开头的请求
|
||||
.excludePathPatterns("/api/login", "/api/upload", "/api/prodect", "/api/logs");// 排除不需要拦截的请求
|
||||
}
|
||||
|
||||
// 配置静态资源路径
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/**")
|
||||
.addResourceLocations("classpath:/static/");
|
||||
// 固定目录:图片放在部署目录之外,重新编译/部署也不会丢;用 toUri 编码中文路径
|
||||
String uploadLocation = Path.of(UploadConstants.UPLOAD_DIR).toUri().toString();
|
||||
if (!uploadLocation.endsWith("/")) uploadLocation += "/";
|
||||
String uploadLocation = Path.of(UploadConstants.UPLOAD_DIR).toUri().toString();// 获取上传路径
|
||||
if (!uploadLocation.endsWith("/")) uploadLocation += "/";// 确保路径以/结尾
|
||||
registry.addResourceHandler("/upload/**")
|
||||
.addResourceLocations(uploadLocation);
|
||||
}
|
||||
|
||||
@@ -64,22 +64,23 @@ public class ProductController {
|
||||
Product product = productService.getById(id);
|
||||
if (product == null || product.getImage() == null) return ResponseEntity.notFound().build();
|
||||
|
||||
String imagePath = product.getImage().replaceFirst("^/", "");
|
||||
String fileName = imagePath.substring(imagePath.lastIndexOf('/') + 1);
|
||||
String imagePath = product.getImage().replaceFirst("^/", "");// 获取图片路径, 移除 /
|
||||
String fileName = imagePath.substring(imagePath.lastIndexOf('/') + 1); // 获取文件名
|
||||
byte[] bytes;
|
||||
try {
|
||||
java.io.File diskFile = new java.io.File(UploadConstants.UPLOAD_DIR, fileName);
|
||||
if (!diskFile.exists()) return ResponseEntity.notFound().build();
|
||||
bytes = java.nio.file.Files.readAllBytes(diskFile.toPath());
|
||||
java.io.File diskFile = new java.io.File(UploadConstants.UPLOAD_DIR, fileName);// 获取文件
|
||||
if (!diskFile.exists()) return ResponseEntity.notFound().build();// 检查文件是否存在
|
||||
bytes = java.nio.file.Files.readAllBytes(diskFile.toPath());// 读取文件内容
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).build();
|
||||
}
|
||||
|
||||
String downloadName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
headers.setContentDispositionFormData("attachment", downloadName);
|
||||
headers.setContentLength(bytes.length);
|
||||
// 设置响应头并返回
|
||||
String downloadName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);// 对文件名进行url编码
|
||||
HttpHeaders headers = new HttpHeaders(); // 创建Http响应头
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);// 设置响应类型为二进制流
|
||||
headers.setContentDispositionFormData("attachment", downloadName);// 设置响应头为附件
|
||||
headers.setContentLength(bytes.length);// 告诉浏览器文件大小
|
||||
return ResponseEntity.ok().headers(headers).body(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import java.util.UUID;
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class UploadController {
|
||||
|
||||
@PostMapping("/upload")
|
||||
public Result<Map<String, String>> upload(@RequestParam("file") MultipartFile file) throws Exception {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
|
||||
@@ -14,20 +14,19 @@ import java.util.Map;
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) {
|
||||
User user = userService.login(dto.getUsername(), dto.getPassword());
|
||||
if (user == null) return Result.error("用户名或密码错误");
|
||||
String token = JwtUtil.generate(user.getId(), user.getUsername());
|
||||
User user = userService.login(dto.getUsername(), dto.getPassword());// 尝试获取用户数据
|
||||
if (user == null) return Result.error("用户名或密码错误");// 前置条件不返回用户数据则代表有问题
|
||||
String token = JwtUtil.generate(user.getId(), user.getUsername()); // 根据信息生成token
|
||||
user.setPassword(null);
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("token", token);
|
||||
data.put("user", user);
|
||||
return Result.success(data);
|
||||
data.put("token", token);// token塞进去
|
||||
data.put("user", user);//user塞进去
|
||||
return Result.success(data);// 登录凭证给用户
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,6 @@ import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
public interface LogMapper {
|
||||
int insert(@Param("username") String username, @Param("pid") String pid);
|
||||
List<DeleteLog> getAll();
|
||||
int insert(@Param("username") String username, @Param("pid") String pid); // 插入日志
|
||||
List<DeleteLog> getAll(); // 获取所有日志
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
public interface ProductMapper {
|
||||
List<Product> getProdectAll();
|
||||
List<Product> getProdectAll(); // 获取所有商品数据
|
||||
Product getById(@Param("id") Integer id);
|
||||
int deleteById(@Param("id") Integer id);
|
||||
int insert(Product product);
|
||||
|
||||
@@ -5,5 +5,5 @@ import com.ycjy.dao.User;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
public interface UserMapper {
|
||||
User getUser(@Param("username") String username);
|
||||
User getUser(@Param("username") String username); // 获取用户信息
|
||||
}
|
||||
|
||||
@@ -16,19 +16,19 @@ public class ProductService {
|
||||
@Autowired
|
||||
private LogMapper logMapper;
|
||||
|
||||
// 获取所有商品信息
|
||||
public List<Product> getAll() {
|
||||
return productMapper.getProdectAll();
|
||||
}
|
||||
|
||||
public boolean add(Product product) {
|
||||
return productMapper.insert(product) > 0;
|
||||
}
|
||||
// 添加
|
||||
public boolean add(Product product) {return productMapper.insert(product) > 0;}
|
||||
|
||||
// 删除
|
||||
public boolean delete(Integer id, String username) {
|
||||
if (productMapper.getById(id) == null) return false;
|
||||
productMapper.deleteById(id);
|
||||
logMapper.insert(username, String.valueOf(id));
|
||||
if (productMapper.getById(id) == null) return false;// 商品不存在
|
||||
productMapper.deleteById(id);// 删除商品
|
||||
logMapper.insert(username, String.valueOf(id));// 记录日志
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -37,12 +37,8 @@ public class ProductService {
|
||||
}
|
||||
|
||||
// 获取日志
|
||||
public List<DeleteLog> getLogs() {
|
||||
return logMapper.getAll();
|
||||
}
|
||||
public List<DeleteLog> getLogs() {return logMapper.getAll();}
|
||||
|
||||
// 修改
|
||||
public boolean update(Product product) {
|
||||
return productMapper.update(product.getId(),product.getName(),product.getP_desc(),product.getPrice(),product.getImage()) > 0;
|
||||
}
|
||||
public boolean update(Product product) {return productMapper.update(product.getId(),product.getName(),product.getP_desc(),product.getPrice(),product.getImage()) > 0;}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ public class UserService {
|
||||
private UserMapper userMapper;
|
||||
|
||||
public User login(String username, String password) {
|
||||
if (username == null || password == null) return null;
|
||||
User user = userMapper.getUser(username);
|
||||
if (user == null || !password.equals(user.getPassword())) return null;
|
||||
return user;
|
||||
if (username == null || password == null) return null; // 检查用户名和密码是否为空
|
||||
User user = userMapper.getUser(username); // 查询用户信息
|
||||
if (user == null || !password.equals(user.getPassword())) return null; // 判断用户名是否存在且密码是否匹配
|
||||
return user;// 返回用户信息
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ycjy.mapper.LogMapper">
|
||||
<!-- 插入日志 -->
|
||||
<insert id="insert">
|
||||
INSERT INTO demo_log (username, pid, delete_date)
|
||||
VALUES (#{username}, #{pid}, now())
|
||||
</insert>
|
||||
<!-- 获取日志数据 -->
|
||||
<select id="getAll" resultType="com.ycjy.dao.DeleteLog">
|
||||
SELECT id, username, pid, delete_date
|
||||
FROM demo_log
|
||||
|
||||
@@ -3,21 +3,26 @@
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ycjy.mapper.ProductMapper">
|
||||
<!-- 获取所有商品 -->
|
||||
<select id="getProdectAll" resultType="com.ycjy.dao.Product">
|
||||
SELECT * FROM demo_product;
|
||||
</select>
|
||||
<!-- 获取数据通过id -->
|
||||
<select id="getById" resultType="com.ycjy.dao.Product">
|
||||
SELECT id, name, p_desc, price, image
|
||||
FROM demo_product
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
<!-- 根据id删除商品 -->
|
||||
<delete id="deleteById">
|
||||
DELETE FROM demo_product WHERE id = #{id}
|
||||
</delete>
|
||||
<!-- 插入商品 -->
|
||||
<insert id="insert" parameterType="com.ycjy.dao.Product">
|
||||
INSERT INTO demo_product (name, p_desc, price, image)
|
||||
VALUES (#{name}, #{p_desc}, #{price}, #{image})
|
||||
</insert>
|
||||
<!-- 更新商品 -->
|
||||
<update id="update" parameterType="com.ycjy.dao.Product">
|
||||
UPDATE demo_product SET name = #{name}, p_desc = #{p_desc}, price = #{price}, image = #{image}
|
||||
WHERE id = #{id};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ycjy.mapper.UserMapper">
|
||||
<!-- 获取用户 -->
|
||||
<select id="getUser" resultType="com.ycjy.dao.User">
|
||||
SELECT id,
|
||||
username,
|
||||
|
||||
Reference in New Issue
Block a user