Commit f16f8f8f by levphon

微信小程序C端账号体系

1 parent a9c90d1c
package cn.com.glsx.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class BeansConfig {
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
...@@ -2,11 +2,10 @@ package cn.com.glsx.app.modules; ...@@ -2,11 +2,10 @@ package cn.com.glsx.app.modules;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.modules.entity.UserAuthRel; import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.model.AppUser; import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.QQLoginBO;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken; import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.app.modules.model.WechatLoginBO;
import cn.com.glsx.app.modules.service.LoginService; import cn.com.glsx.app.modules.service.LoginService;
import cn.com.glsx.app.modules.service.UserService; import cn.com.glsx.app.modules.service.UserService;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
...@@ -15,7 +14,9 @@ import com.glsx.plat.core.web.R; ...@@ -15,7 +14,9 @@ import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
...@@ -41,65 +42,45 @@ public class ThirdLoginController { ...@@ -41,65 +42,45 @@ public class ThirdLoginController {
private WxMaService wxMaService; private WxMaService wxMaService;
@NoLogin @NoLogin
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code) throws WxErrorException {
//获取openId、unionid、session_key
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
Map<String, Object> rtnMap = thirdLogin(authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@PostMapping(value = "/wechatLogin")
public R wechatLogin(@RequestBody WechatLoginBO loginBO) {
log.info("微信授权参数 {}", loginBO.toString());
UserAuthRel wxAuthRel = loginService.wechatLogin(loginBO);
Map<String, Object> rtnMap = thirdLogin(wxAuthRel);
return R.ok().data(rtnMap);
}
@NoLogin
@GetMapping(value = "/qqAuth") @GetMapping(value = "/qqAuth")
public R qqAuth(@RequestParam String code) { public R qqAuth(@RequestParam String code) {
//获取openId、unionId等
QQOAuth2AccessToken accessToken = new QQOAuth2AccessToken(); QQOAuth2AccessToken accessToken = new QQOAuth2AccessToken();
log.info("QQ code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(accessToken)); log.info("QQ code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(accessToken));
UserAuthRel authRel = loginService.qqAuth(accessToken); UserAuthRel authRel = loginService.qqAuth(accessToken);
Map<String, Object> rtnMap = thirdLogin(authRel); Map<String, Object> rtnMap = thirdLogin(LoginType.QQ, authRel);
return R.ok().data(rtnMap); return R.ok().data(rtnMap);
} }
@NoLogin @NoLogin
@PostMapping(value = "/qqLogin") @GetMapping(value = "/wechatAuth")
public R qqLogin(@RequestBody QQLoginBO loginBO) { public R wechatAuth(@RequestParam String code) throws WxErrorException {
log.info("QQ授权参数 {}", loginBO.toString()); //获取openId、unionid、session_key
UserAuthRel qqAuthRel = loginService.qqLogin(loginBO); WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
Map<String, Object> rtnMap = thirdLogin(qqAuthRel); Map<String, Object> rtnMap = thirdLogin(LoginType.WECHAT, authRel);
return R.ok().data(rtnMap); return R.ok().data(rtnMap);
} }
private Map<String, Object> thirdLogin(UserAuthRel authRel) { private Map<String, Object> thirdLogin(LoginType loginType, UserAuthRel authRel) {
Map<String, Object> rtnMap = new HashMap<>(); Map<String, Object> rtnMap = new HashMap<>();
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId()); Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (optUserId.isPresent()) { if (optUserId.isPresent()) {
//如果用户id存在,理论上User不为空 //已绑定本地账号
AppUser user = userService.getAppUserByUserId(optUserId.get()); AppUser user = userService.getAppUserByUserId(authRel.getUserId());
String token = userService.createToken(user); String token = userService.createToken(user);
rtnMap.put("token", token);
rtnMap.put("user", user); rtnMap.put("user", user);
rtnMap.put("token", token);
} else { } else {
//没有绑定用户,跳转登录界面登录,回传此id //未绑定本地账号,跳转登录界面登录,回传此id
rtnMap.put("authRelId", authRel.getId()); rtnMap.put("authRelId", authRel.getId());
} }
return rtnMap; return rtnMap;
......
package cn.com.glsx.app.modules.entity;
import com.glsx.plat.mybatis.base.BaseEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Table;
@Data
@Table(name = "t_cust_user")
public class User extends BaseEntity {
/**
* 登录token
*/
private String token;
/**
* token过期时间
*/
@Column(name = "expire_in")
private Long expireIn;
/**
* 登录失败次数
*/
@Column(name = "try_times")
private Integer tryTimes;
/**
* 用户账号/手机号
*/
private String account;
/**
* 状态:0白名单,临时封禁(blacklist>0,值为封禁天数),永久封禁(del_flag=-1)
*/
private Integer blacklist;
}
\ No newline at end of file \ No newline at end of file
...@@ -11,9 +11,15 @@ import javax.persistence.Table; ...@@ -11,9 +11,15 @@ import javax.persistence.Table;
public class UserLocalAuth extends BaseEntity { public class UserLocalAuth extends BaseEntity {
/** /**
* 用户id * 用户账号
*/ */
@Column(name = "user_id") @Column(name = "account")
private Long userId; private String account;
/**
* 密码
*/
@Column(name = "account")
private String password;
} }
\ No newline at end of file \ No newline at end of file
...@@ -10,16 +10,8 @@ import java.util.List; ...@@ -10,16 +10,8 @@ import java.util.List;
@Mapper @Mapper
public interface UserAuthRelMapper extends CommonBaseMapper<UserAuthRel> { public interface UserAuthRelMapper extends CommonBaseMapper<UserAuthRel> {
UserAuthRel selectById(@Param("id") Long id);
UserAuthRel selectLocalAuthRelByUserId(@Param("userId") Long userId);
UserAuthRel selectByAuthTypeAndAuthId(@Param("authType") String authType, @Param("authId") Long authId); UserAuthRel selectByAuthTypeAndAuthId(@Param("authType") String authType, @Param("authId") Long authId);
List<UserAuthRel> selectByAuthTypeAndUserId(@Param("authType") String authType, @Param("userId") Long userId); List<UserAuthRel> selectByAuthTypeAndUserId(@Param("authType") String authType, @Param("userId") Long userId);
List<UserAuthRel> selectByUserId(@Param("userId") Long userId);
int logicDeleteByUserId(@Param("userId") Long userId);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.app.modules.mapper;
import cn.com.glsx.app.modules.entity.UserLocalAccount;
import cn.com.glsx.app.modules.model.AppUser;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserLocalAccountMapper extends CommonBaseMapper<UserLocalAccount> {
UserLocalAccount selectByAuthId(@Param("authId") Long authId);
AppUser selectAppUserByUserId(@Param("userId") Long userId);
}
\ No newline at end of file \ No newline at end of file
...@@ -8,10 +8,8 @@ import org.apache.ibatis.annotations.Param; ...@@ -8,10 +8,8 @@ import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface UserLocalAuthMapper extends CommonBaseMapper<UserLocalAuth> { public interface UserLocalAuthMapper extends CommonBaseMapper<UserLocalAuth> {
UserLocalAuth selectById(@Param("id") Long id); UserLocalAuth selectByAuthId(@Param("authId") Long authId);
int updatePasswordByUserId(@Param("userId") Long userId, @Param("password") String password); UserLocalAuth selectByUserId(@Param("userId") Long userId);
int logicDeleteById(@Param("id") Long id);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.app.modules.mapper;
import cn.com.glsx.app.modules.entity.User;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper extends CommonBaseMapper<User> {
User selectById(@Param("id") Long id);
User selectByAccount(@Param("account") String account);
int saveOrUpdate(User user);
}
\ No newline at end of file \ No newline at end of file
...@@ -10,8 +10,6 @@ public interface UserThirdAuthMapper extends CommonBaseMapper<UserThirdAuth> { ...@@ -10,8 +10,6 @@ public interface UserThirdAuthMapper extends CommonBaseMapper<UserThirdAuth> {
UserThirdAuth selectByOpenid(@Param("openid") String openid); UserThirdAuth selectByOpenid(@Param("openid") String openid);
UserThirdAuth selectByLoginTypeAndUserId(@Param("loginType") String loginType, @Param("userId") Long userId); UserThirdAuth selectByLoginTypeAndAuthId(@Param("loginType") String loginType, @Param("authId") Long authId);
int logicDeleteById(@Param("id") Long id);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.app.modules.model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class QQLoginBO {
@NotBlank
private String openId;
}
package cn.com.glsx.app.modules.model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class WechatLoginBO {
@NotBlank
private String openId;
@NotBlank
private String unionId;
}
...@@ -7,29 +7,41 @@ import cn.com.glsx.app.common.exception.AppServerException; ...@@ -7,29 +7,41 @@ import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.converter.UserConverter; import cn.com.glsx.app.modules.converter.UserConverter;
import cn.com.glsx.app.modules.entity.*; import cn.com.glsx.app.modules.entity.*;
import cn.com.glsx.app.modules.mapper.*; import cn.com.glsx.app.modules.mapper.*;
import cn.com.glsx.app.modules.model.*; import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.LoginByAccountBO;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.auth.api.AuthFeignClient; import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser; import cn.com.glsx.auth.model.SimpleUser;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R; import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Optional;
@Slf4j
@Service @Service
public class LoginService { public class LoginService {
@Resource @Resource
private UserAuthRelMapper authRelMapper; private UserMapper userMapper;
@Resource @Resource
private UserLocalAuthMapper localAuthMapper; private UserAuthRelMapper userAuthRelMapper;
@Resource @Resource
private UserThirdAuthMapper thirdAuthMapper; private UserLocalAuthMapper userLocalAuthMapper;
@Resource
private UserLocalAccountMapper userLocalAccountMapper;
@Resource
private UserThirdAuthMapper userThirdAuthMapper;
@Resource @Resource
private UserWechatAccountMapper wechatAccountMapper; private UserWechatAccountMapper wechatAccountMapper;
...@@ -40,6 +52,9 @@ public class LoginService { ...@@ -40,6 +52,9 @@ public class LoginService {
@Autowired @Autowired
private AuthFeignClient authFeignClient; private AuthFeignClient authFeignClient;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public AppUser localAuth(LoginByAccountBO loginBO) { public AppUser localAuth(LoginByAccountBO loginBO) {
//内部登录转成appUser返回前端 //内部登录转成appUser返回前端
...@@ -50,23 +65,49 @@ public class LoginService { ...@@ -50,23 +65,49 @@ public class LoginService {
if (!userR.isSuccess() || simpleUser == null) { if (!userR.isSuccess() || simpleUser == null) {
throw new AppServerException(userR.getMessage()); throw new AppServerException(userR.getMessage());
} }
UserAuthRel userAuthRel = authRelMapper.selectLocalAuthRelByUserId(simpleUser.getId());
UserLocalAuth localAuth = localAuthMapper.selectById(userAuthRel.getAuthId()); User user = Optional.ofNullable(userMapper.selectByAccount(simpleUser.getAccount())).orElse(new User());
user.setAccount(simpleUser.getAccount());
user.setBlacklist(0);
userMapper.saveOrUpdate(user);
UserLocalAuth localAuth = userLocalAuthMapper.selectByUserId(user.getId());
if (localAuth == null) { if (localAuth == null) {
throw new AppServerException("请先开通登录权限"); localAuth = new UserLocalAuth();
localAuth.setAccount(loginBO.getAccount());
String encryptedPasswd = bCryptPasswordEncoder.encode(loginBO.getAccount());
localAuth.setPassword(encryptedPasswd);
userLocalAuthMapper.insertUseGeneratedKeys(localAuth);
} }
//2022/3/25 绑定第三方授权 UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.LOCAL.getType(), localAuth.getId());
if (!Objects.isNull(loginBO.getAuthRelId())) { if (userAuthRel == null) {
UserAuthRel authRel = authRelMapper.selectById(loginBO.getAuthRelId()); userAuthRel = new UserAuthRel();
if (authRel != null) { userAuthRel.setUserId(user.getId());
authRel.setUserId(simpleUser.getId()); userAuthRel.setAuthId(localAuth.getId());
authRel.setUpdatedDate(new Date()); userAuthRel.setAuthType(AuthType.LOCAL.getType());
authRelMapper.updateByPrimaryKeySelective(authRel); 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 UserConverter.INSTANCE.su2au(simpleUser); return UserConverter.INSTANCE.su2au(simpleUser);
} else if (loginBO.getLoginType() == 2) { } else if (loginBO.getLoginType() == 2) {
//验证码校验 //验证码校验
...@@ -77,114 +118,59 @@ public class LoginService { ...@@ -77,114 +118,59 @@ public class LoginService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) { public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(accessToken.getOpenId()); UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(accessToken.getOpenId());
if (thirdAuth == null) { if (thirdAuth == null) {
thirdAuth = new UserThirdAuth(); thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(accessToken.getOpenId()); thirdAuth.setOpenid(accessToken.getOpenId());
thirdAuth.setAccessToken(accessToken.getAccessToken()); thirdAuth.setAccessToken(accessToken.getAccessToken());
thirdAuth.setLoginType(LoginType.QQ.getType()); thirdAuth.setLoginType(LoginType.QQ.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth); userThirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
if (qqAccount == null) {
qqAccount = new UserQQAccount();
qqAccount.setAuthId(thirdAuth.getId());
qqAccount.setUnionId(accessToken.getUnionId());
qqAccountMapper.insertUseGeneratedKeys(qqAccount);
} }
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId()); UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) { if (authRel == null) {
authRel = new UserAuthRel(); authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId()); authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType()); authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel); userAuthRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel qqLogin(QQLoginBO loginBO) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(loginBO.getOpenId());
thirdAuth.setLoginType(LoginType.QQ.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
} }
UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId()); UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
if (qqAccount == null) { if (qqAccount == null) {
qqAccount = new UserQQAccount(); qqAccount = new UserQQAccount();
qqAccount.setAuthId(thirdAuth.getId()); qqAccount.setAuthId(thirdAuth.getId());
qqAccount.setUnionId(accessToken.getUnionId());
qqAccountMapper.insertUseGeneratedKeys(qqAccount); qqAccountMapper.insertUseGeneratedKeys(qqAccount);
} }
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel; return authRel;
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo) { public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(sessionInfo.getOpenid()); UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(sessionInfo.getOpenid());
if (thirdAuth == null) { if (thirdAuth == null) {
thirdAuth = new UserThirdAuth(); thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(sessionInfo.getOpenid()); thirdAuth.setOpenid(sessionInfo.getOpenid());
thirdAuth.setAccessToken(sessionInfo.getSessionKey()); thirdAuth.setAccessToken(sessionInfo.getSessionKey());
thirdAuth.setLoginType(LoginType.WECHAT.getType()); thirdAuth.setLoginType(LoginType.WECHAT.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth); userThirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
if (wechatAccount == null) {
wechatAccount = new UserWechatAccount();
wechatAccount.setAuthId(thirdAuth.getId());
wechatAccount.setUnionId(sessionInfo.getUnionid());
wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
} }
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId()); UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) { if (authRel == null) {
authRel = new UserAuthRel(); authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId()); authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType()); authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel); userAuthRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatLogin(WechatLoginBO loginBO) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(loginBO.getOpenId());
thirdAuth.setLoginType(LoginType.WECHAT.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
} }
UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId()); UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
if (wechatAccount == null) { if (wechatAccount == null) {
wechatAccount = new UserWechatAccount(); wechatAccount = new UserWechatAccount();
wechatAccount.setAuthId(thirdAuth.getId()); wechatAccount.setAuthId(thirdAuth.getId());
wechatAccount.setUnionId(loginBO.getUnionId()); wechatAccount.setUnionId(sessionInfo.getUnionid());
wechatAccountMapper.insertUseGeneratedKeys(wechatAccount); wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
} }
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel; return authRel;
} }
......
package cn.com.glsx.app.modules.service; package cn.com.glsx.app.modules.service;
import cn.com.glsx.app.common.exception.AppServerException; import cn.com.glsx.app.common.enums.AuthType;
import cn.com.glsx.app.modules.converter.UserConverter; import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.entity.UserThirdAuth;
import cn.com.glsx.app.modules.mapper.UserAuthRelMapper;
import cn.com.glsx.app.modules.mapper.UserLocalAccountMapper;
import cn.com.glsx.app.modules.mapper.UserThirdAuthMapper;
import cn.com.glsx.app.modules.model.AppUser; import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import cn.hutool.core.lang.UUID; import cn.hutool.core.lang.UUID;
import com.glsx.plat.common.utils.ObjectUtils; import com.glsx.plat.common.utils.ObjectUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.jwt.base.ComJwtUser; import com.glsx.plat.jwt.base.ComJwtUser;
import com.glsx.plat.jwt.util.JwtUtils; import com.glsx.plat.jwt.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Map; import java.util.Map;
@Service @Service
...@@ -22,8 +25,14 @@ public class UserService { ...@@ -22,8 +25,14 @@ public class UserService {
@Autowired @Autowired
private JwtUtils<ComJwtUser> jwtUtils; private JwtUtils<ComJwtUser> jwtUtils;
@Autowired @Resource
private AuthFeignClient authFeignClient; private UserAuthRelMapper userAuthRelMapper;
@Resource
private UserThirdAuthMapper userThirdAuthMapper;
@Resource
private UserLocalAccountMapper userLocalAccountMapper;
/** /**
* 生成带用户信息的token * 生成带用户信息的token
...@@ -45,12 +54,17 @@ public class UserService { ...@@ -45,12 +54,17 @@ public class UserService {
} }
public AppUser getAppUserByUserId(Long userId) { public AppUser getAppUserByUserId(Long userId) {
R<SimpleUser> userR = authFeignClient.getSimpleUserById(userId); //获取用户本地信息
SimpleUser simpleUser = userR.getData(SimpleUser.class); return userLocalAccountMapper.selectAppUserByUserId(userId);
if (!userR.isSuccess() || simpleUser == null) { }
throw new AppServerException(userR.getMessage());
} public AppUser getAppUserByAuthId(LoginType loginType, Long authId) {
return UserConverter.INSTANCE.su2au(simpleUser); //获取具体第三方授权
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByLoginTypeAndAuthId(loginType.getType(), authId);
//获取第三方授权关联
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
//获取用户本地信息
return this.getAppUserByUserId(userAuthRel.getUserId());
} }
} }
...@@ -28,7 +28,7 @@ public class VehicleMonitorService { ...@@ -28,7 +28,7 @@ public class VehicleMonitorService {
} }
public List<VehicleLocationDTO> getVehicleLocations(String sns) { public List<VehicleLocationDTO> getVehicleLocations(String sns) {
R<List<DeviceLocationDTO>> listR = gpsBsFeignService.listDeviceMonitorInfo(sns, null); R<List<DeviceLocationDTO>> listR = gpsBsFeignService.listDeviceMonitorInfo(sns);
List<DeviceLocationDTO> dataList = listR.getDataList(new TypeToken<List<DeviceLocationDTO>>() { List<DeviceLocationDTO> dataList = listR.getDataList(new TypeToken<List<DeviceLocationDTO>>() {
}.getType()); }.getType());
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
where auth_type = #{authType} where auth_type = #{authType}
and auth_id = #{authId} and auth_id = #{authId}
and del_flag = 0 and del_flag = 0
limit 1
</select> </select>
<select id="selectByAuthTypeAndUserId" resultMap="BaseResultMap"> <select id="selectByAuthTypeAndUserId" resultMap="BaseResultMap">
...@@ -40,36 +41,4 @@ ...@@ -40,36 +41,4 @@
and user_id = #{userId} and user_id = #{userId}
and del_flag = 0 and del_flag = 0
</select> </select>
<select id="selectLocalAuthRelByUserId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where auth_type = 'local'
and user_id = #{userId}
and del_flag = 0
limit 1
</select>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where id = #{id}
and del_flag = 0
</select>
<select id="selectByUserId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where user_id = #{userId}
and del_flag = 0
</select>
<update id="logicDeleteByUserId">
update t_user_auth_rel
set del_flag = -1
where user_id = #{userId}
</update>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.app.modules.mapper.UserLocalAccountMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.app.modules.entity.UserLocalAccount">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_id" jdbcType="INTEGER" property="userId"/>
<result column="auth_id" jdbcType="INTEGER" property="authId"/>
<result column="union_id" jdbcType="VARCHAR" property="unionId"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="avatar" jdbcType="VARCHAR" property="avatar"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/>
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
<result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
-->
id, user_id, auth_id, union_id, username, avatar, del_flag, created_date, created_by,
updated_date, updated_by
</sql>
<select id="selectByAuthId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user_local_account
where auth_id = #{authId}
and del_flag = 0
</select>
<select id="selectAppUserByUserId" resultType="cn.com.glsx.app.modules.model.AppUser">
select tcula.user_id as id,
tcula.union_id unionId,
tcu.account,
tcula.username,
tcu.blacklist
from t_cust_user_local_account tcula
inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
left join t_cust_user tcu on tcu.id = tcula.user_id
where tcula.del_flag = 0
and tcuar.auth_type = 'local'
and tcula.user_id = #{userId} limit 1
</select>
</mapper>
\ No newline at end of file \ No newline at end of file
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
WARNING - @mbg.generated WARNING - @mbg.generated
--> -->
<id column="id" jdbcType="INTEGER" property="id"/> <id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_id" jdbcType="INTEGER" property="userId"/> <result column="account" jdbcType="VARCHAR" property="account"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/> <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/> <result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/> <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
...@@ -17,29 +18,26 @@ ...@@ -17,29 +18,26 @@
<!-- <!--
WARNING - @mbg.generated WARNING - @mbg.generated
--> -->
id, user_id, del_flag, created_date, created_by, id, account, `password`, del_flag, created_date, created_by,
updated_date, updated_by updated_date, updated_by
</sql> </sql>
<select id="selectById" resultMap="BaseResultMap"> <select id="selectByAuthId" resultMap="BaseResultMap">
select select tcula.*
<include refid="Base_Column_List"/> from t_cust_user_local_auth tcula
from t_user_local_auth inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
where id = #{id} where tcuar.del_flag = 0
and del_flag = 0 and tcuar.auth_type = 'local'
and tcuar.auth_id = #{authId} limit 1
</select> </select>
<update id="updatePasswordByUserId"> <select id="selectByUserId" resultMap="BaseResultMap">
update t_user_local_auth tcula ,t_user_auth_rel tcuar select tcula.*
set tcula.password = #{password} from t_cust_user_local_auth tcula
where tcula.id = tcuar.auth_id inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
and auth_type = 'local' inner join t_cust_user_local_account tcula2 on tcula2.auth_id = tcuar.auth_id
and tcuar.user_id = #{userId} where tcuar.del_flag = 0
</update> and tcuar.auth_type = 'local'
and tcula2.user_id = #{userId} limit 1
<update id="logicDeleteById"> </select>
update t_user_local_auth
set del_flag = -1
where id = #{id}
</update>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.app.modules.mapper.UserMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.app.modules.entity.User">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="token" jdbcType="VARCHAR" property="token"/>
<result column="expire_in" jdbcType="INTEGER" property="expireIn"/>
<result column="try_times" jdbcType="INTEGER" property="tryTimes"/>
<result column="account" jdbcType="VARCHAR" property="account"/>
<result column="blacklist" jdbcType="TINYINT" property="blacklist"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/>
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
<result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
-->
id, token, expire_in, try_times, account, blacklist, del_flag, created_date, created_by, updated_date,
updated_by
</sql>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user
where id = #{id}
and del_flag = 0
</select>
<select id="selectByAccount" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user
where account = #{account}
and del_flag = 0
</select>
<insert id="saveOrUpdate" useGeneratedKeys="true" keyProperty="id" parameterType="cn.com.glsx.app.modules.entity.User">
INSERT INTO t_cust_user (id, account, blacklist, del_flag, created_date)
VALUES (#{id}, #{account}, #{blacklist}, 0, sysdate()) ON DUPLICATE KEY
UPDATE updated_date = sysdate()
</insert>
</mapper>
\ No newline at end of file \ No newline at end of file
...@@ -31,21 +31,13 @@ ...@@ -31,21 +31,13 @@
and del_flag = 0 and del_flag = 0
</select> </select>
<select id="selectByLoginTypeAndUserId" resultMap="BaseResultMap"> <select id="selectByLoginTypeAndAuthId" resultMap="BaseResultMap">
select tcuta.* select tcuta.*
from t_user_third_auth tcuta, from t_cust_user_third_auth tcuta
t_user_auth_rel tcuar inner join t_cust_user_auth_rel tcuar on tcuta.id = tcuar.auth_id
where tcuta.id = tcuar.auth_id where tcuta.del_flag = 0
and tcuar.auth_type = 'third' and tcuar.auth_type = 'third'
and tcuta.login_type = #{loginType} and tcuta.login_type = #{loginType}
and tcuar.user_id = #{userId} and tcuar.auth_id = #{userId};
and tcuta.del_flag = 0
and tcuar.del_flag = 0
</select> </select>
<update id="logicDeleteById">
update t_user_third_auth
set del_flag = -1
where id = #{id}
</update>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!