From d26ffa1dd4b7725f0cd4b6cf8e516e4ed63dcf6f Mon Sep 17 00:00:00 2001 From: fanxb Date: Tue, 30 Jul 2019 17:13:12 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[=E5=90=8E=E5=8F=B0]:?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 13 + .../bookmark/business/user/dao/UserDao.java | 122 +++---- .../business/user/service/UserService.java | 306 +++++++++--------- .../main/resources/mapper/user-userMapper.xml | 84 ++--- 4 files changed, 285 insertions(+), 240 deletions(-) diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java index 42c42d3..6a5d98b 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java @@ -4,6 +4,7 @@ import com.fanxb.bookmark.business.user.entity.LoginBody; import com.fanxb.bookmark.business.user.entity.RegisterBody; import com.fanxb.bookmark.business.user.service.UserService; import com.fanxb.bookmark.common.entity.Result; +import com.fanxb.bookmark.common.util.UserContextHolder; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -50,6 +51,18 @@ public class UserController { return Result.success(null); } + /** + * Description: 获取当前登录用户的基本信息 + * + * @return com.fanxb.bookmark.common.entity.Result + * @author fanxb + * @date 2019/7/30 15:14 + */ + @GetMapping("/currentUserInfo") + public Result currentUserInfo() { + return Result.success(userService.getUserInfo(UserContextHolder.get().getUserId())); + } + /** * Description: 用户登录 * diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java index 7636c32..69a6422 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java @@ -1,56 +1,66 @@ -package com.fanxb.bookmark.business.user.dao; - -import com.fanxb.bookmark.common.entity.User; -import org.apache.ibatis.annotations.Param; -import org.springframework.stereotype.Component; - -/** - * 类功能简述: - * 类功能详述: - * - * @author fanxb - * @date 2019/7/6 11:36 - */ -@Component -public interface UserDao { - - /** - * Description: 新增一个用户 - * - * @param user user - * @author fanxb - * @date 2019/7/6 11:37 - */ - void addOne(User user); - - /** - * Description: 通过用户名或者email获取用户信息 - * - * @param name username - * @param email email - * @return com.fanxb.bookmark.common.entity.User - * @author fanxb - * @date 2019/7/6 16:45 - */ - User selectByUsernameOrEmail(@Param("name") String name, @Param("email") String email); - - /** - * Description: 更新用户上次登录时间 - * - * @param time 时间 - * @param userId 用户id - * @author fanxb - * @date 2019/7/6 16:46 - */ - void updateLastLoginTime(@Param("time") long time, @Param("userId") int userId); - - /** - * Description: 更新一个参数 - * - * @param password 新密码 - * @param email 邮箱 - * @author fanxb - * @date 2019/7/9 20:03 - */ - void resetPassword(@Param("password") String password,@Param("email") String email); -} +package com.fanxb.bookmark.business.user.dao; + +import com.fanxb.bookmark.common.entity.User; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Component; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 11:36 + */ +@Component +public interface UserDao { + + /** + * Description: 新增一个用户 + * + * @param user user + * @author fanxb + * @date 2019/7/6 11:37 + */ + void addOne(User user); + + /** + * Description: 通过用户名或者email获取用户信息 + * + * @param name username + * @param email email + * @return com.fanxb.bookmark.common.entity.User + * @author fanxb + * @date 2019/7/6 16:45 + */ + User selectByUsernameOrEmail(@Param("name") String name, @Param("email") String email); + + /** + * Description: 更新用户上次登录时间 + * + * @param time 时间 + * @param userId 用户id + * @author fanxb + * @date 2019/7/6 16:46 + */ + void updateLastLoginTime(@Param("time") long time, @Param("userId") int userId); + + /** + * Description: 更新一个参数 + * + * @param password 新密码 + * @param email 邮箱 + * @author fanxb + * @date 2019/7/9 20:03 + */ + void resetPassword(@Param("password") String password,@Param("email") String email); + + /** + * Description: 根据用户id查询用户信息 + * + * @author fanxb + * @date 2019/7/30 16:08 + * @param userId userId + * @return com.fanxb.bookmark.common.entity.User + */ + User selectByUserId(int userId); +} diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java index fbebc4d..315522f 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java @@ -1,147 +1,159 @@ -package com.fanxb.bookmark.business.user.service; - -import com.fanxb.bookmark.business.user.dao.UserDao; -import com.fanxb.bookmark.business.user.entity.LoginBody; -import com.fanxb.bookmark.business.user.entity.LoginRes; -import com.fanxb.bookmark.business.user.entity.RegisterBody; -import com.fanxb.bookmark.common.constant.Constant; -import com.fanxb.bookmark.common.entity.MailInfo; -import com.fanxb.bookmark.common.entity.User; -import com.fanxb.bookmark.common.exception.CustomException; -import com.fanxb.bookmark.common.exception.FormDataException; -import com.fanxb.bookmark.common.util.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.HashMap; -import java.util.Map; - -/** - * 类功能简述: - * 类功能详述: - * - * @author fanxb - * @date 2019/7/5 17:39 - */ -@Service -public class UserService { - - private static final String DEFAULT_ICON = "defaultIcon.png"; - /** - * 短期jwt失效时间 - */ - private static final long SHORT_EXPIRE_TIME = 2 * 60 * 60 * 1000; - /** - * 长期jwt失效时间 - */ - private static final long LONG_EXPIRE_TIME = 30L * TimeUtil.DAY_MS; - - @Autowired - private UserDao userDao; - - /** - * Description: 向目标发送验证码 - * - * @param email 目标 - * @author fanxb - * @date 2019/7/5 17:48 - */ - public void sendAuthCode(String email) { - MailInfo info = new MailInfo(); - info.setSubject("签签世界注册验证码"); - String code = StringUtil.getRandomString(6, 2); - info.setContent("欢迎注册 签签世界 ,本次验证码"); - info.setContent(code + " 是您的验证码,注意验证码有效期为15分钟哦!"); - info.setReceiver(email); - if (Constant.isDev) { - code = "123456"; - } else { - MailUtil.sendTextMail(info); - } - RedisUtil.set(Constant.authCodeKey(email), code, Constant.AUTH_CODE_EXPIRE); - } - - /** - * Description: 用户注册 - * - * @param body 注册表单 - * @author fanxb - * @date 2019/7/6 11:30 - */ - public void register(RegisterBody body) { - String codeKey = Constant.authCodeKey(body.getEmail()); - String realCode = RedisUtil.get(codeKey, String.class); - if ((!StringUtil.isEmpty(realCode)) && (!realCode.equals(body.getAuthCode()))) { - throw new CustomException("验证码错误"); - } - RedisUtil.delete(codeKey); - User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail()); - if (user != null) { - if (user.getUsername().equals(body.getUsername())) { - throw new FormDataException("用户名已经被注册"); - } - if (user.getEmail().equals(body.getEmail())) { - throw new FormDataException("邮箱已经被注册"); - } - } - user = new User(); - user.setUsername(body.getUsername()); - user.setEmail(body.getEmail()); - user.setIcon(DEFAULT_ICON); - user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword()))); - user.setCreateTime(System.currentTimeMillis()); - user.setLastLoginTime(0); - userDao.addOne(user); - } - - /** - * Description: 登录 - * - * @param body 登录表单 - * @return LoginRes - * @author fanxb - * @date 2019/7/6 16:37 - */ - public LoginRes login(LoginBody body) { - User userInfo = userDao.selectByUsernameOrEmail(body.getStr(), body.getStr()); - if (userInfo == null) { - throw new FormDataException("账号/密码错误"); - } - if (!HashUtil.sha1(HashUtil.md5(body.getPassword())).equals(userInfo.getPassword())) { - throw new FormDataException("账号/密码错误"); - } - Map data = new HashMap<>(1); - data.put("userId", String.valueOf(userInfo.getUserId())); - String token = JwtUtil.encode(data, Constant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME); - LoginRes res = new LoginRes(); - res.setToken(token); - res.setUserId(userInfo.getUserId()); - res.setUsername(userInfo.getUsername()); - res.setEmail(userInfo.getEmail()); - res.setIcon(userInfo.getIcon()); - userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId()); - return res; - } - - /** - * Description: 重置密码 - * - * @param body 重置密码 由于参数和注册差不多,所以用同一个表单 - * @author fanxb - * @date 2019/7/9 19:59 - */ - public void resetPassword(RegisterBody body) { - User user = userDao.selectByUsernameOrEmail(body.getEmail(), body.getEmail()); - if (user == null) { - throw new FormDataException("用户不存在"); - } - String codeKey = Constant.authCodeKey(body.getEmail()); - String realCode = RedisUtil.get(codeKey, String.class); - if (StringUtil.isEmpty(realCode) || (!realCode.equals(body.getAuthCode()))) { - throw new FormDataException("验证码错误"); - } - RedisUtil.delete(codeKey); - String newPassword = HashUtil.sha1(HashUtil.md5(body.getPassword())); - userDao.resetPassword(newPassword, body.getEmail()); - } -} +package com.fanxb.bookmark.business.user.service; + +import com.fanxb.bookmark.business.user.dao.UserDao; +import com.fanxb.bookmark.business.user.entity.LoginBody; +import com.fanxb.bookmark.business.user.entity.LoginRes; +import com.fanxb.bookmark.business.user.entity.RegisterBody; +import com.fanxb.bookmark.common.constant.Constant; +import com.fanxb.bookmark.common.entity.MailInfo; +import com.fanxb.bookmark.common.entity.User; +import com.fanxb.bookmark.common.exception.CustomException; +import com.fanxb.bookmark.common.exception.FormDataException; +import com.fanxb.bookmark.common.util.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/5 17:39 + */ +@Service +public class UserService { + + private static final String DEFAULT_ICON = "defaultIcon.png"; + /** + * 短期jwt失效时间 + */ + private static final long SHORT_EXPIRE_TIME = 2 * 60 * 60 * 1000; + /** + * 长期jwt失效时间 + */ + private static final long LONG_EXPIRE_TIME = 30L * TimeUtil.DAY_MS; + + @Autowired + private UserDao userDao; + + /** + * Description: 向目标发送验证码 + * + * @param email 目标 + * @author fanxb + * @date 2019/7/5 17:48 + */ + public void sendAuthCode(String email) { + MailInfo info = new MailInfo(); + info.setSubject("签签世界注册验证码"); + String code = StringUtil.getRandomString(6, 2); + info.setContent("欢迎注册 签签世界 ,本次验证码"); + info.setContent(code + " 是您的验证码,注意验证码有效期为15分钟哦!"); + info.setReceiver(email); + if (Constant.isDev) { + code = "123456"; + } else { + MailUtil.sendTextMail(info); + } + RedisUtil.set(Constant.authCodeKey(email), code, Constant.AUTH_CODE_EXPIRE); + } + + /** + * Description: 用户注册 + * + * @param body 注册表单 + * @author fanxb + * @date 2019/7/6 11:30 + */ + public void register(RegisterBody body) { + String codeKey = Constant.authCodeKey(body.getEmail()); + String realCode = RedisUtil.get(codeKey, String.class); + if ((!StringUtil.isEmpty(realCode)) && (!realCode.equals(body.getAuthCode()))) { + throw new CustomException("验证码错误"); + } + RedisUtil.delete(codeKey); + User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail()); + if (user != null) { + if (user.getUsername().equals(body.getUsername())) { + throw new FormDataException("用户名已经被注册"); + } + if (user.getEmail().equals(body.getEmail())) { + throw new FormDataException("邮箱已经被注册"); + } + } + user = new User(); + user.setUsername(body.getUsername()); + user.setEmail(body.getEmail()); + user.setIcon(DEFAULT_ICON); + user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword()))); + user.setCreateTime(System.currentTimeMillis()); + user.setLastLoginTime(0); + userDao.addOne(user); + } + + /** + * Description: 登录 + * + * @param body 登录表单 + * @return LoginRes + * @author fanxb + * @date 2019/7/6 16:37 + */ + public LoginRes login(LoginBody body) { + User userInfo = userDao.selectByUsernameOrEmail(body.getStr(), body.getStr()); + if (userInfo == null) { + throw new FormDataException("账号/密码错误"); + } + if (!HashUtil.sha1(HashUtil.md5(body.getPassword())).equals(userInfo.getPassword())) { + throw new FormDataException("账号/密码错误"); + } + Map data = new HashMap<>(1); + data.put("userId", String.valueOf(userInfo.getUserId())); + String token = JwtUtil.encode(data, Constant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME); + LoginRes res = new LoginRes(); + res.setToken(token); + res.setUserId(userInfo.getUserId()); + res.setUsername(userInfo.getUsername()); + res.setEmail(userInfo.getEmail()); + res.setIcon(userInfo.getIcon()); + userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId()); + return res; + } + + /** + * Description: 重置密码 + * + * @param body 重置密码 由于参数和注册差不多,所以用同一个表单 + * @author fanxb + * @date 2019/7/9 19:59 + */ + public void resetPassword(RegisterBody body) { + User user = userDao.selectByUsernameOrEmail(body.getEmail(), body.getEmail()); + if (user == null) { + throw new FormDataException("用户不存在"); + } + String codeKey = Constant.authCodeKey(body.getEmail()); + String realCode = RedisUtil.get(codeKey, String.class); + if (StringUtil.isEmpty(realCode) || (!realCode.equals(body.getAuthCode()))) { + throw new FormDataException("验证码错误"); + } + RedisUtil.delete(codeKey); + String newPassword = HashUtil.sha1(HashUtil.md5(body.getPassword())); + userDao.resetPassword(newPassword, body.getEmail()); + } + + /** + * Description: 根据userId获取用户信息 + * + * @author fanxb + * @date 2019/7/30 15:57 + * @param userId userId + * @return com.fanxb.bookmark.common.entity.User + */ + public User getUserInfo(int userId){ + return userDao.selectByUserId(userId); + } +} diff --git a/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml b/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml index 495e26c..b2fc498 100644 --- a/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml +++ b/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml @@ -1,38 +1,48 @@ - - - - - - - insert into user (username, email, icon, password, createTime, lastLoginTime) - value - (#{username}, #{email}, #{icon}, #{password}, #{createTime}, #{lastLoginTime}) - - - - - - update user - set lastLoginTime = #{time} - where userId = #{userId} - - - - update user - set password = #{password} - where email = #{email} - - - + + + + + + + insert into user (username, email, icon, password, createTime, lastLoginTime) + value + (#{username}, #{email}, #{icon}, #{password}, #{createTime}, #{lastLoginTime}) + + + + + + update user + set lastLoginTime = #{time} + where userId = #{userId} + + + + update user + set password = #{password} + where email = #{email} + + + + + \ No newline at end of file