changsl 1 сар өмнө
parent
commit
df052b3a2e

+ 33 - 0
.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/

+ 100 - 0
pom.xml

@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.example</groupId>
+    <artifactId>test03-30</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>test03-30</name>
+    <description>test03-30</description>
+    <properties>
+        <java.version>1.8</java.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring-boot.version>2.6.13</spring-boot.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>2.2.2</version>
+        </dependency>
+
+
+
+        <dependency>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <!-- Validation -->
+        <dependency>
+            <groupId>jakarta.validation</groupId>
+            <artifactId>jakarta.validation-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.33</version> <!-- 根据实际情况选择版本 -->
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring-boot.version}</version>
+                <configuration>
+                    <mainClass>com.example.test0330.Test0330Application</mainClass>
+<!--                    <skip>true</skip>-->
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>repackage</id>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
src/main/java/com/example/test0330/Test0330Application.java

@@ -0,0 +1,13 @@
+package com.example.test0330;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Test0330Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Test0330Application.class, args);
+    }
+
+}

+ 28 - 0
src/main/java/com/example/test0330/demos/web/controller/RequwatEntityController.java

@@ -0,0 +1,28 @@
+package com.example.test0330.demos.web.controller;
+
+
+import com.example.test0330.demos.web.pojo.RequestEntity;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+
+@RestController
+@RequestMapping("api")
+@Slf4j
+public class RequwatEntityController {
+
+    @PostMapping("/post")
+    public String post(@RequestHeader("reqId") String reqId, @Valid @RequestBody RequestEntity request){
+        System.err.println(request);
+        return "reqId= "+reqId+" request="+request;
+    }
+
+    @GetMapping("get")
+    public String get(@RequestHeader("reqId") String reqId,@Valid @ModelAttribute RequestEntity request){
+        System.err.println(request);
+        return "reqId= "+reqId;
+    }
+
+
+}

+ 263 - 0
src/main/java/com/example/test0330/demos/web/pojo/EraHelper.java

@@ -0,0 +1,263 @@
+package com.example.test0330.demos.web.pojo;
+
+import lombok.extern.slf4j.Slf4j;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.UUID;
+
+
+/**
+ * 项目相关帮助函数。
+ * 
+ *
+ */
+@Slf4j
+public class EraHelper {
+
+	 
+
+	/**
+	 * 生产新的UUID。
+	 * 
+	 * @return uuid
+	 */
+	public static final String newUUID() {
+		return UUID.randomUUID().toString().replaceAll("-", "");
+	}
+
+	/**
+	 * 判断银行卡号是否合法。
+	 * 
+	 * @param cardNumber
+	 *            银行卡号
+	 * @return boolean 表示合法
+	 */
+	public static final boolean isBankCard(String cardNumber) {
+		if (cardNumber == null || cardNumber.isEmpty() || cardNumber.length() <= 2) {
+			return false;
+		}
+		char[] chs = cardNumber.trim().toCharArray();
+		int luhmSum = 0;
+		for (int i = chs.length - 2, j = 0; i >= 0; i--, j++) {
+			if (!Character.isDigit(chs[i])) {
+				return false;
+			}
+			int k = chs[i] - '0';
+			if (j % 2 == 0) {
+				k *= 2;
+				k = k / 10 + k % 10;
+			}
+			luhmSum += k;
+		}
+		return ((luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0')) == chs[chs.length - 1];
+	}
+
+	/**
+	 * 获取远程请求IP
+	 * 
+	 * @param request
+	 *            HTTP请求
+	 * @return String ip
+	 */
+	public static String getRemoteIP(HttpServletRequest request) {
+		String xff = request.getHeader("X-Forwarded-For");
+		if (xff != null && !xff.isEmpty()) {
+			return xff.split(", ?")[0];
+		} else {
+			return request.getRemoteAddr();
+		}
+	}
+
+	/**
+	 * 判断手机号是否合法。
+	 * 
+	 * @param mobile
+	 *            手机号码
+	 * @return boolean 表示合法
+	 */
+	public static boolean isMobile(String mobile) {
+		if (mobile == null || mobile.length() != 11) {
+			return false;
+		}
+		int index = 0;
+		for (char ch : mobile.toCharArray()) {
+			if (index == 0 && ch != '1') {
+				return false;
+			}
+			if (!Character.isDigit(ch)) {
+				return false;
+			}
+			index++;
+		}
+		return true;
+	}
+
+	/**
+	 * 手机号打码
+	 * 
+	 * @param mobile
+	 *            手机号码
+	 * @return String 打码后的手机号码
+	 */
+	public static String maskMobile(String mobile) {
+		StringBuilder builder = new StringBuilder();
+		builder.append(mobile, 0, 3);
+		builder.append("****").append(mobile.substring(7));
+		return builder.toString();
+	}
+
+	/**
+	 * 银行卡号打码
+	 * 
+	 * @param cardNumber
+	 *            银行卡号
+	 * @return String 打码后的银行卡号
+	 */
+	public static String maskBankCard(String cardNumber) {
+		StringBuilder builder = new StringBuilder();
+		if (cardNumber.length() > 8) {
+			builder.append(cardNumber, 0, 4);
+			builder.append(" **** ").append(" **** ").append(cardNumber.substring(cardNumber.length() - 4));
+		}
+		return builder.toString();
+	}
+
+	/**
+	 * 判断短信验证码是否合法。
+	 * 
+	 * @param verifyCode
+	 *            短信验证码
+	 * @return boolean 表示合法
+	 */
+	public static boolean isSMSCode(String verifyCode) {
+		if (verifyCode == null || verifyCode.length() != 4) {
+			return false;
+		}
+		for (char ch : verifyCode.toCharArray()) {
+			if (!Character.isDigit(ch)) {
+				return false;
+			}
+		}
+		return true;
+	}
+
+	/**
+	 * 转换为十六进制字符串
+	 * 
+	 * @param b
+	 *            字节数组
+	 * @return String 十六进制字符串
+	 */
+	public static String byte2HexStr(byte[] b) {
+		StringBuilder sb = new StringBuilder("");
+		for (int n = 0; n < b.length; n++) {
+			String stmp = Integer.toHexString(b[n] & 0xFF);
+			sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
+		}
+		return sb.toString().toUpperCase().trim();
+	}
+
+	 
+
+	/**
+	 * 密码等资料混淆摘要
+	 * 
+	 * @param content
+	 *            待混淆内容
+	 * @return String
+	 */
+	public static String confuse(String content) {
+		if (content == null || content.isEmpty()) {
+			return null;
+		}
+		try {
+			MessageDigest md5 = MessageDigest.getInstance("MD5");
+			md5.update(content.getBytes("UTF-8"));
+			MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
+			return byte2HexStr(sha256.digest(md5.digest()));
+		} catch (Throwable e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+
+	
+	
+	 
+	private static double EARTH_RADIUS = 6378.137;// 地球半径
+
+	private static double rad(double d) {
+		return d * Math.PI / 180.0;
+	}
+
+	/**
+	 * 计算两个经纬度坐标距离.
+	 * 
+	 * @param lat1
+	 * @param lng1
+	 * @param lat2
+	 * @param lng2
+	 * @return double
+	 */
+	public static BigDecimal getDistance(double lat1, double lng1, double lat2, double lng2) {
+		double radLat1 = rad(lat1);
+		double radLat2 = rad(lat2);
+		double a = radLat1 - radLat2;
+		double b = rad(lng1) - rad(lng2);
+		double s = 2 * Math.asin(Math.sqrt(
+				Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
+		s = s * EARTH_RADIUS;
+		return BigDecimal.valueOf(s).setScale(2, BigDecimal.ROUND_HALF_UP);
+	}
+
+	/**
+	 * 判断是否为微信。
+	 * 
+	 * @param userAgent
+	 *            终端类型字符串
+	 * @return boolean true表示终端类型为微信
+	 */
+	public static boolean isWeiXin(String userAgent) {
+		return userAgent == null || userAgent.isEmpty() ? false : userAgent.indexOf("MicroMessenger") != -1;
+	}
+
+	/**
+	 * 判断是否为安卓。
+	 * 
+	 * @param userAgent
+	 *            终端类型字符串
+	 * @return boolean true表示终端类型为安卓
+	 */
+	public static boolean isAndroid(String userAgent) {
+		return userAgent == null || userAgent.isEmpty() ? false : userAgent.indexOf("Android") != -1;
+	}
+
+	/**
+	 * 判断是否为iPhone。
+	 * 
+	 * @param userAgent
+	 *            终端类型字符串
+	 * @return boolean true表示终端类型为iPhone
+	 */
+	public static boolean isIPhone(String userAgent) {
+		return userAgent == null || userAgent.isEmpty() ? false : userAgent.indexOf("iPhone") != -1;
+	}
+
+	public static void main(String args[]){
+		String param="123456";
+		String sign = null;
+		try {
+			sign = EraHelper
+					.byte2HexStr(MessageDigest.getInstance("MD5").digest(param.toString().getBytes("UTF-8")));
+		} catch (NoSuchAlgorithmException e) {
+			throw new RuntimeException(e);
+		} catch (UnsupportedEncodingException e) {
+			throw new RuntimeException(e);
+		}
+		log.info("sign========="+sign);
+	}
+}

+ 36 - 0
src/main/java/com/example/test0330/demos/web/pojo/RequestEntity.java

@@ -0,0 +1,36 @@
+package com.example.test0330.demos.web.pojo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import javax.validation.constraints.*;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class RequestEntity implements Serializable {
+    @NotBlank(message = "应用id不能为空")
+    private String appId;
+ //   private String sign;
+
+
+    @NotNull(message = "时间戳不能为空")
+    private Long reqTime;
+    @NotNull(message = "商品id不能为空")
+    private Long goodsId;
+    @NotNull(message = "数量不能为空")
+    private Integer amount;
+    @NotNull(message = "价格不能为空")
+    @DecimalMax(value = "9999",message = "最大值9999")
+    @DecimalMin(value = "0",message = "最小值0")
+    private BigDecimal price; //价格
+    @NotBlank(message = "手机号不能为空")
+    @Pattern(regexp = "^((13[0-9])|(14[0-9])|(15[0-9])|(166)|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$",message = "手机号格式不正确")
+    private String mobile;
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",shape = JsonFormat.Shape.STRING)
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
+    @NotNull(message = "日期不能为空")
+    private Date nowDate;
+}

+ 3 - 0
src/main/resources/application.properties

@@ -0,0 +1,3 @@
+## 应用服务 WEB 访问端口
+#server.port=8080
+#

+ 8 - 0
src/main/resources/application.yml

@@ -0,0 +1,8 @@
+server:
+  port: 7070
+spring:
+  datasource:
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://localhost:3306
+    username: root
+    password: root

+ 13 - 0
src/test/java/com/example/test0330/Test0330ApplicationTests.java

@@ -0,0 +1,13 @@
+package com.example.test0330;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class Test0330ApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}