mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-10 18:27:07 +00:00
小程序登录接口修改
This commit is contained in:
@@ -2,6 +2,9 @@ package org.ruoyi.controller;
|
|||||||
|
|
||||||
import cn.dev33.satoken.annotation.SaIgnore;
|
import cn.dev33.satoken.annotation.SaIgnore;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import cn.hutool.json.ObjectMapper;
|
||||||
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import org.ruoyi.common.core.constant.Constants;
|
import org.ruoyi.common.core.constant.Constants;
|
||||||
import org.ruoyi.common.core.domain.R;
|
import org.ruoyi.common.core.domain.R;
|
||||||
import org.ruoyi.common.core.domain.model.EmailLoginBody;
|
import org.ruoyi.common.core.domain.model.EmailLoginBody;
|
||||||
@@ -48,6 +51,15 @@ public class AuthController {
|
|||||||
private final SysRegisterService registerService;
|
private final SysRegisterService registerService;
|
||||||
private final ISysTenantService tenantService;
|
private final ISysTenantService tenantService;
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/xcxLogin")
|
||||||
|
public R<LoginVo> login(@Validated @RequestBody String xcxCode) throws WxErrorException {
|
||||||
|
|
||||||
|
String openidFromCode = loginService.getOpenidFromCode((String) JSONUtil.parseObj(xcxCode).get("xcxCode"));
|
||||||
|
LoginVo loginVo = loginService.mpLogin(openidFromCode);
|
||||||
|
return R.ok(loginVo);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录方法
|
* 登录方法
|
||||||
*
|
*
|
||||||
@@ -85,6 +97,7 @@ public class AuthController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 访客登录
|
* 访客登录
|
||||||
|
*
|
||||||
* @param loginBody 登录信息
|
* @param loginBody 登录信息
|
||||||
* @return token信息
|
* @return token信息
|
||||||
*/
|
*/
|
||||||
@@ -123,7 +136,7 @@ public class AuthController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public R<Void> register(@Validated @RequestBody RegisterBody user, HttpServletRequest request) {
|
public R<Void> register(@Validated @RequestBody RegisterBody user, HttpServletRequest request) {
|
||||||
String domainName = request.getServerName();
|
String domainName = request.getServerName();
|
||||||
user.setDomainName(domainName);
|
user.setDomainName(domainName);
|
||||||
registerService.register(user);
|
registerService.register(user);
|
||||||
return R.ok();
|
return R.ok();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.ruoyi.system.service;
|
package org.ruoyi.system.service;
|
||||||
|
|
||||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||||
|
import cn.binarywang.wx.miniapp.api.WxMaUserService;
|
||||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||||
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
||||||
import cn.dev33.satoken.exception.NotLoginException;
|
import cn.dev33.satoken.exception.NotLoginException;
|
||||||
@@ -68,6 +69,19 @@ public class SysLoginService {
|
|||||||
@Value("${user.password.lockTime}")
|
@Value("${user.password.lockTime}")
|
||||||
private Integer lockTime;
|
private Integer lockTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取微信
|
||||||
|
* @param xcxCode 获取xcxCode
|
||||||
|
*/
|
||||||
|
public String getOpenidFromCode(String xcxCode) {
|
||||||
|
try {
|
||||||
|
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(xcxCode);
|
||||||
|
return sessionInfo.getOpenid();
|
||||||
|
} catch (WxErrorException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 登录验证
|
* 登录验证
|
||||||
*
|
*
|
||||||
@@ -135,15 +149,14 @@ public class SysLoginService {
|
|||||||
public void visitorLogin(VisitorLoginBody loginBody) {
|
public void visitorLogin(VisitorLoginBody loginBody) {
|
||||||
String openid = "";
|
String openid = "";
|
||||||
// PC端游客登录
|
// PC端游客登录
|
||||||
if(LoginUserType.PC.getCode().equals(loginBody.getType())){
|
if (LoginUserType.PC.getCode().equals(loginBody.getType())) {
|
||||||
openid = loginBody.getCode();
|
openid = loginBody.getCode();
|
||||||
}else {
|
} else {
|
||||||
// 小程序匿名登录
|
// 小程序匿名登录
|
||||||
try {
|
try {
|
||||||
WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(loginBody.getCode());
|
WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(loginBody.getCode());
|
||||||
openid = session.getOpenid();
|
openid = session.getOpenid();
|
||||||
} catch (
|
} catch (WxErrorException e) {
|
||||||
WxErrorException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
// 清理ThreadLocal
|
// 清理ThreadLocal
|
||||||
@@ -159,7 +172,8 @@ public class SysLoginService {
|
|||||||
if (ObjectUtil.isNull(user)) {
|
if (ObjectUtil.isNull(user)) {
|
||||||
SysUserBo sysUser = new SysUserBo();
|
SysUserBo sysUser = new SysUserBo();
|
||||||
// 改为自增
|
// 改为自增
|
||||||
String name = "用户" + UUIDShortUtil.generateShortUuid();;
|
String name = "用户" + UUIDShortUtil.generateShortUuid();
|
||||||
|
;
|
||||||
// 设置默认用户名
|
// 设置默认用户名
|
||||||
sysUser.setUserName(name);
|
sysUser.setUserName(name);
|
||||||
// 设置默认昵称
|
// 设置默认昵称
|
||||||
@@ -170,7 +184,7 @@ public class SysLoginService {
|
|||||||
sysUser.setOpenId(openid);
|
sysUser.setOpenId(openid);
|
||||||
String configValue = configService.getConfigValue("mail", "amount");
|
String configValue = configService.getConfigValue("mail", "amount");
|
||||||
// 设置默认余额
|
// 设置默认余额
|
||||||
sysUser.setUserBalance(NumberUtils.toDouble(configValue,1));
|
sysUser.setUserBalance(NumberUtils.toDouble(configValue, 1));
|
||||||
// 注册用户,设置默认租户为0
|
// 注册用户,设置默认租户为0
|
||||||
SysUser registerUser = userService.registerUser(sysUser, "0");
|
SysUser registerUser = userService.registerUser(sysUser, "0");
|
||||||
|
|
||||||
@@ -284,10 +298,7 @@ public class SysLoginService {
|
|||||||
|
|
||||||
private SysUserVo loadUserByUsername(String tenantId, String username) {
|
private SysUserVo loadUserByUsername(String tenantId, String username) {
|
||||||
|
|
||||||
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
|
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().select(SysUser::getUserName, SysUser::getStatus).eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId).eq(SysUser::getUserName, username));
|
||||||
.select(SysUser::getUserName, SysUser::getStatus)
|
|
||||||
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
|
|
||||||
.eq(SysUser::getUserName, username));
|
|
||||||
if (ObjectUtil.isNull(user)) {
|
if (ObjectUtil.isNull(user)) {
|
||||||
log.info("登录用户:{} 不存在.", username);
|
log.info("登录用户:{} 不存在.", username);
|
||||||
throw new UserException("user.not.exists", username);
|
throw new UserException("user.not.exists", username);
|
||||||
@@ -302,10 +313,7 @@ public class SysLoginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SysUserVo loadUserByPhonenumber(String tenantId, String phonenumber) {
|
private SysUserVo loadUserByPhonenumber(String tenantId, String phonenumber) {
|
||||||
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
|
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().select(SysUser::getPhonenumber, SysUser::getStatus).eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId).eq(SysUser::getPhonenumber, phonenumber));
|
||||||
.select(SysUser::getPhonenumber, SysUser::getStatus)
|
|
||||||
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
|
|
||||||
.eq(SysUser::getPhonenumber, phonenumber));
|
|
||||||
if (ObjectUtil.isNull(user)) {
|
if (ObjectUtil.isNull(user)) {
|
||||||
log.info("登录用户:{} 不存在.", phonenumber);
|
log.info("登录用户:{} 不存在.", phonenumber);
|
||||||
throw new UserException("user.not.exists", phonenumber);
|
throw new UserException("user.not.exists", phonenumber);
|
||||||
@@ -320,10 +328,7 @@ public class SysLoginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SysUserVo loadUserByEmail(String tenantId, String email) {
|
private SysUserVo loadUserByEmail(String tenantId, String email) {
|
||||||
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
|
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().select(SysUser::getPhonenumber, SysUser::getStatus).eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId).eq(SysUser::getEmail, email));
|
||||||
.select(SysUser::getPhonenumber, SysUser::getStatus)
|
|
||||||
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
|
|
||||||
.eq(SysUser::getEmail, email));
|
|
||||||
if (ObjectUtil.isNull(user)) {
|
if (ObjectUtil.isNull(user)) {
|
||||||
log.info("登录用户:{} 不存在.", email);
|
log.info("登录用户:{} 不存在.", email);
|
||||||
throw new UserException("user.not.exists", email);
|
throw new UserException("user.not.exists", email);
|
||||||
@@ -419,8 +424,7 @@ public class SysLoginService {
|
|||||||
} else if (TenantStatus.DISABLE.getCode().equals(tenant.getStatus())) {
|
} else if (TenantStatus.DISABLE.getCode().equals(tenant.getStatus())) {
|
||||||
log.info("登录租户:{} 已被停用.", tenantId);
|
log.info("登录租户:{} 已被停用.", tenantId);
|
||||||
throw new TenantException("tenant.blocked");
|
throw new TenantException("tenant.blocked");
|
||||||
} else if (ObjectUtil.isNotNull(tenant.getExpireTime())
|
} else if (ObjectUtil.isNotNull(tenant.getExpireTime()) && new Date().after(tenant.getExpireTime())) {
|
||||||
&& new Date().after(tenant.getExpireTime())) {
|
|
||||||
log.info("登录租户:{} 已超过有效期.", tenantId);
|
log.info("登录租户:{} 已超过有效期.", tenantId);
|
||||||
throw new TenantException("tenant.expired");
|
throw new TenantException("tenant.expired");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user