From ff030bf22d56f4fd53a41d37dd701797fa65b5aa Mon Sep 17 00:00:00 2001
From: Bunny <1319900154@qq.com>
Date: Tue, 26 Dec 2023 15:12:19 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/inspectionProfiles/Project_Default.xml | 2 +
.idea/workspace.xml | 90 +++++--
.../java/cn/bunny/config/Knife4jConfig.java | 10 +-
spzx-common/common-util/pom.xml | 27 ++
.../main/java/cn/bunny/common/HttpUtils.java | 238 ++++++++++++++++++
.../src/main/resources/application-dev.yml | 4 +
.../bunny/web/product/config/RedisConfig.java | 7 +-
.../service/imp/ProductServiceImpl.java | 19 +-
.../src/main/resources/application-dev.yml | 2 -
spzx-service/service-user/pom.xml | 4 +-
.../src/main/java/cn/bunny/user/User.java | 13 +
.../cn/bunny/user/config/RedisConfig.java | 51 ++++
.../bunny/user/controller/SmsController.java | 24 ++
.../cn/bunny/user/service/SmsService.java | 6 +
.../user/service/impl/SmsServiceImpl.java | 63 +++++
.../src/main/java/cn/bunny/web/App.java | 13 -
.../src/main/resources/application-dev.yml | 22 ++
.../src/main/resources/application.yml | 3 +
18 files changed, 537 insertions(+), 61 deletions(-)
create mode 100644 spzx-common/common-util/src/main/java/cn/bunny/common/HttpUtils.java
create mode 100644 spzx-service/service-user/src/main/java/cn/bunny/user/User.java
create mode 100644 spzx-service/service-user/src/main/java/cn/bunny/user/config/RedisConfig.java
create mode 100644 spzx-service/service-user/src/main/java/cn/bunny/user/controller/SmsController.java
create mode 100644 spzx-service/service-user/src/main/java/cn/bunny/user/service/SmsService.java
create mode 100644 spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/SmsServiceImpl.java
delete mode 100644 spzx-service/service-user/src/main/java/cn/bunny/web/App.java
create mode 100644 spzx-service/service-user/src/main/resources/application-dev.yml
create mode 100644 spzx-service/service-user/src/main/resources/application.yml
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index df280b1..b99173c 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -1,7 +1,9 @@
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index bc615df..0b455dc 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,17 +4,25 @@
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
-
+
+
+
@@ -24,9 +32,9 @@
@@ -78,7 +86,7 @@
"code.cleanup.on.save": "true",
"git-widget-placeholder": "master",
"jdk.selected.JAVA_MODULE": "21",
- "last_opened_file_path": "F:/File/Java/spzx-parent/spzx-service/service-product/src/main/java/cn/bunny/web/product",
+ "last_opened_file_path": "F:/File/Java/spzx-parent/spzx-service/service-user/src/main/java/cn/bunny/user",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -102,6 +110,8 @@
+
+
@@ -119,7 +129,7 @@
-
+
@@ -146,10 +156,38 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
@@ -163,7 +201,7 @@
1703487755445
-
+
@@ -221,7 +259,15 @@
1703559306235
-
+
+
+ 1703567148967
+
+
+
+ 1703567148967
+
+
@@ -234,18 +280,10 @@
-
+
+
-
-
-
- file://$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
- 33
-
-
-
-
diff --git a/spzx-common/common-service/src/main/java/cn/bunny/config/Knife4jConfig.java b/spzx-common/common-service/src/main/java/cn/bunny/config/Knife4jConfig.java
index 7337c9a..6a3e5a2 100644
--- a/spzx-common/common-service/src/main/java/cn/bunny/config/Knife4jConfig.java
+++ b/spzx-common/common-service/src/main/java/cn/bunny/config/Knife4jConfig.java
@@ -13,11 +13,19 @@ public class Knife4jConfig {
@Bean
public GroupedOpenApi adminApi() {// 创建API分组
return GroupedOpenApi.builder()
- .group("admin-api111")
+ .group("后端接口")
.pathsToMatch("/admin/**")
.build();
}
+ @Bean
+ public GroupedOpenApi UserApi() {// 创建API分组
+ return GroupedOpenApi.builder()
+ .group("前端用户")
+ .pathsToMatch("/api/**")
+ .build();
+ }
+
@Bean
public OpenAPI customerOpenApi() {
return new OpenAPI().info(new Info().title("接口文档").version("1.0").contact(new Contact()
diff --git a/spzx-common/common-util/pom.xml b/spzx-common/common-util/pom.xml
index 81aa3ae..512c076 100644
--- a/spzx-common/common-util/pom.xml
+++ b/spzx-common/common-util/pom.xml
@@ -41,5 +41,32 @@
spring-boot-starter-web
provided
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.2.1
+
+
+ org.apache.httpcomponents
+ httpcore
+ 4.2.1
+
+
+ commons-lang
+ commons-lang
+ 2.6
+
+
+ org.eclipse.jetty
+ jetty-util
+ 9.3.7.v20160115
+
+
+ junit
+ junit
+ 4.5
+ test
+
\ No newline at end of file
diff --git a/spzx-common/common-util/src/main/java/cn/bunny/common/HttpUtils.java b/spzx-common/common-util/src/main/java/cn/bunny/common/HttpUtils.java
new file mode 100644
index 0000000..3190e88
--- /dev/null
+++ b/spzx-common/common-util/src/main/java/cn/bunny/common/HttpUtils.java
@@ -0,0 +1,238 @@
+package cn.bunny.common;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class HttpUtils {
+ public static HttpResponse doGet(String host, String path, String method,
+ Map headers,
+ Map querys)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpGet request = new HttpGet(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ return httpClient.execute(request);
+ }
+
+ public static HttpResponse doPost(String host, String path, String method,
+ Map headers,
+ Map querys,
+ Map bodys)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ if (bodys != null) {
+ List nameValuePairList = new ArrayList();
+
+ for (String key : bodys.keySet()) {
+ nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
+ }
+ UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
+ formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
+ request.setEntity(formEntity);
+ }
+
+ return httpClient.execute(request);
+ }
+
+
+ public static HttpResponse doPost(String host, String path, String method,
+ Map headers,
+ Map querys,
+ String body)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ if (StringUtils.isNotBlank(body)) {
+ request.setEntity(new StringEntity(body, "utf-8"));
+ }
+
+ return httpClient.execute(request);
+ }
+
+
+ public static HttpResponse doPost(String host, String path, String method,
+ Map headers,
+ Map querys,
+ byte[] body)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ if (body != null) {
+ request.setEntity(new ByteArrayEntity(body));
+ }
+
+ return httpClient.execute(request);
+ }
+
+
+ public static HttpResponse doPut(String host, String path, String method,
+ Map headers,
+ Map querys,
+ String body)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpPut request = new HttpPut(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ if (StringUtils.isNotBlank(body)) {
+ request.setEntity(new StringEntity(body, "utf-8"));
+ }
+
+ return httpClient.execute(request);
+ }
+
+
+ public static HttpResponse doPut(String host, String path, String method,
+ Map headers,
+ Map querys,
+ byte[] body)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpPut request = new HttpPut(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ if (body != null) {
+ request.setEntity(new ByteArrayEntity(body));
+ }
+
+ return httpClient.execute(request);
+ }
+
+
+ public static HttpResponse doDelete(String host, String path, String method,
+ Map headers,
+ Map querys)
+ throws Exception {
+ HttpClient httpClient = wrapClient(host);
+
+ HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
+ for (Map.Entry e : headers.entrySet()) {
+ request.addHeader(e.getKey(), e.getValue());
+ }
+
+ return httpClient.execute(request);
+ }
+
+ private static String buildUrl(String host, String path, Map querys) throws UnsupportedEncodingException {
+ StringBuilder sbUrl = new StringBuilder();
+ sbUrl.append(host);
+ if (!StringUtils.isBlank(path)) {
+ sbUrl.append(path);
+ }
+ if (null != querys) {
+ StringBuilder sbQuery = new StringBuilder();
+ for (Map.Entry query : querys.entrySet()) {
+ if (0 < sbQuery.length()) {
+ sbQuery.append("&");
+ }
+ if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
+ sbQuery.append(query.getValue());
+ }
+ if (!StringUtils.isBlank(query.getKey())) {
+ sbQuery.append(query.getKey());
+ if (!StringUtils.isBlank(query.getValue())) {
+ sbQuery.append("=");
+ sbQuery.append(URLEncoder.encode(query.getValue(), StandardCharsets.UTF_8));
+ }
+ }
+ }
+ if (0 < sbQuery.length()) {
+ sbUrl.append("?").append(sbQuery);
+ }
+ }
+
+ return sbUrl.toString();
+ }
+
+ private static HttpClient wrapClient(String host) {
+ HttpClient httpClient = new DefaultHttpClient();
+ if (host.startsWith("https://")) {
+ sslClient(httpClient);
+ }
+
+ return httpClient;
+ }
+
+ private static void sslClient(HttpClient httpClient) {
+ try {
+ SSLContext ctx = SSLContext.getInstance("TLS");
+ X509TrustManager tm = new X509TrustManager() {
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+
+ public void checkClientTrusted(X509Certificate[] xcs, String str) {
+
+ }
+
+ public void checkServerTrusted(X509Certificate[] xcs, String str) {
+
+ }
+ };
+ ctx.init(null, new TrustManager[]{tm}, null);
+ SSLSocketFactory ssf = new SSLSocketFactory(ctx);
+ ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+ ClientConnectionManager ccm = httpClient.getConnectionManager();
+ SchemeRegistry registry = ccm.getSchemeRegistry();
+ registry.register(new Scheme("https", 443, ssf));
+ } catch (KeyManagementException ex) {
+ throw new RuntimeException(ex);
+ } catch (NoSuchAlgorithmException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+}
\ No newline at end of file
diff --git a/spzx-server-gateway/src/main/resources/application-dev.yml b/spzx-server-gateway/src/main/resources/application-dev.yml
index 0c5dab3..08b9eed 100644
--- a/spzx-server-gateway/src/main/resources/application-dev.yml
+++ b/spzx-server-gateway/src/main/resources/application-dev.yml
@@ -26,6 +26,10 @@ spring:
uri: lb://service-product
predicates:
- Path=/*/product/**
+ - id: service-user
+ uri: lb://service-user
+ predicates:
+ - Path=/*/user/**
datasource:
type: com.zaxxer.hikari.HikariDataSource
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
index a14d3ae..472d7ff 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
@@ -34,14 +34,13 @@ public class RedisConfig {
@Bean
public CacheManager cacheManager(LettuceConnectionFactory connectionFactory) {
- //定义序列化器
+ // 定义序列化器
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
-
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
- //过期时间600秒
- .entryTtl(Duration.ofSeconds(600))
+ // 过期时间600秒
+ .entryTtl(Duration.ofSeconds(604800))
// 配置序列化
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
index 73f213c..a33b4db 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
@@ -23,10 +23,8 @@ import java.util.*;
public class ProductServiceImpl implements ProductService {
@Resource
private ProductSkuMapper productSkuMapper;
-
@Resource
private ProductMapper productMapper;
-
@Resource
private ProductDetailsMapper productDetailsMapper;
@Resource
@@ -35,8 +33,8 @@ public class ProductServiceImpl implements ProductService {
@Cacheable(value = "productSku", key = "'one'")
@Override
public List selectProductSkuBySal() {
-/* // 1. 查询redis是否有一级分类
- Object productSkuJson = redisTemplate.opsForValue().get("productSku:one");
+ // 1. 查询redis是否有一级分类
+ Object productSkuJson = redisTemplate.opsForValue().get("productSku::one");
String productSkuJsonString = JSON.toJSONString(productSkuJson);
// 2. 如果存在一级分类直接返回即可
@@ -44,15 +42,10 @@ public class ProductServiceImpl implements ProductService {
return JSON.parseArray(productSkuJsonString);
}
// 3. 如果redis没有一级分类,查询数据库,把数据库内容返回,并且查询放到redis中
- List productSkuList = productSkuMapper.selectProductSkuBySale();
- redisTemplate.opsForValue()
- .set("productSku:one", JSON.toJSON(productSkuList), 7, TimeUnit.DAYS);
- return productSkuList; */
- Object productSkuJson = redisTemplate.opsForValue().get("productSku:one");
- String productSkuJsonString = JSON.toJSONString(productSkuJson);
- if (!Objects.equals(productSkuJsonString, "null")) {
- return JSON.parseArray(productSkuJsonString);
- }
+ // List productSkuList = productSkuMapper.selectProductSkuBySale();
+ // redisTemplate.opsForValue()
+ // .set("productSku::one", JSON.toJSON(productSkuList), 7, TimeUnit.DAYS);
+ // return productSkuList;
return productSkuMapper.selectProductSkuBySale();
}
diff --git a/spzx-service/service-product/src/main/resources/application-dev.yml b/spzx-service/service-product/src/main/resources/application-dev.yml
index cb3a4aa..9f4cf36 100644
--- a/spzx-service/service-product/src/main/resources/application-dev.yml
+++ b/spzx-service/service-product/src/main/resources/application-dev.yml
@@ -8,8 +8,6 @@ spring:
nacos:
discovery:
server-addr: 192.168.2.156:8848
-
-
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
diff --git a/spzx-service/service-user/pom.xml b/spzx-service/service-user/pom.xml
index 50ceefe..2df4fe5 100644
--- a/spzx-service/service-user/pom.xml
+++ b/spzx-service/service-user/pom.xml
@@ -1,8 +1,8 @@
-
4.0.0
- org.example
+ cn.bunny
spzx-service
1.0-SNAPSHOT
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/user/User.java b/spzx-service/service-user/src/main/java/cn/bunny/user/User.java
new file mode 100644
index 0000000..80e4d79
--- /dev/null
+++ b/spzx-service/service-user/src/main/java/cn/bunny/user/User.java
@@ -0,0 +1,13 @@
+package cn.bunny.user;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@ComponentScan(basePackages = {"cn.bunny"})
+public class User {
+ public static void main(String[] args) {
+ SpringApplication.run(User.class, args);
+ }
+}
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/user/config/RedisConfig.java b/spzx-service/service-user/src/main/java/cn/bunny/user/config/RedisConfig.java
new file mode 100644
index 0000000..3aed9b0
--- /dev/null
+++ b/spzx-service/service-user/src/main/java/cn/bunny/user/config/RedisConfig.java
@@ -0,0 +1,51 @@
+package cn.bunny.user.config;
+
+import org.springframework.cache.CacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializationContext;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.time.Duration;
+
+@Configuration
+public class RedisConfig {
+ @Bean
+ public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+ RedisTemplate redisTemplate = new RedisTemplate<>();
+ redisTemplate.setConnectionFactory(lettuceConnectionFactory);
+
+ // 设置key序列化为String
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
+ // 设置value序列化方式为JSON,使用GenericJackson2JsonRedisSerializer替换为默认序列化
+ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+ redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
+
+ return redisTemplate;
+ }
+
+ @Bean
+ public CacheManager cacheManager(LettuceConnectionFactory connectionFactory) {
+
+ // 定义序列化器
+ GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
+ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
+
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
+ // 过期时间600秒
+ .entryTtl(Duration.ofSeconds(600))
+ // 配置序列化
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
+
+ return RedisCacheManager.builder(connectionFactory)
+ .cacheDefaults(config)
+ .build();
+ }
+}
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/user/controller/SmsController.java b/spzx-service/service-user/src/main/java/cn/bunny/user/controller/SmsController.java
new file mode 100644
index 0000000..5a85a8f
--- /dev/null
+++ b/spzx-service/service-user/src/main/java/cn/bunny/user/controller/SmsController.java
@@ -0,0 +1,24 @@
+package cn.bunny.user.controller;
+
+import cn.bunny.common.spzx.model.vo.common.Result;
+import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
+import cn.bunny.user.service.SmsService;
+import io.swagger.v3.oas.annotations.Operation;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/api/user/sms")
+public class SmsController {
+ @Resource
+ private SmsService smsService;
+
+ @GetMapping(value = "/sendCode/{phone}")
+ @Operation(summary = "短信验证码", operationId = "发送短信验证码")
+ public Result sendValidateCode(@PathVariable("phone") String phone) {
+ return Result.build(smsService.sendCode(phone), ResultCodeEnum.SUCCESS);
+ }
+}
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/user/service/SmsService.java b/spzx-service/service-user/src/main/java/cn/bunny/user/service/SmsService.java
new file mode 100644
index 0000000..610529f
--- /dev/null
+++ b/spzx-service/service-user/src/main/java/cn/bunny/user/service/SmsService.java
@@ -0,0 +1,6 @@
+package cn.bunny.user.service;
+
+public interface SmsService {
+ // 短信验证码
+ Object sendCode(String phone);
+}
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/SmsServiceImpl.java b/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/SmsServiceImpl.java
new file mode 100644
index 0000000..87dc158
--- /dev/null
+++ b/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/SmsServiceImpl.java
@@ -0,0 +1,63 @@
+package cn.bunny.user.service.impl;
+
+import cn.bunny.common.HttpUtils;
+import cn.bunny.user.service.SmsService;
+import com.alibaba.nacos.common.utils.StringUtils;
+import jakarta.annotation.Resource;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.http.HttpResponse;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class SmsServiceImpl implements SmsService {
+ @Resource
+ private RedisTemplate redisTemplate;
+
+ // 短信验证码
+ @Override
+ public Object sendCode(String phone) {
+ Object code = redisTemplate.opsForValue().get(phone);
+ if (StringUtils.hasText((String) code)) {
+ return code;
+ }
+
+ // 1.生成短信验证码
+ code = RandomStringUtils.randomNumeric(4);
+ // 2.放入Redis中,设置过期时间
+ redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
+ // 3. 向手机号发送短信验证码
+ sendMessage(phone, (String) code);
+
+ return code;
+ }
+
+ // 发送验证码
+ private void sendMessage(String phone, String code) {
+ String host = "https://dfsns.market.alicloudapi.com";
+ String path = "/data/send_sms";
+ String method = "POST";
+ String appcode = "5a4cb12374904a7fa2975ec5a1a9d50f";
+ Map headers = new HashMap();
+ headers.put("Authorization", "APPCODE " + appcode);
+ // 根据API的要求,定义相对应的Content-Type
+ headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+ Map querys = new HashMap();
+ Map bodys = new HashMap();
+ bodys.put("content", "code:" + code);
+ bodys.put("template_id", "CST_ptdie100"); // 该模板为调试接口专用,短信下发有受限制,调试成功后请联系客服报备专属模板
+ bodys.put("phone_number", phone);
+
+
+ try {
+ HttpResponse httpResponse = HttpUtils.doPost(host, path, method, headers, querys, bodys);
+ System.out.println(httpResponse.toString());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/spzx-service/service-user/src/main/java/cn/bunny/web/App.java b/spzx-service/service-user/src/main/java/cn/bunny/web/App.java
deleted file mode 100644
index dc5b5b5..0000000
--- a/spzx-service/service-user/src/main/java/cn/bunny/web/App.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package cn.bunny.web;
-
-/**
- * Hello world!
- *
- */
-public class App
-{
- public static void main( String[] args )
- {
- System.out.println( "Hello World!" );
- }
-}
diff --git a/spzx-service/service-user/src/main/resources/application-dev.yml b/spzx-service/service-user/src/main/resources/application-dev.yml
new file mode 100644
index 0000000..626b340
--- /dev/null
+++ b/spzx-service/service-user/src/main/resources/application-dev.yml
@@ -0,0 +1,22 @@
+server:
+ port: 8512
+
+spring:
+ application:
+ name: service-user
+ cloud:
+ nacos:
+ discovery:
+ server-addr: 192.168.2.156:8848
+
+
+ datasource:
+ type: com.zaxxer.hikari.HikariDataSource
+ driver-class-name: com.mysql.cj.jdbc.Driver
+ url: jdbc:mysql://60.204.230.80:3306/db_spzx?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+ data:
+ redis:
+ port: 6379
+ host: 192.168.2.156
\ No newline at end of file
diff --git a/spzx-service/service-user/src/main/resources/application.yml b/spzx-service/service-user/src/main/resources/application.yml
new file mode 100644
index 0000000..caf4dfc
--- /dev/null
+++ b/spzx-service/service-user/src/main/resources/application.yml
@@ -0,0 +1,3 @@
+spring:
+ profiles:
+ active: dev
\ No newline at end of file