261024
This commit is contained in:
@@ -30,9 +30,17 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@@ -46,6 +54,11 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/api/file")
|
||||
public class FileController extends BaseController {
|
||||
private static final int FACE_MIN_WIDTH = 480;
|
||||
private static final int FACE_MIN_HEIGHT = 640;
|
||||
private static final int FACE_MAX_WIDTH = 1080;
|
||||
private static final int FACE_MAX_HEIGHT = 1920;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties config;
|
||||
@Resource
|
||||
@@ -172,6 +185,40 @@ public class FileController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:file:upload')")
|
||||
@OperationLog
|
||||
@ApiOperation("上传人脸图片并返回base64")
|
||||
@PostMapping("/upload/face")
|
||||
public ApiResult<String> uploadFace(@RequestParam MultipartFile file) {
|
||||
try {
|
||||
if (file == null || file.isEmpty()) {
|
||||
return fail("上传失败,文件为空", null);
|
||||
}
|
||||
byte[] originalBytes = file.getBytes();
|
||||
BufferedImage image;
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBytes)) {
|
||||
image = ImageIO.read(inputStream);
|
||||
}
|
||||
if (image == null) {
|
||||
return fail("上传失败,无法解析图片", null);
|
||||
}
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
boolean inRange = width >= FACE_MIN_WIDTH && height >= FACE_MIN_HEIGHT
|
||||
&& width <= FACE_MAX_WIDTH && height <= FACE_MAX_HEIGHT;
|
||||
boolean isJpeg = isJpegImage(file.getContentType(), file.getOriginalFilename());
|
||||
if (inRange && isJpeg) {
|
||||
return success(Base64.getEncoder().encodeToString(originalBytes));
|
||||
}
|
||||
BufferedImage converted = resizeToRange(toRgbImage(image), width, height);
|
||||
byte[] jpegBytes = toJpegBytes(converted);
|
||||
return success(Base64.getEncoder().encodeToString(jpegBytes));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return fail("上传失败", null).setError(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:file:list')")
|
||||
@OperationLog
|
||||
@ApiOperation("根据id查询文件")
|
||||
@@ -350,4 +397,61 @@ public class FileController extends BaseController {
|
||||
return fail("修改失败");
|
||||
}
|
||||
|
||||
private static boolean isJpegImage(String contentType, String fileName) {
|
||||
if (contentType != null) {
|
||||
String ct = contentType.toLowerCase();
|
||||
if (ct.contains("jpeg") || ct.contains("jpg")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (fileName != null) {
|
||||
String lower = fileName.toLowerCase();
|
||||
return lower.endsWith(".jpg") || lower.endsWith(".jpeg");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static BufferedImage toRgbImage(BufferedImage source) {
|
||||
if (source.getType() == BufferedImage.TYPE_INT_RGB) {
|
||||
return source;
|
||||
}
|
||||
BufferedImage rgb = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = rgb.createGraphics();
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(0, 0, rgb.getWidth(), rgb.getHeight());
|
||||
g2d.drawImage(source, 0, 0, null);
|
||||
g2d.dispose();
|
||||
return rgb;
|
||||
}
|
||||
|
||||
private static BufferedImage resizeToRange(BufferedImage image, int width, int height) {
|
||||
double scale = 1.0;
|
||||
if (width < FACE_MIN_WIDTH || height < FACE_MIN_HEIGHT) {
|
||||
scale = Math.max(FACE_MIN_WIDTH / (double) width, FACE_MIN_HEIGHT / (double) height);
|
||||
} else if (width > FACE_MAX_WIDTH || height > FACE_MAX_HEIGHT) {
|
||||
scale = Math.min(FACE_MAX_WIDTH / (double) width, FACE_MAX_HEIGHT / (double) height);
|
||||
} else {
|
||||
return image;
|
||||
}
|
||||
int targetWidth = Math.max(1, (int) Math.round(width * scale));
|
||||
int targetHeight = Math.max(1, (int) Math.round(height * scale));
|
||||
BufferedImage resized = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = resized.createGraphics();
|
||||
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.drawImage(image, 0, 0, targetWidth, targetHeight, null);
|
||||
g2d.dispose();
|
||||
return resized;
|
||||
}
|
||||
|
||||
private static byte[] toJpegBytes(BufferedImage image) throws Exception {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
if (!ImageIO.write(image, "jpg", outputStream)) {
|
||||
throw new IllegalStateException("jpeg encode fail");
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,9 +4,6 @@ import cn.hutool.core.util.IdUtil;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.shop.entity.UserBalanceLog;
|
||||
import com.gxwebsoft.shop.service.RechargeOrderService;
|
||||
import com.gxwebsoft.shop.entity.RechargeOrder;
|
||||
import com.gxwebsoft.shop.param.RechargeOrderParam;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.core.web.BatchParam;
|
||||
|
||||
@@ -8,14 +8,18 @@ import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.core.web.BatchParam;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.BalanceConstants.BALANCE_ADMIN;
|
||||
|
||||
/**
|
||||
* 用户余额变动明细表控制器
|
||||
*
|
||||
@@ -28,6 +32,8 @@ import java.util.List;
|
||||
public class UserBalanceLogController extends BaseController {
|
||||
@Resource
|
||||
private UserBalanceLogService userBalanceLogService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@ApiOperation("分页查询用户余额变动明细表")
|
||||
@GetMapping("/page")
|
||||
@@ -54,10 +60,33 @@ public class UserBalanceLogController extends BaseController {
|
||||
@ApiOperation("添加用户余额变动明细表")
|
||||
@PostMapping()
|
||||
public ApiResult<?> save(@RequestBody UserBalanceLog userBalanceLog) {
|
||||
// 记录当前登录用户id
|
||||
if (userBalanceLog.getUserId() == null) {
|
||||
return fail("用户ID不能为空");
|
||||
}
|
||||
if (userBalanceLog.getMoney() == null || userBalanceLog.getMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return fail("充值金额必须大于0");
|
||||
}
|
||||
User user = userService.addBalanceInner(userBalanceLog.getUserId(), userBalanceLog.getMoney());
|
||||
if (user == null) {
|
||||
return fail("用户不存在");
|
||||
}
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
userBalanceLog.setUserId(loginUser.getUserId());
|
||||
if (userBalanceLog.getScene() == null) {
|
||||
userBalanceLog.setScene(BALANCE_ADMIN);
|
||||
}
|
||||
userBalanceLog.setBalance(user.getBalance());
|
||||
if (loginUser != null && userBalanceLog.getAdminId() == null) {
|
||||
userBalanceLog.setAdminId(loginUser.getUserId());
|
||||
}
|
||||
if ((userBalanceLog.getComments() == null || userBalanceLog.getComments().trim().isEmpty())
|
||||
&& userBalanceLog.getRemark() != null) {
|
||||
userBalanceLog.setComments(userBalanceLog.getRemark());
|
||||
}
|
||||
if (userBalanceLog.getStatus() == null) {
|
||||
userBalanceLog.setStatus(0);
|
||||
}
|
||||
if (userBalanceLog.getDeleted() == null) {
|
||||
userBalanceLog.setDeleted(0);
|
||||
}
|
||||
if (userBalanceLogService.save(userBalanceLog)) {
|
||||
return success("添加成功");
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.gxwebsoft.shop.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.gxwebsoft.shop.entity.RechargeOrder;
|
||||
import com.gxwebsoft.shop.param.RechargeOrderParam;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.gxwebsoft.shop.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.shop.entity.RechargeOrder;
|
||||
import com.gxwebsoft.shop.param.RechargeOrderParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package com.gxwebsoft.shop.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.gxwebsoft.shop.mapper.RechargeOrderMapper;
|
||||
import com.gxwebsoft.shop.service.RechargeOrderService;
|
||||
import com.gxwebsoft.shop.entity.RechargeOrder;
|
||||
import com.gxwebsoft.shop.param.RechargeOrderParam;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
Reference in New Issue
Block a user