完成剩下的公众号登录功能修改

This commit is contained in:
timfruit 2021-12-08 00:17:01 +08:00
parent 13a9405082
commit 450ca8f907
6 changed files with 112 additions and 34 deletions

View File

@ -81,19 +81,6 @@ public class SysAuthController {
return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri));
} }
// TODO @timfruit这个接口是要删除的么
@GetMapping("/social-login-get")
@ApiOperation("微信公众号授权回调地址输出social-login2的必要参数用于测试使用 code 授权码")
@ResponseBody
@Deprecated
public CommonResult<MbrAuthSocialLoginReqVO> socialLoginGet(HttpServletRequest request,String code,String state) {
// 返回结果
MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build();
reqVO.setType(12);
//输出social-login2的必要参数用于测试
System.out.println(JSON.toJSON(reqVO));
return success(reqVO);
}
@PostMapping("/social-login") @PostMapping("/social-login")
@ApiOperation("社交登录,使用 code 授权码") @ApiOperation("社交登录,使用 code 授权码")
@ -102,10 +89,9 @@ public class SysAuthController {
return success(SysAuthLoginRespVO.builder().token(token).build()); return success(SysAuthLoginRespVO.builder().token(token).build());
} }
// TODO @timfruit社交登陆时使用手机验证码来验证哈这块我当时没设计好改改嘿嘿
@PostMapping("/social-login2") @PostMapping("/social-login2")
@ApiOperation("社交登录,使用 code 授权码 + 账号密") @ApiOperation("社交登录,使用 手机号 + 手机验证")
public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) {
String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent());
return success(SysAuthLoginRespVO.builder().token(token).build()); return success(SysAuthLoginRespVO.builder().token(token).build());

View File

@ -34,15 +34,16 @@ public class MbrAuthSocialLogin2ReqVO {
@NotEmpty(message = "state 不能为空") @NotEmpty(message = "state 不能为空")
private String state; private String state;
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma") @ApiModelProperty(value = "手机号", required = true, example = "15119100000")
@NotEmpty(message = "登录账号不能为空") @NotEmpty(message = "手机号不能为空")
@Length(min = 4, max = 16, message = "账号长度为 4-16 位") @Length(min = 11, max = 11, message = "手机号是11位数字")
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") private String mobile;
private String username;
@ApiModelProperty(value = "手机验证码", required = true, example = "1024")
@NotEmpty(message = "手机验证码不能为空")
@Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位")
@Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
private String smsCode;
@ApiModelProperty(value = "密码", required = true, example = "buzhidao")
@NotEmpty(message = "密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
} }

View File

@ -46,7 +46,7 @@ public interface SysAuthService extends SecurityAuthFrameworkService {
String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent); String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent);
/** /**
* 社交登录使用 code 授权码 + 账号密 * 社交登录使用 手机号 + 手机验证
* *
* @param reqVO 登录信息 * @param reqVO 登录信息
* @param userIp 用户 IP * @param userIp 用户 IP

View File

@ -140,19 +140,21 @@ public class SysAuthServiceImpl implements SysAuthService {
@Override @Override
public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) {
// 使用 code 授权码进行登录
AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState());
org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); org.springframework.util.Assert.notNull(authUser, "授权用户不为空");
// 使用账号密码进行登录 // 使用手机号手机验证码登录
LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); SysAuthSmsLoginReqVO loginReqVO = SysAuthSmsLoginReqVO
// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 .builder()
.mobile(reqVO.getMobile())
.code(reqVO.getSmsCode())
.build();
String sessionId = this.smsLogin(loginReqVO, userIp, userAgent);
LoginUser loginUser = userSessionCoreService.getLoginUser(sessionId);
// 绑定社交用户新增 // 绑定社交用户新增
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM); socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
return sessionId;
// 缓存登录用户到 Redis 返回 sessionId 编号
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
} }
@Override @Override

View File

@ -13,14 +13,16 @@
</div> </div>
</body> </body>
<script> <script>
let server = 'http://127.0.0.1:28080'; // let server = 'http://127.0.0.1:28080';
let server = 'http://192.168.1.2:28080';
// 微信公众号 // 微信公众号
$( "#wx_pub").on( "click", function() { $( "#wx_pub").on( "click", function() {
// 获得授权链接 // 获得授权链接
$.ajax({ $.ajax({
url: server + "/api/social-auth-redirect?type=31&redirectUri=" + url: server + "/api/social-auth-redirect?type=31&redirectUri=" +
encodeURIComponent(server + '/api/social-login-get'), encodeURIComponent(server + '/static/social-login2.html'), //重定向地址
method: 'GET', method: 'GET',
success: function( result ) { success: function( result ) {
if (result.code !== 0) { if (result.code !== 0) {

View File

@ -0,0 +1,87 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
<title>社交登陆测试页</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
<div>点击如下按钮,授权登录</div>
<div>
手机号<input id="mobile" value="15601691300"><br>
手机验证码<input id="smsCode">
<button id="send_sms_code">发送手机验证码</button>
<br>
<button id="wx_pub">微信公众号授权登录</button>
</div>
</body>
<script>
// let server = 'http://127.0.0.1:28080';
let server = 'http://192.168.1.2:28080';
let type = 31; //登录类型 微信公众号
// 微信公众号
$("#wx_pub").on("click", function () {
let code = getUrlParam("code"); // 访问授权连接后会回调本页面地址参数在本页面url后面
let state = getUrlParam("state");
console.log("获取code: " + code + ", state: " + state)
let data = {
'mobile': $('#mobile').val(),
'smsCode': $('#smsCode').val(),
'code': code,
'state': state,
'type': type
}
// 调用授权登录接口
$.ajax({
url: server + "/api/social-login2",
method: 'POST',
data: JSON.stringify(data),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function( result ) {
if (result.code !== 0) {
alert('调用授权登录接口失败,原因:' + result.msg)
return;
}
alert("授权登录成功, token: "+result.data.token)
}
})
});
// 发送手机验证码
$("#send_sms_code").on("click", function () {
let data = {
'mobile': $('#mobile').val(),
'scene': 1 // 手机号登陆 类型
}
$.ajax({
url: server + "/api/send-sms-code",
method: 'POST',
data: JSON.stringify(data),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
if (result.code !== 0) {
alert('发送手机验证码失败,原因:' + result.msg)
return;
}
alert("发送成功, 请查看日志");
}
})
})
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]);
return null; //返回参数值
}
</script>
</html>