|
|
@@ -0,0 +1,180 @@
|
|
|
+package com.ytpm.adage.controller;
|
|
|
+
|
|
|
+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.model.YtDyzUser;
|
|
|
+import com.ytpm.app.param.WxLoginParam;
|
|
|
+import com.ytpm.app.view.WxUserInfo;
|
|
|
+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 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.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Marx
|
|
|
+ * @date 2025/9/2 15:02
|
|
|
+ */
|
|
|
+@Api(tags = "游客管理")
|
|
|
+@RefreshScope
|
|
|
+@RestController
|
|
|
+@RequestMapping("/visitor")
|
|
|
+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;
|
|
|
+
|
|
|
+ @PostMapping("/login")
|
|
|
+ @ApiOperation("游客登录")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<YtDyzUser> visitorLogin(@RequestBody WxLoginParam param, HttpServletRequest request) {
|
|
|
+ param.setLoginIp(getClientIp(request));
|
|
|
+ YtDyzUser old = appUserMapper.getByDeviceAndDitch(param.getDeviceId(),param.getDitchId());
|
|
|
+ //当前设备在该渠道未曾注册
|
|
|
+ if(Objects.isNull(old)){
|
|
|
+ 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);
|
|
|
+ // 添加用户登录记录
|
|
|
+ addLoginRecord(param,old.getUserId());
|
|
|
+ //校验游客风控
|
|
|
+ Result<?> result = riskFeign.checkLoginRisk(old);
|
|
|
+ if(result.getCode()!=200){
|
|
|
+ return new Result<>(StatusCode.ACCESS_ERR,result.getMessage());
|
|
|
+ }
|
|
|
+ return Result.resultObjOk(old);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册用户
|
|
|
+ */
|
|
|
+ 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());
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|