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