Skip to content

Commit

Permalink
增加会员登录事件,对token处理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
chopper711 committed Jan 11, 2022
1 parent cc7d2c0 commit eddcbc2
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 59 deletions.
2 changes: 1 addition & 1 deletion config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ spring:
props:
#是否打印逻辑SQL语句和实际SQL语句,建议调试时打印,在生产环境关闭
sql:
show: false
show: true

# 忽略鉴权url
ignored:
Expand Down
19 changes: 19 additions & 0 deletions consumer/src/main/java/cn/lili/event/MemberLoginEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.lili.event;

import cn.lili.modules.member.entity.dos.Member;

/**
* 会员登录消息
*
* @author Chopper
* @since 2020/11/17 7:13 下午
*/
public interface MemberLoginEvent {

/**
* 会员登录
*
* @param member 会员
*/
void memberLogin(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface MemberRegisterEvent {

/**
* 会员登录
* 会员注册
*
* @param member 会员
*/
Expand Down
25 changes: 25 additions & 0 deletions consumer/src/main/java/cn/lili/event/impl/MemberExecute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.lili.event.impl;

import cn.lili.event.MemberLoginEvent;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 会员自身业务
*
* @author Chopper
* @version v1.0
* 2022-01-11 11:08
*/
@Service
public class MemberExecute implements MemberLoginEvent {
@Autowired
private MemberService memberService;

@Override
public void memberLogin(Member member) {
memberService.updateById(member);
}
}
22 changes: 22 additions & 0 deletions consumer/src/main/java/cn/lili/listener/MemberMessageListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.lili.listener;

import cn.hutool.json.JSONUtil;
import cn.lili.event.MemberLoginEvent;
import cn.lili.event.MemberPointChangeEvent;
import cn.lili.event.MemberRegisterEvent;
import cn.lili.event.MemberWithdrawalEvent;
Expand Down Expand Up @@ -51,6 +52,12 @@ public class MemberMessageListener implements RocketMQListener<MessageExt> {
@Autowired
private List<MemberRegisterEvent> memberSignEvents;

/**
* 会员注册
*/
@Autowired
private List<MemberLoginEvent> memberLoginEvents;


@Override
public void onMessage(MessageExt messageExt) {
Expand All @@ -69,6 +76,21 @@ public void onMessage(MessageExt messageExt) {
}
}
break;

case MEMBER_LOGIN:

for (MemberLoginEvent memberLoginEvent : memberLoginEvents) {
try {
Member member = JSONUtil.toBean(new String(messageExt.getBody()), Member.class);
memberLoginEvent.memberLogin(member);
} catch (Exception e) {
log.error("会员{},在{}业务中,状态修改事件执行异常",
new String(messageExt.getBody()),
memberLoginEvent.getClass().getName(),
e);
}
}
break;
//会员签到
case MEMBER_SING:
MemberSign memberSign = JSONUtil.toBean(new String(messageExt.getBody()), MemberSign.class);
Expand Down
3 changes: 1 addition & 2 deletions framework/src/main/java/cn/lili/common/enums/ResultCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public enum ResultCode {
USER_AUTH_EXPIRED(20004, "用户已退出,请重新登录"),
USER_AUTHORITY_ERROR(20005, "权限不足"),
USER_CONNECT_LOGIN_ERROR(20006, "未找到登录信息"),
USER_NAME_EXIST(20007, "该用户名已被注册"),
USER_PHONE_EXIST(20008, "该手机号已被注册"),
USER_EXIST(20008, "该用户名或手机号已被注册"),
USER_PHONE_NOT_EXIST(20009, "手机号不存在"),
USER_PASSWORD_ERROR(20010, "密码不正确"),
USER_NOT_PHONE(20011, "非当前用户的手机号"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public abstract class AbstractTokenGenerate {
/**
* 生成token
*
* @param username 用户名
* @param user 用户名
* @param longTerm 是否长时间有效
* @return TOKEN对象
*/
public abstract Token createToken(String username, Boolean longTerm);
public abstract Token createToken(Object user, Boolean longTerm);

/**
* 刷新token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Token unionLoginCallback(String type, String unionid, String uuid, boolea
this.remove(queryWrapper);
throw new NoPermissionException("未绑定用户");
}
return memberTokenGenerate.createToken(member.getUsername(), longTerm);
return memberTokenGenerate.createToken(member, longTerm);
} catch (NoPermissionException e) {
throw e;
}
Expand Down Expand Up @@ -222,15 +222,15 @@ public Token phoneMpBindAndLogin(String sessionKey, WechatMPLoginParams params,
//如果不存在会员,则进行绑定微信openid 和 unionid,并且登录
if (member != null) {
bindMpMember(openId, unionId, member);
return memberTokenGenerate.createToken(member.getUsername(), true);
return memberTokenGenerate.createToken(member, true);
}

//如果没有会员,则根据手机号注册会员
Member newMember = new Member("m" + phone, "111111", phone, params.getNickName(), params.getImage());
memberService.save(newMember);
newMember = memberService.findByUsername(newMember.getUsername());
bindMpMember(openId, unionId, newMember);
return memberTokenGenerate.createToken(newMember.getUsername(), true);
return memberTokenGenerate.createToken(newMember, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cn.lili.common.sensitive.SensitiveWordsFilter;
import cn.lili.common.utils.BeanUtil;
import cn.lili.common.utils.CookieUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.common.utils.UuidUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.connect.config.ConnectAuthEnum;
Expand Down Expand Up @@ -143,7 +144,7 @@ public Token usernameLogin(String username, String password) {
// throw new ServiceException(ResultCode.USER_PASSWORD_ERROR);
// }
// loginBindUser(member);
return memberTokenGenerate.createToken(member.getUsername(), false);
return memberTokenGenerate.createToken(member, false);
}

@Override
Expand All @@ -168,7 +169,7 @@ public Token usernameStoreLogin(String username, String password) {
throw new ServiceException(ResultCode.USER_NOT_EXIST);
}

return storeTokenGenerate.createToken(member.getUsername(), false);
return storeTokenGenerate.createToken(member, false);
}

/**
Expand Down Expand Up @@ -196,12 +197,10 @@ public Token autoRegister(ConnectAuthUser authUser) {
String username = UuidUtils.getUUID();
Member member = new Member(username, UuidUtils.getUUID(), authUser.getAvatar(), authUser.getNickname(),
authUser.getGender() != null ? Convert.toInt(authUser.getGender().getCode()) : 0);
//保存会员
this.save(member);
Member loadMember = this.findByUsername(username);
registerHandler(member);
//绑定登录方式
loginBindUser(loadMember, authUser.getUuid(), authUser.getSource());
return memberTokenGenerate.createToken(username, false);
loginBindUser(member, authUser.getUuid(), authUser.getSource());
return memberTokenGenerate.createToken(member, false);
} catch (ServiceException e) {
log.error("自动注册服务泡出异常:", e);
throw e;
Expand Down Expand Up @@ -235,13 +234,23 @@ public Token mobilePhoneLogin(String mobilePhone) {
//如果手机号不存在则自动注册用户
if (member == null) {
member = new Member(mobilePhone, UuidUtils.getUUID(), mobilePhone);
//保存会员
this.save(member);
String destination = rocketmqCustomProperties.getMemberTopic() + ":" + MemberTagsEnum.MEMBER_REGISTER.name();
rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());
registerHandler(member);
}
loginBindUser(member);
return memberTokenGenerate.createToken(member.getUsername(), false);
return memberTokenGenerate.createToken(member, false);
}

/**
* 注册方法抽象
*
* @param member
*/
private void registerHandler(Member member) {
member.setId(SnowFlake.getIdStr());
//保存会员
this.save(member);
String destination = rocketmqCustomProperties.getMemberTopic() + ":" + MemberTagsEnum.MEMBER_REGISTER.name();
rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());
}

@Override
Expand Down Expand Up @@ -281,13 +290,9 @@ public Token register(String userName, String password, String mobilePhone) {
//设置会员信息
Member member = new Member(userName, new BCryptPasswordEncoder().encode(password), mobilePhone);
//注册成功后用户自动登录
if (this.save(member)) {
Token token = memberTokenGenerate.createToken(member.getUsername(), false);
String destination = rocketmqCustomProperties.getMemberTopic() + ":" + MemberTagsEnum.MEMBER_REGISTER.name();
rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());
return token;
}
return null;
registerHandler(member);
Token token = memberTokenGenerate.createToken(member, false);
return token;
}

@Override
Expand Down Expand Up @@ -331,9 +336,7 @@ public Member addMember(MemberAddDTO memberAddDTO) {

//添加会员
Member member = new Member(memberAddDTO.getUsername(), new BCryptPasswordEncoder().encode(memberAddDTO.getPassword()), memberAddDTO.getMobile());
this.save(member);
String destination = rocketmqCustomProperties.getMemberTopic() + ":" + MemberTagsEnum.MEMBER_REGISTER.name();
rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());
registerHandler(member);
return member;
}

Expand Down Expand Up @@ -430,10 +433,11 @@ public Boolean updateMemberStatus(List<String> memberIds, Boolean status) {
* @param mobilePhone 手机号
* @return 会员
*/
private Member findByPhone(String mobilePhone) {
private List<Member> findMember(String mobilePhone, String userName) {
QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("mobile", mobilePhone);
return this.baseMapper.selectOne(queryWrapper);
queryWrapper.eq("mobile", mobilePhone)
.or().eq("username", userName);
return this.baseMapper.selectList(queryWrapper);
}

/**
Expand Down Expand Up @@ -589,13 +593,9 @@ public void logout(UserEnums userEnums) {
* @param mobilePhone 手机号
*/
private void checkMember(String userName, String mobilePhone) {
//判断用户名是否存在
if (findByUsername(userName) != null) {
throw new ServiceException(ResultCode.USER_NAME_EXIST);
}
//判断手机号是否存在
if (findByPhone(mobilePhone) != null) {
throw new ServiceException(ResultCode.USER_PHONE_EXIST);
if (findMember(userName, mobilePhone) != null) {
throw new ServiceException(ResultCode.USER_EXIST);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.lili.modules.member.token;

import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.common.security.token.Token;
Expand All @@ -9,6 +10,9 @@
import cn.lili.common.enums.ClientTypeEnum;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.service.MemberService;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.MemberTagsEnum;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -28,11 +32,16 @@ public class MemberTokenGenerate extends AbstractTokenGenerate {
private MemberService memberService;
@Autowired
private TokenUtil tokenUtil;
@Autowired
private RocketmqCustomProperties rocketmqCustomProperties;

@Autowired
private RocketMQTemplate rocketMQTemplate;

@Override
public Token createToken(String username, Boolean longTerm) {
public Token createToken(Object user, Boolean longTerm) {

Member member = memberService.findByUsername(username);
Member member = (Member) user;

//获取客户端类型
String clientType = ThreadContextHolder.getHttpRequest().getHeader("clientType");
Expand All @@ -50,11 +59,12 @@ public Token createToken(String username, Boolean longTerm) {
//记录最后登录时间,客户端类型
member.setLastLoginDate(new Date());
member.setClientEnum(clientTypeEnum.name());
memberService.updateById(member);
String destination = rocketmqCustomProperties.getMemberTopic() + ":" + MemberTagsEnum.MEMBER_LOGIN.name();
rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());

AuthUser authUser = new AuthUser(member.getUsername(), member.getId(), member.getNickName(), member.getFace(), UserEnums.MEMBER);
//登陆成功生成token
return tokenUtil.createToken(username, authUser, longTerm, UserEnums.MEMBER);
return tokenUtil.createToken(member.getUsername(), authUser, longTerm, UserEnums.MEMBER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public class StoreTokenGenerate extends AbstractTokenGenerate {
private TokenUtil tokenUtil;

@Override
public Token createToken(String username, Boolean longTerm) {
public Token createToken(Object user, Boolean longTerm) {
//生成token
Member member = memberService.findByUsername(username);
Member member = (Member) user;
if (!member.getHaveStore()) {
throw new ServiceException(ResultCode.STORE_NOT_OPEN);
}
LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Store::getMemberId, member.getId());
Store store = storeService.getOne(queryWrapper);
AuthUser user = new AuthUser(member.getUsername(), member.getId(), member.getNickName(), store.getStoreLogo(), UserEnums.STORE);
AuthUser authUser = new AuthUser(member.getUsername(), member.getId(), member.getNickName(), store.getStoreLogo(), UserEnums.STORE);

user.setStoreId(store.getId());
user.setStoreName(store.getStoreName());
return tokenUtil.createToken(username, user, longTerm, UserEnums.STORE);
authUser.setStoreId(store.getId());
authUser.setStoreName(store.getStoreName());
return tokenUtil.createToken(member.getUsername(), authUser, longTerm, UserEnums.STORE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Token login(String username, String password) {
throw new ServiceException(ResultCode.USER_PASSWORD_ERROR);
}
try {
return managerTokenGenerate.createToken(username, false);
return managerTokenGenerate.createToken(adminUser, false);
} catch (Exception e) {
log.error("管理员登录错误", e);
}
Expand Down
Loading

0 comments on commit eddcbc2

Please sign in to comment.