|
@@ -9,22 +9,22 @@ import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.hfln.portal.common.constant.redis.RedisCacheConstant;
|
|
|
+import com.hfln.portal.common.dto.data.oss.OssFileDTO;
|
|
|
import com.hfln.portal.common.dto.data.user.UserDto;
|
|
|
import com.hfln.portal.common.request.user.*;
|
|
|
import com.hfln.portal.common.response.user.UserInfoWxRes;
|
|
|
+import com.hfln.portal.domain.customer.OssBusiType;
|
|
|
import com.hfln.portal.domain.customer.util.PasswordUtil;
|
|
|
import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
import com.hfln.portal.domain.gateway.UserGateway;
|
|
|
-import com.hfln.portal.infrastructure.po.DevGroup;
|
|
|
-import com.hfln.portal.infrastructure.po.DevInfo;
|
|
|
-import com.hfln.portal.infrastructure.po.DevShare;
|
|
|
-import com.hfln.portal.infrastructure.po.GroupShare;
|
|
|
-import com.hfln.portal.infrastructure.po.UserInfo;
|
|
|
-import com.hfln.portal.infrastructure.po.WxRelation;
|
|
|
+import com.hfln.portal.infrastructure.oss.OssClient;
|
|
|
+import com.hfln.portal.infrastructure.oss.OssUtils;
|
|
|
+import com.hfln.portal.infrastructure.po.*;
|
|
|
import com.hfln.portal.infrastructure.service.DevGroupService;
|
|
|
import com.hfln.portal.infrastructure.service.DevInfoService;
|
|
|
import com.hfln.portal.infrastructure.service.DevShareService;
|
|
|
import com.hfln.portal.infrastructure.service.GroupShareService;
|
|
|
+import com.hfln.portal.infrastructure.service.OssFileService;
|
|
|
import com.hfln.portal.infrastructure.service.UserService;
|
|
|
import com.hfln.portal.infrastructure.service.WxRelationService;
|
|
|
import lombok.SneakyThrows;
|
|
@@ -44,9 +44,14 @@ import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
@@ -88,6 +93,11 @@ public class UserGatewayImpl implements UserGateway {
|
|
|
@Autowired
|
|
|
private WxRelationService wxRelationService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OssClient ossClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OssFileService ossFileService;
|
|
|
|
|
|
public static String WXX_CX_ACCESS_TOKEN = "";
|
|
|
|
|
@@ -147,6 +157,7 @@ public class UserGatewayImpl implements UserGateway {
|
|
|
userInfo.setOpenid(params.getOpenId());
|
|
|
userInfo.setPhone(params.getPhone());
|
|
|
userInfo.setUnionId(params.getUnionId());
|
|
|
+ userInfo.setAvatarUrl(params.getAvatarUrl());
|
|
|
userService.save(userInfo);
|
|
|
return true;
|
|
|
}
|
|
@@ -416,4 +427,76 @@ public class UserGatewayImpl implements UserGateway {
|
|
|
// 4. 如果不存在,返回false表示该号码未注册
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void uploadCarousel(MultipartFile file) throws IOException {
|
|
|
+ // 校验文件非空
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ throw new BizException(ErrorEnum.FILE_IS_EMPTY.getErrorCode(), ErrorEnum.FILE_IS_EMPTY.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验文件类型
|
|
|
+ String contentType = file.getContentType();
|
|
|
+ if (contentType == null || (!contentType.equalsIgnoreCase("image/png") && !contentType.equalsIgnoreCase("image/jpeg"))) {
|
|
|
+ throw new BizException(ErrorEnum.FILE_ONLY_PNG_JPG.getErrorCode(), ErrorEnum.FILE_ONLY_PNG_JPG.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验文件头
|
|
|
+ byte[] fileHeader = new byte[8];
|
|
|
+ try (InputStream is = file.getInputStream()) {
|
|
|
+ int readBytes = is.read(fileHeader);
|
|
|
+ if (readBytes < 8) {
|
|
|
+ throw new BizException(ErrorEnum.FILE_NOT_COMPLETE.getErrorCode(), ErrorEnum.FILE_NOT_COMPLETE.getErrorMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean isPng = checkPngMagicNumber(fileHeader);
|
|
|
+ boolean isJpeg = checkJpegMagicNumber(fileHeader);
|
|
|
+
|
|
|
+ if (!isPng && !isJpeg) {
|
|
|
+ throw new BizException(ErrorEnum.FILE_ONLY_PNG_JPG02.getErrorCode(), ErrorEnum.FILE_ONLY_PNG_JPG02.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成OSS对象名称
|
|
|
+ String objectName = OssUtils.getObjectName(OssBusiType.CAROUSEL.getCode(), file.getOriginalFilename());
|
|
|
+
|
|
|
+ // 上传文件到OSS
|
|
|
+ ossClient.upload(file.getInputStream(), OssUtils.BUCKET_NAME, objectName);
|
|
|
+
|
|
|
+ // 保存文件记录到数据库
|
|
|
+ TblOssFile ossFile = new TblOssFile();
|
|
|
+ ossFile.setBusiType(OssBusiType.CAROUSEL.getCode());
|
|
|
+ ossFile.setBusiKey("0");
|
|
|
+ ossFile.setFileName(file.getOriginalFilename());
|
|
|
+ ossFile.setOssUrl(OssUtils.BUCKET_NAME + "/" + objectName);
|
|
|
+ ossFileService.save(ossFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<OssFileDTO> getCarouselList() {
|
|
|
+ // 查询所有轮播图文件记录
|
|
|
+ List<TblOssFile> ossFiles = ossFileService.queryFile(OssBusiType.CAROUSEL.getCode(), null);
|
|
|
+
|
|
|
+ // 转换为DTO并获取可访问的URL
|
|
|
+ return ossFiles.stream()
|
|
|
+ .map(ossFile -> {
|
|
|
+ OssFileDTO dto = new OssFileDTO();
|
|
|
+ dto.setFileId(ossFile.getFileId());
|
|
|
+ dto.setBusiType(ossFile.getBusiType());
|
|
|
+ dto.setFileName(ossFile.getFileName());
|
|
|
+ // 获取可访问的URL
|
|
|
+ dto.setFileUrl(ossClient.getDownloadUrl(ossFile.getOssUrl()));
|
|
|
+ return dto;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkPngMagicNumber(byte[] fileHeader) {
|
|
|
+ byte[] pngMagicNumber = {(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
|
|
|
+ return Arrays.equals(Arrays.copyOfRange(fileHeader, 0, 8), pngMagicNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkJpegMagicNumber(byte[] fileHeader) {
|
|
|
+ byte[] jpegMagicNumber = {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF};
|
|
|
+ return Arrays.equals(Arrays.copyOfRange(fileHeader, 0, 3), jpegMagicNumber);
|
|
|
+ }
|
|
|
}
|