|
@@ -2,15 +2,23 @@ package com.neko.service.impl;
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.repository.AbstractRepository;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.neko.config.GiteeProperties;
|
|
|
import com.neko.config.ThirdlyLoginTypeConstant;
|
|
|
+import com.neko.domain.dto.friendDto.UserPwdDto;
|
|
|
import com.neko.domain.dto.userDto.PhoneLoginDto;
|
|
|
import com.neko.domain.dto.userDto.YanDto;
|
|
|
import com.neko.domain.pojo.User;
|
|
|
+import com.neko.domain.pojo.UserDevice;
|
|
|
+import com.neko.domain.pojo.UserLog;
|
|
|
+import com.neko.domain.pojo.UserProfile;
|
|
|
+import com.neko.mapper.UserLogMapper;
|
|
|
+import com.neko.mapper.UserProfileMapper;
|
|
|
import com.neko.service.UserService;
|
|
|
import com.neko.mapper.UserMapper;
|
|
|
import com.neko.utils.*;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -19,8 +27,11 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.DigestUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.UnknownHostException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author 金聪
|
|
@@ -37,23 +48,32 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
private RedisTemplate redisTemplate;
|
|
|
@Autowired
|
|
|
GiteeProperties giteeProperties;
|
|
|
+ @Autowired
|
|
|
+ UserProfileMapper userProfileMapper;
|
|
|
+ @Autowired
|
|
|
+ UserLogMapper userLogMapper;
|
|
|
|
|
|
@Override
|
|
|
- public ResultVo phoenLogin(PhoneLoginDto dto) {
|
|
|
+ public ResultVo phoenLogin(PhoneLoginDto dto) throws Exception {
|
|
|
//判断手机号是否正确
|
|
|
if(StringUtils.isEmpty(dto.getPhone())){
|
|
|
+ log.info("手机号不能为空");
|
|
|
return ResultVo.error(ApiServiceExceptionEnum.USER_NOT_NULL);
|
|
|
}
|
|
|
+ InetAddress localHost = null;
|
|
|
+ localHost = InetAddress.getLocalHost();
|
|
|
if(StringUtils.isEmpty(dto.getPassword())){
|
|
|
log.info("验证码登录");
|
|
|
if(StringUtils.isEmpty(dto.getSmsCode())){
|
|
|
//判断验证码是否为空
|
|
|
+ log.info("验证码不能为空");
|
|
|
return ResultVo.error(ApiServiceExceptionEnum.YAN_NOT_NULL);
|
|
|
}
|
|
|
//从reids中获取验证码
|
|
|
String yan = (String) redisTemplate.opsForValue().get("yan");
|
|
|
if(!yan.equals(dto.getSmsCode())){
|
|
|
//判断验证码是否正确
|
|
|
+ log.info("验证码错误");
|
|
|
return ResultVo.error(ApiServiceExceptionEnum.YAN_ERROR);
|
|
|
}
|
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
@@ -80,14 +100,63 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
u.setUsername("PTYH"+dto.getPhone());
|
|
|
u.setCreateTime(new Date());
|
|
|
userMapper.insert(u);
|
|
|
+ //添加用户信息
|
|
|
+ log.info("添加用户信息");
|
|
|
+ UserProfile userProfile = new UserProfile();
|
|
|
+ userProfile.setId(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
+ userProfile.setUserId(u.getId());
|
|
|
+ userProfile.setNickname(u.getUsername());
|
|
|
+ userProfile.setAvatar("http://47.95.170.81:19000/loli/bf4e13220f87c5e68e058da3f7dc48b.png");
|
|
|
+ userProfile.setCreateTime(new Date());
|
|
|
+ userProfileMapper.insert(userProfile);
|
|
|
+ //添加用户设备信息
|
|
|
+// log.info("添加用户设备信息");
|
|
|
+// UserDevice device = new UserDevice();
|
|
|
+// device.setId(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
+// device.setUserId(u.getId());
|
|
|
+// device.setDeviceId(UUID.randomUUID().toString().replaceAll("-",""));
|
|
|
+// device.setDeviceType();
|
|
|
+// device.setDeviceModel();
|
|
|
|
|
|
+ //日志添加
|
|
|
+ log.info("添加用户日志信息");
|
|
|
+ UserLog userLog = new UserLog();
|
|
|
+ userLog.setUserId(u.getId());
|
|
|
+ userLog.setUserName(u.getUsername());
|
|
|
+ userLog.setLoginZhu(localHost.getHostName());
|
|
|
+ userLog.setZhuIp(localHost.getHostAddress());
|
|
|
+ userLog.setLoginTime(new Date());
|
|
|
+ userLog.setDescribes("手机号为"+dto.getPhone()+"注册了新用户");
|
|
|
+ userLogMapper.insert(userLog);
|
|
|
+
|
|
|
+ //线程
|
|
|
+ UserThreadLocalhostUtil.setUser(u.getId());
|
|
|
String token = AppJwtUtil.getToken(Long.valueOf(u.getId()));
|
|
|
return ResultVo.success(ApiServiceExceptionEnum.LOGIN_OK,token);
|
|
|
}else{
|
|
|
log.info("用户存在,登录");
|
|
|
+ if(user.getStatus()==2){
|
|
|
+ log.info("用户不存在");
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
+ }
|
|
|
+ if(user.getStatus()==0){
|
|
|
+ log.info("此用户已被封禁");
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.BAN_USER);
|
|
|
+ }
|
|
|
+ //日志添加
|
|
|
+ log.info("添加用户日志信息");
|
|
|
+ UserLog userLog = new UserLog();
|
|
|
+ userLog.setUserId(user.getId());
|
|
|
+ userLog.setUserName(user.getUsername());
|
|
|
+ userLog.setLoginZhu(localHost.getHostName());
|
|
|
+ userLog.setZhuIp(localHost.getHostAddress());
|
|
|
+ userLog.setLoginTime(new Date());
|
|
|
+ userLog.setDescribes("验证码登录");
|
|
|
+ userLogMapper.insert(userLog);
|
|
|
|
|
|
+ //线程
|
|
|
+ UserThreadLocalhostUtil.setUser(user.getId());
|
|
|
String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
|
|
|
-
|
|
|
return ResultVo.success(ApiServiceExceptionEnum.LOGIN_OK,token);
|
|
|
}
|
|
|
|
|
@@ -105,6 +174,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
log.info("用户不存在");
|
|
|
return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
}
|
|
|
+
|
|
|
+ if(user.getStatus()==2){
|
|
|
+ log.info("用户不存在");
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
+ }
|
|
|
+ if(user.getStatus()==0){
|
|
|
+ log.info("此用户已被封禁");
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.BAN_USER);
|
|
|
+ }
|
|
|
+
|
|
|
String pwd = DigestUtils.md5DigestAsHex((user.getSalt()+dto.getPassword()).getBytes(StandardCharsets.UTF_8));
|
|
|
if(!user.getPassword().equals(pwd)){
|
|
|
//判断密码是否正确
|
|
@@ -112,8 +191,20 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
return ResultVo.error(ApiServiceExceptionEnum.PASSWORD_ERROR);
|
|
|
}
|
|
|
|
|
|
- String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
|
|
|
+ //日志添加
|
|
|
+ log.info("添加用户日志信息");
|
|
|
+ UserLog userLog = new UserLog();
|
|
|
+ userLog.setUserId(user.getId());
|
|
|
+ userLog.setUserName(user.getUsername());
|
|
|
+ userLog.setLoginZhu(localHost.getHostName());
|
|
|
+ userLog.setZhuIp(localHost.getHostAddress());
|
|
|
+ userLog.setLoginTime(new Date());
|
|
|
+ userLog.setDescribes("密码登录");
|
|
|
+ userLogMapper.insert(userLog);
|
|
|
|
|
|
+ //线程
|
|
|
+ UserThreadLocalhostUtil.setUser(user.getId());
|
|
|
+ String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
|
|
|
return ResultVo.success(ApiServiceExceptionEnum.LOGIN_OK,token);
|
|
|
}
|
|
|
}
|
|
@@ -137,7 +228,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
String sixDigitNumber = sb.toString();
|
|
|
|
|
|
//将验证码存入redis中
|
|
|
- redisTemplate.opsForValue().set("yan",sixDigitNumber);
|
|
|
+ redisTemplate.opsForValue().set("yan",sixDigitNumber,180, TimeUnit.SECONDS);
|
|
|
|
|
|
Map<String, String> querys = new HashMap<String, String>();
|
|
|
querys.put("mobile", dto.getPhone());
|
|
@@ -190,8 +281,76 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
String userInfoUrl = "https://gitee.com/api/v5/user?access_token=" + accessToken;
|
|
|
userInfo = HttpUtil.createGet(userInfoUrl).execute().body();
|
|
|
}
|
|
|
+ User user = new User();
|
|
|
+ user.setId(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
+
|
|
|
return userInfo;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultVo logout(String token)throws Exception {
|
|
|
+ //解密token
|
|
|
+ Claims claims = AppJwtUtil.getClaimsBody(token);
|
|
|
+ String id = claims.get("id").toString();
|
|
|
+ User u = userMapper.selectById(id);
|
|
|
+
|
|
|
+ InetAddress localHost = null;
|
|
|
+ localHost = InetAddress.getLocalHost();
|
|
|
+ //日志添加
|
|
|
+ log.info("添加用户日志信息");
|
|
|
+ UserLog userLog = new UserLog();
|
|
|
+ userLog.setUserId(u.getId());
|
|
|
+ userLog.setUserName(u.getUsername());
|
|
|
+ userLog.setLoginZhu(localHost.getHostName());
|
|
|
+ userLog.setZhuIp(localHost.getHostAddress());
|
|
|
+ userLog.setLoginTime(new Date());
|
|
|
+ userLog.setDescribes("用户登出");
|
|
|
+ userLogMapper.insert(userLog);
|
|
|
+
|
|
|
+ //清理线程
|
|
|
+ UserThreadLocalhostUtil.remove();
|
|
|
+ return ResultVo.success(ApiServiceExceptionEnum.LOGOUT_OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultVo myUserData(String token) {
|
|
|
+ log.info("用户个人信息");
|
|
|
+ Claims claims = AppJwtUtil.getClaimsBody(token);
|
|
|
+ String id = claims.get("id").toString();
|
|
|
+ User user = userMapper.selectById(id);
|
|
|
+ if(user==null){
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
+ }
|
|
|
+ QueryWrapper<UserProfile> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(UserProfile::getUserId,user.getId());
|
|
|
+ UserProfile userProfile = userProfileMapper.selectOne(queryWrapper);
|
|
|
+ user.setProfiles(userProfile);
|
|
|
+
|
|
|
+ return ResultVo.success(ApiServiceExceptionEnum.RESULT_SUCCES,user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultVo updapeUserPwd(UserPwdDto dto) {
|
|
|
+ Claims claims = AppJwtUtil.getClaimsBody(dto.getToken());
|
|
|
+ String id = claims.get("id").toString();
|
|
|
+ User user = userMapper.selectById(id);
|
|
|
+ if(user==null){
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
+ }
|
|
|
+ //从reids中获取验证码
|
|
|
+ String yan = (String) redisTemplate.opsForValue().get("yan");
|
|
|
+ if(!yan.equals(dto.getSmsCode())){
|
|
|
+ //判断验证码是否正确
|
|
|
+ log.info("验证码错误");
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YAN_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ String pwd = DigestUtils.md5DigestAsHex((user.getSalt()+dto.getPassword()).getBytes(StandardCharsets.UTF_8));
|
|
|
+ user.setPassword(pwd);
|
|
|
+ userMapper.updateById(user);
|
|
|
+
|
|
|
+ return ResultVo.success(ApiServiceExceptionEnum.RESULT_SUCCES,"修改密码成功");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|