整合作业
This commit is contained in:
@@ -68,16 +68,6 @@
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 校验 -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
|
||||
@@ -6,8 +6,7 @@ import lombok.Getter;
|
||||
public enum ResultCode {
|
||||
SUCCESS(200, "成功"),
|
||||
BAD_REQUEST(400, "参数错误"),
|
||||
UNAUTHORIZED(401, "未登录"),
|
||||
ERROR(500, "服务器内部错误");
|
||||
UNAUTHORIZED(401, "未登录");
|
||||
|
||||
private final Integer code;
|
||||
private final String message;
|
||||
|
||||
@@ -2,17 +2,16 @@ package com.ycjy.config;
|
||||
|
||||
import jakarta.servlet.MultipartConfigElement;
|
||||
import jakarta.servlet.ServletRegistration;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
@Override
|
||||
protected Class<?> @Nullable [] getRootConfigClasses() {
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> @Nullable [] getServletConfigClasses() {
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{WebAppConfig.class};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ package com.ycjy.controller;
|
||||
import com.ycjy.common.JwtUtil;
|
||||
import com.ycjy.common.Result;
|
||||
import com.ycjy.common.ResultCode;
|
||||
import com.ycjy.dao.DeleteLog;
|
||||
import com.ycjy.dao.Product;
|
||||
import com.ycjy.mapper.ProductMapper;
|
||||
import com.ycjy.service.ProductService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,16 +22,12 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class ProductController {
|
||||
@Autowired
|
||||
private ProductMapper getProdect;
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
@Autowired
|
||||
private com.ycjy.mapper.LogMapper logMapper;
|
||||
|
||||
@RequestMapping("/prodect")
|
||||
public Result<List<Product>> getProdect(){
|
||||
return Result.success(getProdect.getProdectAll());
|
||||
@GetMapping("/prodect")
|
||||
public Result<List<Product>> getProdect() {
|
||||
return Result.success(productService.getAll());
|
||||
}
|
||||
|
||||
@PostMapping("/product")
|
||||
@@ -40,62 +36,49 @@ public class ProductController {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/** 删除商品,必须登录;DELETE /api/product/{id} */
|
||||
@DeleteMapping("/product/{id}")
|
||||
public Result<Void> delete(@PathVariable("id") Integer id,
|
||||
public Result<Void> delete(@PathVariable Integer id,
|
||||
@RequestHeader(value = "Authorization", required = false) String auth) {
|
||||
if (auth == null || !auth.startsWith("Bearer ")) return Result.error(ResultCode.UNAUTHORIZED.getCode(), "请先登录");
|
||||
Claims claims = JwtUtil.parse(auth.substring(7));
|
||||
if (claims == null) return Result.error(ResultCode.UNAUTHORIZED.getCode(), "登录已失效");
|
||||
String username = claims.get("username", String.class);
|
||||
|
||||
boolean ok = productService.delete(id, username);
|
||||
if (!ok) return Result.error("商品不存在");
|
||||
if (!productService.delete(id, claims.get("username", String.class))) return Result.error("商品不存在");
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/logs")
|
||||
public Result<java.util.List<com.ycjy.dao.DeleteLog>> logs() {
|
||||
return Result.success(logMapper.getAll());
|
||||
public Result<List<DeleteLog>> logs() {
|
||||
return Result.success(productService.getLogs());
|
||||
}
|
||||
|
||||
/** 下载:价格 > 5000 必须登录,其余免登录 */
|
||||
@GetMapping("/product/{id}/download")
|
||||
public ResponseEntity<byte[]> download(@PathVariable("id") Integer id,
|
||||
public ResponseEntity<byte[]> download(@PathVariable Integer id,
|
||||
@RequestHeader(value = "Authorization", required = false) String auth) {
|
||||
Product product = productService.getById(id);
|
||||
if (product == null || product.getImage() == null) return ResponseEntity.notFound().build();
|
||||
|
||||
// 价格超过 5000 必须登录
|
||||
if (product.getPrice() > 5000) {
|
||||
if (auth == null || !auth.startsWith("Bearer ")) return ResponseEntity.status(401).build();
|
||||
Claims claims = JwtUtil.parse(auth.substring(7));
|
||||
if (claims == null) return ResponseEntity.status(401).build();
|
||||
if (JwtUtil.parse(auth.substring(7)) == null) return ResponseEntity.status(401).build();
|
||||
}
|
||||
|
||||
String fileName = product.getImage().replaceFirst("^/", "");
|
||||
byte[] bytes = null;
|
||||
|
||||
// 先找 classpath:/static/(老图片 /a.png)
|
||||
ClassPathResource resource = new ClassPathResource("static/" + fileName);
|
||||
byte[] bytes;
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource("static/" + fileName);
|
||||
if (resource.exists()) {
|
||||
try (InputStream in = resource.getInputStream()) {
|
||||
bytes = in.readAllBytes();
|
||||
}
|
||||
} else {
|
||||
// 再找磁盘 upload 目录(新上传图片 upload/xxx.png)
|
||||
java.io.File diskFile = new java.io.File(System.getProperty("user.dir"), fileName);
|
||||
if (diskFile.exists()) {
|
||||
bytes = java.nio.file.Files.readAllBytes(diskFile.toPath());
|
||||
}
|
||||
if (!diskFile.exists()) return ResponseEntity.notFound().build();
|
||||
bytes = java.nio.file.Files.readAllBytes(diskFile.toPath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).build();
|
||||
}
|
||||
|
||||
if (bytes == null) return ResponseEntity.notFound().build();
|
||||
|
||||
String downloadName = URLEncoder.encode(fileName.substring(fileName.lastIndexOf('/') + 1), StandardCharsets.UTF_8);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ycjy.controller;
|
||||
|
||||
import com.ycjy.common.Result;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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();
|
||||
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
String fileName = UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
|
||||
File dir = new File(System.getProperty("user.dir") + "/upload");
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
file.transferTo(new File(dir, fileName));
|
||||
|
||||
Map<String, String> data = new HashMap<>();
|
||||
data.put("url", "/upload/" + fileName);
|
||||
return Result.success(data);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ycjy.service;
|
||||
|
||||
import com.ycjy.dao.DeleteLog;
|
||||
import com.ycjy.dao.Product;
|
||||
import com.ycjy.mapper.LogMapper;
|
||||
import com.ycjy.mapper.ProductMapper;
|
||||
@@ -11,27 +12,30 @@ import java.util.List;
|
||||
@Service
|
||||
public class ProductService {
|
||||
@Autowired
|
||||
private ProductMapper getProdect;
|
||||
private ProductMapper productMapper;
|
||||
@Autowired
|
||||
private LogMapper logMapper;
|
||||
|
||||
public List<Product> getProduct(){
|
||||
return getProdect.getProdectAll();
|
||||
public List<Product> getAll() {
|
||||
return productMapper.getProdectAll();
|
||||
}
|
||||
|
||||
public boolean add(Product product) {
|
||||
return getProdect.insert(product) > 0;
|
||||
return productMapper.insert(product) > 0;
|
||||
}
|
||||
|
||||
public boolean delete(Integer id, String username) {
|
||||
Product product = getProdect.getById(id);
|
||||
if (product == null) return false;
|
||||
getProdect.deleteById(id);
|
||||
if (productMapper.getById(id) == null) return false;
|
||||
productMapper.deleteById(id);
|
||||
logMapper.insert(username, String.valueOf(id));
|
||||
return true;
|
||||
}
|
||||
|
||||
public Product getById(Integer id) {
|
||||
return getProdect.getById(id);
|
||||
return productMapper.getById(id);
|
||||
}
|
||||
|
||||
public List<DeleteLog> getLogs() {
|
||||
return logMapper.getAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,9 @@ public class UserService {
|
||||
private UserMapper userMapper;
|
||||
|
||||
public User login(String username, String password) {
|
||||
System.out.println(">>> 入参 username=[" + username + "] password=[" + password + "]");
|
||||
if (username == null || password == null) return null;
|
||||
User user = userMapper.getUser(username);
|
||||
System.out.println(">>> 查询结果 user=" + user);
|
||||
if (user == null) return null;
|
||||
System.out.println(">>> DB密码=[" + user.getPassword() + "]");
|
||||
if (!password.equals(user.getPassword())) return null;
|
||||
if (user == null || !password.equals(user.getPassword())) return null;
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user