Skip to content

Commit

Permalink
Merge pull request #27 from BingChunMoLi/develop
Browse files Browse the repository at this point in the history
refactor(v3.8.2): 优化天气与地址查询
  • Loading branch information
BingChunMoLi authored Jun 8, 2022
2 parents 14cf1af + 14edfad commit 226e8ff
Show file tree
Hide file tree
Showing 40 changed files with 4,485 additions and 3,311 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ src/main/resources/application-*.yml
src/main/java/com/bingchunmoli/api/me/
src/test/java/com/bingchunmoli/api/me/
src/main/resources/mapper/FavMapper.xml
deploy.bat
deploy.bat
!src/main/resources/application-h2.yml
27 changes: 24 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.bingchunmoli</groupId>
<artifactId>moliapi</artifactId>
<version>3.7.0</version>
<version>3.8.2</version>
<name>MoLiApi</name>
<description>MoLiApi</description>

Expand All @@ -20,10 +20,10 @@
<mybatis-plus.version>3.5.1</mybatis-plus.version>
<swagger.version>3.0.0</swagger.version>
<knife4j.version>3.0.3</knife4j.version>
<fastjson.version>2.0.2</fastjson.version>
<fastjson.version>2.0.3</fastjson.version>
<docker-plugin.version>1.4.9</docker-plugin.version>
<aspectjweaver.version>1.9.9.1</aspectjweaver.version>
<hutool.version>5.7.22</hutool.version>
<hutool.version>5.8.0</hutool.version>
<zxing.version>3.5.0</zxing.version>
<emoji.version>5.1.1</emoji.version>
<ip2region.version>1.7.2</ip2region.version>
Expand Down Expand Up @@ -118,11 +118,32 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
<version>2.1.212</version>
</dependency>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>${ip2region.version}</version>
</dependency>
<dependency>
<groupId>com.jthinking.common</groupId>
<artifactId>ip-info</artifactId>
<version>2.1.5</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.maxmind.geoip2</groupId>-->
<!-- <artifactId>geoip2</artifactId>-->
<!-- <version>2.15.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springdoc</groupId>-->
<!-- <artifactId>springdoc-openapi-ui</artifactId>-->
<!-- <version>1.6.8</version>-->
<!-- </dependency>-->
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bingchunmoli/api/ApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class ApiApplication {

public static void main(String[] args) {
System.setProperty("spring.config.additional-location","optional:file:${user.home}/.api/");
SpringApplication.run(ApiApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
Expand All @@ -24,7 +26,9 @@
*/
@Configuration
@EnableCaching
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport {

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.bingchunmoli.api.controller.advice;

import com.alibaba.fastjson.JSON;
import com.bingchunmoli.api.bean.MailMessage;
import com.bingchunmoli.api.bean.ResultVO;
import com.bingchunmoli.api.bean.enums.CodeEnum;
import com.bingchunmoli.api.even.MailMessageEven;
import com.bingchunmoli.api.exception.ApiException;
import com.bingchunmoli.api.exception.ApiParamException;
import com.bingchunmoli.api.qrcode.exception.FileIsEmptyException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -14,45 +20,53 @@
* @author 冰彦糖
* @version 0.0.1-SNAPSHOT
**/
@RestControllerAdvice
@Slf4j
@RestControllerAdvice
@RequiredArgsConstructor
public class ExceptionControllerAdvice {


private final ApplicationEventPublisher applicationEventPublisher;
@Value("${spring.mail.enable}")
private boolean mailEnable;

@ExceptionHandler
public ResultVO<String> fileIsEmptyException(FileIsEmptyException e) {
log.error(e.getMessage());
log.error("文件为空, ", e);
return new ResultVO<>(CodeEnum.ERROR.getCode(), e.getMessage(), "文件为空,请确认文件是否存在");
}

@ExceptionHandler
public ResultVO<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.error(e.getMessage());
log.error("没有受支持的方法", e);
return new ResultVO<>(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMsg(), "没有受支持的请求方法,请求方法错误.");
}

@ExceptionHandler
public ResultVO<String> defaultException(Exception e) {
log.error("defaultException: {}, msg: {}", e, e.getMessage());
log.error("defaultException: {}, msg: {}", e.getMessage(), e);
e.printStackTrace();
if (mailEnable) {
MailMessage errMailMessage = MailMessage.builder().title("出现未分类异常").body("defaultException: "+ JSON.toJSONString(e)).build();
applicationEventPublisher.publishEvent(new MailMessageEven(errMailMessage));
}
return new ResultVO<>(CodeEnum.FAILURE.getCode(), CodeEnum.FAILURE.getMsg(), "默认未分类异常");
}

@ExceptionHandler
public ResultVO<String> defaultThrowable(Throwable throwable) {
log.error(throwable.getMessage());
log.error("系统错误: ", throwable);
return new ResultVO<>(CodeEnum.FAILURE.getCode(), CodeEnum.FAILURE.getMsg(), "严重异常");
}

@ExceptionHandler
public ResultVO<String> apiException(ApiException e) {
log.error(e.getMessage());
log.error("api顶层异常: ", e);
return new ResultVO<>(CodeEnum.FAILURE.getCode(), CodeEnum.FAILURE.getMsg(), "api异常");
}

@ExceptionHandler
public ResultVO<String> apiParamException(ApiParamException e) {
log.error(e.getMessage());
log.error("请求参数不正确或不支持: ", e);
return new ResultVO<>(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMsg(), "请求参数异常");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.bingchunmoli.api.bean.ApiConstant;
import com.bingchunmoli.api.img.service.IImgService;
import com.bingchunmoli.api.properties.ApiKeyProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,6 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.awt.image.BufferedImage;
import java.util.concurrent.ThreadLocalRandom;

/**
* 随机图
Expand All @@ -19,9 +22,11 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("img")
@ConditionalOnProperty(prefix = "moli.api-config", name = {"pcPath", "mobilePath", "path1080"})
public class ImgController {

private final IImgService imgService;
private final ApiKeyProperties apiKeyProperties;

/**
* 手机版
Expand Down Expand Up @@ -50,5 +55,14 @@ public BufferedImage randomImgByPc() {
return imgService.getRandomImageByPc();
}

/**
* 返回随机1080P图片接口
* @return 1080Pwebp图片
*/
@GetMapping(value = "1080", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<FileSystemResource> get1080PImage(){
int i = ThreadLocalRandom.current().nextInt(48);
return ResponseEntity.ok(new FileSystemResource(apiKeyProperties.getPath1080() + "1080P" + i + ".webp"));
}

}
1 change: 0 additions & 1 deletion src/main/java/com/bingchunmoli/api/img/task/ImgTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void saveImg() {
redisTemplate.opsForList().leftPushAll(ApiConstant.PC_IMG, pcPath.stream().map(Path :: toString).toArray());
redisTemplate.opsForList().trim(ApiConstant.MOBILE_IMG, - 1, - 2);
redisTemplate.opsForList().leftPushAll(ApiConstant.MOBILE_IMG, mobilePath.stream().map(Path :: toString).toArray());
serverSauce.send("随机图定时任务 更新成功", "更新PC图片:" + pcPath.size() + " 更新移动图片:" + mobilePath.size());
applicationEventPublisher.publishEvent(new MailMessageEven(MailMessage.builder().title("随机图定时任务 更新成功").body("更新PC图片:" + pcPath.size() + " 更新移动图片:" + mobilePath.size()).build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class RedisCommandLineRunner implements CommandLineRunner {
private final ApplicationEventPublisher applicationEventPublisher;
@Value("${spring.profiles.active}")
String profile;

@Override
public void run(String... args) {
Long yiYanLen = redisTemplate.opsForList().size(ApiConstant.YI_YAN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bingchunmoli.api.interceptor;

import cn.hutool.extra.servlet.ServletUtil;
import com.bingchunmoli.api.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
Expand All @@ -21,11 +22,15 @@
@RequiredArgsConstructor
public class IpInterceptor implements HandlerInterceptor {
private final RedisTemplate<String, Object> redisTemplate;
private final RedisUtil redisUtil;


@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// ServletContext servletContext = request.getServletContext();
if (redisUtil.isNotEnable()) {
return true;
}
String requestURI = request.getRequestURI();
String clientIP = ServletUtil.getClientIP(request);
StringBuffer stringBuffer = new StringBuffer();
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/bingchunmoli/api/ip/controller/IpController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.bingchunmoli.api.ip.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.jthinking.common.util.ip.IPInfo;
import com.jthinking.common.util.ip.IPInfoUtils;
import org.lionsoul.ip2region.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
* 来源IP
Expand All @@ -34,10 +36,10 @@ public String ip(HttpServletRequest request) {
* @throws DbMakerConfigException 地址数据库配置异常
* @throws IOException 内存异常
*/
@GetMapping("address")
public String getAddress(HttpServletRequest request) throws DbMakerConfigException, IOException {
@GetMapping("v4Address")
public String getV4Address(HttpServletRequest request) throws DbMakerConfigException, IOException {
String ip = ip(request);
DbSearcher dbSearcher = new DbSearcher(new DbConfig(), Objects.requireNonNull(this.getClass().getClassLoader().getResource("ip2region.db")).getFile());
DbSearcher dbSearcher = new DbSearcher(new DbConfig(), IoUtil.readBytes(this.getClass().getClassLoader().getResourceAsStream("ip2region.db")));
String region;
if (Util.isIpAddress(ip)) {
DataBlock dataBlock = dbSearcher.memorySearch(ip);
Expand All @@ -46,4 +48,9 @@ public String getAddress(HttpServletRequest request) throws DbMakerConfigExcepti
}
return "unknown";
}

@GetMapping("address")
public IPInfo getAddress(HttpServletRequest request){
return IPInfoUtils.getIpInfo(ip(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.bingchunmoli.api.utils.SendMailUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -15,15 +17,21 @@
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnBean(SendMailUtil.class)
public class MailMessageEvenListener implements ApplicationListener<MailMessageEven> {
private final SendMailUtil sendMailUtil;

@Value("${spring.mail.enable}")
private boolean enable;
@Async
@Override
public void onApplicationEvent(MailMessageEven event) {
if (log.isDebugEnabled()) {
log.debug("MailMessageEvenListener.onApplicationEvent: {}", event);
}
if (!enable) {
log.debug("MailMessageEvenListener 未启用发消息");
return;
}
if (sendMailUtil.sendEvenMail(event)) {
log.debug("MailMessageEvenListener.onApplicationEvent发送成功");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
@Data
@Component
@ConfigurationProperties("moli.api-key")
@ConfigurationProperties("moli.api-config")
public class ApiKeyProperties {
/**
* 和风天气key
Expand All @@ -36,5 +36,9 @@ public class ApiKeyProperties {
* mobile图片路径
*/
private String mobilePath;
/**
* 1080p
*/
private String path1080;

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.bingchunmoli.api.shici.controller;


import com.bingchunmoli.api.bean.ApiConstant;
import com.bingchunmoli.api.shici.service.IShiCiService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -19,8 +17,6 @@
@RequestMapping("shici")
public class ShiCiController {
private final IShiCiService shiCiService;
private final RedisTemplate<String, Object> redisTemplate;


/**
* 指定ID诗词
Expand All @@ -30,11 +26,7 @@ public class ShiCiController {
*/
@GetMapping("{id}")
public Object getShiCi(@PathVariable Integer id) {
Object shiCi = redisTemplate.opsForList().index(ApiConstant.SHI_CI, id);
if (shiCi == null) {
return shiCiService.getById(id);
}
return shiCi;
return shiCiService.getShiCi(id);
}

/**
Expand Down
Loading

0 comments on commit 226e8ff

Please sign in to comment.