feat: app扫码登录
This commit is contained in:
@@ -487,7 +487,16 @@ public enum CachePrefix {
|
||||
/**
|
||||
* 敏感词
|
||||
*/
|
||||
SENSITIVE;
|
||||
SENSITIVE,
|
||||
|
||||
/**
|
||||
* 扫码登录
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
QR_CODE_LOGIN_SESSION
|
||||
|
||||
;
|
||||
|
||||
|
||||
public static String removePrefix(String str) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.lili.modules.member.entity.enums;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum QRCodeLoginSessionStatusEnum {
|
||||
|
||||
/**
|
||||
* 二维码创建完毕,等待app端扫码
|
||||
*/
|
||||
WAIT_SCANNING(0,"等待扫码"),
|
||||
|
||||
/**
|
||||
* app端已经扫码,等待确认同意登录
|
||||
*/
|
||||
SCANNING(1,"已经扫码"),
|
||||
|
||||
/**
|
||||
* 用户在app端点击了同意登录
|
||||
*/
|
||||
VERIFIED(2,"确认登录"),
|
||||
|
||||
/**
|
||||
* 用户在app端点击了取消登录
|
||||
*/
|
||||
CANCELED(3,"取消登录"),
|
||||
|
||||
/**
|
||||
* 二维码不存在/或者已经过期
|
||||
*/
|
||||
NO_EXIST(4,"二维码已过期")
|
||||
|
||||
;
|
||||
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String desc;
|
||||
|
||||
|
||||
QRCodeLoginSessionStatusEnum(Integer code,String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.lili.modules.member.entity.vo;
|
||||
|
||||
import cn.lili.modules.member.entity.enums.QRCodeLoginSessionStatusEnum;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class QRCodeLoginSessionVo implements Serializable {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 8793639296995408322L;
|
||||
|
||||
private String token;
|
||||
|
||||
private Integer status;
|
||||
|
||||
private long duration;
|
||||
|
||||
private long userId;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.lili.modules.member.entity.vo;
|
||||
|
||||
import cn.lili.common.security.token.Token;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class QRLoginResultVo {
|
||||
|
||||
private Token token;
|
||||
|
||||
private int status;
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import cn.lili.modules.member.entity.dto.MemberAddDTO;
|
||||
import cn.lili.modules.member.entity.dto.MemberEditDTO;
|
||||
import cn.lili.modules.member.entity.vo.MemberSearchVO;
|
||||
import cn.lili.modules.member.entity.vo.MemberVO;
|
||||
import cn.lili.modules.member.entity.vo.QRLoginResultVo;
|
||||
import cn.lili.modules.member.entity.vo.QRCodeLoginSessionVo;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
@@ -272,4 +274,12 @@ public interface MemberService extends IService<Member> {
|
||||
* @return 用户VO
|
||||
*/
|
||||
MemberVO getMember(String id);
|
||||
|
||||
QRCodeLoginSessionVo createPcSession();
|
||||
|
||||
Object appScanner(String token);
|
||||
|
||||
boolean appSConfirm(String token, Integer code);
|
||||
|
||||
QRLoginResultVo loginWithSession(String token);
|
||||
}
|
||||
@@ -26,8 +26,11 @@ import cn.lili.modules.member.aop.annotation.PointLogPoint;
|
||||
import cn.lili.modules.member.entity.dos.Member;
|
||||
import cn.lili.modules.member.entity.dto.*;
|
||||
import cn.lili.modules.member.entity.enums.PointTypeEnum;
|
||||
import cn.lili.modules.member.entity.enums.QRCodeLoginSessionStatusEnum;
|
||||
import cn.lili.modules.member.entity.vo.MemberSearchVO;
|
||||
import cn.lili.modules.member.entity.vo.MemberVO;
|
||||
import cn.lili.modules.member.entity.vo.QRLoginResultVo;
|
||||
import cn.lili.modules.member.entity.vo.QRCodeLoginSessionVo;
|
||||
import cn.lili.modules.member.mapper.MemberMapper;
|
||||
import cn.lili.modules.member.service.MemberService;
|
||||
import cn.lili.modules.member.token.MemberTokenGenerate;
|
||||
@@ -49,10 +52,8 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 会员接口业务层实现
|
||||
@@ -668,6 +669,74 @@ public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> impleme
|
||||
return new MemberVO(this.getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public QRCodeLoginSessionVo createPcSession() {
|
||||
QRCodeLoginSessionVo session = new QRCodeLoginSessionVo();
|
||||
session.setStatus(QRCodeLoginSessionStatusEnum.WAIT_SCANNING.getCode());
|
||||
//过期时间,10s
|
||||
Long duration= 10 * 1000L;
|
||||
session.setDuration(duration);
|
||||
String token = CachePrefix.QR_CODE_LOGIN_SESSION.name()+SnowFlake.getIdStr();
|
||||
session.setToken(token);
|
||||
cache.put(token,session,duration, TimeUnit.MILLISECONDS);
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object appScanner(String token) {
|
||||
QRCodeLoginSessionVo session = (QRCodeLoginSessionVo) cache.get(token);
|
||||
if(session == null){
|
||||
return QRCodeLoginSessionStatusEnum.NO_EXIST.getCode();
|
||||
}
|
||||
session.setStatus(QRCodeLoginSessionStatusEnum.SCANNING.getCode());
|
||||
cache.put(token,session,session.getDuration(), TimeUnit.MILLISECONDS);
|
||||
return QRCodeLoginSessionStatusEnum.SCANNING.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appSConfirm(String token, Integer code) {
|
||||
QRCodeLoginSessionVo session = (QRCodeLoginSessionVo) cache.get(token);
|
||||
if(session == null){
|
||||
return false;
|
||||
}
|
||||
if(code==1){
|
||||
//同意
|
||||
session.setStatus(QRCodeLoginSessionStatusEnum.VERIFIED.getCode());
|
||||
AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
|
||||
session.setUserId(Long.valueOf(currentUser.getId()));
|
||||
}else{
|
||||
//拒绝
|
||||
session.setStatus(QRCodeLoginSessionStatusEnum.CANCELED.getCode());
|
||||
}
|
||||
cache.put(token,session,session.getDuration(), TimeUnit.MILLISECONDS);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QRLoginResultVo loginWithSession(String sessionToken) {
|
||||
QRLoginResultVo result = new QRLoginResultVo();
|
||||
result.setStatus(QRCodeLoginSessionStatusEnum.NO_EXIST.getCode());
|
||||
QRCodeLoginSessionVo session = (QRCodeLoginSessionVo) cache.get(sessionToken);
|
||||
if(session == null){
|
||||
return result;
|
||||
}
|
||||
result.setStatus(session.getStatus());
|
||||
if(QRCodeLoginSessionStatusEnum.VERIFIED.getCode().equals(session.getStatus())){
|
||||
//生成token
|
||||
Member member = this.getById(session.getUserId());
|
||||
if(member==null){
|
||||
throw new ServiceException(ResultCode.USER_NOT_EXIST);
|
||||
}else{
|
||||
//生成token
|
||||
Token token = memberTokenGenerate.createToken(member, false);
|
||||
result.setToken(token);
|
||||
cache.vagueDel(sessionToken);
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测会员
|
||||
*
|
||||
|
||||
@@ -37,17 +37,18 @@ public class MemberTokenGenerate extends AbstractTokenGenerate<Member> {
|
||||
@Override
|
||||
public Token createToken(Member member, Boolean longTerm) {
|
||||
|
||||
//获取客户端类型
|
||||
String clientType = ThreadContextHolder.getHttpRequest().getHeader("clientType");
|
||||
|
||||
ClientTypeEnum clientTypeEnum;
|
||||
try {
|
||||
//获取客户端类型
|
||||
String clientType = ThreadContextHolder.getHttpRequest().getHeader("clientType");
|
||||
//如果客户端为空,则缺省值为PC,pc第三方登录时不会传递此参数
|
||||
if (clientType == null) {
|
||||
clientTypeEnum = ClientTypeEnum.PC;
|
||||
} else {
|
||||
clientTypeEnum = ClientTypeEnum.valueOf(clientType);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (Exception e) {
|
||||
clientTypeEnum = ClientTypeEnum.UNKNOWN;
|
||||
}
|
||||
//记录最后登录时间,客户端类型
|
||||
|
||||
Reference in New Issue
Block a user