Commit 5ae97a31 by chamberone

feat: 添加用户登录和获取用户接口

1 parent baf13f7e
package com.dituhui.pea.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@Data
public class UserLoginDTO {
private String account;
@JsonIgnore
private String password;
private String nickname;
private String email;
private String phone;
private String birthday;
private String region;
private Integer sex;
private String avatar;
private String wechat;
private String token;
}
package com.dituhui.pea.user; package com.dituhui.pea.user;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.ThirdPartyEnum; import com.dituhui.pea.enums.ThirdPartyEnum;
import com.dituhui.pea.pojo.ThirdUserInfo; import com.dituhui.pea.pojo.ThirdUserInfo;
import com.dituhui.pea.pojo.UserInfo; import com.dituhui.pea.pojo.UserInfo;
import com.dituhui.pea.pojo.UserLoginDTO;
import com.dituhui.pea.pojo.WebResult; import com.dituhui.pea.pojo.WebResult;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -17,6 +20,12 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -17,6 +20,12 @@ import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "user") @FeignClient(value = "user")
public interface IUser { public interface IUser {
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public Result<?> userLogin(@RequestBody UserLoginDTO user);
@RequestMapping(value = "/user/userInfo", method = RequestMethod.GET)
public Result<?> getUserInfo(@RequestHeader(name="Authorization", required = true) String authorization);
/** /**
* 获取当前登陆用户信息 * 获取当前登陆用户信息
* *
......
...@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController;
/** /**
* 角色控制层 * 角色控制层
* @author zl
*/ */
@RestController @RestController
@RefreshScope @RefreshScope
......
...@@ -2,6 +2,8 @@ package com.dituhui.pea.user.controller; ...@@ -2,6 +2,8 @@ package com.dituhui.pea.user.controller;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.RedisKeyGroup; import com.dituhui.pea.enums.RedisKeyGroup;
import com.dituhui.pea.enums.StatusCodeEnum; import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.enums.ThirdPartyEnum; import com.dituhui.pea.enums.ThirdPartyEnum;
...@@ -13,13 +15,15 @@ import org.apache.commons.lang.StringUtils; ...@@ -13,13 +15,15 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
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.RequestHeader;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* 用户控制层 * 用户控制层
* *
* @author zl
* @since 2020-12-09 15:42:00
*/ */
@RestController @RestController
public class UserController implements IUser { public class UserController implements IUser {
...@@ -30,6 +34,14 @@ public class UserController implements IUser { ...@@ -30,6 +34,14 @@ public class UserController implements IUser {
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
public Result<?> userLogin(UserLoginDTO user) {
return userService.userLogin(user.getAccount(), user.getAccount());
}
public Result<?> getUserInfo(String token) {
return userService.getUserInfo(token);
}
@Override @Override
public WebResult<UserInfo> getCurrentUserInfo(String userToken, Boolean needTeamInfo) { public WebResult<UserInfo> getCurrentUserInfo(String userToken, Boolean needTeamInfo) {
UserAuthInfo userInfoCache = getUserAuthInfoFromToken(userToken); UserAuthInfo userInfoCache = getUserAuthInfoFromToken(userToken);
......
package com.dituhui.pea.user.dao; package com.dituhui.pea.user.dao;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
...@@ -15,11 +14,12 @@ import com.dituhui.pea.user.entity.UserEntity; ...@@ -15,11 +14,12 @@ import com.dituhui.pea.user.entity.UserEntity;
* @author zl * @author zl
* @since 2020-12-09 15:34:42 * @since 2020-12-09 15:34:42
*/ */
public interface UserDao extends JpaRepository<UserEntity, String>, public interface UserDao extends JpaRepository<UserEntity, String>, JpaSpecificationExecutor<UserEntity>,
JpaSpecificationExecutor<UserEntity>, CrudRepository<UserEntity, String> { CrudRepository<UserEntity, String> {
/** /**
* 根据手机号查询 * 根据手机号查询
*
* @param phoneNumber 手机号码 * @param phoneNumber 手机号码
* @return * @return
*/ */
...@@ -27,6 +27,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -27,6 +27,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据邮箱查询 * 根据邮箱查询
*
* @param email 邮箱 * @param email 邮箱
* @return * @return
*/ */
...@@ -34,6 +35,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -34,6 +35,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据微信查询 * 根据微信查询
*
* @param wechat 微信 * @param wechat 微信
* @return * @return
*/ */
...@@ -41,6 +43,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -41,6 +43,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据微信小程序查询 * 根据微信小程序查询
*
* @param wechatMiniProgram 微信小程序openId * @param wechatMiniProgram 微信小程序openId
* @return * @return
*/ */
...@@ -48,6 +51,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -48,6 +51,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据账号查询 * 根据账号查询
*
* @param account 账号 * @param account 账号
* @return * @return
*/ */
...@@ -55,6 +59,16 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -55,6 +59,16 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据账号查询 * 根据账号查询
*
* @param account 账号
* @param password 加密密码
* @return
*/
UserEntity findByAccountAndPassword(String account, String password);
/**
* 根据账号查询
*
* @param account 账号 * @param account 账号
* @return * @return
*/ */
...@@ -62,6 +76,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>, ...@@ -62,6 +76,7 @@ public interface UserDao extends JpaRepository<UserEntity, String>,
/** /**
* 根据用户ID批量查询 * 根据用户ID批量查询
*
* @param userIds 用户ID集合 * @param userIds 用户ID集合
* @return * @return
*/ */
......
...@@ -12,11 +12,15 @@ import org.springframework.data.jpa.domain.Specification; ...@@ -12,11 +12,15 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.RedisKeyGroup;
import com.dituhui.pea.enums.StatusCodeEnum; import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.enums.ThirdPartyEnum; import com.dituhui.pea.enums.ThirdPartyEnum;
import com.dituhui.pea.exception.BusinessException; import com.dituhui.pea.exception.BusinessException;
import com.dituhui.pea.pojo.ThirdUserInfo; import com.dituhui.pea.pojo.ThirdUserInfo;
import com.dituhui.pea.pojo.UserInfo; import com.dituhui.pea.pojo.UserInfo;
import com.dituhui.pea.pojo.UserLoginDTO;
import com.dituhui.pea.user.commom.RedisService;
import com.dituhui.pea.user.constant.TextConstant; import com.dituhui.pea.user.constant.TextConstant;
import com.dituhui.pea.user.dao.UserDao; import com.dituhui.pea.user.dao.UserDao;
import com.dituhui.pea.user.entity.UserEntity; import com.dituhui.pea.user.entity.UserEntity;
...@@ -27,28 +31,60 @@ import com.dituhui.pea.user.utils.TextHelper; ...@@ -27,28 +31,60 @@ import com.dituhui.pea.user.utils.TextHelper;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
/** /**
* 用户业务层 * 用户业务层
* @author zl *
*/ */
@Service @Service
public class UserService { public class UserService {
/**
* 失效时间ms
*/
private static final int aliveTime = 7200000;
@Autowired @Autowired
UserDao userDao; UserDao userDao;
@Autowired @Autowired
ThirdStrategyFactory thirdStrategyFactory; ThirdStrategyFactory thirdStrategyFactory;
@Autowired
private RedisService redisService;
public Result<?> userLogin(String account, String password) {
UserEntity user = userDao.findByAccountAndPassword(account, SecureUtil.md5(password));
if (null == user) {
// 生成token
UserLoginDTO userDTO = BeanUtil.copyProperties(user, UserLoginDTO.class);
// 生成token
String uuid = IdUtil.simpleUUID();
// 2小时
long timestamp = System.currentTimeMillis() + aliveTime;
redisService.set(RedisKeyGroup.authToken + ":" + uuid, user, timestamp);
userDTO.setToken(uuid);
return Result.success(userDTO);
} else {
return Result.failed("鉴权失败");
}
}
public Result<?> getUserInfo(String token) {
UserEntity user = (UserEntity) redisService.get(RedisKeyGroup.authToken + ":" + token);
return Result.success(user);
}
public String getUserName(String userId) { public String getUserName(String userId) {
return "Jown Snow " + userId; return "Jown Snow " + userId;
} }
/** /**
* 查询用户信息 * 查询用户信息
*
* @param id 第三方唯一id * @param id 第三方唯一id
* @param type 第三方类型 * @param type 第三方类型
* @return * @return
...@@ -87,13 +123,13 @@ public class UserService { ...@@ -87,13 +123,13 @@ public class UserService {
public UserInfo queryTeamByAccount(String account) { public UserInfo queryTeamByAccount(String account) {
UserEntity userEntity = null; UserEntity userEntity = null;
if (TextHelper.check(account, TextConstant.CHINA_PHONE)) { if (TextHelper.check(account, TextConstant.CHINA_PHONE)) {
//手机号码 // 手机号码
userEntity = userDao.findByPhone(account); userEntity = userDao.findByPhone(account);
} else if (TextHelper.check(account, TextConstant.USERNAME)) { } else if (TextHelper.check(account, TextConstant.USERNAME)) {
//用户名 // 用户名
userEntity = userDao.findByAccount(account); userEntity = userDao.findByAccount(account);
} else if (TextHelper.check(account, TextConstant.EMAIL)) { } else if (TextHelper.check(account, TextConstant.EMAIL)) {
//邮箱 // 邮箱
userEntity = userDao.findByEmail(account); userEntity = userDao.findByEmail(account);
} }
if (userEntity == null) { if (userEntity == null) {
...@@ -105,6 +141,7 @@ public class UserService { ...@@ -105,6 +141,7 @@ public class UserService {
/** /**
* 查询用户信息 * 查询用户信息
*
* @param id 用户ID * @param id 用户ID
* @return * @return
*/ */
...@@ -118,6 +155,7 @@ public class UserService { ...@@ -118,6 +155,7 @@ public class UserService {
/** /**
* 保存用户信息 * 保存用户信息
*
* @param userInfo * @param userInfo
* @return * @return
*/ */
...@@ -133,9 +171,9 @@ public class UserService { ...@@ -133,9 +171,9 @@ public class UserService {
return BeanUtil.copyProperties(userEntity, UserInfo.class); return BeanUtil.copyProperties(userEntity, UserInfo.class);
} }
/** /**
* 注册用户 * 注册用户
*
* @param userInfo 用户信息 * @param userInfo 用户信息
* @return * @return
*/ */
...@@ -199,6 +237,7 @@ public class UserService { ...@@ -199,6 +237,7 @@ public class UserService {
/** /**
* 绑定三方用户 * 绑定三方用户
*
* @param thirdId 第三方ID * @param thirdId 第三方ID
* @param thirdPartyEnum 第三方类型枚举 * @param thirdPartyEnum 第三方类型枚举
* @param phone 手机号 * @param phone 手机号
...@@ -222,7 +261,6 @@ public class UserService { ...@@ -222,7 +261,6 @@ public class UserService {
return userInfo; return userInfo;
} }
private String initUserAccount(String thirdType) { private String initUserAccount(String thirdType) {
ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdType); ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdType);
String prefix = ""; String prefix = "";
...@@ -253,9 +291,9 @@ public class UserService { ...@@ -253,9 +291,9 @@ public class UserService {
throw new BusinessException(StatusCodeEnum.FAILED); throw new BusinessException(StatusCodeEnum.FAILED);
} }
/** /**
* 更新用户信息 * 更新用户信息
*
* @param userInfo * @param userInfo
* @return * @return
*/ */
...@@ -274,14 +312,13 @@ public class UserService { ...@@ -274,14 +312,13 @@ public class UserService {
return userInfo; return userInfo;
} }
/** /**
* 删除用户信息 * 删除用户信息
*
* @param userId 用户ID * @param userId 用户ID
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteUser(String userId){ public void deleteUser(String userId) {
// 查询用户信息是否存在 // 查询用户信息是否存在
UserEntity userEntity = userDao.findById(userId).orElse(null); UserEntity userEntity = userDao.findById(userId).orElse(null);
if (ObjectUtil.isNull(userEntity)) { if (ObjectUtil.isNull(userEntity)) {
...@@ -293,6 +330,7 @@ public class UserService { ...@@ -293,6 +330,7 @@ public class UserService {
/** /**
* 校验 [账号 手机号] 是否存在 * 校验 [账号 手机号] 是否存在
*
* @param phone 手机号 * @param phone 手机号
* @return * @return
*/ */
...@@ -308,9 +346,9 @@ public class UserService { ...@@ -308,9 +346,9 @@ public class UserService {
return CollUtil.isNotEmpty(userEntities); return CollUtil.isNotEmpty(userEntities);
} }
/** /**
* 校验账号 * 校验账号
*
* @param userInfo 用户信息 * @param userInfo 用户信息
*/ */
private void checkAccount(UserInfo userInfo) { private void checkAccount(UserInfo userInfo) {
...@@ -342,16 +380,15 @@ public class UserService { ...@@ -342,16 +380,15 @@ public class UserService {
} }
} }
/** /**
* 校验邮箱是否存在 * 校验邮箱是否存在
*
* @param email 邮箱 * @param email 邮箱
* @return * @return
*/ */
private Boolean checkEmail(String email, String id) { private Boolean checkEmail(String email, String id) {
UserEntity userEntity = userDao.findByEmail(email); UserEntity userEntity = userDao.findByEmail(email);
if (ObjectUtil.isNotNull(userEntity)){ if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致 // 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id); return !StringUtils.equals(userEntity.getId(), id);
} else { } else {
...@@ -361,12 +398,13 @@ public class UserService { ...@@ -361,12 +398,13 @@ public class UserService {
/** /**
* 校验账号是否存在 * 校验账号是否存在
*
* @param account 账号 * @param account 账号
* @return * @return
*/ */
private Boolean checkAccount(String account, String id){ private Boolean checkAccount(String account, String id) {
UserEntity userEntity = userDao.findByAccount(account); UserEntity userEntity = userDao.findByAccount(account);
if (ObjectUtil.isNotNull(userEntity)){ if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致 // 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id); return !StringUtils.equals(userEntity.getId(), id);
} else { } else {
...@@ -377,13 +415,14 @@ public class UserService { ...@@ -377,13 +415,14 @@ public class UserService {
/** /**
* 校验手机号是否存在 * 校验手机号是否存在
*
* @param phone 账号 * @param phone 账号
* @param id 用户Id * @param id 用户Id
* @return * @return
*/ */
private Boolean checkPhone(String phone, String id){ private Boolean checkPhone(String phone, String id) {
UserEntity userEntity = userDao.findByPhone(phone); UserEntity userEntity = userDao.findByPhone(phone);
if (ObjectUtil.isNotNull(userEntity)){ if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致 // 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id); return !StringUtils.equals(userEntity.getId(), id);
} else { } else {
...@@ -393,6 +432,7 @@ public class UserService { ...@@ -393,6 +432,7 @@ public class UserService {
/** /**
* 组装更新用户信息 * 组装更新用户信息
*
* @param formUserInfo * @param formUserInfo
* @param toUserEntity * @param toUserEntity
*/ */
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!