提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.controller.admin.ip; |
H |
2 |
|
|
3 |
import cn.hutool.core.lang.Assert; |
|
4 |
import com.iailab.framework.common.pojo.CommonResult; |
|
5 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
6 |
import com.iailab.framework.ip.core.Area; |
|
7 |
import com.iailab.framework.ip.core.utils.AreaUtils; |
|
8 |
import com.iailab.framework.ip.core.utils.IPUtils; |
|
9 |
import com.iailab.module.system.controller.admin.ip.vo.AreaNodeRespVO; |
|
10 |
import io.swagger.v3.oas.annotations.Operation; |
|
11 |
import io.swagger.v3.oas.annotations.Parameter; |
|
12 |
import io.swagger.v3.oas.annotations.tags.Tag; |
|
13 |
import org.springframework.validation.annotation.Validated; |
|
14 |
import org.springframework.web.bind.annotation.GetMapping; |
|
15 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
16 |
import org.springframework.web.bind.annotation.RequestParam; |
|
17 |
import org.springframework.web.bind.annotation.RestController; |
|
18 |
|
|
19 |
import java.util.List; |
|
20 |
|
|
21 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
22 |
|
|
23 |
@Tag(name = "管理后台 - 地区") |
|
24 |
@RestController |
|
25 |
@RequestMapping("/system/area") |
|
26 |
@Validated |
|
27 |
public class AreaController { |
|
28 |
|
|
29 |
@GetMapping("/tree") |
|
30 |
@Operation(summary = "获得地区树") |
|
31 |
public CommonResult<List<AreaNodeRespVO>> getAreaTree() { |
|
32 |
Area area = AreaUtils.getArea(Area.ID_CHINA); |
|
33 |
Assert.notNull(area, "获取不到中国"); |
|
34 |
return success(BeanUtils.toBean(area.getChildren(), AreaNodeRespVO.class)); |
|
35 |
} |
|
36 |
|
|
37 |
@GetMapping("/get-by-ip") |
|
38 |
@Operation(summary = "获得 IP 对应的地区名") |
|
39 |
@Parameter(name = "ip", description = "IP", required = true) |
|
40 |
public CommonResult<String> getAreaByIp(@RequestParam("ip") String ip) { |
|
41 |
// 获得城市 |
|
42 |
Area area = IPUtils.getArea(ip); |
|
43 |
if (area == null) { |
|
44 |
return success("未知"); |
|
45 |
} |
|
46 |
// 格式化返回 |
|
47 |
return success(AreaUtils.format(area.getId())); |
|
48 |
} |
|
49 |
|
|
50 |
} |