Compare commits

..

3 Commits

9 changed files with 80 additions and 50 deletions

32
pom.xml
View File

@ -10,12 +10,33 @@
</parent> </parent>
<groupId>com.rabbiter</groupId> <groupId>com.rabbiter</groupId>
<artifactId>ElectronicMallApi</artifactId> <artifactId>ElectronicMallApi</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0</version>
<name>ElectronicMallApi</name> <name>ElectronicMallApi</name>
<description>ElectronicMallApi</description> <description>ElectronicMallApi</description>
<packaging>jar</packaging>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.rabbiter.em.ElectronicMallApplication</mainClass> <!-- 指定主类 -->
<arguments>
<argument>-XDignore.symbol.file</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -108,6 +129,14 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.sun.xml.fastinfoset</groupId>
<artifactId>FastInfoset</artifactId>
<version>1.2.18</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
@ -134,4 +163,5 @@
</snapshots> </snapshots>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
</project> </project>

View File

@ -1,7 +1,10 @@
package com.rabbiter.em.constants; package com.rabbiter.em.constants;
import com.rabbiter.em.utils.PathUtils; import com.rabbiter.em.utils.PathUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Constants { public class Constants {
public static final String CODE_200 = "200";//成功 public static final String CODE_200 = "200";//成功
public static final String CODE_500 = "500";//系统错误 public static final String CODE_500 = "500";//系统错误
@ -9,8 +12,12 @@ public class Constants {
public static final String CODE_401 = "401";//无权限 public static final String CODE_401 = "401";//无权限
public static final String TOKEN_ERROR = "401";//token无效 public static final String TOKEN_ERROR = "401";//token无效
public static final String CODE_403 = "403";//拒绝执行 public static final String CODE_403 = "403";//拒绝执行
public static final String CODE_400 = "400";//无效请求 public static final String CODE_400 = "400";//无效请求
//文件存储位置 //文件存储位置
public static final String fileFolderPath = PathUtils.getClassLoadRootPath() + "/file/"; @Value("${file.file}")
public static final String avatarFolderPath = PathUtils.getClassLoadRootPath() + "/avatar/"; public String fileFolderPath ;
@Value("${file.avatar}")
public String avatarFolderPath ;
} }

View File

@ -46,6 +46,8 @@ public class AddressController {
String apiUrl = "https://api.deepseek.com/qq/" + qq; String apiUrl = "https://api.deepseek.com/qq/" + qq;
String response = restTemplate.getForObject(apiUrl, String.class); String response = restTemplate.getForObject(apiUrl, String.class);
return Result.success(response); return Result.success(response);
} catch (Exception e) { } catch (Exception e) {
return Result.error(Constants.CODE_500, "调用DeepSeek失败: " + e.getMessage()); return Result.error(Constants.CODE_500, "调用DeepSeek失败: " + e.getMessage());
@ -56,14 +58,26 @@ public class AddressController {
*/ */
@PostMapping @PostMapping
public Result save(@RequestBody Address address) { public Result save(@RequestBody List<Address> addresses) {
boolean b = addressService.saveOrUpdate(address); // 验证每个地址的IP格式
if(b){ String ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
return Result.success(); for(Address address : addresses) {
}else{ String ipAddress = address.getLinkAddress();
return Result.error(Constants.CODE_500,"保存地址失败"); if(ipAddress != null && !ipAddress.matches(ipPattern)) {
return Result.error(Constants.CODE_400, "无效的IP地址格式: " + ipAddress);
}
} }
boolean success = true;
if(!addressService.saveOrUpdateBatch(addresses)) {
success = false;
}
if(success){
return Result.success();
}else{
return Result.error(Constants.CODE_500,"批量保存地址失败");
}
} }
@PutMapping @PutMapping

View File

@ -9,7 +9,7 @@ import com.rabbiter.em.entity.Order;
import com.rabbiter.em.service.OrderService; import com.rabbiter.em.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.internal.fastinfoset.stax.events.Util; import com.sun.xml.fastinfoset.stax.events.Util;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;

View File

@ -13,7 +13,7 @@ import com.rabbiter.em.entity.User;
import com.rabbiter.em.entity.dto.UserDTO; import com.rabbiter.em.entity.dto.UserDTO;
import com.rabbiter.em.service.UserService; import com.rabbiter.em.service.UserService;
import com.rabbiter.em.utils.TokenUtils; import com.rabbiter.em.utils.TokenUtils;
import com.sun.xml.internal.fastinfoset.stax.events.Util; import com.sun.xml.fastinfoset.stax.events.Util;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;

View File

@ -25,6 +25,9 @@ public class AvatarService {
@Resource @Resource
private AvatarMapper avatarMapper; private AvatarMapper avatarMapper;
@Resource
private Constants constants;
public String upload(MultipartFile uploadFile){ public String upload(MultipartFile uploadFile){
String url = null; String url = null;
//通过md5判断文件是否已经存在防止在服务器存储相同文件 //通过md5判断文件是否已经存在防止在服务器存储相同文件
@ -42,7 +45,7 @@ public class AvatarService {
System.out.println(originalFilename+" "+type); System.out.println(originalFilename+" "+type);
long size = uploadFile.getSize() / 1024; //文件大小单位kb long size = uploadFile.getSize() / 1024; //文件大小单位kb
//文件不存在,则保存文件 //文件不存在,则保存文件
File folder = new File(Constants.avatarFolderPath); File folder = new File(constants.avatarFolderPath);
if(!folder.exists()){ if(!folder.exists()){
folder.mkdir(); folder.mkdir();
} }
@ -69,7 +72,7 @@ public class AvatarService {
} }
//根据文件名下载文件 //根据文件名下载文件
public void download(String fileName, HttpServletResponse response){ public void download(String fileName, HttpServletResponse response){
File file = new File(Constants.avatarFolderPath+fileName); File file = new File(constants.avatarFolderPath+fileName);
if(!file.exists()){ if(!file.exists()){
throw new ServiceException(Constants.CODE_500,"文件不存在"); throw new ServiceException(Constants.CODE_500,"文件不存在");
} }
@ -92,7 +95,7 @@ public class AvatarService {
if(delete==1){ if(delete==1){
String fileName = StrUtil.subAfter(avatar.getUrl(),"/",true); String fileName = StrUtil.subAfter(avatar.getUrl(),"/",true);
System.out.println(fileName); System.out.println(fileName);
File file = new File(Constants.avatarFolderPath+fileName); File file = new File(constants.avatarFolderPath+fileName);
System.out.println(file.getAbsolutePath()); System.out.println(file.getAbsolutePath());
if(file.exists()){ if(file.exists()){

View File

@ -30,6 +30,9 @@ public class FileService extends ServiceImpl<FileMapper, MyFile> {
@Resource @Resource
private FileMapper fileMapper; private FileMapper fileMapper;
@Resource
private Constants constants;
public String upload(MultipartFile uploadFile){ public String upload(MultipartFile uploadFile){
String originalFilename = uploadFile.getOriginalFilename(); //文件原始名字 String originalFilename = uploadFile.getOriginalFilename(); //文件原始名字
String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1); //文件后缀 String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1); //文件后缀
@ -57,7 +60,7 @@ public class FileService extends ServiceImpl<FileMapper, MyFile> {
myFile.setUrl(url); myFile.setUrl(url);
}else{ }else{
//文件不存在,则保存文件 //文件不存在,则保存文件
File folder = new File(Constants.fileFolderPath); File folder = new File(constants.fileFolderPath);
if(!folder.exists()){ if(!folder.exists()){
folder.mkdir(); folder.mkdir();
} }
@ -84,9 +87,9 @@ public class FileService extends ServiceImpl<FileMapper, MyFile> {
//根据文件名下载文件 //根据文件名下载文件
public void download(String fileName, HttpServletResponse response){ public void download(String fileName, HttpServletResponse response){
File file = new File(Constants.fileFolderPath+fileName); File file = new File(constants.fileFolderPath+fileName);
if(!file.exists()){ if(!file.exists()){
throw new ServiceException(Constants.CODE_500,"文件不存在"); throw new ServiceException(constants.CODE_500,"文件不存在");
} }
try { try {
ServletOutputStream os = response.getOutputStream(); ServletOutputStream os = response.getOutputStream();

View File

@ -11,8 +11,9 @@ spring:
max-file-size: 30MB max-file-size: 30MB
redis: redis:
database: 0 database: 0
host: 127.0.0.1 host: 139.196.201.231
port: 6379 port: 6379
password: Amk812s$
lettuce: lettuce:
pool: pool:
min-idle: 0 min-idle: 0
@ -25,3 +26,7 @@ mybatis:
mapper-locations: classpath:mapper/*.xml mapper-locations: classpath:mapper/*.xml
configuration: configuration:
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
file:
file: /home/fzhang5/file/file/
avatar: /home/fzhang5/file/avatar/

View File

@ -1,32 +0,0 @@
package com.rabbiter.em.mapper;
import com.rabbiter.em.entity.Cart;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CartMapperTest {
@Autowired
private CartMapper cartMapper;
@Test
public void selectByUserId() {
List<Map<String, Object>> list = cartMapper.selectByUserId(2L);
System.out.println(list);
}
@Test
public void selectAll() {
cartMapper.selectList(null).forEach(System.out::println);
}
}