commit 4f87766aba9d8a37dfe6b763cbf30f25a842212c
Author: liuchen <23082234@qq.com>
Date: Thu Jan 25 16:46:36 2024 +0800
新增代码
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..6d8752f
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,66 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.15
+
+
+ com.win.mq
+ mq
+ 0.0.1-SNAPSHOT
+ mq
+ win mq
+
+ 17
+
+
+
+
+ org.apache.rocketmq
+ rocketmq-spring-boot-starter
+ 2.2.2
+
+
+ org.apache.tomcat
+ annotations-api
+
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.28
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/src/main/java/com/win/mq/Application.java b/src/main/java/com/win/mq/Application.java
new file mode 100644
index 0000000..51d39e0
--- /dev/null
+++ b/src/main/java/com/win/mq/Application.java
@@ -0,0 +1,18 @@
+package com.win.mq;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+/**
+ * 启动程序
+ *
+ * @author win
+ */
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+public class Application {
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class, args);
+ System.out.println("闻音启动成功");
+ }
+}
diff --git a/src/main/java/com/win/mq/common/CommonResult.java b/src/main/java/com/win/mq/common/CommonResult.java
new file mode 100644
index 0000000..58fa653
--- /dev/null
+++ b/src/main/java/com/win/mq/common/CommonResult.java
@@ -0,0 +1,112 @@
+package com.win.mq.common;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.win.mq.exception.ErrorCode;
+import com.win.mq.exception.GlobalErrorCodeConstants;
+import com.win.mq.exception.ServiceException;
+import lombok.Data;
+import org.springframework.util.Assert;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * 通用返回
+ *
+ * @param 数据泛型
+ */
+@Data
+public class CommonResult implements Serializable {
+
+ /**
+ * 错误码
+ *
+ * @see ErrorCode#getCode()
+ */
+ private Integer code;
+ /**
+ * 返回数据
+ */
+ private T data;
+ /**
+ * 错误提示,用户可阅读
+ *
+ * @see ErrorCode#getMsg() ()
+ */
+ private String msg;
+
+ /**
+ * 将传入的 result 对象,转换成另外一个泛型结果的对象
+ *
+ * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
+ *
+ * @param result 传入的 result 对象
+ * @param 返回的泛型
+ * @return 新的 CommonResult 对象
+ */
+ public static CommonResult error(CommonResult> result) {
+ return error(result.getCode(), result.getMsg());
+ }
+
+ public static CommonResult error(Integer code, String message) {
+ Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
+ CommonResult result = new CommonResult<>();
+ result.code = code;
+ result.msg = message;
+ return result;
+ }
+
+ public static CommonResult error(ErrorCode errorCode) {
+ return error(errorCode.getCode(), errorCode.getMsg());
+ }
+
+ public static CommonResult success(T data) {
+ CommonResult result = new CommonResult<>();
+ result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
+ result.data = data;
+ result.msg = "";
+ return result;
+ }
+
+ public static boolean isSuccess(Integer code) {
+ return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
+ }
+
+ @JsonIgnore // 避免 jackson 序列化
+ public boolean isSuccess() {
+ return isSuccess(code);
+ }
+
+ @JsonIgnore // 避免 jackson 序列化
+ public boolean isError() {
+ return !isSuccess();
+ }
+
+ // ========= 和 Exception 异常体系集成 =========
+
+ /**
+ * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
+ */
+ public void checkError() throws ServiceException {
+ if (isSuccess()) {
+ return;
+ }
+ // 业务异常
+ throw new ServiceException(code, msg);
+ }
+
+ /**
+ * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
+ * 如果没有,则返回 {@link #data} 数据
+ */
+ @JsonIgnore // 避免 jackson 序列化
+ public T getCheckedData() {
+ checkError();
+ return data;
+ }
+
+ public static CommonResult error(ServiceException serviceException) {
+ return error(serviceException.getCode(), serviceException.getMessage());
+ }
+
+}
diff --git a/src/main/java/com/win/mq/config/RMQConfigure.java b/src/main/java/com/win/mq/config/RMQConfigure.java
new file mode 100644
index 0000000..bdc4e72
--- /dev/null
+++ b/src/main/java/com/win/mq/config/RMQConfigure.java
@@ -0,0 +1,28 @@
+package com.win.mq.config;
+
+
+import lombok.Data;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.rocketmq.common.MixAll;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.web.server.ErrorPage;
+import org.springframework.boot.web.server.ErrorPageRegistrar;
+import org.springframework.boot.web.server.ErrorPageRegistry;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpStatus;
+
+import java.io.File;
+
+import static org.apache.rocketmq.client.ClientConfig.SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "rocketmq.config")
+public class RMQConfigure {
+
+ private String dataPath;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/win/mq/config/RestTemplateConfig.java b/src/main/java/com/win/mq/config/RestTemplateConfig.java
new file mode 100644
index 0000000..13877cb
--- /dev/null
+++ b/src/main/java/com/win/mq/config/RestTemplateConfig.java
@@ -0,0 +1,31 @@
+package com.win.mq.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.client.ClientHttpRequestFactory;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.http.converter.StringHttpMessageConverter;
+import org.springframework.web.client.RestTemplate;
+
+import java.nio.charset.StandardCharsets;
+
+@Configuration
+public class RestTemplateConfig {
+
+ @Bean
+ public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
+ RestTemplate restTemplate = new RestTemplate(factory);
+ // 支持中文编码
+ restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+ return restTemplate;
+ }
+
+ @Bean
+ public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
+ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
+ factory.setReadTimeout(5000);//单位为ms
+ factory.setConnectTimeout(5000);//单位为ms
+ return factory;
+ }
+
+}
diff --git a/src/main/java/com/win/mq/config/SmfsConfigure.java b/src/main/java/com/win/mq/config/SmfsConfigure.java
new file mode 100644
index 0000000..a58204e
--- /dev/null
+++ b/src/main/java/com/win/mq/config/SmfsConfigure.java
@@ -0,0 +1,34 @@
+package com.win.mq.config;
+
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "sfms")
+public class SmfsConfigure {
+
+ /**
+ * 用户名
+ */
+ private String username;
+ /**
+ * 密码
+ */
+ private String password;
+ /**
+ * 采购订单请求地址
+ */
+ private String purchaseOrder;
+ /**
+ * 访问token
+ */
+ private String accessToken;
+ /**
+ * 刷新token
+ */
+ private String refreshToken;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/win/mq/controller/ProduceController.java b/src/main/java/com/win/mq/controller/ProduceController.java
new file mode 100644
index 0000000..5d415b6
--- /dev/null
+++ b/src/main/java/com/win/mq/controller/ProduceController.java
@@ -0,0 +1,98 @@
+package com.win.mq.controller;
+
+import com.win.mq.common.CommonResult;
+import com.win.mq.config.RMQConfigure;
+import com.win.mq.exception.GlobalErrorCodeConstants;
+import com.win.mq.rocket.RocketMQProducer;
+import com.win.mq.utils.FileUtil;
+import com.win.mq.utils.ProfileUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.DigestUtils;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@Slf4j
+@RestController
+@RequestMapping("/producer")
+public class ProduceController {
+
+ @Autowired
+ private RMQConfigure configure;
+ @Autowired
+ private FileUtil fileUtil;
+ @Autowired
+ private RocketMQProducer rocketMQProducer;
+
+ /**
+ * 对外提供一个接口,通过header中的interfaceName反射机制调用方法,方法必须写到这个controller中,并且不用加PostMapping注解。
+ *
+ * @param request request
+ * @param body 请求主体
+ * @return 结果
+ */
+ @PostMapping("/api")
+ @SuppressWarnings("unchecked")
+ public CommonResult api(HttpServletRequest request, @RequestBody String body) {
+ String interfaceName = request.getHeader("interface");
+ String sign = request.getHeader("sign");
+ String timeStr = request.getHeader("timestamp");
+ if(timeStr == null || timeStr.isEmpty()) {
+ return CommonResult.error(GlobalErrorCodeConstants.TIMESTAMP_ERROR);
+ }
+ long timestamp = 0;
+ try {
+ timestamp = Long.parseLong(timeStr);
+ } catch (NumberFormatException e) {
+ return CommonResult.error(GlobalErrorCodeConstants.TIMESTAMP_ERROR);
+ }
+ String tmp = interfaceName + body + timestamp;
+ String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes());
+ log.info("{}, interfaceName: {}", "interfaceName", interfaceName);
+ log.info("{}, sign: {}", "sign", sign);
+ log.info("{}, timestamp: {}", "timestamp", timestamp);
+ log.info("{}, tmp: {}", "tmp", tmp);
+ log.info("{}, computeSign: {}", "computeSign", computeSign);
+ long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳
+ long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳
+ //过期
+ if(timestamp > currentTimestamp || tenTimestamp < currentTimestamp) {
+ return CommonResult.error(GlobalErrorCodeConstants.EXPIRE_ERROR);
+ }
+ List activeProfile = ProfileUtil.getActiveProfile();
+ //dev环境不校验签名
+ if(!activeProfile.contains("dev") && !StringUtils.equals(sign, computeSign.toUpperCase())) {
+ return CommonResult.error(GlobalErrorCodeConstants.SIGN_ERROR);
+ }
+ try {
+ Method method = this.getClass().getMethod(interfaceName, String.class);
+ return (CommonResult) method.invoke(this, body);
+ } catch (NoSuchMethodException e) {
+ return CommonResult.error(GlobalErrorCodeConstants.INTERFACE_ERROR);
+ } catch (Exception e) {
+ return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ public CommonResult