|
|
@@ -1,43 +1,20 @@
|
|
|
package com.ytpm.adage.controller;
|
|
|
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
-import cn.hutool.core.util.RandomUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
import com.ytpm.adage.dao.AppUserMapper;
|
|
|
-import com.ytpm.adage.dao.LoginRecordMapper;
|
|
|
-import com.ytpm.adage.dao.QuestionMapper;
|
|
|
-import com.ytpm.adage.redis.RedisService;
|
|
|
-import com.ytpm.agent.enums.UserStatusEnum;
|
|
|
-import com.ytpm.app.model.YtDyzLoginRecord;
|
|
|
+import com.ytpm.app.enums.LoginType;
|
|
|
import com.ytpm.app.model.YtDyzUser;
|
|
|
import com.ytpm.app.param.WxLoginParam;
|
|
|
-import com.ytpm.app.view.WxDefaultConfig;
|
|
|
-import com.ytpm.constant.StrConstant;
|
|
|
-import com.ytpm.feign.RiskFeign;
|
|
|
import com.ytpm.general.Result;
|
|
|
-import com.ytpm.general.StatusCode;
|
|
|
-import com.ytpm.handle.CustomerException;
|
|
|
+import com.ytpm.handle.LoginServiceFactory;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author Marx
|
|
|
@@ -51,177 +28,28 @@ public class VisitorController {
|
|
|
|
|
|
@Autowired
|
|
|
private AppUserMapper appUserMapper;
|
|
|
- @Autowired
|
|
|
- private LoginRecordMapper loginRecordMapper;
|
|
|
- @Autowired
|
|
|
- private QuestionMapper questionMapper;
|
|
|
- @Autowired
|
|
|
- private RiskFeign riskFeign;
|
|
|
- @Autowired
|
|
|
- private RedisService redisService;
|
|
|
- @Value("${risk.config.banned.tips}")
|
|
|
- private String tips;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LoginServiceFactory loginServiceFactory;
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
@ApiOperation("游客登录")
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Result<YtDyzUser> visitorLogin(@RequestBody WxLoginParam param, HttpServletRequest request) {
|
|
|
- // 唯一性判断
|
|
|
- // 1.查询设备ID不存在记录 则根据设备id+渠道id注册用户
|
|
|
- // 2.查询设备ID存在记录 则根据渠道ID控制唯一性,同步出路风控记录
|
|
|
- //根据应用获取登陆配置
|
|
|
- WxDefaultConfig defaultConfig = appUserMapper.getDefaultConfig(param.getAppType());
|
|
|
- if(Objects.isNull(defaultConfig)){
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR,"游客登录失败,未找到相应配置!");
|
|
|
- }
|
|
|
- param.setAppId(defaultConfig.getPlatformAppId());
|
|
|
- param.setLoginIp(getClientIp(request));
|
|
|
- //获取该设备最后一次注册的用户信息(包含查询风控记录)
|
|
|
- List<YtDyzUser> registryUser = appUserMapper.getLastRegistryUser(param.getDeviceId());
|
|
|
- //该设备未注册过用户
|
|
|
- YtDyzUser old;
|
|
|
- if(CollUtil.isEmpty(registryUser)) {
|
|
|
- old = new YtDyzUser();
|
|
|
- registerUser(param, old);
|
|
|
- }else{
|
|
|
- //查询当前设备当前渠道是否存在该用户
|
|
|
- old = appUserMapper.getByDeviceAndDitch(param.getDeviceId(), param.getDitchId());
|
|
|
- //不存在则说明该渠道为新注册用户,需要风控校验注册是否在规则允许范围内
|
|
|
- if(Objects.isNull(old)) {
|
|
|
- YtDyzUser ytDyzUser = registryUser.stream()
|
|
|
- .sorted(Comparator.comparing(YtDyzUser::getRegistryTime).reversed())
|
|
|
- .collect(Collectors.toList()).get(0);
|
|
|
- Result<?> result = riskFeign.checkRegRisk(ytDyzUser);
|
|
|
- if(result.getCode()!=200){
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR,result.getMessage());
|
|
|
- }
|
|
|
- old = new YtDyzUser();
|
|
|
- registerUser(param, old);
|
|
|
- }else{
|
|
|
- //当前渠道已有用户,校验用户是否处于风控中 & 更新用户信息
|
|
|
- if(!old.getUserStatus().equals(UserStatusEnum.NORMAL.getCode())){
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR,getTipsMsg());
|
|
|
- }
|
|
|
- deadWithUserCrud(old,param);
|
|
|
- }
|
|
|
- }
|
|
|
- //设置最后一次答题问题ID、今日答题数、历史答题数
|
|
|
- setExtInfo(old);
|
|
|
- //校验指定时间内登录的渠道数
|
|
|
- Result<?> result = riskFeign.checkLoginRisk(old);
|
|
|
- if(result.getCode()!=200){
|
|
|
- // 添加用户登录记录
|
|
|
- addLoginRecord(param,old.getUserId());
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR,result.getMessage());
|
|
|
- }
|
|
|
- return Result.resultObjOk(old);
|
|
|
+ return loginServiceFactory.login(LoginType.VISITOR, param, request);
|
|
|
}
|
|
|
|
|
|
@ApiOperation("设备在指定时间内注册的渠道")
|
|
|
@GetMapping("/getDitchCount")
|
|
|
- public int getDitchCount(@RequestParam("deviceId")String deviceId,@RequestParam("hours")Integer hours) {
|
|
|
- return appUserMapper.countDitch(deviceId,hours);
|
|
|
+ public int getDitchCount(@RequestParam("deviceId") String deviceId, @RequestParam("hours") Integer hours) {
|
|
|
+ return appUserMapper.countDitch(deviceId, hours);
|
|
|
}
|
|
|
|
|
|
@ApiOperation("设备在指定时间内注册的渠道")
|
|
|
@GetMapping("/getLoginDitchCount")
|
|
|
- public int getLoginDitchCount(@RequestParam("deviceId")String deviceId,@RequestParam("hours")Integer hours) {
|
|
|
- return appUserMapper.countLoginDitch(deviceId,hours);
|
|
|
+ public int getLoginDitchCount(@RequestParam("deviceId") String deviceId, @RequestParam("hours") Integer hours) {
|
|
|
+ return appUserMapper.countLoginDitch(deviceId, hours);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 注册用户
|
|
|
- */
|
|
|
- private YtDyzUser registerUser(WxLoginParam param, YtDyzUser old) {
|
|
|
- old.setUserId(redisService.getAppUserId());
|
|
|
- old.setPhone(param.getPhone());
|
|
|
- old.setDeviceId(param.getDeviceId());
|
|
|
- old.setNickName("visitor_"+RandomUtil.randomString(10));
|
|
|
- old.setLastLoginTime(new Date());
|
|
|
- old.setRegistryTime(new Date());
|
|
|
- old.setLastLoginIp(param.getLoginIp());
|
|
|
- old.setLoginDays(1);
|
|
|
- old.setPower(0);
|
|
|
- old.setTotalVideo(0);
|
|
|
- old.setTotalIncome(BigDecimal.ZERO);
|
|
|
- old.setRedPacketAmount(BigDecimal.ZERO);
|
|
|
- old.setRedPacketBalance(BigDecimal.ZERO);
|
|
|
- old.setPointsBalance(BigDecimal.ZERO);
|
|
|
- old.setPointsTotal(BigDecimal.ZERO);
|
|
|
- old.setWithdrawTotal(BigDecimal.ZERO);
|
|
|
- old.setDitchId(param.getDitchId());
|
|
|
- old.setSignDays(0);
|
|
|
- old.setAppId(param.getAppId());
|
|
|
- old.setUserStatus(UserStatusEnum.NORMAL.getCode());
|
|
|
- String platformId = appUserMapper.getPlatformByDeviceId(param.getDeviceId());
|
|
|
- old.setPlatformId(StrUtil.isBlank(platformId)?
|
|
|
- (StrConstant.PLATFORM_ID_PREFIX + IdUtil.getSnowflakeNextIdStr()):platformId);
|
|
|
- appUserMapper.addOne(old);
|
|
|
- return old;
|
|
|
- }
|
|
|
|
|
|
- private String getClientIp(HttpServletRequest request) {
|
|
|
- String xfHeader = request.getHeader("X-Forwarded-For");
|
|
|
- if (xfHeader == null) {
|
|
|
- return request.getRemoteAddr();
|
|
|
- }
|
|
|
- return xfHeader.split(",")[0]; // 可能会有多个IP,这里取第一个逗号前的IP
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取随机提示
|
|
|
- */
|
|
|
- private String getTipsMsg(){
|
|
|
- String[] split = tips.split(",");
|
|
|
- return split[RandomUtil.randomInt(split.length)];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置扩展信息
|
|
|
- */
|
|
|
- private void setExtInfo(YtDyzUser old) {
|
|
|
- old.setLastQuestionId(questionMapper.getLastQuestionId(old.getUserId()));
|
|
|
- old.setTodayAnswerCount(questionMapper.getAnswerCount(old.getUserId(),1));
|
|
|
- old.setHistoryAnswerCount(questionMapper.getAnswerCount(old.getUserId(),2));
|
|
|
- old.setAnswerRecordList(questionMapper.getAnswerRecords(old.getUserId()));
|
|
|
- old.setLoginRecordList(loginRecordMapper.getLoginRecords(old.getUserId()));
|
|
|
- }
|
|
|
- /**
|
|
|
- * 处理用户数据
|
|
|
- */
|
|
|
- private void deadWithUserCrud(YtDyzUser old, WxLoginParam param) {
|
|
|
- //处于风控状态的用户不允许登录
|
|
|
- if(!old.getUserStatus().equals(UserStatusEnum.NORMAL.getCode())){
|
|
|
- throw new CustomerException(getTipsMsg());
|
|
|
- }
|
|
|
- YtDyzUser newUser = new YtDyzUser();
|
|
|
- newUser.setUserId(old.getUserId());
|
|
|
- newUser.setLastLoginTime(new Date());
|
|
|
- newUser.setLastLoginIp(param.getLoginIp());
|
|
|
- newUser.setPhone(param.getPhone());
|
|
|
- newUser.setDeviceId(param.getDeviceId());
|
|
|
- //如果当前登录是本日第一次登录则登录天数+1
|
|
|
- int loginCount = loginRecordMapper.getTodayLoginCount(old.getUserId());
|
|
|
- if(loginCount < 1){
|
|
|
- newUser.setLoginDays(old.getLoginDays()+1);
|
|
|
- }
|
|
|
- appUserMapper.updateUser(newUser);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 增加用户登录记录
|
|
|
- */
|
|
|
- private void addLoginRecord(WxLoginParam param,String userId) {
|
|
|
- YtDyzLoginRecord loginRecord = new YtDyzLoginRecord();
|
|
|
- loginRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
- loginRecord.setUserId(userId);
|
|
|
- loginRecord.setLoginTime(new Date());
|
|
|
- loginRecord.setDeviceBrand(param.getBrand());
|
|
|
- loginRecord.setDeviceModel(param.getModel());
|
|
|
- loginRecord.setLoginIp(param.getLoginIp());
|
|
|
- loginRecord.setOperator(param.getIpOperator());
|
|
|
- loginRecord.setIpAddr(param.getIpLocation());
|
|
|
- loginRecord.setPhoneJson(param.getPhoneJson());
|
|
|
- loginRecordMapper.insertOne(loginRecord);
|
|
|
- }
|
|
|
}
|