晚上验证码登录

This commit is contained in:
weiyachao 2023-08-07 15:27:39 +08:00
parent 094e86130d
commit e82de75e08
3 changed files with 132 additions and 13 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

@ -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<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 -> {
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<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 -> {
System.out.println("registerRes = " + 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;
}
}

View File

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