fix:登录接口返回值修改
feat:新增refresh登录;新增登出接口
This commit is contained in:
@@ -40,6 +40,7 @@ public enum ErrorCodeEnum {
|
||||
LOGIN_ERROR(400004, "登录失败", null),
|
||||
ENTERPRISE_INIT(400006, "企业正在初始化,请稍后访问!",null),
|
||||
NOT_AUTH(400007, "暂无权限,请联系管理员!", null),
|
||||
REFRESH_TOKEN_INVALID(400008, "refresh token invalid", null),
|
||||
USER_FREEZE(1021019,"账号被冻结,请联系管理员",null),
|
||||
ENTERPRISE_NOT_EXIST(1021020,"企业不存在",null),
|
||||
USER_NOT_EXIST(1021021,"用户不存在",null),
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.cool.store.dto.login;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* RefreshToken登录DTO
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/9/5
|
||||
*/
|
||||
@Data
|
||||
public class UserRefreshLoginDTO {
|
||||
@ApiModelProperty("RefreshToken")
|
||||
private String refreshToken;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.cool.store.userholder;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* RefreshToken用户信息
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/9/5
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class RefreshUser {
|
||||
/**
|
||||
* 用户Id
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* RefreshToken
|
||||
*/
|
||||
private String refreshToken;
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String mobile;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.cool.store.vo.login;
|
||||
|
||||
import com.cool.store.entity.SysRoleDO;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 登录用户基本信息VO
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/9/5
|
||||
*/
|
||||
@Data
|
||||
public class UserBaseInfoVO {
|
||||
private String id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String name;
|
||||
|
||||
private Boolean isAdmin;
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String email;
|
||||
|
||||
private String avatar;
|
||||
|
||||
private String roles;
|
||||
|
||||
private String language;
|
||||
|
||||
private SysRoleDO sysRoleDO;
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.cool.store.vo.login;
|
||||
|
||||
import com.cool.store.vo.point.UserBaseInfoVO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -12,6 +13,8 @@ import lombok.Data;
|
||||
* @since 2025/9/4
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserLoginVO {
|
||||
/**
|
||||
* 登录token
|
||||
@@ -24,12 +27,12 @@ public class UserLoginVO {
|
||||
private String refreshToken;
|
||||
|
||||
/**
|
||||
* 是否需要完善用户信息
|
||||
* accessToken过期时间
|
||||
*/
|
||||
private Boolean isNeedImproveUserInfo;
|
||||
private Integer expire;
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
private UserBaseInfoVO userInfo;
|
||||
private UserBaseInfoVO user;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.cool.store.service;
|
||||
|
||||
import com.cool.store.userholder.CurrentUser;
|
||||
import com.cool.store.userholder.RefreshUser;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
* @Date 2025/5/29 16:34
|
||||
@@ -13,7 +16,10 @@ public interface EnterpriseService {
|
||||
* @param mobile
|
||||
* @return
|
||||
*/
|
||||
String getAccessToken(String mobile);
|
||||
|
||||
CurrentUser getLoginInfo(String mobile);
|
||||
|
||||
/**
|
||||
* 获取并缓存refreshToken
|
||||
*/
|
||||
RefreshUser getRefreshUser(String userId, String mobile);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,10 @@ import com.cool.store.exception.ServiceException;
|
||||
import com.cool.store.mapper.SysRoleMapper;
|
||||
import com.cool.store.service.EnterpriseService;
|
||||
import com.cool.store.userholder.CurrentUser;
|
||||
import com.cool.store.userholder.RefreshUser;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.cool.store.utils.poi.DateUtils;
|
||||
import com.cool.store.utils.poi.constant.Constants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.crypto.RandomNumberGenerator;
|
||||
@@ -48,7 +50,7 @@ public class EnterpriseServiceImpl implements EnterpriseService {
|
||||
private String eid;
|
||||
|
||||
@Override
|
||||
public String getAccessToken(String mobile) {
|
||||
public CurrentUser getLoginInfo(String mobile) {
|
||||
CurrentUser currentUser = new CurrentUser();
|
||||
EnterpriseUserDO enterpriseUser = enterpriseUserDAO.selectByMobile(mobile);
|
||||
if (Objects.isNull(enterpriseUser)){
|
||||
@@ -107,8 +109,20 @@ public class EnterpriseServiceImpl implements EnterpriseService {
|
||||
currentUser.setAppType("qw_self_dkf");
|
||||
currentUser.setUnionid(enterpriseUser.getUnionid());
|
||||
currentUser.setUserType(enterpriseUser.getUserType());
|
||||
redisUtilPool.setString(RedisConstant.ACCESS_TOKEN_PREFIX + currentUser.getAccessToken(), JSON.toJSONString(currentUser), 24 * 60 * 60);
|
||||
return currentUser.getAccessToken();
|
||||
redisUtilPool.setString(RedisConstant.ACCESS_TOKEN_PREFIX + currentUser.getAccessToken(), JSON.toJSONString(currentUser), Constants.ACTION_TOKEN_EXPIRE);
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RefreshUser getRefreshUser(String userId, String mobile) {
|
||||
if (StringUtils.isBlank(mobile)) {
|
||||
EnterpriseUserDO userInfo = enterpriseUserDAO.getUserInfoById(userId);
|
||||
mobile = userInfo.getMobile();
|
||||
}
|
||||
String refreshToken = getToken();
|
||||
RefreshUser refreshUser = new RefreshUser(userId, refreshToken, mobile);
|
||||
redisUtilPool.setString(RedisConstant.REFRESH_TOKEN_PREFIX + refreshToken, JSON.toJSONString(refreshUser), Constants.REFRESH_TOKEN_EXPIRE);
|
||||
return refreshUser;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
package com.cool.store.service.login;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cool.store.constants.RedisConstant;
|
||||
import com.cool.store.context.CurrentUserHolder;
|
||||
import com.cool.store.context.LoginUserInfo;
|
||||
import com.cool.store.dao.EnterpriseUserDAO;
|
||||
import com.cool.store.dto.login.UserLoginDTO;
|
||||
import com.cool.store.dto.login.UserRefreshLoginDTO;
|
||||
import com.cool.store.entity.EnterpriseUserDO;
|
||||
import com.cool.store.entity.login.UserLoginDO;
|
||||
import com.cool.store.enums.ErrorCodeEnum;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.service.EnterpriseService;
|
||||
import com.cool.store.userholder.CurrentUser;
|
||||
import com.cool.store.userholder.RefreshUser;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.cool.store.utils.poi.constant.Constants;
|
||||
import com.cool.store.vo.login.UserBaseInfoVO;
|
||||
import com.cool.store.vo.login.UserLoginVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.MessageFormat;
|
||||
@@ -27,6 +36,7 @@ import java.time.LocalDate;
|
||||
* @since 2025/9/3
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public abstract class LoginBaseService implements LoginStrategy {
|
||||
@Resource
|
||||
private RedisUtilPool redisUtilPool;
|
||||
@@ -56,13 +66,40 @@ public abstract class LoginBaseService implements LoginStrategy {
|
||||
return userLogin(param, userLoginDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult refreshLogin(UserRefreshLoginDTO param) {
|
||||
String refreshTokenKey = RedisConstant.REFRESH_TOKEN_PREFIX + param.getRefreshToken();
|
||||
String refreshUserStr = redisUtilPool.getString(refreshTokenKey);
|
||||
if (StringUtils.isBlank(refreshUserStr)) {
|
||||
return ResponseResult.fail(ErrorCodeEnum.REFRESH_TOKEN_INVALID);
|
||||
}
|
||||
RefreshUser refreshUser = JSONObject.parseObject(refreshUserStr, RefreshUser.class);
|
||||
if (StringUtils.isBlank(refreshUser.getMobile())) {
|
||||
return ResponseResult.fail(ErrorCodeEnum.REFRESH_TOKEN_INVALID);
|
||||
}
|
||||
UserLoginDO userLoginDO = new UserLoginDO(refreshUser.getUserId(), refreshUser.getMobile(), null);
|
||||
return ResponseResult.success(getUserLoginInfo(userLoginDO));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult logout() {
|
||||
LoginUserInfo currentUser = CurrentUserHolder.getUser();
|
||||
String accessToken = currentUser.getAccessToken();
|
||||
String key = RedisConstant.ACCESS_TOKEN_PREFIX + accessToken;
|
||||
redisUtilPool.delKey(key);
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录accessToken
|
||||
*
|
||||
* @param userLoginDO 用户登录信息
|
||||
* @return accessToken
|
||||
*/
|
||||
public String getAccessToken(UserLoginDO userLoginDO) {
|
||||
return enterpriseService.getAccessToken(userLoginDO.getMobile());
|
||||
public UserLoginVO getUserLoginInfo(UserLoginDO userLoginDO) {
|
||||
CurrentUser currentUser = enterpriseService.getLoginInfo(userLoginDO.getMobile());
|
||||
UserBaseInfoVO userBAseInfoVO = BeanUtil.toBean(currentUser, UserBaseInfoVO.class);
|
||||
RefreshUser refreshUser = enterpriseService.getRefreshUser(userLoginDO.getUserId(), userLoginDO.getMobile());
|
||||
return new UserLoginVO(currentUser.getAccessToken(), refreshUser.getRefreshToken(), Constants.ACTION_TOKEN_EXPIRE, userBAseInfoVO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.cool.store.service.login;
|
||||
|
||||
import com.cool.store.dto.login.UserLoginDTO;
|
||||
import com.cool.store.dto.login.UserRefreshLoginDTO;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
|
||||
/**
|
||||
@@ -14,8 +15,16 @@ import com.cool.store.response.ResponseResult;
|
||||
public interface LoginStrategy {
|
||||
/**
|
||||
* 登录基础方法
|
||||
* @param param
|
||||
* @return
|
||||
*/
|
||||
ResponseResult login(UserLoginDTO param);
|
||||
|
||||
/**
|
||||
* refreshToken登录
|
||||
*/
|
||||
ResponseResult refreshLogin(UserRefreshLoginDTO param);
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*/
|
||||
ResponseResult logout();
|
||||
}
|
||||
|
||||
@@ -49,6 +49,6 @@ public class PasswordLoginServiceImpl extends LoginBaseService {
|
||||
}
|
||||
return ResponseResult.fail(ErrorCodeEnum.PASSWORD_ERROR_MULTI, errorNum.toString());
|
||||
}
|
||||
return ResponseResult.success(getAccessToken(userLoginDO));
|
||||
return ResponseResult.success(getUserLoginInfo(userLoginDO));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,4 +229,14 @@ public class Constants
|
||||
* 用户密码
|
||||
*/
|
||||
public static final String USER_AUTH_KEY = "user_auth_key";
|
||||
|
||||
/**
|
||||
* accessToken有效期,单位秒
|
||||
*/
|
||||
public static final int ACTION_TOKEN_EXPIRE = 24 * 60 * 60;
|
||||
|
||||
/**
|
||||
* refreshToken有效期,单位秒
|
||||
*/
|
||||
public static final int REFRESH_TOKEN_EXPIRE = 30 * 24 * 60 * 60;
|
||||
}
|
||||
|
||||
@@ -59,8 +59,7 @@ public class SignValidateFilter implements Filter {
|
||||
"/zxjp/mini/program/v1/partnerManage/openArea/areaApplyQuery",
|
||||
"/zxjp/**/api/audit/result",
|
||||
"/zxjp/**/api/license",
|
||||
"/zxjp/mini/line/getRegionPayPic",
|
||||
"/zxjp/v3/login/accountLogin"
|
||||
"/zxjp/mini/line/getRegionPayPic"
|
||||
|
||||
);
|
||||
|
||||
|
||||
@@ -53,7 +53,8 @@ public class TokenValidateFilter implements Filter {
|
||||
"/zxjp/**/api/audit/result",
|
||||
"/zxjp/pc/video/**",
|
||||
"/zxjp/**/api/license",
|
||||
"/zxjp/v3/login/accountLogin"
|
||||
"/zxjp/pc/v3/login/accountLogin",
|
||||
"/zxjp/pc/v3/login/refreshLogin"
|
||||
|
||||
|
||||
);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.cool.store.controller.webb;
|
||||
|
||||
import com.cool.store.dto.login.UserLoginDTO;
|
||||
import com.cool.store.dto.login.UserRefreshLoginDTO;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.service.login.LoginBaseService;
|
||||
import com.cool.store.service.login.LoginStrategy;
|
||||
import com.cool.store.utils.SpringContextUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
@@ -19,13 +21,26 @@ import org.springframework.web.bind.annotation.*;
|
||||
*/
|
||||
@Api(tags = "登录")
|
||||
@RestController
|
||||
@RequestMapping("/v3/login")
|
||||
@RequestMapping("/pc/v3/login")
|
||||
@RequiredArgsConstructor
|
||||
public class LoginController {
|
||||
private final LoginBaseService loginBaseService;
|
||||
|
||||
@ApiOperation("账号密码登录")
|
||||
@PostMapping("/accountLogin")
|
||||
public ResponseResult accountLogin(@RequestBody UserLoginDTO param) {
|
||||
return SpringContextUtil.getBean(param.getLoginType().getClazzName(), LoginStrategy.class).login(param);
|
||||
}
|
||||
|
||||
@ApiOperation("refresh登录")
|
||||
@PostMapping("/refreshLogin")
|
||||
public ResponseResult refreshLogin(@RequestBody UserRefreshLoginDTO param) {
|
||||
return loginBaseService.refreshLogin(param);
|
||||
}
|
||||
|
||||
@ApiOperation("登出")
|
||||
@PostMapping("/logout")
|
||||
public ResponseResult logout() {
|
||||
return loginBaseService.logout();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import com.cool.store.dto.ShopAccount.ShopAccountDTO;
|
||||
import com.cool.store.request.GetPasswordDTO;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.service.*;
|
||||
import com.cool.store.vo.PartnerUserInfoVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@@ -95,7 +94,7 @@ public class MiniShopAccountController {
|
||||
@ApiOperation("获取标品登录token")
|
||||
@GetMapping("/getAccessToken")
|
||||
public ResponseResult<String> getAccessToken() {
|
||||
return ResponseResult.success(enterpriseService.getAccessToken(PartnerUserHolder.getUser().getMobile()));
|
||||
return ResponseResult.success(enterpriseService.getLoginInfo(PartnerUserHolder.getUser().getMobile()).getAccessToken());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user