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..220d3f3 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 @@ -2,14 +2,15 @@ package com.qiuguo.iot.user.api.controller; import com.alibaba.fastjson.JSONObject; import java.util.Objects; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; 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.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; @@ -30,7 +31,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 +46,180 @@ public class UserController { @Value("${userUrl.sendSmsUrl}") private String sendSmsUrl; + @Value("${userUrl.registerUrl}") + private String registerUrl; + + @Value("${userUrl.changeUrl}") + private String changeUrl; + + @Value("${userUrl.userCancelUrl}") + private String userCancelUrl; + + @Value("${userUrl.userInfoUrl}") + private String userInfoUrl; + + @Value("${userUrl.firstPasswordUrl}") + private String firstPasswordUrl; + + @Value("${userUrl.editUserInfoUrl}") + private String editUserInfoUrl; + + /** + * 修改登录密码-auth + */ + @PostMapping("/change") + public Mono change(@RequestBody JSONObject jsonObject, @RequestHeader("Api-Token") String token, + @RequestHeader("Api-Type") String type) { + WebClient authWebClient = getAuthWebClient(token, type); + return authWebClient.post().uri(baseUrl + changeUrl).bodyValue(getMultiValueMap(jsonObject)).retrieve() + .bodyToMono(JSONObject.class).doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 1)) { + throw new RuntimeException(res.getString("info")); + } + }); + } + + /** + * 账号注销-auth + */ + @PostMapping("/userCance") + public Mono userCance(@RequestBody JSONObject jsonObject, @RequestHeader("Api-Token") String token, + @RequestHeader("Api-Type") String type) { + return getAuthWebClient(token, type).post().uri(baseUrl + userCancelUrl).bodyValue(getMultiValueMap(jsonObject)).retrieve() + .bodyToMono(JSONObject.class).doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 1)) { + throw new RuntimeException(res.getString("info")); + } + }); + } + + /** + * 修改用户信息-auth + */ + @PostMapping("/edit/userInfo") + public Mono editUserInfo(@RequestBody JSONObject jsonObject, @RequestHeader("Api-Token") String token, + @RequestHeader("Api-Type") String type) { + return WebClient.builder() + .defaultHeader("Api-Token", token) + .defaultHeader("Api-Type", type).build().post().uri(editUserInfoUrl).bodyValue(jsonObject).retrieve() + .bodyToMono(JSONObject.class).doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 200)) { + throw new RuntimeException(res.getString("msg")); + } + }); + } + + /** + * 个人信息管理-auth + */ + @GetMapping("/userInfo") + public Mono getUserInfo(@RequestHeader("Api-Token") String token, + @RequestHeader("Api-Type") String type) { + return getAuthWebClient(token, type).get().uri(userInfoUrl).retrieve() + .bodyToMono(JSONObject.class).doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 200)) { + throw new RuntimeException(res.getString("msg")); + } + }); + } + + /** + * 是否设置登录密码-auth + */ + @GetMapping("/first/password") + public Mono firstPassword(@RequestHeader("Api-Token") String token, + @RequestHeader("Api-Type") String type) { + return getAuthWebClient(token, type).get().uri(firstPasswordUrl).retrieve() + .bodyToMono(JSONObject.class).doOnNext(res -> { + if (!Objects.equals(res.getInteger("code"), 200)) { + throw new RuntimeException(res.getString("msg")); + } + }); + } + + + /** + * 发送验证码 + */ + @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 -> { + 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 -> { + 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; + } + + private WebClient getAuthWebClient(String token, String type) { + return WebClient.builder() + .defaultHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE) + .defaultHeader("Api-Token", token) + .defaultHeader("Api-Type", type).build(); } - } 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..373a66d 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,13 @@ 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' + changeUrl: '/data/api.login/change' + userCancelUrl: '/data/api.auth.center/userCancel' + userInfoUrl: 'https://qiuguo-app.pre.qiuguojihua.com/pre-api/user/user/box/userInfo' + firstPasswordUrl: 'https://qiuguo-app.pre.qiuguojihua.com/pre-api/user/user/box/first/password' + editUserInfoUrl: 'https://qiuguo-app.pre.qiuguojihua.com/pre-api/user/user/box/edit/userInfo' \ No newline at end of file