Commit a7bbd26c authored by 王亮's avatar 王亮

log user tag(create session and save user)

parent 6cfd9390
...@@ -50,8 +50,11 @@ CREATE TABLE `xyqb_user`.`user_tag` ( ...@@ -50,8 +50,11 @@ CREATE TABLE `xyqb_user`.`user_tag` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT, `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`user_id` BIGINT(20) NOT NULL COMMENT '用户表id', `user_id` BIGINT(20) NOT NULL COMMENT '用户表id',
`registered_from` BIGINT(20) NULL COMMENT '来源(之前字段)', `registered_from` BIGINT(20) NULL COMMENT '来源(之前字段)',
`tenant_id` INT NOT NULL COMMENT '租户id',
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
UNIQUE `udx_user_id` (`user_id` ASC),
INDEX `idx_user_phone` (`registered_from` ASC),
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));
......
...@@ -3,13 +3,19 @@ package cn.quantgroup.xyqb.config; ...@@ -3,13 +3,19 @@ package cn.quantgroup.xyqb.config;
import cn.quantgroup.xyqb.util.DefaultSequencer; import cn.quantgroup.xyqb.util.DefaultSequencer;
import cn.quantgroup.xyqb.util.IdentitySequencer; import cn.quantgroup.xyqb.util.IdentitySequencer;
import cn.quantgroup.xyqb.util.ServerUtils; import cn.quantgroup.xyqb.util.ServerUtils;
import com.sensorsdata.analytics.javasdk.ISensorsAnalytics;
import com.sensorsdata.analytics.javasdk.SensorsAnalytics;
import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import java.io.IOException;
/** /**
* Created by hechao on 2020/1/22. * Created by hechao on 2020/1/22.
*/ */
...@@ -38,4 +44,16 @@ public class AutoConfiguration { ...@@ -38,4 +44,16 @@ public class AutoConfiguration {
return new DefaultSequencer(workId, sequencerProperties); return new DefaultSequencer(workId, sequencerProperties);
} }
} }
@Value("${sc.url}")
private String dataUrl;
@Bean(destroyMethod = "shutdown")
public ISensorsAnalytics init() throws IOException {
// //本地日志模式(此模式会在指定路径生成相应的日志文件)
// return new SensorsAnalytics(new ConcurrentLoggingConsumer("/Users/hongzhi/scEvent/"));
//网络批量发送模式(此模式在容器关闭的时候,如果存在数据还没有发送完毕,就会丢失未发送的数据!!!)
return new SensorsAnalytics(new BatchConsumer(dataUrl));
}
} }
\ No newline at end of file
...@@ -800,6 +800,7 @@ public class UserController implements IBaseController { ...@@ -800,6 +800,7 @@ public class UserController implements IBaseController {
if (geetestLogId != null) { if (geetestLogId != null) {
geetestLogService.updateByUidGeetestLog(geetestLogId, user.getId()); geetestLogService.updateByUidGeetestLog(geetestLogId, user.getId());
} }
//更新session //更新session
return new JsonResult(sessionService.createSession(user, loginProperties, LoginType.ACCOUNTPASSWORD.ordinal(),tenantId)); return new JsonResult(sessionService.createSession(user, loginProperties, LoginType.ACCOUNTPASSWORD.ordinal(),tenantId));
} }
......
package cn.quantgroup.xyqb.entity;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "user_tag")
@Builder
public class UserTag extends BaseEntity implements Serializable {
@Column(name = "user_id")
private Long userId;
//第一次用户来源 channel_id
@Column(name = "registered_from")
private Long registeredFrom;
@Column(name = "tenant_id")
private Integer tenantId;
}
package cn.quantgroup.xyqb.event; package cn.quantgroup.xyqb.event;
import com.sensorsdata.analytics.javasdk.ISensorsAnalytics; import cn.quantgroup.xyqb.entity.UserTag;
import com.sensorsdata.analytics.javasdk.SensorsAnalytics; import lombok.Builder;
import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; import lombok.Data;
import org.springframework.beans.factory.annotation.Value; import lombok.Getter;
import org.springframework.context.annotation.Bean; import lombok.Setter;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/** /**
* @author :hongzhi * @author :hongzhi
*/ */
@Configuration
public class UserLoginEvent {
@Value("${sc.url}")
private String dataUrl;
@Bean(destroyMethod = "shutdown") @Getter
public ISensorsAnalytics init() throws IOException { @Setter
// //本地日志模式(此模式会在指定路径生成相应的日志文件) public class UserLoginEvent extends ApplicationEvent {
// return new SensorsAnalytics(new ConcurrentLoggingConsumer("/Users/hongzhi/scEvent/"));
//网络批量发送模式(此模式在容器关闭的时候,如果存在数据还没有发送完毕,就会丢失未发送的数据!!!) private UserTag userTag;
return new SensorsAnalytics(new BatchConsumer(dataUrl));
} public UserLoginEvent(Object source) {
super(source);
}
public UserLoginEvent(Object source, UserTag userTag) {
super(source);
this.userTag = userTag;
}
} }
package cn.quantgroup.xyqb.event;
import cn.quantgroup.xyqb.entity.UserTag;
import cn.quantgroup.xyqb.repository.IUserTagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* user_tag表,用来监听
*/
@Component
public class UserTagLoginEventListener implements ApplicationListener<UserLoginEvent> {
@Autowired
private IUserTagRepository userTagRepository;
/**
* 逻辑 每次登录发送UserLoginEvent,落user_tag表
* 如果没有就新增,如果有就更新
* @param userLoginEvent UserLoginEvent
*/
@Override
public void onApplicationEvent(UserLoginEvent userLoginEvent) {
if (userLoginEvent.getUserTag() != null) {
UserTag userTag = userLoginEvent.getUserTag();
UserTag preTag = userTagRepository.findByUserIdAndTenantId(userTag.getUserId(), userTag.getTenantId());
if (preTag == null) {
preTag = userTag;
}
preTag.setCreatedAt(LocalDateTime.now());
preTag.setUpdatedAt(LocalDateTime.now());
userTagRepository.save(preTag);
}
}
}
package cn.quantgroup.xyqb.repository;
import cn.quantgroup.xyqb.entity.UserTag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface IUserTagRepository extends JpaRepository<UserTag, Long>, JpaSpecificationExecutor<UserTag> {
UserTag findByUserIdAndTenantId(Long userId,Integer tenantId);
void deleteByUserIdAndTenantId(Long userId,Integer tenantId);
}
...@@ -3,6 +3,8 @@ package cn.quantgroup.xyqb.service.session.impl; ...@@ -3,6 +3,8 @@ package cn.quantgroup.xyqb.service.session.impl;
import cn.quantgroup.xyqb.constant.enums.RecordType; import cn.quantgroup.xyqb.constant.enums.RecordType;
import cn.quantgroup.xyqb.Constants; import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.entity.User; import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.entity.UserTag;
import cn.quantgroup.xyqb.event.UserLoginEvent;
import cn.quantgroup.xyqb.model.AuthBean; import cn.quantgroup.xyqb.model.AuthBean;
import cn.quantgroup.xyqb.model.LoginProperties; import cn.quantgroup.xyqb.model.LoginProperties;
import cn.quantgroup.xyqb.model.UserStatistics; import cn.quantgroup.xyqb.model.UserStatistics;
...@@ -17,9 +19,11 @@ import com.alibaba.fastjson.JSON; ...@@ -17,9 +19,11 @@ import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching; import org.springframework.cache.annotation.Caching;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
...@@ -47,6 +51,9 @@ public class SessionServiceImpl implements ISessionService { ...@@ -47,6 +51,9 @@ public class SessionServiceImpl implements ISessionService {
@Value("${token.prefix}") @Value("${token.prefix}")
private String prefix; private String prefix;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
/** /**
* 更新session * 更新session
...@@ -85,6 +92,10 @@ public class SessionServiceImpl implements ISessionService { ...@@ -85,6 +92,10 @@ public class SessionServiceImpl implements ISessionService {
// 添加登陆日志 // 添加登陆日志
loginRecordService.saveLoginRecord(user.getId(), RecordType.LOGINRECORD.getName(), loginType); loginRecordService.saveLoginRecord(user.getId(), RecordType.LOGINRECORD.getName(), loginType);
//更新user_tag记录
applicationEventPublisher.publishEvent(new UserLoginEvent(this, UserTag.builder()
.userId(user.getId()).registeredFrom(user.getRegisteredFrom()).tenantId(user.getTenantId()).build()));
return authBean; return authBean;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment