Browse Source

first commit

咸鱼香香 1 tháng trước cách đây
commit
fb85832de8

+ 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/

+ 104 - 0
pom.xml

@@ -0,0 +1,104 @@
+<?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>cn.zhentao</groupId>
+    <artifactId>demo</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>demo</name>
+    <description>Demo project for Spring Boot</description>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>3.0.2</version>
+        <relativePath/>
+    </parent>
+
+    <properties>
+        <java.version>17</java.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.25</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>2.0.42</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <excludes>
+                        <exclude>
+                            <groupId>org.projectlombok</groupId>
+                            <artifactId>lombok</artifactId>
+                        </exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <repositories>
+        <repository>
+            <id>aliyun</id>
+            <name>aliyun</name>
+            <url>https://maven.aliyun.com/repository/public</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
+
+    <pluginRepositories>
+        <pluginRepository>
+            <id>aliyun-plugin</id>
+            <name>aliyun-plugin</name>
+            <url>https://maven.aliyun.com/repository/public</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </pluginRepository>
+    </pluginRepositories>
+</project> 

+ 14 - 0
src/main/java/cn/zhentao/Day0331Application.java

@@ -0,0 +1,14 @@
+package cn.zhentao;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+public class Day0331Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Day0331Application.class, args);
+    }
+
+}

+ 36 - 0
src/main/java/cn/zhentao/controller/OrderController.java

@@ -0,0 +1,36 @@
+package cn.zhentao.controller;
+
+import cn.zhentao.dto.OrderRequest;
+import jakarta.validation.Valid;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/api/orders")
+public class OrderController {
+
+    @PostMapping("/create")
+    public ResponseEntity<Map<String, Object>> createOrder(
+            @RequestHeader("reqId") String reqId,
+            @Valid @RequestBody OrderRequest request) {
+        Map<String, Object> response = new HashMap<>();
+        response.put("reqId", reqId);
+        response.put("success", true);
+        response.put("message", "订单创建成功");
+        return ResponseEntity.ok(response);
+    }
+
+    @GetMapping("/create")
+    public ResponseEntity<Map<String, Object>> createOrderForm(
+            @RequestHeader("reqId") String reqId,
+            @Valid @ModelAttribute OrderRequest request) {
+        Map<String, Object> response = new HashMap<>();
+        response.put("reqId", reqId);
+        response.put("success", true);
+        response.put("message", "订单创建成功");
+        return ResponseEntity.ok(response);
+    }
+} 

+ 35 - 0
src/main/java/cn/zhentao/dto/OrderRequest.java

@@ -0,0 +1,35 @@
+package cn.zhentao.dto;
+
+import lombok.Data;
+import jakarta.validation.constraints.NotBlank;
+import jakarta.validation.constraints.NotNull;
+import jakarta.validation.constraints.Pattern;
+import java.math.BigDecimal;
+
+@Data
+public class OrderRequest {
+    @NotNull(message = "appId不能为空")
+    private Integer appId;
+
+    @NotBlank(message = "sign不能为空")
+    private String sign;
+
+    @NotBlank(message = "reqTime不能为空")
+    private String reqTime;
+
+    @NotNull(message = "goodsId不能为空")
+    private Integer goodsId;
+
+    @NotNull(message = "amount不能为空")
+    private Integer amount;
+
+    @NotNull(message = "price不能为空")
+    private BigDecimal price;
+
+    @NotBlank(message = "mobile不能为空")
+    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
+    private String mobile;
+
+    @NotBlank(message = "nowDate不能为空")
+    private String nowDate;
+} 

+ 264 - 0
src/main/java/cn/zhentao/test/EraHelper.java

@@ -0,0 +1,264 @@
+package cn.zhentao.test;
+
+import jakarta.servlet.http.HttpServletRequest;
+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);
+	}
+}

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

@@ -0,0 +1,8 @@
+server.port=8080
+spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
+spring.jackson.time-zone=GMT+8
+spring.jackson.serialization.write-dates-as-timestamps=false
+spring.datasource.url=jdbc:mysql://localhost:3306/week1
+spring.datasource.username=root
+spring.datasource.password=root
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

+ 6 - 0
src/main/resources/static/index.html

@@ -0,0 +1,6 @@
+<html>
+<body>
+<h1>hello word!!!</h1>
+<p>this is a html page</p>
+</body>
+</html>

+ 13 - 0
src/test/java/cn/zhentao/day0331/Day0331ApplicationTests.java

@@ -0,0 +1,13 @@
+package cn.zhentao.day0331;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class Day0331ApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}