Merge branch 'feature-BOX一期' of http://47.99.132.106:10081/wulin/qiuguo-iot into feature-BOX一期

This commit is contained in:
wulin 2023-08-07 21:02:02 +08:00
commit 2915b559ad
3 changed files with 208 additions and 15 deletions

View File

@ -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();
}
}

View File

@ -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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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<String, String> 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<String, String> 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<JSONObject> 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<String,String> getMultiValueMap(JSONObject jsonObject) {
MultiValueMap<String,String> object = new LinkedMultiValueMap<>();
jsonObject.forEach((k,v)->{
object.add(k, String.valueOf(v));
});
Mono<JSONObject> 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();
}
}

View File

@ -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'