From e82de75e0842deb5b25bb8364d9080fbdc65ecb7 Mon Sep 17 00:00:00 2001 From: weiyachao <13526234727@126.com> Date: Mon, 7 Aug 2023 15:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=99=9A=E4=B8=8A=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/user/api/config/WebClientConfig.java | 26 +++++ .../user/api/controller/UserController.java | 110 ++++++++++++++++-- .../src/main/resources/bootstrap.yml | 9 +- 3 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/config/WebClientConfig.java diff --git a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/config/WebClientConfig.java b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/config/WebClientConfig.java new file mode 100644 index 0000000..353689f --- /dev/null +++ b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/config/WebClientConfig.java @@ -0,0 +1,26 @@ +package com.qiuguo.iot.user.api.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE; + +/** + * XXX + * + * @author weiyachao + * @since 2023/8/7 11:19 + */ +@Component +public class WebClientConfig { + + @Bean + public WebClient webClient() { + return WebClient.builder() + .defaultHeader(HttpHeaders.CONTENT_TYPE,APPLICATION_FORM_URLENCODED_VALUE) + .defaultHeader("Api-Type","web") + .build(); + } +} diff --git a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/UserController.java b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/UserController.java index 73a98a9..b1fb4f7 100644 --- a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/UserController.java +++ b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/UserController.java @@ -8,9 +8,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @@ -30,7 +32,7 @@ import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VAL public class UserController { private final WebClient webClient = WebClient.builder() .defaultHeader(HttpHeaders.CONTENT_TYPE,APPLICATION_FORM_URLENCODED_VALUE) - // .defaultHeader("Api-Type","box") + .defaultHeader("Api-Type","web") .build(); @Value("${userUrl.baseUrl}") @@ -45,20 +47,110 @@ public class UserController { @Value("${userUrl.sendSmsUrl}") private String sendSmsUrl; + @Value("${userUrl.registerUrl}") + private String registerUrl; + + + + /** + * 设置登录密码-auth + */ + + /** + * 修改登录密码-auth + */ + + /** + * 账号注销-auth + */ + + /** + * 退出登录 -auth + */ + + /** + * 修改用户信息-auth + */ + + + /** + * 发送验证码 + */ + @PostMapping("/sendSms") + public Mono sendSms(@RequestBody JSONObject jsonObject) { + return webClient.post().uri(baseUrl + sendSmsUrl).bodyValue(getMultiValueMap(jsonObject)).retrieve() + .bodyToMono(JSONObject.class) + .doOnNext(res->{ + if (!Objects.equals(res.getInteger("code"), 1)) { + throw new RuntimeException(res.getString("info")); + } + }); + } + + /** + * 验证码登录 + */ + @PostMapping("/login/phone") + public Mono loginPhone(@RequestBody JSONObject jsonObject) { + //登录 + return webClient.post().uri(baseUrl + smsUrl).bodyValue(getMultiValueMap(jsonObject)) + .retrieve().bodyToMono(JSONObject.class).flatMap(res -> { + System.out.println("res = " + res); + if (Objects.equals(res.getInteger("code"), 1) && !res.getString("info") + .contains("该手机号还没有注册哦")) { + return Mono.just(res); + } else if(!res.getString("info").contains("该手机号还没有注册哦")){ + return Mono.error(new RuntimeException(res.getString("info"))); + }else { + // 注册 + MultiValueMap register = new LinkedMultiValueMap<>(); + register.add("phone", jsonObject.getString("phone")); + register.add("password", "null1null2null3"); + register.add("true_password", "null1null2null3"); + register.add("verify", jsonObject.getString("verify")); + register.add("pay_password", null); + return webClient.post().uri(baseUrl + registerUrl).bodyValue(register).retrieve() + .bodyToMono(JSONObject.class).flatMap(registerRes -> { + System.out.println("registerRes = " + registerRes); + if (!Objects.equals(registerRes.getInteger("code"), 1)) { + return Mono.error(new RuntimeException(registerRes.getString("info"))); + } else { + MultiValueMap object = new LinkedMultiValueMap<>(); + object.add("phone", jsonObject.getString("phone")); + object.add("verify", jsonObject.getString("verify")); + return webClient.post().uri(baseUrl + smsUrl).bodyValue(object).retrieve() + .bodyToMono(JSONObject.class).doOnNext(twoRes -> { + if (!Objects.equals(twoRes.getInteger("code"), 1)) { + throw new RuntimeException(twoRes.getString("info")); + } + }); + } + }); + } + }); + } + + /** + * 密码登录 + */ @PostMapping("/login/pwd") public Mono loginByPwd(@RequestBody JSONObject jsonObject) { + log.info("UserController[]loginByPwd[]jsonObject:{}", jsonObject); + return webClient.post().uri(baseUrl + pwdUrl).bodyValue(getMultiValueMap(jsonObject)).retrieve() + .bodyToMono(JSONObject.class) + .doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 1)) { + throw new RuntimeException(res.getString("info")); + } + }); + } + + private MultiValueMap getMultiValueMap(JSONObject jsonObject) { MultiValueMap object = new LinkedMultiValueMap<>(); jsonObject.forEach((k,v)->{ object.add(k, String.valueOf(v)); }); - Mono jsonObjectMono = webClient.post().uri(baseUrl + pwdUrl).bodyValue(object).retrieve() - .bodyToMono(JSONObject.class) - .doOnNext(res -> { - if (!Objects.equals(res.getInteger("code"), 1)) { - throw new RuntimeException("参数错误"); - } - }); - return jsonObjectMono; + return object; } } diff --git a/iot-modules/iot-box-user-api/src/main/resources/bootstrap.yml b/iot-modules/iot-box-user-api/src/main/resources/bootstrap.yml index 1fb3f89..5889f1a 100644 --- a/iot-modules/iot-box-user-api/src/main/resources/bootstrap.yml +++ b/iot-modules/iot-box-user-api/src/main/resources/bootstrap.yml @@ -47,7 +47,8 @@ hsweb: excludes: #不包装的类 - org.springdoc userUrl: - baseUrl: 'https://exper.qiuguojihua.com/data' - pwdUrl: '/api.login/in' - smsUrl: '/api.login/phone' - sendSmsUrl: '/api.login/sendsms' + baseUrl: 'https://exper.qiuguojihua.com' + pwdUrl: '/data/api.login/in' + smsUrl: '/data/api.login/phone' + sendSmsUrl: '/data/api.login/sendsms' + registerUrl: '/data/api.login/register' \ No newline at end of file