|
|
@@ -1,6 +1,8 @@
|
|
|
package com.cyksj.service.user.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
@@ -8,15 +10,19 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.cyksj.common.constant.Constant;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
+import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
+import com.cyksj.mapper.PhoneCodeMapper;
|
|
|
import com.cyksj.mapper.UserMapper;
|
|
|
import com.cyksj.mapper.channel.ChannelPopularizeMapper;
|
|
|
import com.cyksj.mapper.manage.cps.UserDistributePopularizeMapper;
|
|
|
import com.cyksj.mapper.market.task.UserBindDetailMapper;
|
|
|
import com.cyksj.model.dto.GzhOAuth2UserInfo;
|
|
|
+import com.cyksj.model.dto.NewUserWelfareDto;
|
|
|
import com.cyksj.model.dto.UserWhoami;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.request.LoginReq;
|
|
|
import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.distribute.DistributeService;
|
|
|
import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
|
|
|
@@ -30,6 +36,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@@ -59,6 +66,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
+ private final PhoneCodeMapper phoneCodeMapper;
|
|
|
+
|
|
|
private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
|
@Override
|
|
|
@@ -96,6 +105,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|
|
.eq(User::getUnionid, unionid).last("limit 1"));
|
|
|
}
|
|
|
this.save(user);
|
|
|
+ //发放新用户福利
|
|
|
+ final Long fUid = user.getId();
|
|
|
+ TASK_POOL.execute(() -> sendNewUserWelfare(fUid));
|
|
|
} else {
|
|
|
if (StrUtil.isEmpty(user.getRegisterEnv())) {
|
|
|
user.setRegisterEnv(registerEnv.name());
|
|
|
@@ -290,4 +302,113 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|
|
return userMapper.getUserIdByShowId(showId);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public User webLogin(LoginReq loginReq) throws Exception {
|
|
|
+ LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).last("limit 1");
|
|
|
+ if (loginReq.getType() == null || loginReq.getType() < 1 && loginReq.getType() > 2) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统异常,请刷新页面重新操作");
|
|
|
+ }
|
|
|
+ if (loginReq.getType() == 1) {
|
|
|
+ if (StrUtil.hasEmpty(loginReq.getPhone(), loginReq.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入对应信息");
|
|
|
+ }
|
|
|
+ DateTime now = DateTime.now();
|
|
|
+ PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, loginReq.getPhone()).last("limit 1"));
|
|
|
+ if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
|
|
|
+ }
|
|
|
+ //3分钟失效
|
|
|
+ Date updateTime = phoneCode.getUpdateTime();
|
|
|
+ DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
+ if (now.isAfter(afterFiveMinutes)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(loginReq.getCode(), phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ wrapper.eq(User::getLoginPhone, loginReq.getPhone());
|
|
|
+ }
|
|
|
+ if (loginReq.getType() == 2) {
|
|
|
+ if (StrUtil.isEmpty(loginReq.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱不能为空");
|
|
|
+ }
|
|
|
+ String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(loginReq.getCode(), code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ wrapper.eq(User::getEmail, loginReq.getEmail());
|
|
|
+ redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
|
|
|
+ }
|
|
|
+ //保存用户信息
|
|
|
+ User user = this.getOne(wrapper);
|
|
|
+ String dsCode = loginReq.getDsCode();
|
|
|
+ Long sharedId = loginReq.getSharedId();
|
|
|
+ Long dsPopularizeId = null;
|
|
|
+ if (StrUtil.isNotBlank(dsCode)) {
|
|
|
+ dsCode = dsCode.trim();
|
|
|
+ User sharedUser = this.getOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getShowId, dsCode).last("limit 1"));
|
|
|
+ if (sharedUser != null) {
|
|
|
+ sharedId = sharedUser.getId();
|
|
|
+ } else {
|
|
|
+ UserDistributePopularize distributePopularize = distributePopularizeMapper.selectOne(Wrappers.lambdaQuery(UserDistributePopularize.class)
|
|
|
+ .eq(UserDistributePopularize::getCode, dsCode)
|
|
|
+ .select(UserDistributePopularize::getUserId, UserDistributePopularize::getId).last("limit 1"));
|
|
|
+ if (distributePopularize != null) {
|
|
|
+ log.info("渠道sharedId:{}设置固定推广链接PC用户分销", distributePopularize.getUserId());
|
|
|
+ sharedId = distributePopularize.getUserId();
|
|
|
+ dsPopularizeId = distributePopularize.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (user == null) {
|
|
|
+ user = new User();
|
|
|
+ StringBuilder name = new StringBuilder();
|
|
|
+ name.append(Constant.DEFAULT_NAME);
|
|
|
+ if (loginReq.getPopularizeId() != null) {
|
|
|
+ user.setPopularizeId(loginReq.getPopularizeId());
|
|
|
+ }
|
|
|
+ if (loginReq.getType() == 1) {
|
|
|
+ user.setLoginPhone(loginReq.getPhone());
|
|
|
+ }
|
|
|
+ if (loginReq.getType() == 2) {
|
|
|
+ user.setEmail(loginReq.getEmail());
|
|
|
+ name.append("@");
|
|
|
+ }
|
|
|
+ user.setNickname(String.format("%s%s", name.toString(), Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.MD5).setStringData(String.format("%s%s", user.getId(), loginReq.getType() == 1 ? user.getLoginPhone() : user.getEmail())).toHexString().substring(0, 6)));
|
|
|
+ //默认头像
|
|
|
+ user.setHeadimgurl(Constant.DEFAULT_HEAD_IMG);
|
|
|
+ String registerEnv = loginReq.getRegisterEnv();
|
|
|
+ user.setRegisterEnv(StrUtil.isEmpty(registerEnv) ? User.RegisterEnv.pc.name() : registerEnv);
|
|
|
+ this.save(user);
|
|
|
+ //新用户分销
|
|
|
+ if (sharedId != null) {
|
|
|
+ distributeService.saveDistributionInvitation(sharedId, user.getId(), dsPopularizeId);
|
|
|
+ }
|
|
|
+ //发放新用户福利
|
|
|
+ final Long fUid = user.getId();
|
|
|
+ TASK_POOL.execute(() -> sendNewUserWelfare(fUid));
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(user.getShowId())) {
|
|
|
+ user.setShowId(getUserShowId(user.getId()));
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发放新用户福利
|
|
|
+ */
|
|
|
+ public void sendNewUserWelfare(Long userId) {
|
|
|
+ SysConfig newUserWelfareConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
|
|
|
+ .eq(SysConfig::getSysKey, Constant.NEW_USER_WELFARE_KEY).last("limit 1"));
|
|
|
+ NewUserWelfareDto newUserWelfareDto = Jsons.parseObject(newUserWelfareConfig, NewUserWelfareDto.class);
|
|
|
+ if (newUserWelfareDto != null) {
|
|
|
+ List<Long> couponIds = newUserWelfareDto.getCouponIds();
|
|
|
+ couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, null, null, null, CouponUser.Channel.new_welfare));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|