qauMa/src/main/java/com/rabbiter/em/controller/AddressController.java

99 lines
2.8 KiB
Java
Raw Normal View History

2024-10-13 16:54:05 +08:00
package com.rabbiter.em.controller;
import com.rabbiter.em.annotation.Authority;
import com.rabbiter.em.constants.Constants;
import com.rabbiter.em.common.Result;
import com.rabbiter.em.entity.AuthorityType;
import com.rabbiter.em.entity.Address;
import com.rabbiter.em.service.AddressService;
import org.springframework.web.bind.annotation.*;
2025-04-06 21:55:03 +08:00
import org.springframework.web.client.RestTemplate;
2024-10-13 16:54:05 +08:00
import javax.annotation.Resource;
import java.util.List;
@Authority(AuthorityType.requireLogin)
@RestController
@RequestMapping("/api/address")
public class AddressController {
@Resource
private AddressService addressService;
/*
*/
@GetMapping("/{userId}")
public Result findAllById(@PathVariable Long userId) {
return Result.success(addressService.findAllById(userId));
}
@GetMapping
public Result findAll() {
List<Address> list = addressService.list();
return Result.success(list);
}
2025-04-06 21:55:03 +08:00
@GetMapping("/deepseek/{qq}")
public Result callDeepSeek(@PathVariable String qq) {
try {
// 验证QQ号格式
if(!qq.matches("^[1-9][0-9]{4,10}$")) {
return Result.error(Constants.CODE_400, "无效的QQ号");
}
// 调用DeepSeek API
RestTemplate restTemplate = new RestTemplate();
String apiUrl = "https://api.deepseek.com/qq/" + qq;
String response = restTemplate.getForObject(apiUrl, String.class);
2025-04-06 21:55:03 +08:00
return Result.success(response);
} catch (Exception e) {
return Result.error(Constants.CODE_500, "调用DeepSeek失败: " + e.getMessage());
}
}
2024-10-13 16:54:05 +08:00
/*
*/
@PostMapping
public Result save(@RequestBody List<Address> addresses) {
// 验证每个地址的IP格式
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]?)$";
for(Address address : addresses) {
String ipAddress = address.getLinkAddress();
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){
2024-10-13 16:54:05 +08:00
return Result.success();
}else{
return Result.error(Constants.CODE_500,"批量保存地址失败");
2024-10-13 16:54:05 +08:00
}
}
@PutMapping
public Result update(@RequestBody Address address) {
addressService.updateById(address);
return Result.success();
}
/*
*/
@DeleteMapping("/{id}")
public Result delete(@PathVariable Long id) {
addressService.removeById(id);
return Result.success();
}
}