mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
refactor:微信认证登陆
This commit is contained in:
parent
d4ee622fac
commit
cac7b840f2
@ -11,4 +11,7 @@ public class MemberDTO {
|
||||
private String password;
|
||||
private Integer status;
|
||||
private String clientId;
|
||||
|
||||
private String avatar;
|
||||
private String nickname;
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.youlai.mall.ums.api.entity;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class UmsMember {
|
||||
|
||||
private Long id;
|
||||
|
@ -74,8 +74,9 @@ public class AuthController {
|
||||
}
|
||||
|
||||
// 微信小程序逻辑处理
|
||||
WxMaUserInfo wxMaUserInfo = null;
|
||||
if (AuthConstants.WEAPP_CLIENT_ID.equals(clientId)) {
|
||||
this.handleParametersForWeapp(parameters);
|
||||
wxMaUserInfo = this.handleParametersForWeapp(parameters);
|
||||
}
|
||||
|
||||
OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
|
||||
@ -83,7 +84,9 @@ public class AuthController {
|
||||
.token(oAuth2AccessToken.getValue())
|
||||
.refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
|
||||
.expiresIn(oAuth2AccessToken.getExpiresIn())
|
||||
.userInfo(wxMaUserInfo)
|
||||
.build();
|
||||
|
||||
return Result.success(oauth2Token);
|
||||
}
|
||||
|
||||
@ -105,7 +108,7 @@ public class AuthController {
|
||||
}
|
||||
|
||||
|
||||
private void handleParametersForWeapp(Map<String, String> parameters) {
|
||||
private WxMaUserInfo handleParametersForWeapp(Map<String, String> parameters) {
|
||||
|
||||
try {
|
||||
String code = parameters.get("code");
|
||||
@ -117,32 +120,40 @@ public class AuthController {
|
||||
String sessionKey = session.getSessionKey();
|
||||
|
||||
MemberDTO memberDTO = remoteUmsMemberService.loadMemberByOpenid(openid);
|
||||
UmsMember member = new UmsMember();
|
||||
WxMaUserInfo userInfo;
|
||||
if (memberDTO == null || memberDTO.getId() == null) {
|
||||
// 注册会员
|
||||
String encryptedData = parameters.get("encryptedData");
|
||||
String iv = parameters.get("iv");
|
||||
|
||||
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
||||
member.setNickname(userInfo.getNickName());
|
||||
member.setAvatar(userInfo.getAvatarUrl());
|
||||
member.setGender(Integer.valueOf(userInfo.getGender()));
|
||||
member.setOpenid(openid);
|
||||
member.setUsername(openid);
|
||||
member.setPassword(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)); // 加密密码移除前缀加密方式 {bcrypt}
|
||||
|
||||
userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
||||
UmsMember member = UmsMember.builder()
|
||||
.nickname(userInfo.getNickName())
|
||||
.avatar(userInfo.getAvatarUrl())
|
||||
.gender(Integer.valueOf(userInfo.getGender()))
|
||||
.openid(openid)
|
||||
.username(openid)
|
||||
.password(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
|
||||
.build();
|
||||
Result result = remoteUmsMemberService.add(member);
|
||||
if (!ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
||||
throw new BizException("注册会员失败");
|
||||
}
|
||||
|
||||
// 微信授权登录数据模拟生成token
|
||||
parameters.put("username", member.getUsername());
|
||||
parameters.put("password", member.getUsername());
|
||||
|
||||
} else {
|
||||
BeanUtil.copyProperties(memberDTO, member);
|
||||
userInfo = new WxMaUserInfo();
|
||||
userInfo.setAvatarUrl(memberDTO.getAvatar());
|
||||
userInfo.setNickName(memberDTO.getNickname());
|
||||
|
||||
parameters.put("username", memberDTO.getUsername());
|
||||
parameters.put("password", memberDTO.getUsername());
|
||||
}
|
||||
|
||||
// 微信授权登录数据模拟生成token
|
||||
parameters.put("username", member.getUsername());
|
||||
parameters.put("password", member.getUsername());
|
||||
|
||||
return userInfo;
|
||||
} catch (WxErrorException e) {
|
||||
e.printStackTrace();
|
||||
throw new BizException("auth failed");
|
||||
|
@ -1,17 +1,21 @@
|
||||
package com.youlai.auth.domain;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.codehaus.jackson.annotate.JsonIgnore;
|
||||
|
||||
@ApiModel
|
||||
@Data
|
||||
@Builder
|
||||
|
||||
public class Oauth2Token {
|
||||
|
||||
@ApiModelProperty("访问令牌")
|
||||
private String token ;
|
||||
private String token;
|
||||
|
||||
@ApiModelProperty("刷新令牌")
|
||||
private String refreshToken;
|
||||
@ -19,4 +23,7 @@ public class Oauth2Token {
|
||||
@ApiModelProperty("有效时间(秒)")
|
||||
private int expiresIn;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private WxMaUserInfo userInfo;
|
||||
|
||||
}
|
||||
|
@ -11,16 +11,6 @@ public class WxLoginInfo {
|
||||
|
||||
private String iv;
|
||||
|
||||
private UserInfo userInfo;
|
||||
|
||||
@Data
|
||||
public class UserInfo {
|
||||
private String nickName;
|
||||
private String avatarUrl;
|
||||
private String country;
|
||||
private String province;
|
||||
private String city;
|
||||
private String language;
|
||||
private Integer gender;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user