Commit 80981577 by levphon

微信小程序C端账号体系

1 parent 3ce30ad0
......@@ -41,14 +41,14 @@
<dependency>
<groupId>com.glsx</groupId>
<artifactId>ne-shield-auth-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- devicemonitor-api -->
<dependency>
<groupId>com.glsx</groupId>
<artifactId>ne-shield-devicemonitor-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
......
package cn.com.glsx.app.config;
import cn.com.glsx.auth.interceptor.WebRequestAuthorizeInterceptor;
import cn.com.glsx.auth.interceptor.AppRequestAuthorizeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
......@@ -14,7 +14,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class WebConfig implements WebMvcConfigurer {
@Autowired
private WebRequestAuthorizeInterceptor authInterceptor;
private AppRequestAuthorizeInterceptor authInterceptor;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
......
......@@ -65,10 +65,13 @@ public class ThirdLoginController {
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code, @RequestParam Integer accessMode) throws WxErrorException {
//获取openId、unionid、session_key
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
WxMaJscode2SessionResult sessionInfo = null;//wxMaService.getUserService().getSessionInfo(code);
sessionInfo = new WxMaJscode2SessionResult();
sessionInfo.setOpenid("ocDtn5a60SvMC33CbObTUUMVGOwE");
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
UserAuthRel authRel = loginService.wechatAuth(sessionInfo, accessMode);
Map<String, Object> rtnMap = thirdLogin(LoginType.WECHAT, accessMode, authRel);
......@@ -107,12 +110,6 @@ public class ThirdLoginController {
//a、存在账号的openid发起免登录
//b、不存在账号的openid发起免登录
AppUser user = userService.getAppUserByAuthId(loginType, authRel.getAuthId());
if (user != null) {
} else {
user = new AppUser();
// TODO: 2022/6/29 设置用户标识
}
user.setUserType(MiniUserType.SHARED.getType());
String token = userService.createToken(user);
......
package cn.com.glsx.app.modules.converter;
import cn.com.glsx.app.modules.entity.User;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.auth.model.SimpleUser;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
......@@ -10,6 +13,10 @@ public interface UserConverter {
UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);
@Mappings(@Mapping(source = "id", target = "userId"))
AppUser user2au(User user);
@Mappings(@Mapping(source = "id", target = "userId"))
AppUser su2au(SimpleUser user);
}
......@@ -6,20 +6,14 @@ import lombok.Data;
public class AppUser {
/**
* 用户id(对应后台t_user表id)
* 看具体用户类型(userType):normal用户id(对应后台t_user表id),C端被分享用户用户id(对应后台t_cust_user表id)
*/
private Long userId;
/**
* C端用户id(对应后台t_cust_user表id)
*/
private Long cuserId;
private String account;
private String username;
private String avatar;
/**
* 用户类型:normal正常用户,shared被分享用户,见枚举MiniUserType
* 用户类型:normal常规用户,shared被分享用户,见枚举MiniUserType
*/
private String userType;
private Integer blacklist;
......
......@@ -3,6 +3,7 @@ package cn.com.glsx.app.modules.service;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.common.enums.AuthType;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.common.enums.MiniUserType;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.converter.UserConverter;
import cn.com.glsx.app.modules.entity.*;
......@@ -13,6 +14,7 @@ import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.enums.SysConstants;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -66,50 +68,10 @@ public class LoginService {
throw new AppServerException(userR.getMessage());
}
User user = Optional.ofNullable(userMapper.selectByAccount(simpleUser.getAccount())).orElse(new User());
user.setLinkerId(simpleUser.getId());
user.setAccount(simpleUser.getAccount());
user.setBlacklist(0);
userMapper.saveOrUpdate(user);
UserLocalAuth localAuth = userLocalAuthMapper.selectByUserId(user.getId());
if (localAuth == null) {
localAuth = new UserLocalAuth();
localAuth.setAccount(loginBO.getAccount());
String encryptedPasswd = bCryptPasswordEncoder.encode(loginBO.getAccount());
localAuth.setPassword(encryptedPasswd);
userLocalAuthMapper.insertUseGeneratedKeys(localAuth);
}
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.LOCAL.getType(), localAuth.getId());
if (userAuthRel == null) {
userAuthRel = new UserAuthRel();
userAuthRel.setUserId(user.getId());
userAuthRel.setAuthId(localAuth.getId());
userAuthRel.setAuthType(AuthType.LOCAL.getType());
userAuthRelMapper.insertUseGeneratedKeys(userAuthRel);
}
User user = register(simpleUser);
//生成本地账号
UserLocalAccount localAccount = userLocalAccountMapper.selectByAuthId(user.getId());
if (localAccount == null) {
localAccount = new UserLocalAccount();
//localAccount.setUserId(user.getId());
localAccount.setAuthId(localAuth.getId());
localAccount.setUsername(simpleUser.getUsername());
//生成平台unionId
String unionId = null;//generateUnionId();
localAccount.setUnionId(unionId);
localAccount.setCreatedDate(new Date());
userLocalAccountMapper.insert(localAccount);
} else {
if (StringUtils.isNotEmpty(localAccount.getUsername()) && !localAccount.getUsername().equals(simpleUser.getUsername())) {
localAccount.setUsername(simpleUser.getUsername());
localAccount.setUpdatedDate(new Date());
userLocalAccountMapper.updateByPrimaryKeySelective(localAccount);
}
}
return UserConverter.INSTANCE.su2au(simpleUser);
AppUser appUser = UserConverter.INSTANCE.su2au(simpleUser);
appUser.setUserType(MiniUserType.NORMAL.getType());
} else if (loginBO.getLoginType() == 2) {
//验证码校验
//smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
......@@ -117,6 +79,58 @@ public class LoginService {
return null;
}
/**
* c端用户账号注册
*
* @param simpleUser
*/
public User register(SimpleUser simpleUser) {
User user = Optional.ofNullable(userMapper.selectByAccount(simpleUser.getAccount())).orElse(new User());
user.setLinkerId(simpleUser.getId());
user.setAccount(simpleUser.getAccount());
user.setBlacklist(simpleUser.getBlacklist());
userMapper.saveOrUpdate(user);
UserLocalAuth localAuth = userLocalAuthMapper.selectByUserId(user.getId());
if (localAuth == null) {
localAuth = new UserLocalAuth();
localAuth.setAccount(user.getAccount());
String encryptedPasswd = bCryptPasswordEncoder.encode(user.getAccount());
localAuth.setPassword(encryptedPasswd);
userLocalAuthMapper.insertUseGeneratedKeys(localAuth);
}
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.LOCAL.getType(), localAuth.getId());
if (userAuthRel == null) {
userAuthRel = new UserAuthRel();
userAuthRel.setUserId(user.getId());
userAuthRel.setAuthId(localAuth.getId());
userAuthRel.setAuthType(AuthType.LOCAL.getType());
userAuthRelMapper.insertUseGeneratedKeys(userAuthRel);
}
//生成本地账号
UserLocalAccount localAccount = userLocalAccountMapper.selectByAuthId(user.getId());
if (localAccount == null) {
localAccount = new UserLocalAccount();
//localAccount.setUserId(user.getId());
localAccount.setAuthId(localAuth.getId());
localAccount.setUsername(simpleUser.getUsername());
//生成平台unionId
String unionId = null;//generateUnionId();
localAccount.setUnionId(unionId);
localAccount.setCreatedDate(new Date());
userLocalAccountMapper.insert(localAccount);
} else {
if (StringUtils.isNotEmpty(localAccount.getUsername()) && !localAccount.getUsername().equals(simpleUser.getUsername())) {
localAccount.setUsername(simpleUser.getUsername());
localAccount.setUpdatedDate(new Date());
userLocalAccountMapper.updateByPrimaryKeySelective(localAccount);
}
}
return user;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) {
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(accessToken.getOpenId());
......@@ -147,7 +161,7 @@ public class LoginService {
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo) {
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo, Integer accessMode) {
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(sessionInfo.getOpenid());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
......@@ -157,10 +171,21 @@ public class LoginService {
userThirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
User user = null;
if (MiniUserType.SHARED.getFlag().equals(accessMode)) {
SimpleUser simpleUser = new SimpleUser();
// simpleUser.setId();
// simpleUser.setAccount();
// simpleUser.setUsername();
simpleUser.setBlacklist(SysConstants.EnableStatus.enable.getCode());
user = register(simpleUser);
}
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
//authRel.setUserId(null);
authRel.setUserId(user != null ? user.getId() : null);
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
userAuthRelMapper.insertUseGeneratedKeys(authRel);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!