Merge branch 'feature/admin' into preview

master
Diyu0904 2025-03-05 18:31:53 +08:00
commit 8500650f8e
7 changed files with 161 additions and 180 deletions

View File

@ -0,0 +1,34 @@
package com.mcwl.web.controller.resource;
import com.mcwl.common.core.domain.R;
import com.mcwl.resource.domain.Collect;
import com.mcwl.resource.service.impl.CollectServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
*
* @author DaiZibo
* @date 2025/3/5
* @apiNote
*/
@Slf4j
@Api(tags = "收藏")
@RestController
@RequestMapping("/collect")
public class CollectController {
@Autowired
private CollectServiceImpl collectService;
@PostMapping("/addCollect")
public R addCollect(@RequestBody Collect collect){
return collectService.addCollect(collect);
}
}

View File

@ -1,180 +0,0 @@
package com.mcwl.web.controller.resource;
import com.mcwl.web.controller.common.OssUtil;
import org.springframework.mock.web.MockMultipartFile;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.AlgorithmParameters;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @author DaiZibo
* @date 2025/2/5
* @apiNote
*/
public class Test {
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String TRANSFORMATION = "AES";
private static final String KEY = "iamkeyeeddzasdfs";
private static byte[] SAVED_IV; // 静态变量存储IV确保解密时可访问
// iv=gHauCGwTlX/mgXrvTUCifQ==, key=aWFta2V5ZWVkZHphc2Rmcw==
public static void main(String[] args) throws Exception {
// 示例:加密文件并获取密钥
// Map<String, String> stringStringMap = encryptFile("D:\\ASE\\encryption\\测试文件1.txt", "D:\\ASE\\decode\\加密测试文件1.txt");
// System.out.println("-------------------"+keyBase64);
// System.out.println("Encryption Key: " + keyBase64);
//
// // 使用上面得到的密钥解密文件
decryptFile("C:\\Users\\Dzb\\Desktop\\a.enc", "D:\\ASE\\encryption\\解密测试文件3.txt","aWFta2V5ZWVkZHphc2Rmcw==","gHauCGwTlX/mgXrvTUCifQ==");
// Map<String, String> stringStringMap = uploadEncryptedFileToOSS("D:\\ASE\\encryption\\测试文件1.txt", "encrypted-test-file1.enc");
// System.out.println("-------------"+stringStringMap);
}
public static Map<String, String> encryptFile(String sourcePath, String encryptedPath) throws Exception {
Cipher cipher = initCipher(Cipher.ENCRYPT_MODE);
try (FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(encryptedPath);
CipherOutputStream cos = new CipherOutputStream(fos, cipher)) {
byte[] buffer = new byte[1024];
int read;
while ((read = fis.read(buffer)) != -1) {
cos.write(buffer, 0, read);
}
}
System.out.println("加密完成");
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), TRANSFORMATION);
Map<String, String> result = new HashMap<>();
result.put("key", Base64.getEncoder().encodeToString(secretKey.getEncoded()));
result.put("iv", Base64.getEncoder().encodeToString(SAVED_IV));
return result;
}
// public static void decryptFile(String encryptedPath, String decryptedPath, String keyBase64) throws Exception {
// byte[] keyBytes = Base64.getDecoder().decode(keyBase64);
// SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, TRANSFORMATION);
// Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secretKeySpec);
//
// try (FileInputStream fis = new FileInputStream(encryptedPath);
// CipherInputStream cis = new CipherInputStream(fis, cipher);
// FileOutputStream fos = new FileOutputStream(decryptedPath)) {
// byte[] buffer = new byte[1024];
// int read;
// while ((read = cis.read(buffer)) != -1) {
// fos.write(buffer, 0, read);
// }
// }
// System.out.println("解密完成");
// }
public static void decryptFile(String encryptedPath, String decryptedPath, String keyBase64, String ivBase64) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(keyBase64);
byte[] ivBytes = Base64.getDecoder().decode(ivBase64);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, TRANSFORMATION);
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
try (FileInputStream fis = new FileInputStream(encryptedPath);
CipherInputStream cis = new CipherInputStream(fis, cipher);
FileOutputStream fos = new FileOutputStream(decryptedPath)) {
byte[] buffer = new byte[1024];
int read;
while ((read = cis.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
}
System.out.println("解密完成");
}
// private static Cipher initCipher(int mode) throws Exception {
//// return initCipher(mode, null);
//// }
////
//// private static Cipher initCipher(int mode, SecretKeySpec secretKeySpec) throws Exception {
//// Cipher cipher = Cipher.getInstance(ALGORITHM);
//// if (secretKeySpec == null) {
//// secretKeySpec = new SecretKeySpec(KEY.getBytes(), TRANSFORMATION);
//// }
//// if (mode == Cipher.ENCRYPT_MODE && SAVED_IV == null) {
//// cipher.init(mode, secretKeySpec);
//// AlgorithmParameters params = cipher.getParameters();
//// SAVED_IV = params.getParameterSpec(IvParameterSpec.class).getIV();
//// System.out.println("Generated IV: " + Base64.getEncoder().encodeToString(SAVED_IV));
//// } else {
//// cipher.init(mode, secretKeySpec, new IvParameterSpec(SAVED_IV));
//// }
//// return cipher;
//// }
private static Cipher initCipher(int mode, SecretKeySpec secretKeySpec, IvParameterSpec ivParameterSpec) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
if (secretKeySpec == null){
secretKeySpec = new SecretKeySpec(KEY.getBytes(), TRANSFORMATION);
}
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(mode, secretKeySpec);
AlgorithmParameters params = cipher.getParameters();
SAVED_IV = params.getParameterSpec(IvParameterSpec.class).getIV();
System.out.println("Generated IV: " + Base64.getEncoder().encodeToString(SAVED_IV));
} else {
cipher.init(mode, secretKeySpec, ivParameterSpec);
}
return cipher;
}
private static Cipher initCipher(int mode) throws Exception {
return initCipher(mode, null, null);
}
private static Cipher initCipher(int mode, SecretKeySpec secretKeySpec) throws Exception {
return initCipher(mode, secretKeySpec, null);
}
public static Map<String, String> uploadEncryptedFileToOSS(String sourcePath, String ossFileName) throws Exception {
Cipher cipher = initCipher(Cipher.ENCRYPT_MODE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (FileInputStream fis = new FileInputStream(sourcePath);
CipherInputStream cis = new CipherInputStream(fis, cipher)) {
byte[] buffer = new byte[1024];
int read;
while ((read = cis.read(buffer)) != -1) {
bos.write(buffer, 0, read);
}
}
System.out.println("加密完成");
// 将加密后的字节数组转换为MultipartFile
MockMultipartFile multipartFile = new MockMultipartFile(ossFileName, ossFileName, "application/octet-stream", bos.toByteArray());
// 调用上传方法
String uploadedUrl = OssUtil.uploadMultipartFile(multipartFile);
Map<String, String> result = new HashMap<>();
result.put("uploadedUrl", uploadedUrl);
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), TRANSFORMATION);
result.put("key",Base64.getEncoder().encodeToString(secretKey.getEncoded()));
result.put("iv", Base64.getEncoder().encodeToString(SAVED_IV));
return result;
}
}

View File

@ -0,0 +1,40 @@
package com.mcwl.resource.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
*
*
* @author DaiZibo
* @date 2025/3/5
* @apiNote
*/
@TableName("collect")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Collect {
private Long id;
private Long productId;
private Long userId;
private Long productType;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}

View File

@ -0,0 +1,18 @@
package com.mcwl.resource.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mcwl.resource.domain.Collect;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author DaiZibo
* @date 2025/3/5
* @apiNote
*/
@Mapper
public interface CollectMapper extends BaseMapper<Collect> {
Collect selectCollect(@Param("userId") Long userId, @Param("productId") Long productId, @Param("productType") Long productType);
}

View File

@ -0,0 +1,15 @@
package com.mcwl.resource.service;
import com.mcwl.common.core.domain.R;
import com.mcwl.resource.domain.Collect;
/**
*
* @author DaiZibo
* @date 2025/3/5
* @apiNote
*/
public interface CollectService {
R addCollect(Collect collect);
}

View File

@ -0,0 +1,41 @@
package com.mcwl.resource.service.impl;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.Collect;
import com.mcwl.resource.mapper.CollectMapper;
import com.mcwl.resource.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author DaiZibo
* @date 2025/3/5
* @apiNote
*/
@Service
public class CollectServiceImpl implements CollectService {
@Autowired
private CollectMapper collectMapper;
@Override
public R addCollect(Collect collect) {
Collect collect1 = collectMapper.selectCollect(SecurityUtils.getUserId(),collect.getProductId(),collect.getProductType());
if (collect1 == null){
//执行收藏
collectMapper.insert(collect);
return R.ok(0);
}
//执行删除/取消收藏
collectMapper.deleteById(collect1.getId());
return R.ok(1);
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mcwl.resource.mapper.CollectMapper">
<select id="selectCollect" resultType="com.mcwl.resource.domain.Collect">
select
id,product_id,user_id,product_type
FROM collect where product_id = #{productId} and user_id = #{userId} and product_type = #{productType}
</select>
</mapper>