99 lines
2.8 KiB
Java
99 lines
2.8 KiB
Java
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.*;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
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);
|
|
}
|
|
|
|
@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);
|
|
|
|
|
|
|
|
return Result.success(response);
|
|
} catch (Exception e) {
|
|
return Result.error(Constants.CODE_500, "调用DeepSeek失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/*
|
|
保存
|
|
*/
|
|
@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){
|
|
return Result.success();
|
|
}else{
|
|
return Result.error(Constants.CODE_500,"批量保存地址失败");
|
|
}
|
|
}
|
|
|
|
@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();
|
|
}
|
|
|
|
}
|